aboutsummaryrefslogtreecommitdiff
path: root/src/sb_nueve
blob: 1d0a315540e53ead6f5d2e489790915b2d8b81ee (plain)
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
#!/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]:
		try:
			key, val = line.split(',')
		except:
			print(report_fn)
		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)
		daily_report = parse_for_day(this_one_iso)

		for x in daily_report:
			if not x in weekly_report:
				weekly_report[x] = daily_report[x]
			else:
				if type(daily_report[x]) == int:
					weekly_report[x] += daily_report[x]

	return weekly_report

def parse_for_month(day):
	tm = time.strptime(day, '%Y-%m-%d')
	since_epoch = time.mktime(tm)
	monthday = tm.tm_mday

	print(tm)

	monthly_report = {}

	for x in range(0, tm.tm_mday):
		this_one = time.localtime(since_epoch - (x * 86400))
		this_one_iso = time.strftime('%Y-%m-%d', this_one)
		try:
			daily_report = parse_for_day(this_one_iso)
		except:
			pass # fuck you

		for x in daily_report:
			if not x in monthly_report:
				monthly_report[x] = daily_report[x]
			else:
				if type(daily_report[x]) == int:
					monthly_report[x] += daily_report[x]

	return monthly_report

def parse_for_year(day):
	tm = time.strptime(day, '%Y-%m-%d')
	since_epoch = time.mktime(tm)
	yearday = tm.tm_yday

	yearly_report = {}

	for x in range(0, tm.tm_yday):
		this_one = time.localtime(since_epoch - (x * 86400))
		this_one_iso = time.strftime('%Y-%m-%d', this_one)
		try:
			daily_report = parse_for_day(this_one_iso)
		except:
			pass # fuck you

		for x in daily_report:
			if not x in yearly_report:
				yearly_report[x] = daily_report[x]
			else:
				if type(daily_report[x]) == int:
					yearly_report[x] += daily_report[x]

	return yearly_report

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'))

			print("""total keystrokes: %s""" % (report["total keypresses"]))
		if args.timeframe == 'w':
			report = parse_for_week(time.strftime('%Y-%m-%d'))
			print("""total keystrokes: %s""" % (report["total keypresses"]))
		if args.timeframe == 'm':
			report = parse_for_month(time.strftime('%Y-%m-%d'))
			print("""total keystrokes: %s""" % (report["total keypresses"]))
		if args.timeframe == 'y':
			report = parse_for_year(time.strftime('%Y-%m-%d'))
			print("""total keystrokes: %s""" % (report["total keypresses"]))

	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())