-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
55 lines (48 loc) · 1.79 KB
/
app.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
#!/usr/bin/env/python
from time import sleep
import io
from datetime import timedelta
from glancemetrics.glance import GlanceMetrics
from glancemetrics.domain.alerts import Alert
from glancemetrics.cli.argsparser import parser
from glancemetrics.cli.ui import render
from glancemetrics.watchdogs.log import logwatcher
from glancemetrics.utils import ensure_file_exists
from glancemetrics.utils.datetime import current_time
args = parser.parse_args()
program_start_time = current_time()
ui_refresh_rate = args.refresh
file_path = ensure_file_exists(args.file)
with open(str(file_path), "r") as log_file:
# seek to end of file for faster app startup in case of large log files
# since we're only interested in current metrics
# TODO: could also implement smartly seeking to first log where timestamp > (now - app_window)
log_file.seek(0, io.SEEK_END)
insights_window = timedelta(seconds=args.timewindow)
alert = Alert(
threshold=args.alert_threshold, window=timedelta(minutes=args.alertwindow)
)
glance_app = GlanceMetrics(
log_stream=logwatcher(log_file),
insights_window=insights_window,
alerts=[alert],
)
try:
while True:
glance_app.refresh()
insights, top_sections = glance_app.insights(
top_sections_limit=args.section_limit
)
render(
insights,
top_sections,
alerts=glance_app.alerts,
insights_window=insights_window,
program_runtime=current_time() - program_start_time,
incident_limit=args.incident_limit,
)
sleep(ui_refresh_rate)
except KeyboardInterrupt:
# could use sys.signal handler instead
pass
print("exiting program, bye 👋")