-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.py
93 lines (77 loc) · 2.58 KB
/
timeline.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
from dateutil.relativedelta import relativedelta
from dateutil.parser import parse
import random
class Timeline:
def __init__(self):
self.elements = []
def has_elements(self):
return not (len(self.elements) == 0)
def add_element(self, element):
self.elements.append(element)
def createTimeLine(self):
endtimes = []
starttimes = []
for e in self.elements:
if e.acquired < e.start:
starttimes.append(e.start)
else:
starttimes.append(e.acquired)
endtimes.append(e.stop)
start = min(starttimes)
stop = max(endtimes)
tlid = helpers.generate_uuid()
data = {
"type": "timelines",
"id": tlid,
"attributes": {
"start": start.isoformat(),
"end": stop.isoformat()
},
"relationships": {
"measurements": {
"data": [
]
}
}
}
included = []
while start <= stop:
count = 0
for e in self.elements:
if e.is_active_skill_on(start): count += 1
mId = helpers.generate_uuid()
m = Measurement(start, count, tlid, mId)
included.append(m.jsonify())
data["relationships"]["measurements"]["data"].append({"type": "measurements", "id": mId})
start = start + relativedelta(weeks=1)
return {"data": data, "included": included}
class TimelineElement:
def __init__(self, acquired, start, stop):
self.acquired = parse(acquired)
self.start = parse(start)
self.stop = parse(stop)
def is_active_skill_on(self, theDate):
return theDate >= self.acquired and self.start <= theDate <= self.stop
class Measurement:
def __init__(self, date, value, tlId, mId):
self.date = date
self.value = value
self.measurementId = mId
self.timelineId = tlId
def jsonify(self):
return {
"type": "measurements",
"id": self.measurementId,
"attributes": {
"date": self.date,
"value": self.value
},
"relations": {
"part-of": {
"data": {
"type": "timelines",
"id": self.timelineId
}
}
}
}