Files
journal/progress.py
2021-05-31 12:47:57 +03:00

78 lines
2.1 KiB
Python

import sys
from collections import defaultdict
from datetime import datetime, timedelta
import math
from common import parse_timestamp
content = open(sys.argv[1]).read().strip()
lines = content.splitlines()
current_chapter = ''
total_chapters = 0
completed_chapters = set()
today = datetime.now().replace(hour=0,minute=0,second=0,microsecond=0)
this_week = today - timedelta(days=7)
total_hours = 0.0
day_hours = 0.0
week_hours = 0.0
oldest_timestamp = datetime.now()
i = 0
while i < len(lines):
line = lines[i].strip()
if line.startswith('#'):
current_chapter = line[line.find(' ')+1:]
total_chapters += 1
else:
completed_chapters.add(current_chapter)
if line.startswith('@start'):
start = parse_timestamp(line.removeprefix('@start '))
if start < oldest_timestamp:
oldest_timestamp = start
i += 1
line = lines[i].strip()
end = parse_timestamp(line.removeprefix('@stop '))
delta = end - start
hours = delta.seconds / 60 / 60
total_hours += hours
if start > this_week:
week_hours += hours
if start > today:
day_hours += hours
i += 1
completed_chapters = len(completed_chapters)
num_days = (datetime.now() - oldest_timestamp).days or 1
hours_per_day = total_hours / num_days
hours_per_chapter = total_hours / completed_chapters
hours_to_completion = hours_per_chapter * (total_chapters - completed_chapters)
days_to_completion = math.ceil(hours_to_completion / hours_per_day)
completion_date = datetime.now() + timedelta(days=days_to_completion)
completion_percentage = completed_chapters/total_chapters*100
print(f'Started on: {oldest_timestamp.strftime("%Y-%m-%d")}')
print(f'Progress: [{completed_chapters}/{total_chapters}] ({round(completion_percentage, 2)}%)')
print(f'Total: {round(total_hours, 2)}h')
print(f'Week: {round(week_hours, 2)}h')
print(f'Day: {round(day_hours, 2)}h')
print(f'Hours per day: {round(hours_per_day, 2)}h')
print(f'Hours to completion: {round(hours_to_completion, 2)}h')
print(f'Completion date: {completion_date.strftime("%Y-%m-%d")}')