diff options
Diffstat (limited to 'event_log.py')
-rw-r--r-- | event_log.py | 302 |
1 files changed, 155 insertions, 147 deletions
diff --git a/event_log.py b/event_log.py index c77c5648..b1f8bdf9 100644 --- a/event_log.py +++ b/event_log.py | |||
@@ -15,161 +15,169 @@ | |||
15 | import json | 15 | import json |
16 | import multiprocessing | 16 | import multiprocessing |
17 | 17 | ||
18 | TASK_COMMAND = 'command' | 18 | TASK_COMMAND = "command" |
19 | TASK_SYNC_NETWORK = 'sync-network' | 19 | TASK_SYNC_NETWORK = "sync-network" |
20 | TASK_SYNC_LOCAL = 'sync-local' | 20 | TASK_SYNC_LOCAL = "sync-local" |
21 | 21 | ||
22 | 22 | ||
23 | class EventLog(object): | 23 | class EventLog(object): |
24 | """Event log that records events that occurred during a repo invocation. | 24 | """Event log that records events that occurred during a repo invocation. |
25 | 25 | ||
26 | Events are written to the log as a consecutive JSON entries, one per line. | 26 | Events are written to the log as a consecutive JSON entries, one per line. |
27 | Each entry contains the following keys: | 27 | Each entry contains the following keys: |
28 | - id: A ('RepoOp', ID) tuple, suitable for storing in a datastore. | 28 | - id: A ('RepoOp', ID) tuple, suitable for storing in a datastore. |
29 | The ID is only unique for the invocation of the repo command. | 29 | The ID is only unique for the invocation of the repo command. |
30 | - name: Name of the object being operated upon. | 30 | - name: Name of the object being operated upon. |
31 | - task_name: The task that was performed. | 31 | - task_name: The task that was performed. |
32 | - start: Timestamp of when the operation started. | 32 | - start: Timestamp of when the operation started. |
33 | - finish: Timestamp of when the operation finished. | 33 | - finish: Timestamp of when the operation finished. |
34 | - success: Boolean indicating if the operation was successful. | 34 | - success: Boolean indicating if the operation was successful. |
35 | - try_count: A counter indicating the try count of this task. | 35 | - try_count: A counter indicating the try count of this task. |
36 | 36 | ||
37 | Optionally: | 37 | Optionally: |
38 | - parent: A ('RepoOp', ID) tuple indicating the parent event for nested | 38 | - parent: A ('RepoOp', ID) tuple indicating the parent event for nested |
39 | events. | 39 | events. |
40 | 40 | ||
41 | Valid task_names include: | 41 | Valid task_names include: |
42 | - command: The invocation of a subcommand. | 42 | - command: The invocation of a subcommand. |
43 | - sync-network: The network component of a sync command. | 43 | - sync-network: The network component of a sync command. |
44 | - sync-local: The local component of a sync command. | 44 | - sync-local: The local component of a sync command. |
45 | 45 | ||
46 | Specific tasks may include additional informational properties. | 46 | Specific tasks may include additional informational properties. |
47 | """ | ||
48 | |||
49 | def __init__(self): | ||
50 | """Initializes the event log.""" | ||
51 | self._log = [] | ||
52 | self._parent = None | ||
53 | |||
54 | def Add(self, name, task_name, start, finish=None, success=None, | ||
55 | try_count=1, kind='RepoOp'): | ||
56 | """Add an event to the log. | ||
57 | |||
58 | Args: | ||
59 | name: Name of the object being operated upon. | ||
60 | task_name: A sub-task that was performed for name. | ||
61 | start: Timestamp of when the operation started. | ||
62 | finish: Timestamp of when the operation finished. | ||
63 | success: Boolean indicating if the operation was successful. | ||
64 | try_count: A counter indicating the try count of this task. | ||
65 | kind: The kind of the object for the unique identifier. | ||
66 | |||
67 | Returns: | ||
68 | A dictionary of the event added to the log. | ||
69 | """ | 47 | """ |
70 | event = { | ||
71 | 'id': (kind, _NextEventId()), | ||
72 | 'name': name, | ||
73 | 'task_name': task_name, | ||
74 | 'start_time': start, | ||
75 | 'try': try_count, | ||
76 | } | ||
77 | |||
78 | if self._parent: | ||
79 | event['parent'] = self._parent['id'] | ||
80 | |||
81 | if success is not None or finish is not None: | ||
82 | self.FinishEvent(event, finish, success) | ||
83 | |||
84 | self._log.append(event) | ||
85 | return event | ||
86 | |||
87 | def AddSync(self, project, task_name, start, finish, success): | ||
88 | """Add a event to the log for a sync command. | ||
89 | |||
90 | Args: | ||
91 | project: Project being synced. | ||
92 | task_name: A sub-task that was performed for name. | ||
93 | One of (TASK_SYNC_NETWORK, TASK_SYNC_LOCAL) | ||
94 | start: Timestamp of when the operation started. | ||
95 | finish: Timestamp of when the operation finished. | ||
96 | success: Boolean indicating if the operation was successful. | ||
97 | 48 | ||
98 | Returns: | 49 | def __init__(self): |
99 | A dictionary of the event added to the log. | 50 | """Initializes the event log.""" |
100 | """ | 51 | self._log = [] |
101 | event = self.Add(project.relpath, task_name, start, finish, success) | 52 | self._parent = None |
102 | if event is not None: | 53 | |
103 | event['project'] = project.name | 54 | def Add( |
104 | if project.revisionExpr: | 55 | self, |
105 | event['revision'] = project.revisionExpr | 56 | name, |
106 | if project.remote.url: | 57 | task_name, |
107 | event['project_url'] = project.remote.url | 58 | start, |
108 | if project.remote.fetchUrl: | 59 | finish=None, |
109 | event['remote_url'] = project.remote.fetchUrl | 60 | success=None, |
110 | try: | 61 | try_count=1, |
111 | event['git_hash'] = project.GetCommitRevisionId() | 62 | kind="RepoOp", |
112 | except Exception: | 63 | ): |
113 | pass | 64 | """Add an event to the log. |
114 | return event | 65 | |
115 | 66 | Args: | |
116 | def GetStatusString(self, success): | 67 | name: Name of the object being operated upon. |
117 | """Converst a boolean success to a status string. | 68 | task_name: A sub-task that was performed for name. |
118 | 69 | start: Timestamp of when the operation started. | |
119 | Args: | 70 | finish: Timestamp of when the operation finished. |
120 | success: Boolean indicating if the operation was successful. | 71 | success: Boolean indicating if the operation was successful. |
121 | 72 | try_count: A counter indicating the try count of this task. | |
122 | Returns: | 73 | kind: The kind of the object for the unique identifier. |
123 | status string. | 74 | |
124 | """ | 75 | Returns: |
125 | return 'pass' if success else 'fail' | 76 | A dictionary of the event added to the log. |
126 | 77 | """ | |
127 | def FinishEvent(self, event, finish, success): | 78 | event = { |
128 | """Finishes an incomplete event. | 79 | "id": (kind, _NextEventId()), |
129 | 80 | "name": name, | |
130 | Args: | 81 | "task_name": task_name, |
131 | event: An event that has been added to the log. | 82 | "start_time": start, |
132 | finish: Timestamp of when the operation finished. | 83 | "try": try_count, |
133 | success: Boolean indicating if the operation was successful. | 84 | } |
134 | 85 | ||
135 | Returns: | 86 | if self._parent: |
136 | A dictionary of the event added to the log. | 87 | event["parent"] = self._parent["id"] |
137 | """ | 88 | |
138 | event['status'] = self.GetStatusString(success) | 89 | if success is not None or finish is not None: |
139 | event['finish_time'] = finish | 90 | self.FinishEvent(event, finish, success) |
140 | return event | 91 | |
141 | 92 | self._log.append(event) | |
142 | def SetParent(self, event): | 93 | return event |
143 | """Set a parent event for all new entities. | 94 | |
144 | 95 | def AddSync(self, project, task_name, start, finish, success): | |
145 | Args: | 96 | """Add a event to the log for a sync command. |
146 | event: The event to use as a parent. | 97 | |
147 | """ | 98 | Args: |
148 | self._parent = event | 99 | project: Project being synced. |
149 | 100 | task_name: A sub-task that was performed for name. | |
150 | def Write(self, filename): | 101 | One of (TASK_SYNC_NETWORK, TASK_SYNC_LOCAL) |
151 | """Writes the log out to a file. | 102 | start: Timestamp of when the operation started. |
152 | 103 | finish: Timestamp of when the operation finished. | |
153 | Args: | 104 | success: Boolean indicating if the operation was successful. |
154 | filename: The file to write the log to. | 105 | |
155 | """ | 106 | Returns: |
156 | with open(filename, 'w+') as f: | 107 | A dictionary of the event added to the log. |
157 | for e in self._log: | 108 | """ |
158 | json.dump(e, f, sort_keys=True) | 109 | event = self.Add(project.relpath, task_name, start, finish, success) |
159 | f.write('\n') | 110 | if event is not None: |
111 | event["project"] = project.name | ||
112 | if project.revisionExpr: | ||
113 | event["revision"] = project.revisionExpr | ||
114 | if project.remote.url: | ||
115 | event["project_url"] = project.remote.url | ||
116 | if project.remote.fetchUrl: | ||
117 | event["remote_url"] = project.remote.fetchUrl | ||
118 | try: | ||
119 | event["git_hash"] = project.GetCommitRevisionId() | ||
120 | except Exception: | ||
121 | pass | ||
122 | return event | ||
123 | |||
124 | def GetStatusString(self, success): | ||
125 | """Converst a boolean success to a status string. | ||
126 | |||
127 | Args: | ||
128 | success: Boolean indicating if the operation was successful. | ||
129 | |||
130 | Returns: | ||
131 | status string. | ||
132 | """ | ||
133 | return "pass" if success else "fail" | ||
134 | |||
135 | def FinishEvent(self, event, finish, success): | ||
136 | """Finishes an incomplete event. | ||
137 | |||
138 | Args: | ||
139 | event: An event that has been added to the log. | ||
140 | finish: Timestamp of when the operation finished. | ||
141 | success: Boolean indicating if the operation was successful. | ||
142 | |||
143 | Returns: | ||
144 | A dictionary of the event added to the log. | ||
145 | """ | ||
146 | event["status"] = self.GetStatusString(success) | ||
147 | event["finish_time"] = finish | ||
148 | return event | ||
149 | |||
150 | def SetParent(self, event): | ||
151 | """Set a parent event for all new entities. | ||
152 | |||
153 | Args: | ||
154 | event: The event to use as a parent. | ||
155 | """ | ||
156 | self._parent = event | ||
157 | |||
158 | def Write(self, filename): | ||
159 | """Writes the log out to a file. | ||
160 | |||
161 | Args: | ||
162 | filename: The file to write the log to. | ||
163 | """ | ||
164 | with open(filename, "w+") as f: | ||
165 | for e in self._log: | ||
166 | json.dump(e, f, sort_keys=True) | ||
167 | f.write("\n") | ||
160 | 168 | ||
161 | 169 | ||
162 | # An integer id that is unique across this invocation of the program. | 170 | # An integer id that is unique across this invocation of the program. |
163 | _EVENT_ID = multiprocessing.Value('i', 1) | 171 | _EVENT_ID = multiprocessing.Value("i", 1) |
164 | 172 | ||
165 | 173 | ||
166 | def _NextEventId(): | 174 | def _NextEventId(): |
167 | """Helper function for grabbing the next unique id. | 175 | """Helper function for grabbing the next unique id. |
168 | 176 | ||
169 | Returns: | 177 | Returns: |
170 | A unique, to this invocation of the program, integer id. | 178 | A unique, to this invocation of the program, integer id. |
171 | """ | 179 | """ |
172 | with _EVENT_ID.get_lock(): | 180 | with _EVENT_ID.get_lock(): |
173 | val = _EVENT_ID.value | 181 | val = _EVENT_ID.value |
174 | _EVENT_ID.value += 1 | 182 | _EVENT_ID.value += 1 |
175 | return val | 183 | return val |