71d450b1c1e0af25642ec334c0d10f9b73ee2f4b
[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):
89     taskqueue.add(url='/api/test/runs/update', params={'id': test_id, 'branchid': branch_id, 'platformid': platform_id})
90     for display_days in [7, 30, 90, 365]:
91         if DashboardImage.needs_update(branch_id, test_id, platform_id, display_days):
92             taskqueue.add(url='/api/test/runs/chart', params={'id': test_id, 'branchid': branch_id, 'platformid': platform_id,
93                 'displayDays': display_days})
94
95
96 def _get_test_branch_platform_ids(handler):
97     try:
98         test_id = int(handler.request.get('id', 0))
99         branch_id = int(handler.request.get('branchid', 0))
100         platform_id = int(handler.request.get('platformid', 0))
101         return test_id, branch_id, platform_id
102     except TypeError:
103         # FIXME: Output an error here
104         return 0, 0, 0
105
106
107 class RunsUpdateHandler(webapp2.RequestHandler):
108     def post(self):
109         self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
110         test_id, branch_id, platform_id = _get_test_branch_platform_ids(self)
111
112         branch = model_from_numeric_id(branch_id, Branch)
113         platform = model_from_numeric_id(platform_id, Platform)
114         test = model_from_numeric_id(test_id, Test)
115         assert branch
116         assert platform
117         assert test
118
119         Runs.update_or_insert(branch, platform, test)
120         self.response.out.write('OK')
121
122
123 class CachedRunsHandler(webapp2.RequestHandler):
124     def get(self):
125         self.response.headers['Content-Type'] = 'application/json'
126
127         test_id, branch_id, platform_id = _get_test_branch_platform_ids(self)
128         runs = Runs.json_by_ids(branch_id, platform_id, test_id)
129         if runs:
130             self.response.out.write(runs)
131         elif model_from_numeric_id(branch_id, Branch) and model_from_numeric_id(platform_id, Platform) and model_from_numeric_id(test_id, Test):
132             schedule_runs_update(test_id, branch_id, platform_id)
133
134
135 class RunsChartHandler(webapp2.RequestHandler):
136     def post(self):
137         self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
138         test_id, branch_id, platform_id = _get_test_branch_platform_ids(self)
139
140         branch = model_from_numeric_id(branch_id, Branch)
141         platform = model_from_numeric_id(platform_id, Platform)
142         test = model_from_numeric_id(test_id, Test)
143         display_days = int(self.request.get('displayDays'))
144         assert branch
145         assert platform
146         assert test
147
148         params = Runs.update_or_insert(branch, platform, test).chart_params(display_days)
149         dashboard_chart_file = urllib.urlopen('http://chart.googleapis.com/chart', urllib.urlencode(params))
150
151         DashboardImage.create(branch.id, platform.id, test.id, display_days, dashboard_chart_file.read())
152
153
154 class DashboardImageHandler(webapp2.RequestHandler):
155     def get(self, test_id, branch_id, platform_id, display_days):
156         try:
157             branch_id = int(branch_id)
158             platform_id = int(platform_id)
159             test_id = int(test_id)
160             display_days = int(display_days)
161         except ValueError:
162             self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
163             self.response.out.write('Failed')
164
165         self.response.headers['Content-Type'] = 'image/png'
166         self.response.out.write(DashboardImage.get_image(branch_id, platform_id, test_id, display_days))
167
168
169 def schedule_report_process(log):
170     taskqueue.add(url='/api/test/report/process', params={'id': log.key().id()})