-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (109 loc) · 3.53 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
import orm
from orm.document import Document, PresentationDocument
from orm.fields import StringField, IntField, BooleanField, DateField
orm.connect(
redis={
'host': '0.0.0.0',
'port': 1234,
'db': 0,
'password': 'password'
},
firebase={
'cred': '',
'database': 'https://fr.firebaseio.com/',
'bucket': 'fr.appspot.com',
}
)
class IncidentPointer(PresentationDocument):
_container = '/incidentEvents'
incident_id = StringField(id=True)
owner = StringField()
class OngoingIncidentPointer(PresentationDocument):
_container = '/ongoingEvents'
incident_id = StringField(id=True)
owner = StringField()
class Incident(Document):
_container = '/incidents'
incident_id = StringField(id=True, presentation=True)
confirmed = BooleanField(default=False)
action = StringField(required=True)
firstResponder = StringField(presentation=True)
owner = StringField(presentation=True)
reliability = IntField(default=0, presentation=True)
confidence = IntField(default=0, presentation=True)
created = DateField(presentation=True)
@staticmethod
def on_save(document):
if 'confirmed' not in document.__changed__:
return
ip = IncidentPointer(
**document,
ignore_non_existing=True
)
op = OngoingIncidentPointer(
**document,
ignore_non_existing=True
)
if document.confirmed:
ip.save(force=True)
op.delete()
else:
op.save(force=True)
ip.delete()
@staticmethod
def on_delete(document):
IncidentPointer(
**document,
ignore_non_existing=True
).delete()
OngoingIncidentPointer(
**document,
ignore_non_existing=True
).delete()
@classmethod
def presentation(cls, document):
return {
'id': document.incident_id,
'rel': document.reliability * document.confidence,
'created': document.created.isoformat(),
'owner': document.owner.replace(' ', '-'),
'firstResponder': document.firstResponder
}
if __name__ == "__main__":
# TODO: Add unit tests
import datetime
from enum import Enum
class Type(Enum):
CREATE = 1
DELETE = 2
UPDATE_PRESENTATION = 3
UPDATE_CACHE = 4
UPDATE_FULL = 5
UPDATE_CACHE_ONLY = 6
action = Type.CREATE
for i in range(0, 20):
if action == Type.CREATE:
Incident(
incident_id=i,
owner='123',
reliability=i % 3,
confidence=i * 2,
confirmed=not bool(i % 2),
action='create',
created=datetime.datetime.utcnow()
).save()
elif action == Type.DELETE:
Incident.get(i).delete()
elif action == Type.UPDATE_PRESENTATION:
with Incident.get(i) as incident:
incident.reliability = i / 4
elif action == Type.UPDATE_CACHE:
with Incident.get(i) as incident:
incident.confirmed = not incident.confirmed
elif action == Type.UPDATE_FULL:
with Incident.get(i) as incident:
incident.firstResponder = 'AAB'
incident.action = 'cleared'
elif action == Type.UPDATE_CACHE_ONLY:
with Incident.get(i) as incident:
incident.action = "hey"