-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigilant.py
executable file
·96 lines (78 loc) · 2.54 KB
/
vigilant.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
#!/usr/bin/env python3
'''
Objectives
----------
1. Make the checkpoints as light as possible.
2. Provide good graphical output
'''
import argparse
import datetime
import sys
import json
DATE_FORMAT = '%a %b %d %Y' # 'Sun Mar 17 2019'
LOG_FILE = '/Users/briantracy/Desktop/projects/vigilant/file.json'
def add_checkpoint(data, interval):
print('{} {} minutes have passed'.format(datetime.datetime.now(), interval))
ts = today_string()
prev = data.get(ts, 0)
data[ts] = prev + interval
def read_log(fname):
try:
with open(fname, 'r') as l:
return json.load(l)
except:
return {'program_name': 'vigilant', 'data': {}}
def write_log(log, fname):
try:
with open(fname, 'w') as l:
l.write(json.dumps(log, indent=2, sort_keys=True))
except:
print('Could not write to log: ' + LOG_FILE)
sys.exit(1)
def today_string():
return datetime.datetime.now().strftime(DATE_FORMAT)
def sort_keys(data):
return sorted(data.keys(), key=lambda s: datetime.datetime.strptime(s, DATE_FORMAT))
def print_table(data):
if not data:
print('no data to display')
return
horz = '+' + '-'*(len(today_string()) + 2) + '+' + \
'-'*(len('00h00m')+2) + '+'
def table_time(mins):
return '{:02d}h{:02d}m'.format(mins // 60, mins % 60)
def row(l, r, mins):
print('| {} | {} | {} '.format(l, r, '*' * (mins // 30)))
times = []
for key in sort_keys(data):
print(horz)
mins = data[key]
times.append(mins)
row(key, table_time(mins), mins)
print(horz)
def average(l):
return sum(l) // len(l)
def median(l):
if len(l) % 2 == 0:
return (l[len(l) // 2] + l[(len(l) // 2) - 1]) // 2
else:
return l[len(l) // 2]
print('avg: ' + table_time(average(times)))
times.sort()
print('med: ' + table_time(median(times)))
print('max: ' + table_time(max(times)))
def main(args):
log = read_log(LOG_FILE)
if args.interval is not None:
add_checkpoint(log['data'], args.interval)
write_log(log, LOG_FILE)
elif args.disp_log:
print(LOG_FILE)
else:
print_table(log['data'])
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-l', action='store_true', dest='disp_log', help='display the path to the log file')
parser.add_argument('--checkpoint', type=int, dest='interval', help='number of minutes between checkpoints')
args = parser.parse_args()
main(args)