Chromium Code Reviews| Index: tools/android/loading/cloud/frontend/frontend_job.py |
| diff --git a/tools/android/loading/cloud/frontend/frontend_job.py b/tools/android/loading/cloud/frontend/frontend_job.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d602c037f78e90a4e8174f5f1db0d0c1b0c3e7fa |
| --- /dev/null |
| +++ b/tools/android/loading/cloud/frontend/frontend_job.py |
| @@ -0,0 +1,90 @@ |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +from google.appengine.ext import ndb |
| + |
| + |
| +class FrontendJob(ndb.Model): |
| + """Class representing a frontend job. |
| + |
| + A frontend job is a Clovis task sent by the user, and associated metadata |
| + (such as the username, the start time...). |
| + It is persisted in the Google Cloud datastore. |
| + |
| + All frontend jobs are ancestors of a single entity called 'FrontendJobList'. |
| + This allows to benefit from strong consistency when querying the job |
| + associated to a tag. |
| + """ |
| + # Base URL path to get information about a job. |
| + SHOW_JOB_URL = '/show_job' |
| + |
| + # ndb properties persisted in the datastore. Indexing is not needed. |
| + email = ndb.StringProperty(indexed=False) |
| + status = ndb.StringProperty(indexed=False) |
| + task_url = ndb.StringProperty(indexed=False) |
| + eta = ndb.DateTimeProperty(indexed=False) |
| + start_time = ndb.DateTimeProperty(auto_now_add=True, indexed=False) |
| + # Not indexed by default. |
| + clovis_task = ndb.TextProperty(compressed=True, indexed=False) |
| + log = ndb.TextProperty(indexed=False) |
| + |
| + @staticmethod |
|
mattcary
2016/06/09 15:40:06
We prefer @classmethod rather than @staticmethod.
droger
2016/06/10 12:14:04
Done.
|
| + def _GetParentKeyFromTag(tag): |
| + """Gets the key that can be used to retrieve a frontend job from the job |
| + list. |
| + """ |
| + return ndb.Key('FrontendJobList', tag) |
| + |
| + @staticmethod |
| + def CreateForTag(tag): |
| + """Creates a frontend job associated with tag.""" |
| + parent_key = FrontendJob._GetParentKeyFromTag(tag) |
| + return FrontendJob(parent=parent_key) |
| + |
| + @staticmethod |
| + def GetFromTag(tag): |
| + """Gets the frontend job associated with tag.""" |
| + parent_key = FrontendJob._GetParentKeyFromTag(tag) |
| + return FrontendJob.query(ancestor=parent_key).get() |
| + |
| + @staticmethod |
| + def DeleteForTag(tag): |
| + """Deletes the frontend job assowiated with tag.""" |
| + parent_key = FrontendJob._GetParentKeyFromTag(tag) |
| + frontend_job = FrontendJob.query(ancestor=parent_key).get(keys_only=True) |
| + if frontend_job: |
| + frontend_job.delete() |
| + |
| + @staticmethod |
| + def ListJobs(): |
| + """Lists all the frontend jobs. |
| + |
| + Returns: |
| + list of strings: The list of tags corresponding to existing frontend jobs. |
| + """ |
| + return [key.parent().string_id() for key in FrontendJob.query().fetch( |
| + 100, keys_only=True)] |
| + |
| + @staticmethod |
| + def GetJobURL(tag): |
| + """Gets the URL that can be used to get information about a specific job.""" |
| + return FrontendJob.SHOW_JOB_URL + '?tag=' + tag |
| + |
| + def RenderAsHtml(self): |
| + """Render a short job description as a HTML table. |
| + |
| + The log and ClovisTask are not included, because they are potentially very |
| + large. |
| + """ |
| + html = '<table>' |
| + |
| + for p in FrontendJob._properties: |
| + if p == 'log' or p == 'clovis_task': |
| + continue |
| + value = getattr(self, p) |
| + if value: |
| + html += '<tr><td>' + p + '</td><td>' + str(value) + '</td></tr>' |
| + |
| + html += '</table>' |
| + return html |