-
Notifications
You must be signed in to change notification settings - Fork 0
/
jurgen_sessions
executable file
·295 lines (240 loc) · 9.91 KB
/
jurgen_sessions
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/python
from __future__ import division
from icalendar import Calendar, Event
import datetime
import argparse
import glob
import os
debug = False
max_dist_between_logs = 15 # in minutes
min_session_size = 15 # in minutes
vision_dir = "/home/joereddington/Jurgen/nextActions/"
__TIME_FORMAT = "%d/%m/%Y-%H:%M:%S "
######################################################################
# Todo:
# The ability to recognise github commits and screenshots as input
# Output to Google Calendar!
# Given we have the timing data for each session - we can use this to import other information into the file for example
#*terminal commands/scripts
#*the desktop tracking information
######################################################################
def addEvent(cal, summary, start,end, uid):
event = Event()
event.add('summary', summary)
event.add('dtstart', start)
event.add('dtend', end)
event.add('dtstamp', end)
event['uid'] = summary+str(start)+str(end)
event.add('priority', 5)
cal.add_component(event)
class Session(object):
project = "Unknown"
start = ""
end = ""
def __init__(self, project, start, end):
self.project, self.start, self.end = project, start, end
def length(self):
return self.end-self.start
def __str__(self):
return " {} to {} ({})".format(
self.start, self.end.time(), self.length())
def setup_argument_list():
"creates and parses the argument list for naCount"
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'-v',
dest='verbatim',
action='store_true',
help='Verbose mode')
parser.set_defaults(verbatim=False)
parser.add_argument( '-d', dest='debug', action='store_true', help='Debugging mode')
parser.add_argument( '-c', dest='calendar', action='store_true', help='Calendar Output')
parser.set_defaults(debug=False)
parser.add_argument("action", help='What to show')
parser.add_argument("delay", nargs='?', default=0,
help='A delay to add in days')
parser.add_argument(
"context", nargs='?', default="0",
help='The context for the action')
parser.add_argument(
"priority", nargs='?', default="0",
help='From 0 to 7 how important is this action')
return parser.parse_args()
def process_project_file(fname, sessions):
with open(fname) as f:
content = f.readlines()
sessions.extend(process_content(content, fname))
return sessions
def get_timevalues(content):
content=[line.replace("BST","GMT") for line in content]
timestamplines = [line for line in content
if "GMT" in line if "#####" in line]
#Now remove the Unhelpful timezone from the lines and the #####
timevalues = [line.split("GMT")[0][7:] for line in timestamplines]
return timevalues
def process_content(content, fname):
"Parses content of Vision file, expected to be MarkDown"
sessions = []
last = datetime.datetime.strptime(
"11/07/2010-10:00:06 ", __TIME_FORMAT)
current = last
start = 0
title = fname
action = ""
in_session = False
# Find title
for line in content:
if "title:" in line:
title = line.split(":")[1].strip()
break
# Get the set of times
timevalues = get_timevalues(content)
for line in timevalues:
current = datetime.datetime.strptime(
line, __TIME_FORMAT)
if (in_session):
# See if the session should end
if ((current-last) > datetime.timedelta(
minutes=max_dist_between_logs)):
action = "end at " + \
(str(last))
in_session = False
# See if the session was long enough to store
if ((last-start) > datetime.timedelta(
minutes=min_session_size)):
sessions.append(
Session(title, start, last))
# last is the end of the session, but
# current is the start of the new one
start = current
in_session = True
else:
action = "continue"
else:
start = current
in_session = True
action = "Start"
last = current
if (debug):
print "Line was {}, action was {}".format(line.strip(), action)
# If all timestamps have been proceed, and you are still in a session,
# then end the session.
if (in_session):
if ((last-start) > datetime.timedelta(minutes=min_session_size)):
sessions.append(Session(title, start, last))
return sessions
def projectreport(name, sessions, verbose):
"Produce report for one project"
project_sessions = [
entry for entry in sessions if (
entry.project == name)]
total_time = sum([entry.length()
for entry in project_sessions], datetime.timedelta())
if verbose:
print "#### {}\n\nTotal Time on this project: {}\n".format(name.ljust(45), total_time)
for entry in project_sessions:
print entry
else:
print "{}: {}".format(name.ljust(45), total_time)
return total_time
def all_sessions_in_folder():
sessions = []
for file in glob.glob("/home/joereddington/Jurgen/nextActions/jurgen.md"):
#print file
sessions = process_project_file(file, sessions)
return sessions
def day_projects(sessions, today=datetime.datetime.today()):
single_date_sessions = [
entry for entry in sessions if (
entry.start.date() == today.date())]
total_time = sum(
[entry.length() for entry in single_date_sessions],
datetime.timedelta())
projects = list(
set([entry.project for entry in single_date_sessions]))
for project in projects:
projectreport(
project, single_date_sessions, args.verbatim)
print "### Summary for {}\nTotal project time - {}".format(today.date(), total_time)
def all_projects(sessions):
total_time = sum([entry.length()
for entry in sessions], datetime.timedelta())
projects = list(set([entry.project for entry in sessions]))
for project in projects:
projectreport(project, sessions, args.verbatim)
print "Total project time".ljust(45)+str(total_time)
def calendarreport(name, sessions, verbose):
"Produce report for one project"
project_sessions = [
entry for entry in sessions if (
entry.project == name)]
total_time = sum([entry.length()
for entry in project_sessions], datetime.timedelta())
for entry in project_sessions:
add
return total_time
def calendar_output(sessions):
cal = Calendar()
cal.add('prodid', '-//My calendar product//mxm.dk//')
cal.add('version', '2.0')
for entry in sessions:
addEvent(cal,entry)
print cal.to_ical()
def addEvent(cal, entry):
event = Event()
event.add('summary', entry.project)
event.add('dtstart', entry.start)
event.add('dtend', entry.end)
event.add('dtstamp', entry.end)
event['uid'] = str(entry).replace(" ","")
event.add('priority', 5)
cal.add_component(event)
#From SE http://stackoverflow.com/questions/13728392/moving-average-or-running-mean
### Running mean/Moving average
def get_running_mean(l, N):
sum = 0
result = list( 0 for x in l)
for i in range( 0, N ):
sum = sum + l[i]
result[i] = sum / (i+1)
for i in range( N, len(l) ):
sum = sum - l[i-N] + l[i]
result[i] = sum / N
return result
def graph_out(sesssions):
DAY_COUNT = 26
total_time = []
for single_date in (
datetime.datetime.today() - datetime.timedelta(days=n)
for n in range(DAY_COUNT)):
single_date_sessions = [
entry for entry in sessions if (
entry.start.date() == single_date.date())]
element = int(sum([entry.length(
) for entry in single_date_sessions], datetime.timedelta()).total_seconds()/60)
total_time = [element]+total_time
running_mean=get_running_mean(total_time,7)
print "jurgen_sessions=["+",".join(str(x) for x in total_time)+"]"
print "jurgen_running_mean=["+",".join(str(x) for x in running_mean)+"]"
args = setup_argument_list()
os.chdir(vision_dir)
sessions = all_sessions_in_folder()
if args.action != "calendar":
if args.action == "graph":
graph_out(sessions)
else:
print "# Project Logs\n"
"""
#### Work Graph
Total Time on this project: 1:43:26
##### Sessions
2016-08-12 06:16:26 to 06:36:32 (0:20:06)
2016-08-12 07:58:21 to 09:21:41 (1:23:20)
### Summary for 2016-08-12
Total project time - 1:43:26"""
if args.action == "today":
day_projects(sessions)
if args.action == "projects":
all_projects(sessions)
if args.action == "calendar":
calendar_output(sessions)