aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorspv <spv@spv.sh>2025-12-05 13:31:33 -0500
committerspv <spv@spv.sh>2025-12-05 13:32:12 -0500
commit0c01714c06d2dfb91b759ba9852c79e815aa0ffd (patch)
treed17de7d61f802a60088da259cf4acc5fb609c244 /src
parent71a46e756e5af9c27cfaf54cb6337cec3966c01f (diff)
add wip python rewrite of `sb`
Diffstat (limited to 'src')
-rwxr-xr-xsrc/sb_nueve136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/sb_nueve b/src/sb_nueve
new file mode 100755
index 0000000..0c3fa0d
--- /dev/null
+++ b/src/sb_nueve
@@ -0,0 +1,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())