David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 1 | # Copyright (C) 2017 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 15 | import json |
| 16 | import multiprocessing |
| 17 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 18 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 19 | TASK_COMMAND = "command" |
| 20 | TASK_SYNC_NETWORK = "sync-network" |
| 21 | TASK_SYNC_LOCAL = "sync-local" |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 22 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 23 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 24 | class EventLog: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 25 | """Event log that records events that occurred during a repo invocation. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 26 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 27 | Events are written to the log as a consecutive JSON entries, one per line. |
| 28 | Each entry contains the following keys: |
| 29 | - id: A ('RepoOp', ID) tuple, suitable for storing in a datastore. |
| 30 | The ID is only unique for the invocation of the repo command. |
| 31 | - name: Name of the object being operated upon. |
| 32 | - task_name: The task that was performed. |
| 33 | - start: Timestamp of when the operation started. |
| 34 | - finish: Timestamp of when the operation finished. |
| 35 | - success: Boolean indicating if the operation was successful. |
| 36 | - try_count: A counter indicating the try count of this task. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 37 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 38 | Optionally: |
| 39 | - parent: A ('RepoOp', ID) tuple indicating the parent event for nested |
| 40 | events. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 41 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 42 | Valid task_names include: |
| 43 | - command: The invocation of a subcommand. |
| 44 | - sync-network: The network component of a sync command. |
| 45 | - sync-local: The local component of a sync command. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 46 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 47 | Specific tasks may include additional informational properties. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 48 | """ |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 49 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 50 | def __init__(self): |
| 51 | """Initializes the event log.""" |
| 52 | self._log = [] |
| 53 | self._parent = None |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 54 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 55 | def Add( |
| 56 | self, |
| 57 | name, |
| 58 | task_name, |
| 59 | start, |
| 60 | finish=None, |
| 61 | success=None, |
| 62 | try_count=1, |
| 63 | kind="RepoOp", |
| 64 | ): |
| 65 | """Add an event to the log. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 66 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 67 | Args: |
| 68 | name: Name of the object being operated upon. |
| 69 | task_name: A sub-task that was performed for name. |
| 70 | start: Timestamp of when the operation started. |
| 71 | finish: Timestamp of when the operation finished. |
| 72 | success: Boolean indicating if the operation was successful. |
| 73 | try_count: A counter indicating the try count of this task. |
| 74 | kind: The kind of the object for the unique identifier. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 75 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 76 | Returns: |
| 77 | A dictionary of the event added to the log. |
| 78 | """ |
| 79 | event = { |
| 80 | "id": (kind, _NextEventId()), |
| 81 | "name": name, |
| 82 | "task_name": task_name, |
| 83 | "start_time": start, |
| 84 | "try": try_count, |
| 85 | } |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 86 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 87 | if self._parent: |
| 88 | event["parent"] = self._parent["id"] |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 89 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 90 | if success is not None or finish is not None: |
| 91 | self.FinishEvent(event, finish, success) |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 92 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 93 | self._log.append(event) |
| 94 | return event |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 95 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 96 | def AddSync(self, project, task_name, start, finish, success): |
| 97 | """Add a event to the log for a sync command. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 98 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 99 | Args: |
| 100 | project: Project being synced. |
| 101 | task_name: A sub-task that was performed for name. |
| 102 | One of (TASK_SYNC_NETWORK, TASK_SYNC_LOCAL) |
| 103 | start: Timestamp of when the operation started. |
| 104 | finish: Timestamp of when the operation finished. |
| 105 | success: Boolean indicating if the operation was successful. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 106 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 107 | Returns: |
| 108 | A dictionary of the event added to the log. |
| 109 | """ |
| 110 | event = self.Add(project.relpath, task_name, start, finish, success) |
| 111 | if event is not None: |
| 112 | event["project"] = project.name |
| 113 | if project.revisionExpr: |
| 114 | event["revision"] = project.revisionExpr |
| 115 | if project.remote.url: |
| 116 | event["project_url"] = project.remote.url |
| 117 | if project.remote.fetchUrl: |
| 118 | event["remote_url"] = project.remote.fetchUrl |
| 119 | try: |
| 120 | event["git_hash"] = project.GetCommitRevisionId() |
| 121 | except Exception: |
| 122 | pass |
| 123 | return event |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 124 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 125 | def GetStatusString(self, success): |
| 126 | """Converst a boolean success to a status string. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 127 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 128 | Args: |
| 129 | success: Boolean indicating if the operation was successful. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 130 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 131 | Returns: |
| 132 | status string. |
| 133 | """ |
| 134 | return "pass" if success else "fail" |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 135 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 136 | def FinishEvent(self, event, finish, success): |
| 137 | """Finishes an incomplete event. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 138 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 139 | Args: |
| 140 | event: An event that has been added to the log. |
| 141 | finish: Timestamp of when the operation finished. |
| 142 | success: Boolean indicating if the operation was successful. |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 143 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 144 | Returns: |
| 145 | A dictionary of the event added to the log. |
| 146 | """ |
| 147 | event["status"] = self.GetStatusString(success) |
| 148 | event["finish_time"] = finish |
| 149 | return event |
| 150 | |
| 151 | def SetParent(self, event): |
| 152 | """Set a parent event for all new entities. |
| 153 | |
| 154 | Args: |
| 155 | event: The event to use as a parent. |
| 156 | """ |
| 157 | self._parent = event |
| 158 | |
| 159 | def Write(self, filename): |
| 160 | """Writes the log out to a file. |
| 161 | |
| 162 | Args: |
| 163 | filename: The file to write the log to. |
| 164 | """ |
| 165 | with open(filename, "w+") as f: |
| 166 | for e in self._log: |
| 167 | json.dump(e, f, sort_keys=True) |
| 168 | f.write("\n") |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 169 | |
| 170 | |
Mike Frysinger | 13f323b | 2019-01-14 16:02:55 -0500 | [diff] [blame] | 171 | # An integer id that is unique across this invocation of the program. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 172 | _EVENT_ID = multiprocessing.Value("i", 1) |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 173 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 174 | |
Mike Frysinger | 13f323b | 2019-01-14 16:02:55 -0500 | [diff] [blame] | 175 | def _NextEventId(): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 176 | """Helper function for grabbing the next unique id. |
Mike Frysinger | 13f323b | 2019-01-14 16:02:55 -0500 | [diff] [blame] | 177 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 178 | Returns: |
| 179 | A unique, to this invocation of the program, integer id. |
| 180 | """ |
| 181 | with _EVENT_ID.get_lock(): |
| 182 | val = _EVENT_ID.value |
| 183 | _EVENT_ID.value += 1 |
| 184 | return val |