-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
252 lines (206 loc) · 7.62 KB
/
main.py
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
import json
import os
from datetime import datetime, timedelta, timezone
from pathlib import Path
import pandas as pd
import psutil
import pytz
from syftbox.lib import Client, SyftPermission
from sdk import (
Settings,
datasites_file_glob,
ensure,
public_url,
should_run,
truncate_file,
)
# define version for future changes
__version__ = "1.0.0"
__author__ = "[email protected]"
# load client
try:
client = Client.load()
except Exception:
# temp work around for old syftbox
config_path = os.path.expanduser("~/.syftbox/config.json")
with open(config_path, "r") as f:
data = json.load(f)
data["sync_folder"] = data["data_dir"] + "/datasites"
data["config_path"] = config_path
del data["data_dir"]
del data["client_url"]
client = Client(**data)
# settings
settings = Settings()
last_run = settings.get("last_run", None)
settings.set("last_run", datetime.now().isoformat())
# do something on first install
if last_run is None:
print("First run.")
# define paths
DATASITES = Path(client.sync_folder)
MY_DATASITE = DATASITES / client.email
PUBLIC_PATH = MY_DATASITE / "public"
METRIC_STUB = "metrics/ftop.jsonl"
METRIC_FILE_PATH = PUBLIC_PATH / METRIC_STUB
PUBLISH_PATH = PUBLIC_PATH / "ftop"
HOME_URL = f"{public_url(PUBLISH_PATH)}/index.html"
MAX_LINES = 60 * 48 # 48 hours of data at one reading per minute
INTERVAL = 60 # Metric collection interval in seconds
# write json lines to a file periodically
def get_metrics():
if not should_run(METRIC_FILE_PATH, interval=INTERVAL):
print("Skipping metric collection, not enough time has passed.")
return
num_cores = psutil.cpu_count(logical=True)
load_avg = psutil.getloadavg()
mem_info = psutil.virtual_memory()
total_ram = mem_info.total
used_ram = mem_info.used
uptime = datetime.now(timezone.utc) - datetime.fromtimestamp(
psutil.boot_time()
).astimezone(timezone.utc)
timestamp = datetime.now(timezone.utc)
data = {
"timestamp": timestamp.isoformat(),
"version": __version__,
"num_cores": num_cores,
"cpu_load_1min": load_avg[0],
"cpu_load_5min": load_avg[1],
"cpu_load_15min": load_avg[2],
"total_ram": total_ram,
"used_ram": used_ram,
"uptime_seconds": int(uptime.total_seconds()),
}
if not os.path.exists(METRIC_FILE_PATH.parent):
os.makedirs(METRIC_FILE_PATH.parent, exist_ok=True)
permission = SyftPermission.mine_with_public_read(email=client.email)
permission.ensure(METRIC_FILE_PATH.parent)
with open(METRIC_FILE_PATH, mode="a") as f:
f.write(json.dumps(data) + "\n")
truncate_file(METRIC_FILE_PATH, max_lines=MAX_LINES)
# aggregate metrics into a dataframe
def load_metrics_to_dataframe():
records = []
results = datasites_file_glob(client, pattern=f"**/{METRIC_STUB}")
print("results", results)
for datasite, file_path in results:
print("Found file:", file_path)
with open(file_path, "r") as f:
for line in f:
record = json.loads(line)
records.append(
{
**record,
"datasite": datasite,
}
)
df = pd.DataFrame(records)
if "timestamp" in df.columns:
df["timestamp"] = pd.to_datetime(df["timestamp"], utc=True)
return df
def analyze_metrics():
run_analysis = settings.get("run_analysis", None)
if (run_analysis is None and client.email != __author__) or run_analysis is False:
return
metrics_df = load_metrics_to_dataframe()
metrics_df.to_csv("./metrics.csv")
if metrics_df.empty:
print("No data available for analysis.")
return {}
# Convert timestamp to datetime if it's not already
metrics_df["timestamp"] = pd.to_datetime(metrics_df["timestamp"])
# Calculate the cutoff time for 48 hours ago (in UTC)
cutoff_time = datetime.now(pytz.UTC) - timedelta(hours=48)
# Filter for last 48 hours
recent_df = metrics_df[metrics_df["timestamp"] >= cutoff_time]
# Group data into 1-minute intervals for the time series
time_intervals = pd.date_range(
start=cutoff_time, end=datetime.now(pytz.UTC), freq="1T", tz="UTC"
)
historical_data = []
for interval in time_intervals:
interval_end = interval + timedelta(minutes=1)
interval_df = recent_df[
(recent_df["timestamp"] >= interval)
& (recent_df["timestamp"] < interval_end)
]
if not interval_df.empty:
historical_data.append(
{
"timestamp": interval.isoformat(),
"cpu_load_avg": round(interval_df["cpu_load_1min"].mean(), 2),
"ram_usage_percent": round(
(interval_df["used_ram"].sum() / interval_df["total_ram"].sum())
* 100,
2,
),
"active_systems": len(interval_df["datasite"].unique()),
}
)
# Rest of the function remains the same
systems_data = []
datasites = []
user_uptimes = []
for datasite in metrics_df["datasite"].unique():
system_df = metrics_df[metrics_df["datasite"] == datasite]
latest = system_df.sort_values("timestamp").iloc[-1]
user_uptime = {
"uptime_seconds": int(latest["uptime_seconds"]),
"email": str(datasite),
}
user_uptimes.append(user_uptime)
system_data = {
"timestamp": latest["timestamp"].isoformat(),
"num_cores": int(latest["num_cores"]),
"cpu_load_1min": float(latest["cpu_load_1min"]),
"cpu_load_5min": float(latest["cpu_load_5min"]),
"cpu_load_15min": float(latest["cpu_load_15min"]),
"total_ram": int(latest["total_ram"]),
"used_ram": int(latest["used_ram"]),
"uptime_seconds": int(latest["uptime_seconds"]),
"email": str(datasite),
}
systems_data.append(system_data)
datasites.append(datasite)
summary_stats = {
"total_systems": len(systems_data),
"total_cpus": sum(sys["num_cores"] for sys in systems_data),
"cpu_load": {
"average": round(
sum(sys["cpu_load_1min"] for sys in systems_data) / len(systems_data), 2
),
"min": round(min(sys["cpu_load_1min"] for sys in systems_data), 2),
"max": round(max(sys["cpu_load_1min"] for sys in systems_data), 2),
},
"ram": {
"total": sum(sys["total_ram"] for sys in systems_data),
"used": sum(sys["used_ram"] for sys in systems_data),
"average_usage_percentage": round(
sum(sys["used_ram"] for sys in systems_data)
/ sum(sys["total_ram"] for sys in systems_data)
* 100,
2,
),
},
"datasites": sorted(datasites),
"historical_data": historical_data,
"user_uptimes": user_uptimes,
}
ensure(
["./widget/index.html", "./widget/index.js", "./widget/syftbox-sdk.js"],
PUBLISH_PATH,
)
output_path = PUBLISH_PATH / "dashboard_metrics.json"
with open(output_path, "w") as f:
json.dump(summary_stats, f, indent=2)
print(f"Writing json to {output_path}")
# for dev
output_path = "./widget/dashboard_metrics.json"
with open(output_path, "w") as f:
json.dump(summary_stats, f, indent=2)
print(f"Dashboard published to {HOME_URL}")
return summary_stats
# Run metric collection
get_metrics()
analyze_metrics()