localwaka/goalctl

81 lines
2.3 KiB
Text
Raw Permalink Normal View History

2020-07-15 19:23:40 +00:00
#!/usr/bin/env python3
# kinds of goals:
# - overall
# "i want to code at least {time} per {interval}"
# - in a project
# - in a language
# - in an editor
from datetime import timedelta, date, datetime, time
import sys
import json
import sqlite3
from config import timeout
2020-12-21 20:06:03 +00:00
import utils
2020-07-15 19:23:40 +00:00
conn = sqlite3.connect("data.db")
if len(sys.argv) < 2:
print("provide a file", file=sys.stderr)
sys.exit(1)
filename = sys.argv[1]
with open(filename) as f:
data = json.load(f)
goal_type = data["type"]
if goal_type == "overall":
# in seconds
goal_min = int(data["goal_min"])
# figure out what timestamps to pull from
if data["interval"] == "day":
# except!
def convert_weekday(s):
s = s.lower()
return dict(
sunday=0,
monday=1,
tuesday=2,
wednesday=3,
thursday=4,
friday=5,
saturday=6,
).get(s)
excepts = list(map(convert_weekday, data["except"]))
if len(sys.argv) > 2:
# if a day is given, print that day
d = datetime.strptime(sys.argv[2], "%Y-%m-%d").date()
start_time = datetime.combine(d, time(hour=0, minute=0, second=0))
end_time = datetime.combine(d, time(hour=23, minute=59, second=59))
else:
# find the current day!
today = date.today()
start_time = datetime.combine(today, time(hour=0, minute=0, second=0))
end_time = datetime.now()
2020-12-21 20:06:03 +00:00
coded_secs = utils.get_seconds_coded(conn, start_time, end_time, timeout)
print("minutes coded:", (coded_secs/60))
2020-07-15 19:23:40 +00:00
2020-10-14 22:16:30 +00:00
# get all the entries
c = conn.cursor()
curs = c.execute("""
select time from heartbeats where time > ? and time < ? order by time;
""", [start_stamp, end_stamp])
coded_secs = 0
for (first, second) in window(curs, 2):
diff = second[0] - first[0]
if diff < timeout:
coded_secs += diff
print("coded: {}:{:02d} min".format(int(coded_secs)//60, int(coded_secs)%60))
2020-07-15 19:23:40 +00:00
elif data["interval"] == "week":
# start of the week
today = date.today()
last_monday = today - timedelta(days=today.weekday())
print(last_monday)
# vim: set ft=python: