summaryrefslogtreecommitdiff
path: root/display.py
blob: 33697939b89eb03dcf75d82a16c88b8c2a968db2 (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
#!/usr/bin/python3
# displays stats as graph. assumes csv is in chronological order, with no
# overlapping sessions. as initial test: make a bar chart for time spent per
# tag, for every day
import pygal
import csv
import argparse
from collections import OrderedDict
import formatting as f
import os
import datetime

parser = argparse.ArgumentParser()
parser.add_argument("logfile", help="log file to display chart of",
                    type=argparse.FileType('r'))
args = parser.parse_args()

day_times = f.per_tag_per_day(args.logfile)

cache_dir = os.environ["HOME"] + "/.cache/chronologgy"
try:
    os.makedirs(cache_dir)
except FileExistsError:
    pass
except PermissionError:
    print("permission denied: unable to create " + config_dir)

# get first day; find its weekday so that running day_count starts correctly;
# first_week so that week numbers are correctly output
first_day = datetime.date.strptime(day_times[0]['date'], "%Y-%m-%d")
first_week = first_day.isocalendar().week
day_count = first_day.weekday()

week_times = [{}] # empty list to append to; list of dicts (tags)
wi = 0 # week index

for day in day_times:
    chart = pygal.Bar()
    pie = pygal.Pie()
    for key, value in day.items():
        if key == "date":
            continue
        week_times[wi][key] = week_times[wi].get(key, 0) + value # add days to corresponding weeks
        chart.add(key, round(value/60/60, 1))
        pie.add(key, round(value/60/60, 1))
    day_count += 1
    if day_count >= 6:
        day_count = 0
        week_times.append({})
        wi += 1
    chart.render_to_file(str(cache_dir + "/" + day["date"]+"-bar.svg"))
    pie.render_to_file(str(cache_dir + "/" + day["date"]+"-pie.svg"))

# currently each csv file should only contain one year... should be easy to fix later; TODO
year = str(first_day.isocalendar().year)
for week_num, week in enumerate(week_times):
    chart = pygal.Bar()
    pie = pygal.Pie()
    for key, value in week.items():
        chart.add(key, round(value/60/60, 1))
        pie.add(key, round(value/60/60, 1))
    chart.render_to_file(str(cache_dir + "/" + year+"-week"+str(first_week+week_num)+"-bar.svg"))
    pie.render_to_file(str(cache_dir + "/" + year+"-week"+str(first_week+week_num)+"-pie.svg"))