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
|
#!/usr/bin/env python3
import argparse
import time
import sys
import os
STROKE_BUDDY_DIR = "/home/spv/.local/stroke_buddy"
def parse_hourly_report(report_fn):
report_f = open(report_fn, "r")
report = report_f.read()
report_f.close()
ret = {}
for line in report.split("\n")[:-1]:
key, val = line.split(',')
if key == "total keypresses": val = int(val)
if key == "work time": val = float(val)
if key == "avg keys/sec": val = float(val) if val != "-nan" else None
if key == "idle time": val = float(val)
ret[key] = val
return ret
def parse_for_day(day):
all_logs = os.listdir(STROKE_BUDDY_DIR)
daily_report = {}
total_keys = 0
work_time = 0
idle_time = 0
per_hour = {}
for log in all_logs:
if not day in log:
continue
hourly_report = parse_hourly_report(STROKE_BUDDY_DIR + '/' + log)
hour = log.split('@')[1].split('.')[0]
keys = 0
hourly_work = 0
hourly_idle = 0
for key in hourly_report:
if key == "total keypresses":
keys = hourly_report[key]
if key == "work time":
hourly_work = hourly_report[key]
if key == "idle time":
hourly_idle = hourly_report[key]
hourly_idle = hourly_idle if hourly_idle > 0.0 else 3600.0
total_keys += keys
work_time += hourly_work
idle_time += hourly_idle if hourly_idle > 0.0 else 3600.0
per_hour[hour] = {"total keypresses": keys, "work time": hourly_work, "idle time": hourly_idle}
# print(log, daily_report)
top_hour = '0'
top_hourly_keys = 0
for hour in per_hour:
if per_hour[hour]["total keypresses"] > top_hourly_keys:
top_hourly_keys = per_hour[hour]["total keypresses"]
top_hour = hour
daily_report["total keypresses"] = total_keys
daily_report["work time"] = work_time
daily_report["idle time"] = idle_time
daily_report["top hour"] = [top_hour, top_hourly_keys]
return daily_report
def parse_for_week(day):
tm = time.strptime(day, '%Y-%m-%d')
since_epoch = time.mktime(tm)
weekday = tm.tm_wday + 1
weekly_report = {}
for x in range(weekday, -1, -1):
this_one = time.localtime(since_epoch - (x * 86400))
this_one_iso = time.strftime('%Y-%m-%d', this_one)
parse_for_day(this_one_iso)
def main():
parser = argparse.ArgumentParser(description='stroke_buddy statistics')
# parser.add_argument('-r', '--report', type=str, default="today", help='(today|week|month|year|all', choices=["today", "week", "month", "year", "all"])
cmd_group = parser.add_mutually_exclusive_group()
cmd_group.add_argument('-r', '--report', action='store_const', dest='action', const='r')
cmd_group.add_argument('-c', '--count', action='store_const', dest='action', const='c')
timeframe_group = parser.add_mutually_exclusive_group()
timeframe_group.add_argument('-t', '--today', action='store_const', dest='timeframe', const='t')
timeframe_group.add_argument('-w', '--week', action='store_const', dest='timeframe', const='w')
timeframe_group.add_argument('-m', '--month', action='store_const', dest='timeframe', const='m')
timeframe_group.add_argument('-y', '--year', action='store_const', dest='timeframe', const='y')
timeframe_group.add_argument('-a', '--all', action='store_const', dest='timeframe', const='a')
parser.set_defaults(action='c', timeframe='t')
args = parser.parse_args()
# print(args)
# print(parse_hourly_report('/home/spv/.local/stroke_buddy/2025-12-05@9.log'))
# print(parse_for_day("2025-12-05"))
report = None
if args.action == 'r':
if args.timeframe == 't':
report = parse_for_day(time.strftime('%Y-%m-%d'))
if args.timeframe == 'w':
report = parse_for_week(time.strftime('%Y-%m-%d'))
if args.action == 'c':
if args.timeframe == 't':
report = parse_for_day(time.strftime('%Y-%m-%d'))
print(report["total keypresses"])
return 0
if __name__ == "__main__":
sys.exit(main())
|