-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotter.py
122 lines (107 loc) · 5.52 KB
/
plotter.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from datetime import datetime, timedelta
import os
import json
import random
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import gitlab
import argparse
gitlab_token = 'YOUR_TOKEN'
gitlab_url = 'https://gitlab.com'
author_name = 'Your Name'
# use environment variables to set the token and url
# in order to set the environment variables in Windows, use the following command:
# setx GITLAB_TOKEN "YOUR_TOKEN"
if 'GITLAB_TOKEN' in os.environ:
gitlab_token = os.environ['GITLAB_TOKEN']
if 'GITLAB_URL' in os.environ:
gitlab_url = os.environ['GITLAB_URL']
if 'GITLAB_AUTHOR_NAME' in os.environ:
author_name = os.environ['GITLAB_AUTHOR_NAME']
parser = argparse.ArgumentParser(description='Create a plot of the time spent on each window.')
parser.add_argument('-d', '--date', help='The date of the plot to create. Format: dd-mm-yyyy')
parser.add_argument('-r', '--range', help='The range of dates to create plots for. Format: dd-mm-yyyy-dd-mm-yyyy')
parser.add_argument('-a', '--all', help='Create plots for all the dates in the timesheets folder', action='store_true')
parser.add_argument('-t', '--today', help='Create a plot for today', action='store_true')
parser.add_argument('-m', '--minTime', help='The minimum time spent on a window to be included in the plot. Default: 60 seconds', type=int)
parser.add_argument('-s', '--show', help='Show the plot. Default: False', action='store_true')
parser.add_argument('-w', '--workingHours', help='Only include windows that were opened during working hours. Default: False', action='store_true')
parser.add_argument('-g', '--gitlab', help='Fetch GitLab commits by me for the specific time period', action='store_true')
args = parser.parse_args()
workingHours = args.workingHours
minTime = args.minTime if args.minTime else 60
path = 'C:\\Users\\' + os.getlogin() + '\\timesheets\\'
# check if output folder exists
if not os.path.exists(path + 'output'):
os.makedirs(path + 'output')
def createPlot(date):
# check if timesheets file exists
if args.gitlab:
messages = []
day = datetime.strptime(date, '%d-%m-%Y')
commits = project.commits.list(ref_name='master', since=day, until=day+timedelta(days=1), get_all=True)
for commit in commits:
if commit.author_name == author_name:
messages.append(commit.title)
if os.path.exists(f'{path}/timesheets_{date}.json'):
with open(f'{path}/timesheets_{date}.json', 'r',encoding='utf_8') as f:
dict = json.load(f)
plot = plt.figure()
plot.suptitle(f'Total time spent: {round(sum([dict[key]["totalTime"] for key in dict])/3600,2)} hours')
# reorder the dict by descending total time
dict = {k: v for k, v in sorted(dict.items(), key=lambda item: item[1]['totalTime'], reverse=False)}
for key in dict:
dict[key]['color']=f'#{random.randint(0, 0xFFFFFF):06x}'
if dict[key]["totalTime"] > minTime:
# set key color to a random color
for time in dict[key]["times"]:
if time[0] != None and time[1] != None:
if workingHours:
if datetime.strptime(time[0], "%H:%M:%S").hour >= 9 and datetime.strptime(time[1], "%H:%M:%S").hour <= 18:
plt.plot([datetime.strptime(time[0], "%H:%M:%S"), datetime.strptime(time[1], "%H:%M:%S")], [key, key], label=dict[key]["name"], linewidth=5, color=dict[key]['color'])
else:
plt.plot([datetime.strptime(time[0], "%H:%M:%S"), datetime.strptime(time[1], "%H:%M:%S")], [key, key], label=dict[key]["name"], linewidth=5, color=dict[key]['color'])
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plt.gcf().subplots_adjust(left=0.4, right=0.99, top=0.95, bottom=0.1)
# show the plot
# plt.show()
plt.gcf().set_size_inches(1920/100, 1080/100)
# show gitlab commits
if args.gitlab:
plt.text(0.01, 0.1, f'Commits for {date}:', transform=plt.gcf().transFigure, fontsize=8)
for i, message in enumerate(messages):
plt.text(0.01, 0.1 - (i+1)*0.01, message, transform=plt.gcf().transFigure, fontsize=8)
plot.savefig(f'{path}/output/{date}.png', bbox_inches='tight', pad_inches=0.2)
else:
print(f'No timesheets file found for {date}')
return
if args.gitlab:
gl = gitlab.Gitlab(gitlab_url,private_token=gitlab_token)
gl.auth()
# get project sfcc-mirakl
project = gl.projects.list(search='sfcc-mirakl')[0]
if args.date:
createPlot(args.date)
elif args.range:
dates = args.range.split('-')
start = datetime.strptime(dates[0], '%d-%m-%Y')
end = datetime.strptime(dates[1], '%d-%m-%Y')
while start <= end:
createPlot(start.strftime('%d-%m-%Y'))
start += timedelta(days=1)
elif args.all:
for file in os.listdir(path):
if file.startswith('timesheets_'):
createPlot(file.split('_')[1].split('.')[0])
start = datetime.now().replace(day=1)
end = datetime.now()
elif args.today:
start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
end = datetime.now()
createPlot(datetime.now().date().strftime('%d-%m-%Y'))
else:
start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
end = datetime.now()
createPlot(datetime.now().date().strftime('%d-%m-%Y'))
if args.show:
plt.show()