-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathself_diagnostics.py
82 lines (63 loc) · 2.41 KB
/
self_diagnostics.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
""" A collection of tools to generate self-diagnostics info to provide it to the GUI.
"""
import psutil
import time
from helper_funcs import append_logs, get_file_modification_ts, synthetic_data7
name4logs = "self_diagnostics"
use_synthetic_data7 = synthetic_data7()
if use_synthetic_data7:
output_postfix = "_synthetic"
else:
output_postfix = "_fetched"
# TODO: move it to configs
methods = ["telemanom", "kitnet"]
def python_script_running7(script_filename):
""" Returns True if the script with the given filename is currently running, False otherwise.
Args:
script_filename (str): e.g. "lib_telemanom_train.py"
"""
res = False
try:
for p in psutil.process_iter():
if len(p.cmdline()) > 1:
if script_filename in p.cmdline()[1]:
res = True
break
except Exception as e:
append_logs("Exception: " + str(e), name4logs, "always")
return res
def get_dead_scripts():
""" Returns a list of scripts that should be running, but are dead instead.
"""
# TODO: load it only once
with open("state_controls/scripts_to_run.txt") as scripts_f:
all_filenames = scripts_f.readlines()
dead_scripts = []
for name in all_filenames:
if not python_script_running7(name.strip()):
dead_scripts.append(name.strip())
return dead_scripts
def get_models_modification_ts(method_name):
""" Returns the UNIX epoch timestamp of the modification time of the given model.
Args:
method_name (str): e.g. "telemanom"
"""
modelpath = "pickled_models/" + method_name + output_postfix + ".pkl"
model_ts = get_file_modification_ts(modelpath)
return model_ts
def get_wenn_models_were_updated_string():
""" Generates a human-readable str about when models were last modified.
The result looks like this: "When the models were retrained:\n Telemamom: 3 min ago"
"""
now = time.time()
status_str = "When the models were retrained:\n"
for m in methods:
modification_ts = get_models_modification_ts(m)
if modification_ts is not None:
timestamps_diff = now - modification_ts
mins_str = str(round(timestamps_diff / 60))
status_str += m + ": " + mins_str + " min ago\n"
else:
status_str += m + ": doesn't exist yet. First launch?\n"
status_str = status_str[:-1]
return status_str