-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstats_tracker.py
More file actions
84 lines (72 loc) · 2.13 KB
/
Copy pathstats_tracker.py
File metadata and controls
84 lines (72 loc) · 2.13 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
import json
import os
import threading
from datetime import datetime
STATS_FILE = "stats.json"
_lock = threading.Lock()
DEFAULT_STATS = {
"system_start_time": "",
"comments_sent": 0,
"comment_failures": 0,
"posts_created": 0,
"post_failures": 0,
"gemini_errors": 0,
"watchdog_restarts": 0,
"last_comment_time": "",
"last_post_time": "",
"last_error": "",
"last_error_time": "",
"last_heartbeat": 0.0
}
def _load_stats():
if not os.path.exists(STATS_FILE):
return DEFAULT_STATS.copy()
try:
with open(STATS_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
# Ensure all keys exist
for k, v in DEFAULT_STATS.items():
if k not in data:
data[k] = v
return data
except Exception:
return DEFAULT_STATS.copy()
def _save_stats(data):
try:
with open(STATS_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
except Exception:
pass
def get_current_time_str():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def init_system_start():
with _lock:
stats = _load_stats()
stats["system_start_time"] = get_current_time_str()
_save_stats(stats)
def update_heartbeat(timestamp: float):
with _lock:
stats = _load_stats()
stats["last_heartbeat"] = timestamp
_save_stats(stats)
def increment(key: str):
with _lock:
stats = _load_stats()
if key in stats:
stats[key] += 1
# Auto-update corresponding timestamps
time_str = get_current_time_str()
if key == "comments_sent":
stats["last_comment_time"] = time_str
elif key == "posts_created":
stats["last_post_time"] = time_str
_save_stats(stats)
def log_error(error_msg: str):
with _lock:
stats = _load_stats()
stats["last_error"] = str(error_msg)
stats["last_error_time"] = get_current_time_str()
_save_stats(stats)
def get_stats():
with _lock:
return _load_stats()