d1932c4e9dec19e9cf3073f09b70ad7b1e545ae3
[WebKit-https.git] / Websites / webkit-perf.appspot.com / models.py
1 #!/usr/bin/env python
2 # Copyright (C) 2012 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import hashlib
31 import json
32 import re
33
34 from datetime import datetime
35 from google.appengine.ext import db
36 from google.appengine.api import memcache
37
38
39 class NumericIdHolder(db.Model):
40     owner = db.ReferenceProperty()
41     # Dummy class whose sole purpose is to generate key().id()
42
43
44 def create_in_transaction_with_numeric_id_holder(callback):
45     id_holder = NumericIdHolder()
46     id_holder.put()
47     id_holder = NumericIdHolder.get(id_holder.key())
48     owner = None
49     try:
50         owner = db.run_in_transaction(callback, id_holder.key().id())
51         if owner:
52             id_holder.owner = owner
53             id_holder.put()
54     finally:
55         if not owner:
56             id_holder.delete()
57     return owner
58
59
60 def delete_model_with_numeric_id_holder(model):
61     id_holder = NumericIdHolder.get_by_id(model.id)
62     model.delete()
63     id_holder.delete()
64
65
66 def model_from_numeric_id(id, expected_kind):
67     id_holder = NumericIdHolder.get_by_id(id)
68     return id_holder.owner if id_holder and id_holder.owner and isinstance(id_holder.owner, expected_kind) else None
69
70
71 class Branch(db.Model):
72     id = db.IntegerProperty(required=True)
73     name = db.StringProperty(required=True)
74
75
76 class Platform(db.Model):
77     id = db.IntegerProperty(required=True)
78     name = db.StringProperty(required=True)
79
80
81 class Builder(db.Model):
82     name = db.StringProperty(required=True)
83     password = db.StringProperty(required=True)
84
85     @staticmethod
86     def create(name, raw_password):
87         return Builder(name=name, password=Builder._hashed_password(raw_password), key_name=name).put()
88
89     def update_password(self, raw_password):
90         self.password = Builder._hashed_password(raw_password)
91         self.put()
92
93     def authenticate(self, raw_password):
94         return self.password == hashlib.sha256(raw_password).hexdigest()
95
96     @staticmethod
97     def _hashed_password(raw_password):
98         return hashlib.sha256(raw_password).hexdigest()
99
100
101 class Build(db.Model):
102     branch = db.ReferenceProperty(Branch, required=True, collection_name='build_branch')
103     platform = db.ReferenceProperty(Platform, required=True, collection_name='build_platform')
104     builder = db.ReferenceProperty(Builder, required=True, collection_name='builder_key')
105     buildNumber = db.IntegerProperty(required=True)
106     revision = db.IntegerProperty(required=True)
107     chromiumRevision = db.IntegerProperty()
108     timestamp = db.DateTimeProperty(required=True)
109
110
111 # Used to generate TestMap in the manifest efficiently
112 class Test(db.Model):
113     id = db.IntegerProperty(required=True)
114     name = db.StringProperty(required=True)
115     branches = db.ListProperty(db.Key)
116     platforms = db.ListProperty(db.Key)
117
118     @staticmethod
119     def cache_key(test_id, branch_id, platform_id):
120         return 'runs:%d,%d,%d' % (test_id, branch_id, platform_id)
121
122
123 class TestResult(db.Model):
124     name = db.StringProperty(required=True)
125     build = db.ReferenceProperty(Build, required=True)
126     value = db.FloatProperty(required=True)
127     valueMedian = db.FloatProperty()
128     valueStdev = db.FloatProperty()
129     valueMin = db.FloatProperty()
130     valueMax = db.FloatProperty()
131
132     @staticmethod
133     def key_name(build, test_name):
134         return build.key().name() + ':' + test_name
135
136
137 class ReportLog(db.Model):
138     timestamp = db.DateTimeProperty(required=True)
139     headers = db.TextProperty()
140     payload = db.TextProperty()
141     commit = db.BooleanProperty()
142
143     def _parsed_payload(self):
144         if self.__dict__.get('_parsed') == None:
145             try:
146                 self._parsed = json.loads(self.payload)
147             except ValueError:
148                 self._parsed = False
149         return self._parsed
150
151     def get_value(self, keyName):
152         if not self._parsed_payload():
153             return None
154         return self._parsed.get(keyName)
155
156     def results(self):
157         return self.get_value('results')
158
159     def builder(self):
160         return self._model_by_key_name_in_payload(Builder, 'builder-name')
161
162     def branch(self):
163         return self._model_by_key_name_in_payload(Branch, 'branch')
164
165     def platform(self):
166         return self._model_by_key_name_in_payload(Platform, 'platform')
167
168     def build_number(self):
169         return self._integer_in_payload('build-number')
170
171     def webkit_revision(self):
172         return self._integer_in_payload('webkit-revision')
173
174     def chromium_revision(self):
175         return self._integer_in_payload('chromium-revision')
176
177     def _model_by_key_name_in_payload(self, model, keyName):
178         key = self.get_value(keyName)
179         if not key:
180             return None
181         return model.get_by_key_name(key)
182
183     def _integer_in_payload(self, keyName):
184         try:
185             return int(self.get_value(keyName))
186         except TypeError:
187             return None
188         except ValueError:
189             return None
190
191     # FIXME: We also have timestamp as a member variable.
192     def timestamp(self):
193         try:
194             return datetime.fromtimestamp(self._integer_in_payload('timestamp'))
195         except TypeError:
196             return None
197         except ValueError:
198             return None
199
200
201 class PersistentCache(db.Model):
202     value = db.TextProperty(required=True)
203
204     @staticmethod
205     def set_cache(name, value):
206         memcache.set(name, value)
207
208         def execute():
209             cache = PersistentCache.get_by_key_name(name)
210             if cache:
211                 cache.value = value
212                 cache.put()
213             else:
214                 PersistentCache(key_name=name, value=value).put()
215
216         db.run_in_transaction(execute)
217
218     @staticmethod
219     def get_cache(name):
220         value = memcache.get(name)
221         if value:
222             return value
223         cache = PersistentCache.get_by_key_name(name)
224         if not cache:
225             return None
226         memcache.set(name, cache.value)
227         return cache.value