2 # Copyright (C) 2012 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
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
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.
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.
32 from google.appengine.api import taskqueue
33 from google.appengine.ext import db
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
46 def schedule_manifest_update():
47 taskqueue.add(url='/api/test/update')
50 class ManifestUpdateHandler(webapp2.RequestHandler):
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')
57 class CachedManifestHandler(webapp2.RequestHandler):
59 self.response.headers['Content-Type'] = 'application/json'
60 manifest = PersistentCache.get_cache('manifest')
62 self.response.out.write(manifest)
64 schedule_manifest_update()
67 def schedule_dashboard_update():
68 taskqueue.add(url='/api/test/dashboard/update')
71 class DashboardUpdateHandler(webapp2.RequestHandler):
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')
78 class CachedDashboardHandler(webapp2.RequestHandler):
80 self.response.headers['Content-Type'] = 'application/json'
81 dashboard = PersistentCache.get_cache('dashboard')
83 self.response.out.write(dashboard)
85 schedule_dashboard_update()
88 def schedule_runs_update(test_id, branch_id, platform_id, regenerate_runs=True):
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})
97 def _get_test_branch_platform_ids(handler):
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
104 # FIXME: Output an error here
108 class RunsUpdateHandler(webapp2.RequestHandler):
110 self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
111 test_id, branch_id, platform_id = _get_test_branch_platform_ids(self)
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)
120 Runs.update_or_insert(branch, platform, test)
121 self.response.out.write('OK')
124 class CachedRunsHandler(webapp2.RequestHandler):
126 self.response.headers['Content-Type'] = 'application/json'
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)
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)
136 class RunsChartHandler(webapp2.RequestHandler):
138 self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
139 test_id, branch_id, platform_id = _get_test_branch_platform_ids(self)
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'))
149 params = Runs.update_or_insert(branch, platform, test).chart_params(display_days)
150 dashboard_chart_file = urllib.urlopen('http://chart.googleapis.com/chart', urllib.urlencode(params))
152 DashboardImage.create(branch.id, platform.id, test.id, display_days, dashboard_chart_file.read())
155 class DashboardImageHandler(webapp2.RequestHandler):
156 def get(self, test_id, branch_id, platform_id, display_days):
158 branch_id = int(branch_id)
159 platform_id = int(platform_id)
160 test_id = int(test_id)
161 display_days = int(display_days)
163 self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
164 self.response.out.write('Failed')
166 self.response.headers['Content-Type'] = 'image/png'
167 self.response.out.write(DashboardImage.get_image(branch_id, platform_id, test_id, display_days))
170 def schedule_report_process(log):
171 taskqueue.add(url='/api/test/report/process', params={'id': log.key().id()})