Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 from google.appengine.ext import ndb | |
| 6 | |
| 7 | |
| 8 class FrontendJob(ndb.Model): | |
| 9 """Class representing a frontend job. | |
| 10 | |
| 11 A frontend job is a Clovis task sent by the user, and associated metadata | |
| 12 (such as the username, the start time...). | |
| 13 It is persisted in the Google Cloud datastore. | |
| 14 | |
| 15 All frontend jobs are ancestors of a single entity called 'FrontendJobList'. | |
| 16 This allows to benefit from strong consistency when querying the job | |
| 17 associated to a tag. | |
| 18 """ | |
| 19 # Base URL path to get information about a job. | |
| 20 SHOW_JOB_URL = '/show_job' | |
| 21 | |
| 22 # ndb properties persisted in the datastore. Indexing is not needed. | |
| 23 email = ndb.StringProperty(indexed=False) | |
| 24 status = ndb.StringProperty(indexed=False) | |
| 25 task_url = ndb.StringProperty(indexed=False) | |
| 26 eta = ndb.DateTimeProperty(indexed=False) | |
| 27 start_time = ndb.DateTimeProperty(auto_now_add=True, indexed=False) | |
| 28 # Not indexed by default. | |
| 29 clovis_task = ndb.TextProperty(compressed=True, indexed=False) | |
| 30 log = ndb.TextProperty(indexed=False) | |
| 31 | |
| 32 @staticmethod | |
|
mattcary
2016/06/09 15:40:06
We prefer @classmethod rather than @staticmethod.
droger
2016/06/10 12:14:04
Done.
| |
| 33 def _GetParentKeyFromTag(tag): | |
| 34 """Gets the key that can be used to retrieve a frontend job from the job | |
| 35 list. | |
| 36 """ | |
| 37 return ndb.Key('FrontendJobList', tag) | |
| 38 | |
| 39 @staticmethod | |
| 40 def CreateForTag(tag): | |
| 41 """Creates a frontend job associated with tag.""" | |
| 42 parent_key = FrontendJob._GetParentKeyFromTag(tag) | |
| 43 return FrontendJob(parent=parent_key) | |
| 44 | |
| 45 @staticmethod | |
| 46 def GetFromTag(tag): | |
| 47 """Gets the frontend job associated with tag.""" | |
| 48 parent_key = FrontendJob._GetParentKeyFromTag(tag) | |
| 49 return FrontendJob.query(ancestor=parent_key).get() | |
| 50 | |
| 51 @staticmethod | |
| 52 def DeleteForTag(tag): | |
| 53 """Deletes the frontend job assowiated with tag.""" | |
| 54 parent_key = FrontendJob._GetParentKeyFromTag(tag) | |
| 55 frontend_job = FrontendJob.query(ancestor=parent_key).get(keys_only=True) | |
| 56 if frontend_job: | |
| 57 frontend_job.delete() | |
| 58 | |
| 59 @staticmethod | |
| 60 def ListJobs(): | |
| 61 """Lists all the frontend jobs. | |
| 62 | |
| 63 Returns: | |
| 64 list of strings: The list of tags corresponding to existing frontend jobs. | |
| 65 """ | |
| 66 return [key.parent().string_id() for key in FrontendJob.query().fetch( | |
| 67 100, keys_only=True)] | |
| 68 | |
| 69 @staticmethod | |
| 70 def GetJobURL(tag): | |
| 71 """Gets the URL that can be used to get information about a specific job.""" | |
| 72 return FrontendJob.SHOW_JOB_URL + '?tag=' + tag | |
| 73 | |
| 74 def RenderAsHtml(self): | |
| 75 """Render a short job description as a HTML table. | |
| 76 | |
| 77 The log and ClovisTask are not included, because they are potentially very | |
| 78 large. | |
| 79 """ | |
| 80 html = '<table>' | |
| 81 | |
| 82 for p in FrontendJob._properties: | |
| 83 if p == 'log' or p == 'clovis_task': | |
| 84 continue | |
| 85 value = getattr(self, p) | |
| 86 if value: | |
| 87 html += '<tr><td>' + p + '</td><td>' + str(value) + '</td></tr>' | |
| 88 | |
| 89 html += '</table>' | |
| 90 return html | |
| OLD | NEW |