-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfallback_server.py
More file actions
213 lines (177 loc) · 6.27 KB
/
fallback_server.py
File metadata and controls
213 lines (177 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
"""
Terradev Fallback Telemetry Server
Runs on port 8080 to handle telemetry when primary server is down
"""
import json
import sqlite3
import threading
from datetime import datetime, timedelta
from flask import Flask, jsonify, request
from collections import defaultdict
import os
app = Flask(__name__)
# In-memory storage for demo purposes
telemetry_data = defaultdict(list)
user_stats = {
'total_users': 0,
'active_users_today': 0,
'daily_active_users': {},
'machine_ids': set(),
'install_ids': set()
}
# Initialize database
def init_db():
"""Initialize SQLite database for telemetry storage"""
conn = sqlite3.connect('telemetry_fallback.db')
cursor = conn.cursor()
# Create tables
cursor.execute('''
CREATE TABLE IF NOT EXISTS telemetry_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
machine_id TEXT,
install_id TEXT,
action TEXT,
timestamp TEXT,
details TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS daily_active_users (
date TEXT PRIMARY KEY,
machine_ids TEXT,
count INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
conn.close()
def log_telemetry(data):
"""Log telemetry data"""
try:
conn = sqlite3.connect('telemetry_fallback.db')
cursor = conn.cursor()
cursor.execute('''
INSERT INTO telemetry_logs
(machine_id, install_id, action, timestamp, details)
VALUES (?, ?, ?, ?, ?)
''', (
data.get('machine_id'),
data.get('install_id'),
data.get('action'),
data.get('timestamp'),
json.dumps(data.get('details', {}))
))
conn.commit()
conn.close()
# Update in-memory stats
machine_id = data.get('machine_id')
install_id = data.get('install_id')
if machine_id:
user_stats['machine_ids'].add(machine_id)
user_stats['total_users'] = len(user_stats['machine_ids'])
if install_id:
user_stats['install_ids'].add(install_id)
# Track daily active users
today = datetime.now().date().isoformat()
if data.get('action') == 'daily_active_users':
user_stats['daily_active_users'][today] = user_stats['daily_active_users'].get(today, 0) + 1
user_stats['active_users_today'] = user_stats['daily_active_users'].get(today, 0)
print(f"✅ [FALLBACK] Logged telemetry: {data.get('action')} from {machine_id[:16]}...")
except Exception as e:
print(f"❌ Error logging telemetry: {e}")
@app.route('/health')
def health():
"""Health check endpoint"""
return jsonify({
'status': 'healthy',
'timestamp': datetime.now().isoformat(),
'version': '1.0.0-fallback',
'users': {
'total_users': user_stats['total_users'],
'active_users_today': user_stats['active_users_today'],
'total_installations': len(user_stats['install_ids'])
}
})
@app.route('/log-usage', methods=['POST'])
def log_usage():
"""Log usage telemetry"""
try:
data = request.get_json()
if not data:
return jsonify({'error': 'No data provided'}), 400
log_telemetry(data)
return jsonify({
'status': 'success',
'message': 'Telemetry logged successfully (fallback)',
'timestamp': datetime.now().isoformat()
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/check-license', methods=['POST'])
def check_license():
"""Check license compliance"""
try:
data = request.get_json()
machine_id = data.get('machine_id', 'unknown')
# Simple license check logic
allowed = True
tier = 'research'
limit = 10
usage = 0
response = {
'allowed': allowed,
'tier': tier,
'limit': limit,
'usage': usage,
'reason': 'Fallback mode - always allowed'
}
print(f"📋 [FALLBACK] License check for {machine_id[:16]}...: {tier} tier")
return jsonify(response)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/user-stats')
def get_user_stats():
"""Get user statistics"""
try:
# Calculate recent activity
recent_activity = defaultdict(int)
conn = sqlite3.connect('telemetry_fallback.db')
cursor = conn.cursor()
# Get last 7 days of activity
for i in range(7):
date = (datetime.now() - timedelta(days=i)).date().isoformat()
cursor.execute('''
SELECT COUNT(DISTINCT machine_id)
FROM telemetry_logs
WHERE date(timestamp) = ?
''', (date,))
count = cursor.fetchone()[0] or 0
recent_activity[date] = count
conn.close()
stats = {
'total_users': user_stats['total_users'],
'active_users_today': user_stats['active_users_today'],
'daily_active_users': dict(user_stats['daily_active_users']),
'recent_activity': dict(recent_activity),
'total_installations': len(user_stats['install_ids']),
'server_info': {
'version': '1.0.0-fallback',
'uptime': 'N/A',
'timestamp': datetime.now().isoformat(),
'mode': 'fallback'
}
}
return jsonify(stats)
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
print("🚀 Starting Terradev Fallback Telemetry API Server...")
print("📊 This is the fallback server (port 8080)")
print("🔍 Health check at: http://localhost:8080/health")
print("📈 User stats at: http://localhost:8080/user-stats")
# Initialize database
init_db()
# Start server
app.run(host='0.0.0.0', port=8080, debug=False)