[work] Implement stats

This commit is contained in:
Lara 2020-02-19 17:59:28 -05:00
parent 278a9565e2
commit d9b0a0fbab

View file

@ -61,10 +61,69 @@ def get_today_fields():
def work(args): def work(args):
total_delta = timedelta()
month_delta = timedelta()
count = 0
month_count = 0
with open(TIME_FILE, "r") as f:
for line in f:
fields = line.strip().split(",", 5)
date = datetime.strptime(fields[0], "%Y-%m-%d")
t = datetime.strptime(fields[4], "%H:%M")
delta = timedelta(hours=t.hour, minutes=t.minute)
if date.month == start_time.month:
month_delta += delta
month_count += 1
total_delta += delta
count += 1
eight_h = timedelta(hours=8) * count
mean_time = total_delta / count
minutes = total_delta - eight_h
month_mean_time = month_delta / month_count
month_min = month_delta - timedelta(hours=(8 * month_count))
print("stats: {}, {}".format(
td_format(mean_time),
td_format(minutes)))
print("month stats: {}, {}".format(
td_format(month_mean_time),
td_format(month_min)))
# Try to calculate remaining time
fields = get_today_fields() fields = get_today_fields()
if len(fields) < 2: hour = start_time.strftime("%H:%M")
die("you haven't started working yet today") if not fields:
print("stats: {}".format(fields)) print("No work ongoing")
return
fields.append(hour)
# Test for even number of timestamp (but fields[0] is the date)
if len(fields) < 3:
die("not enough fields in {}".format(TODAY_FILE))
elif len(fields) % 2 == 0:
# Break in progress
log("Break in progress")
return
begin_time = None
worked_time = timedelta()
for field in fields[1:]:
try:
if begin_time is None:
begin_time = datetime.strptime(field, "%H:%M")
else:
end_time = datetime.strptime(field, "%H:%M")
worked_time += end_time - begin_time
begin_time = None
except ValueError:
die("couldn't parse field '{}' in {}".format(
field, TODAY_FILE))
day_start = datetime.strptime(fields[1], "%H:%M")
day_end = datetime.strptime(fields[-1], "%H:%M")
total_time = day_end - day_start
break_time = total_time - worked_time
day_end_estimation = day_start + timedelta(hours=8) + break_time
print("Worked {} already today. Estimated leave at {}".format(
td_format(worked_time),
day_end_estimation.strftime("%H:%M")))
def work_start(args): def work_start(args):