-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreport_generator.py
More file actions
50 lines (36 loc) · 1.29 KB
/
report_generator.py
File metadata and controls
50 lines (36 loc) · 1.29 KB
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
import os
import numpy
import pandas
from pandas.plotting import register_matplotlib_converters
from read_config import get_config
register_matplotlib_converters()
import matplotlib.pyplot as plt
# read config file
config = get_config()
# log file location
log_file = os.path.expanduser(config["log_file"])
df = pandas.read_csv(log_file, parse_dates=[0], index_col=0)
# convert to local timezone
df = df.tz_localize("UTC").tz_convert("Europe/Berlin")
# display only last two weeks of records
cond = (df.index >= df.index.max() - pandas.Timedelta(days=config["report_days"]))
df = df[cond]
# calculate duration
df['duration'] = numpy.roll(df.index.to_series(keep_tz=True).diff(),-1)#.astype("timedelta64[h]")
# clean non-numerical and "Done"-entries
df = df.dropna()
df = df.where(df['action'] != "Done")
# make report
df_grouped = df.groupby([df.index.date, "action"])
report = df_grouped['duration'].sum()
print(report)
ru = report.unstack(0).fillna(pandas.Timedelta(0))
# make a stacked bar plot
bottom = numpy.zeros(len(ru.columns))
for action_label, row in ru.iterrows():
hours = row.values.astype("double") / (3600 * 1e9) # nano-seconds to hours
plt.bar(ru.columns, hours, label=action_label, bottom=bottom)
bottom += hours
plt.title("Tracked Time in hours")
plt.legend()
plt.show()