forked from artynusov/MacTimeLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistics.py
119 lines (97 loc) · 3.73 KB
/
Statistics.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
#
# Statistics.py
# Statistic generator
#
# Copyright 2009 Artem Yunusov. All rights reserved.
#
from DataManager import DataManager
from Timings import Timings
from Tasks import Tasks
class Statistics(object):
_avgWork = 0
_avgSlacking = 0
_maxValue = 0
def __init__(self, fromDate, toDate):
self.timings = Timings()
self._fromDate = fromDate
self._toDate = toDate
def _countAttrib(self, res):
"""Count basic attributes(average work, average slacking and maximum value)"""
days = (self._toDate - self._fromDate).days
self._avgWork = int(self.timings.spentSeconds / days)
self._avgSlacking = int(self.timings.slackingSeconds / days)
if res:
self._maxValue = max(res)
def _countObject(self, objType, targetAction):
"""Generic function for calculating projects data or slacking statistics"""
self._data = DataManager.getByRange(self._fromDate, self._toDate)
res = {}
for date, task, projectName in self._data:
if task == "__start__":
self.timings.setPrevDate(None)
objKey = projectName if objType == "project" else task
spentSeconds = self.timings.count(date, Tasks.taskType(task))
if Tasks.taskType(task) != targetAction:
self.timings.setPrevDate(date)
continue
if spentSeconds:
if objKey not in res:
res[objKey] = spentSeconds
else:
res[objKey] += spentSeconds
self._countAttrib(res.values())
if res:
return sorted(res.iteritems(), key=lambda item:item[1], reverse=True)
else:
return []
def countProjects(self):
"""Count project statistics"""
return self._countObject("project", "work")
def countSlacking(self):
"""Count slacking statistics"""
return self._countObject("task", "slack")
def countTasks(self):
"""Count tasks statistics divided by projects"""
self._data = DataManager.getByRange(self._fromDate, self._toDate)
res = {}
for date, task, projectName in self._data:
if task == "__start__":
self.timings.setPrevDate(None)
spentSeconds = self.timings.count(date, Tasks.taskType(task))
if Tasks.taskType(task) != "work":
continue
if spentSeconds:
if projectName not in res:
res[projectName] = {}
if task not in res[projectName]:
res[projectName][task] = spentSeconds
else:
res[projectName][task] += spentSeconds
self._countAttrib([v for k in res for v in res[k].values()])
if res:
ret = {}
for k in res.keys():
ret[k] = sorted(res[k].iteritems(), key=lambda item:item[1], reverse=True)
return ret
else:
return {}
@property
def maxValue(self):
"""Maximum value of calculated object (project /task / slackings)"""
return self._maxValue
@property
def totalWork(self):
"""Total work done"""
return self.timings.spentSeconds
@property
def avgWork(self):
"""Average work per day"""
return self._avgWork
@property
def totalSlacking(self):
"""Total slacking"""
return self.timings.slackingSeconds
@property
def avgSlacking(self):
"""Average slacking per day"""
return self._avgSlacking