perf-o-matic should store test results' units
[WebKit-https.git] / Websites / webkit-perf.appspot.com / controller.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 urllib
31 import webapp2
32 from google.appengine.api import taskqueue
33 from google.appengine.ext import db
34
35 from json_generators import DashboardJSONGenerator
36 from json_generators import ManifestJSONGenerator
37 from models import Branch
38 from models import DashboardImage
39 from models import PersistentCache
40 from models import Platform
41 from models import Runs
42 from models import Test
43 from models import model_from_numeric_id
44
45
46 def schedule_manifest_update():
47     taskqueue.add(url='/api/test/update')
48
49
50 class ManifestUpdateHandler(webapp2.RequestHandler):
51     def post(self):
52         self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
53         PersistentCache.set_cache('manifest', ManifestJSONGenerator().to_json())
54         self.response.out.write('OK')
55
56
57 class CachedManifestHandler(webapp2.RequestHandler):
58     def get(self):
59         self.response.headers['Content-Type'] = 'application/json'
60         manifest = PersistentCache.get_cache('manifest')
61         if manifest:
62             self.response.out.write(manifest)
63         else:
64             schedule_manifest_update()
65
66
67 def schedule_dashboard_update():
68     taskqueue.add(url='/api/test/dashboard/update')
69
70
71 class DashboardUpdateHandler(webapp2.RequestHandler):
72     def post(self):
73         self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
74         PersistentCache.set_cache('dashboard', DashboardJSONGenerator().to_json())
75         self.response.out.write('OK')
76
77
78 class CachedDashboardHandler(webapp2.RequestHandler):
79     def get(self):
80         self.response.headers['Content-Type'] = 'application/json'
81         dashboard = PersistentCache.get_cache('dashboard')
82         if dashboard:
83             self.response.out.write(dashboard)
84         else:
85             schedule_dashboard_update()
86
87
88 def schedule_runs_update(test_id, branch_id, platform_id, regenerate_runs=True):
89     if regenerate_runs:
90         taskqueue.add(url='/api/test/runs/update', params={'id': test_id, 'branchid': branch_id, 'platformid': platform_id})
91     for display_days in [7, 30, 90, 365]:
92         if DashboardImage.needs_update(branch_id, platform_id, test_id, display_days):
93             taskqueue.add(url='/api/test/runs/chart', params={'id': test_id, 'branchid': branch_id, 'platformid': platform_id,
94                 'displayDays': display_days})
95
96
97 def _get_test_branch_platform_ids(handler):
98     try:
99         test_id = int(handler.request.get('id', 0))
100         branch_id = int(handler.request.get('branchid', 0))
101         platform_id = int(handler.request.get('platformid', 0))
102         return test_id, branch_id, platform_id
103     except TypeError:
104         # FIXME: Output an error here
105         return 0, 0, 0
106
107
108 class RunsUpdateHandler(webapp2.RequestHandler):
109     def post(self):
110         self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
111         test_id, branch_id, platform_id = _get_test_branch_platform_ids(self)
112
113         branch = model_from_numeric_id(branch_id, Branch)
114         platform = model_from_numeric_id(platform_id, Platform)
115         test = model_from_numeric_id(test_id, Test)
116         assert branch
117         assert platform
118         assert test
119
120         Runs.update_or_insert(branch, platform, test)
121         self.response.out.write('OK')
122
123
124 class CachedRunsHandler(webapp2.RequestHandler):
125     def get(self):
126         self.response.headers['Content-Type'] = 'application/json'
127
128         test_id, branch_id, platform_id = _get_test_branch_platform_ids(self)
129         runs = Runs.json_by_ids(branch_id, platform_id, test_id)
130         if runs:
131             self.response.out.write(runs)
132         elif model_from_numeric_id(branch_id, Branch) and model_from_numeric_id(platform_id, Platform) and model_from_numeric_id(test_id, Test):
133             schedule_runs_update(test_id, branch_id, platform_id)
134
135
136 class RunsChartHandler(webapp2.RequestHandler):
137     def post(self):
138         self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
139         test_id, branch_id, platform_id = _get_test_branch_platform_ids(self)
140
141         branch = model_from_numeric_id(branch_id, Branch)
142         platform = model_from_numeric_id(platform_id, Platform)
143         test = model_from_numeric_id(test_id, Test)
144         display_days = int(self.request.get('displayDays'))
145         assert branch
146         assert platform
147         assert test
148
149         params = Runs.update_or_insert(branch, platform, test).chart_params(display_days)
150         if not params:
151             return
152         dashboard_chart_file = urllib.urlopen('http://chart.googleapis.com/chart', urllib.urlencode(params))
153
154         DashboardImage.create(branch.id, platform.id, test.id, display_days, dashboard_chart_file.read())
155
156
157 class DashboardImageHandler(webapp2.RequestHandler):
158     def get(self, test_id, branch_id, platform_id, display_days):
159         try:
160             branch_id = int(branch_id)
161             platform_id = int(platform_id)
162             test_id = int(test_id)
163             display_days = int(display_days)
164         except ValueError:
165             self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
166             self.response.out.write('Failed')
167
168         self.response.headers['Content-Type'] = 'image/png'
169         self.response.out.write(DashboardImage.get_image(branch_id, platform_id, test_id, display_days))
170
171
172 def schedule_report_process(log):
173     taskqueue.add(url='/api/test/report/process', params={'id': log.key().id()})