-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodels.py
115 lines (97 loc) · 2.95 KB
/
models.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
import json
from stores import actors_store, logs_store
class DbDict(dict):
def __getattr__(self, key):
return self[key]
def __setattr__(self, key, value):
self[key] = value
def to_db(self):
return json.dumps(self)
class Actor(DbDict):
"""Basic data access object for working with Actors."""
def __init__(self, d):
super(Actor, self).__init__(d)
if not self.get('id'):
self['id'] = Actor.get_id(self.name)
@classmethod
def get_id(cls, name):
idx = 0
while True:
id = name + "_" + str(idx)
if id not in actors_store:
return id
idx = idx + 1
@classmethod
def from_db(cls, s):
a = Actor(json.loads(s))
return a
@classmethod
def set_status(cls, actor_id, status):
"""Update the status of an actor"""
actor = Actor.from_db(actors_store[actor_id])
actor.status = status
actors_store[actor.id] = actor.to_db()
class Subscription(DbDict):
"""Basic data access object for an Actor's subscription to an event."""
def __init__(self, actor, d):
super(Subscription, self).__init__(d)
if not self.get('id'):
self['id'] = Subscription.get_id(actor)
@classmethod
def get_id(cls, actor):
idx = 0
actor_id = actor.id
try:
subs = actor.subscriptions
except AttributeError:
return actor_id + "_sub_0"
if not subs:
return actor_id + "_sub_0"
while True:
id = actor_id + "_sub_" + str(idx)
if id not in subs:
return id
idx = idx + 1
class Execution(DbDict):
"""Basic data access object representing an Actor execution."""
def __init__(self, actor, d):
super(Execution, self).__init__(d)
if not self.get('id'):
self['id'] = Execution.get_id(actor)
@classmethod
def get_id(cls, actor):
idx = 0
actor_id = actor.id
try:
excs = actor.executions
except KeyError:
return actor_id + "_exc_0"
if not excs:
return actor_id + "_exc_0"
while True:
id = actor_id + "_exc_" + str(idx)
if id not in excs:
return id
idx = idx + 1
@classmethod
def add_execution(cls, actor_id, ex):
"""
Add an execution to an actor.
:param actor_id: str
:param ex: dict
:return:
"""
actor = Actor.from_db(actors_store[actor_id])
execution = Execution(actor, ex)
actor.executions[execution.id] = execution
actors_store[actor_id] = actor.to_db()
return execution.id
@classmethod
def set_logs(cls, exc_id, logs):
"""
Set the logs for an execution.
:param actor_id: str
:param ex: dict
:return:
"""
logs_store[exc_id] = logs