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 @classmethod |
| 33 def _GetParentKeyFromTag(cls, 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 @classmethod |
| 40 def CreateForTag(cls, tag): |
| 41 """Creates a frontend job associated with tag.""" |
| 42 parent_key = cls._GetParentKeyFromTag(tag) |
| 43 return cls(parent=parent_key) |
| 44 |
| 45 @classmethod |
| 46 def GetFromTag(cls, tag): |
| 47 """Gets the frontend job associated with tag.""" |
| 48 parent_key = cls._GetParentKeyFromTag(tag) |
| 49 return cls.query(ancestor=parent_key).get() |
| 50 |
| 51 @classmethod |
| 52 def DeleteForTag(cls, tag): |
| 53 """Deletes the frontend job assowiated with tag.""" |
| 54 parent_key = cls._GetParentKeyFromTag(tag) |
| 55 frontend_job = cls.query(ancestor=parent_key).get(keys_only=True) |
| 56 if frontend_job: |
| 57 frontend_job.delete() |
| 58 |
| 59 @classmethod |
| 60 def ListJobs(cls): |
| 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 cls.query().fetch( |
| 67 100, keys_only=True)] |
| 68 |
| 69 @classmethod |
| 70 def GetJobURL(cls, tag): |
| 71 """Gets the URL that can be used to get information about a specific job.""" |
| 72 return cls.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 |