Perf-o-matic should process reports in background
[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
37
38 class NumericIdHolder(db.Model):
39     owner = db.ReferenceProperty()
40     # Dummy class whose sole purpose is to generate key().id()
41
42
43 def create_in_transaction_with_numeric_id_holder(callback):
44     id_holder = NumericIdHolder()
45     id_holder.put()
46     id_holder = NumericIdHolder.get(id_holder.key())
47     owner = db.run_in_transaction(callback, id_holder.key().id())
48     if owner:
49         id_holder.owner = owner
50         id_holder.put()
51     else:
52         id_holder.delete()
53     return owner
54
55
56 def delete_model_with_numeric_id_holder(model):
57     id_holder = NumericIdHolder.get_by_id(model.id)
58     model.delete()
59     id_holder.delete()
60
61
62 def modelFromNumericId(id, expected_kind):
63     id_holder = NumericIdHolder.get_by_id(id)
64     return id_holder.owner if id_holder and id_holder.owner and isinstance(id_holder.owner, expected_kind) else None
65
66
67 class Branch(db.Model):
68     id = db.IntegerProperty(required=True)
69     name = db.StringProperty(required=True)
70
71
72 class Platform(db.Model):
73     id = db.IntegerProperty(required=True)
74     name = db.StringProperty(required=True)
75
76
77 class Builder(db.Model):
78     name = db.StringProperty(required=True)
79     password = db.StringProperty(required=True)
80
81     def authenticate(self, raw_password):
82         return self.password == hashlib.sha256(raw_password).hexdigest()
83
84     @staticmethod
85     def hashed_password(raw_password):
86         return hashlib.sha256(raw_password).hexdigest()
87
88
89 class Build(db.Model):
90     branch = db.ReferenceProperty(Branch, required=True, collection_name='build_branch')
91     platform = db.ReferenceProperty(Platform, required=True, collection_name='build_platform')
92     builder = db.ReferenceProperty(Builder, required=True, collection_name='builder_key')
93     buildNumber = db.IntegerProperty(required=True)
94     revision = db.IntegerProperty(required=True)
95     chromiumRevision = db.IntegerProperty()
96     timestamp = db.DateTimeProperty(required=True)
97
98
99 # Used to generate TestMap in the manifest efficiently
100 class Test(db.Model):
101     id = db.IntegerProperty(required=True)
102     name = db.StringProperty(required=True)
103     branches = db.ListProperty(db.Key)
104     platforms = db.ListProperty(db.Key)
105
106     @staticmethod
107     def cache_key(test_id, branch_id, platform_id):
108         return 'runs:%d,%d,%d' % (test_id, branch_id, platform_id)
109
110
111 class TestResult(db.Model):
112     name = db.StringProperty(required=True)
113     build = db.ReferenceProperty(Build, required=True)
114     value = db.FloatProperty(required=True)
115     valueMedian = db.FloatProperty()
116     valueStdev = db.FloatProperty()
117     valueMin = db.FloatProperty()
118     valueMax = db.FloatProperty()
119
120
121 # Temporarily store log reports sent by bots
122 class ReportLog(db.Model):
123     timestamp = db.DateTimeProperty(required=True)
124     headers = db.TextProperty()
125     payload = db.TextProperty()
126     commit = db.BooleanProperty()
127
128     def _parsed_payload(self):
129         if self.__dict__.get('_parsed') == None:
130             try:
131                 self._parsed = json.loads(self.payload)
132             except ValueError:
133                 self._parsed = False
134         return self._parsed
135
136     def get_value(self, keyName):
137         if not self._parsed_payload():
138             return None
139         return self._parsed.get(keyName, '')
140
141     def results(self):
142         return self.get_value('results')
143
144     def builder(self):
145         return self._model_by_key_name_in_payload(Builder, 'builder-name')
146
147     def branch(self):
148         return self._model_by_key_name_in_payload(Branch, 'branch')
149
150     def platform(self):
151         return self._model_by_key_name_in_payload(Platform, 'platform')
152
153     def build_number(self):
154         return self._integer_in_payload('build-number')
155
156     def webkit_revision(self):
157         return self._integer_in_payload('webkit-revision')
158
159     def chromium_revision(self):
160         return self._integer_in_payload('chromium-revision')
161
162     def _model_by_key_name_in_payload(self, model, keyName):
163         key = self.get_value(keyName)
164         if not key:
165             return None
166         return model.get_by_key_name(key)
167
168     def _integer_in_payload(self, keyName):
169         try:
170             return int(self.get_value(keyName))
171         except ValueError:
172             return None
173
174     def timestamp(self):
175         try:
176             return datetime.fromtimestamp(self._integer_in_payload('timestamp'))
177         except TypeError:
178             return None
179         except ValueError:
180             return None
181
182
183 # Used when memcache entry is evicted
184 class PersistentCache(db.Model):
185     value = db.TextProperty(required=True)