-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.py
More file actions
426 lines (348 loc) · 13.8 KB
/
analytics.py
File metadata and controls
426 lines (348 loc) · 13.8 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""
Centralized analytics tracking module for the Reeld pipeline.
Thread-safe metrics collector supporting:
- videos_processed, captions_generated, claude_api_calls
- processing_times, errors per script
- Batch flushes for parallel workers
Uses SQLite for storage with schema:
- runs: timestamp, script_name, status
- metrics: run_id, metric_type, value, timestamp
Usage:
from analytics import Analytics
# Initialize (usually at script start)
analytics = Analytics()
# Track metrics
analytics.track("videos_processed", 1)
analytics.track("processing_time_ms", 1234)
analytics.track("errors", 1, tags={"error_type": "ffmpeg"})
# Flush at end of script
analytics.flush()
# Or use context manager for auto-flush
with Analytics(script_name="spoof_videos") as analytics:
analytics.track("videos_processed", 1)
Environment variables:
ANALYTICS_ENABLED: Set to "false" to disable tracking (default: true)
ANALYTICS_DB_PATH: Path to SQLite database (default: analytics.db)
"""
import os
import json
import sqlite3
import threading
import atexit
from datetime import datetime
from pathlib import Path
from typing import Optional, Dict, Any, List
from contextlib import contextmanager
# Environment configuration
ANALYTICS_ENABLED = os.environ.get("ANALYTICS_ENABLED", "true").lower() != "false"
ANALYTICS_DB_PATH = os.environ.get("ANALYTICS_DB_PATH", "analytics.db")
class Analytics:
"""Thread-safe analytics collector with SQLite persistence."""
_instance: Optional["Analytics"] = None
_lock = threading.Lock()
def __init__(
self,
script_name: Optional[str] = None,
db_path: str = ANALYTICS_DB_PATH,
enabled: bool = ANALYTICS_ENABLED,
):
"""Initialize analytics collector.
Args:
script_name: Name of the script using analytics (auto-detected if None)
db_path: Path to SQLite database file
enabled: Whether tracking is enabled
"""
self.script_name = script_name or self._detect_script_name()
self.db_path = db_path
self.enabled = enabled
self.run_id: Optional[int] = None
self.start_time = datetime.now()
# Thread-local storage for metrics buffer
self._local = threading.local()
self._global_buffer: List[Dict[str, Any]] = []
self._buffer_lock = threading.Lock()
if self.enabled:
self._init_db()
self._start_run()
# Register flush on exit
atexit.register(self.flush)
def _detect_script_name(self) -> str:
"""Detect the calling script name."""
import sys
if sys.argv:
return Path(sys.argv[0]).stem
return "unknown"
def _init_db(self) -> None:
"""Initialize SQLite database schema."""
with self._get_connection() as conn:
conn.executescript("""
CREATE TABLE IF NOT EXISTS runs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
script_name TEXT NOT NULL,
start_time TEXT NOT NULL,
end_time TEXT,
status TEXT DEFAULT 'running',
metadata TEXT
);
CREATE TABLE IF NOT EXISTS metrics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
run_id INTEGER,
metric_type TEXT NOT NULL,
value REAL NOT NULL,
tags TEXT,
timestamp TEXT NOT NULL,
FOREIGN KEY (run_id) REFERENCES runs(id)
);
CREATE INDEX IF NOT EXISTS idx_metrics_run_id ON metrics(run_id);
CREATE INDEX IF NOT EXISTS idx_metrics_type ON metrics(metric_type);
CREATE INDEX IF NOT EXISTS idx_metrics_timestamp ON metrics(timestamp);
CREATE INDEX IF NOT EXISTS idx_runs_script ON runs(script_name);
CREATE INDEX IF NOT EXISTS idx_runs_start ON runs(start_time);
""")
@contextmanager
def _get_connection(self):
"""Get a thread-safe database connection."""
conn = sqlite3.connect(self.db_path, timeout=30)
conn.row_factory = sqlite3.Row
try:
yield conn
conn.commit()
finally:
conn.close()
def _start_run(self) -> None:
"""Record the start of a new run."""
with self._get_connection() as conn:
cursor = conn.execute(
"INSERT INTO runs (script_name, start_time, status) VALUES (?, ?, ?)",
(self.script_name, self.start_time.isoformat(), "running")
)
self.run_id = cursor.lastrowid
def track(
self,
metric_type: str,
value: float = 1,
tags: Optional[Dict[str, Any]] = None,
) -> None:
"""Track a metric value.
Thread-safe: can be called from worker threads.
Args:
metric_type: Type of metric (e.g., "videos_processed", "errors")
value: Numeric value to record (default: 1 for counters)
tags: Optional tags for categorization
"""
if not self.enabled:
return
metric = {
"metric_type": metric_type,
"value": value,
"tags": json.dumps(tags) if tags else None,
"timestamp": datetime.now().isoformat(),
}
with self._buffer_lock:
self._global_buffer.append(metric)
# Auto-flush if buffer gets large
if len(self._global_buffer) >= 100:
self._flush_buffer()
def increment(self, metric_type: str, amount: float = 1) -> None:
"""Convenience method for incrementing counters.
Args:
metric_type: Type of metric to increment
amount: Amount to increment by (default: 1)
"""
self.track(metric_type, amount)
def timing(self, metric_type: str, duration_ms: float) -> None:
"""Record a timing metric.
Args:
metric_type: Type of timing metric
duration_ms: Duration in milliseconds
"""
self.track(metric_type, duration_ms, tags={"unit": "ms"})
def error(self, error_type: str, message: str = "") -> None:
"""Record an error.
Args:
error_type: Type/category of error
message: Optional error message
"""
self.track("errors", 1, tags={"error_type": error_type, "message": message[:200]})
def _flush_buffer(self) -> None:
"""Flush buffered metrics to database (internal, assumes lock held)."""
if not self._global_buffer:
return
metrics_to_write = self._global_buffer.copy()
self._global_buffer.clear()
try:
with self._get_connection() as conn:
conn.executemany(
"""INSERT INTO metrics (run_id, metric_type, value, tags, timestamp)
VALUES (?, ?, ?, ?, ?)""",
[
(self.run_id, m["metric_type"], m["value"], m["tags"], m["timestamp"])
for m in metrics_to_write
]
)
except Exception as e:
# Don't crash the main script if analytics fails
print(f"[Analytics] Warning: Failed to flush metrics: {e}")
def flush(self, status: str = "completed") -> None:
"""Flush all buffered metrics and mark run as complete.
Args:
status: Final run status ("completed", "failed", "cancelled")
"""
if not self.enabled:
return
with self._buffer_lock:
self._flush_buffer()
# Update run status
if self.run_id:
try:
with self._get_connection() as conn:
conn.execute(
"UPDATE runs SET end_time = ?, status = ? WHERE id = ?",
(datetime.now().isoformat(), status, self.run_id)
)
except Exception as e:
print(f"[Analytics] Warning: Failed to update run status: {e}")
def __enter__(self) -> "Analytics":
"""Context manager entry."""
return self
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
"""Context manager exit with auto-flush."""
status = "failed" if exc_type else "completed"
self.flush(status)
# -------------------------------------------------------------------------
# Query methods for dashboard
# -------------------------------------------------------------------------
def get_metrics_today(self) -> Dict[str, float]:
"""Get aggregated metrics for today.
Returns:
Dict mapping metric_type to total value
"""
today = datetime.now().strftime("%Y-%m-%d")
return self._get_metrics_for_date(today)
def get_metrics_range(self, days: int = 7) -> List[Dict[str, Any]]:
"""Get daily metrics for the last N days.
Args:
days: Number of days to include
Returns:
List of daily summaries with date and metrics
"""
results = []
for i in range(days):
from datetime import timedelta
date = (datetime.now() - timedelta(days=i)).strftime("%Y-%m-%d")
metrics = self._get_metrics_for_date(date)
if metrics:
results.append({"date": date, "metrics": metrics})
return results
def _get_metrics_for_date(self, date: str) -> Dict[str, float]:
"""Get aggregated metrics for a specific date."""
try:
with self._get_connection() as conn:
cursor = conn.execute(
"""SELECT metric_type, SUM(value) as total
FROM metrics
WHERE timestamp LIKE ?
GROUP BY metric_type""",
(f"{date}%",)
)
return {row["metric_type"]: row["total"] for row in cursor}
except Exception:
return {}
def get_runs(self, limit: int = 50) -> List[Dict[str, Any]]:
"""Get recent pipeline runs.
Args:
limit: Maximum number of runs to return
Returns:
List of run records
"""
try:
with self._get_connection() as conn:
cursor = conn.execute(
"""SELECT id, script_name, start_time, end_time, status
FROM runs
ORDER BY start_time DESC
LIMIT ?""",
(limit,)
)
return [dict(row) for row in cursor]
except Exception:
return []
def get_run_metrics(self, run_id: int) -> List[Dict[str, Any]]:
"""Get metrics for a specific run.
Args:
run_id: ID of the run
Returns:
List of metric records
"""
try:
with self._get_connection() as conn:
cursor = conn.execute(
"""SELECT metric_type, SUM(value) as total, COUNT(*) as count
FROM metrics
WHERE run_id = ?
GROUP BY metric_type""",
(run_id,)
)
return [dict(row) for row in cursor]
except Exception:
return []
# Global instance for simple usage
_analytics: Optional[Analytics] = None
def get_analytics(script_name: Optional[str] = None) -> Analytics:
"""Get or create the global analytics instance.
Args:
script_name: Optional script name override
Returns:
Analytics instance
"""
global _analytics
if _analytics is None:
_analytics = Analytics(script_name=script_name)
return _analytics
def track(metric_type: str, value: float = 1, tags: Optional[Dict[str, Any]] = None) -> None:
"""Convenience function for tracking metrics using global instance.
Args:
metric_type: Type of metric
value: Value to record
tags: Optional tags
"""
get_analytics().track(metric_type, value, tags)
def flush(status: str = "completed") -> None:
"""Flush the global analytics instance.
Args:
status: Final run status
"""
if _analytics:
_analytics.flush(status)
# CLI for quick reporting
if __name__ == "__main__":
import sys
analytics = Analytics(script_name="analytics_cli")
if len(sys.argv) > 1:
cmd = sys.argv[1]
if cmd == "--today":
metrics = analytics.get_metrics_today()
print("=== Today's Metrics ===")
for metric, value in sorted(metrics.items()):
print(f" {metric}: {value:,.0f}")
elif cmd == "--summary":
days = int(sys.argv[2]) if len(sys.argv) > 2 else 7
print(f"=== Last {days} Days ===")
for day_data in analytics.get_metrics_range(days):
print(f"\n{day_data['date']}:")
for metric, value in sorted(day_data["metrics"].items()):
print(f" {metric}: {value:,.0f}")
elif cmd == "--runs":
limit = int(sys.argv[2]) if len(sys.argv) > 2 else 20
runs = analytics.get_runs(limit)
print(f"=== Recent Runs (last {limit}) ===")
for run in runs:
status_icon = {"completed": "+", "failed": "X", "running": "~"}.get(run["status"], "?")
print(f" [{status_icon}] {run['script_name']} @ {run['start_time'][:19]}")
else:
print("Usage: python analytics.py [--today | --summary [days] | --runs [limit]]")
else:
print("Usage: python analytics.py [--today | --summary [days] | --runs [limit]]")
print("\nAnalytics tracking module for Reeld pipeline.")
print(f"Database: {ANALYTICS_DB_PATH}")
print(f"Enabled: {ANALYTICS_ENABLED}")