forked from luci/luci-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_stats.py
112 lines (88 loc) · 3 KB
/
handlers_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2014 The LUCI Authors. All rights reserved.
# Use of this source code is governed by the Apache v2.0 license that can be
# found in the LICENSE file.
"""Frontend handlers for statistics."""
import datetime
import webapp2
import stats
import template
from components import stats_framework
from components import stats_framework_gviz
from components import utils
from gviz import gviz_api
# GViz data description.
_GVIZ_DESCRIPTION = {
'failures': ('number', 'Failures'),
'requests': ('number', 'Total'),
'other_requests': ('number', 'Other'),
'uploads': ('number', 'Uploads'),
'uploads_bytes': ('number', 'Uploaded'),
'downloads': ('number', 'Downloads'),
'downloads_bytes': ('number', 'Downloaded'),
'contains_requests': ('number', 'Lookups'),
'contains_lookups': ('number', 'Items looked up'),
}
# Warning: modifying the order here requires updating templates/stats.html.
_GVIZ_COLUMNS_ORDER = (
'key',
'requests',
'other_requests',
'failures',
'uploads',
'downloads',
'contains_requests',
'uploads_bytes',
'downloads_bytes',
'contains_lookups',
)
### Handlers
class StatsHandler(webapp2.RequestHandler):
"""Returns the statistics web page."""
def get(self):
"""Presents nice recent statistics.
It fetches data from the 'JSON' API.
"""
# Preloads the data to save a complete request.
resolution = self.request.params.get('resolution', 'hours')
duration = utils.get_request_as_int(self.request, 'duration', 120, 1, 1000)
description = _GVIZ_DESCRIPTION.copy()
description.update(stats_framework_gviz.get_description_key(resolution))
table = stats_framework.get_stats(
stats.STATS_HANDLER, resolution, None, duration, True)
params = {
'duration': duration,
'initial_data': gviz_api.DataTable(description, table).ToJSon(
columns_order=_GVIZ_COLUMNS_ORDER),
'now': datetime.datetime.utcnow(),
'resolution': resolution,
}
self.response.write(template.render('isolate/stats.html', params))
class StatsGvizHandlerBase(webapp2.RequestHandler):
RESOLUTION = None
def get(self):
description = _GVIZ_DESCRIPTION.copy()
description.update(
stats_framework_gviz.get_description_key(self.RESOLUTION))
try:
stats_framework_gviz.get_json(
self.request,
self.response,
stats.STATS_HANDLER,
self.RESOLUTION,
description,
_GVIZ_COLUMNS_ORDER)
except ValueError as e:
self.abort(400, str(e))
class StatsGvizDaysHandler(StatsGvizHandlerBase):
RESOLUTION = 'days'
class StatsGvizHoursHandler(StatsGvizHandlerBase):
RESOLUTION = 'hours'
class StatsGvizMinutesHandler(StatsGvizHandlerBase):
RESOLUTION = 'minutes'
def get_routes():
return [
webapp2.Route(r'/stats', StatsHandler),
webapp2.Route(r'/isolate/api/v1/stats/days', StatsGvizDaysHandler),
webapp2.Route(r'/isolate/api/v1/stats/hours', StatsGvizHoursHandler),
webapp2.Route(r'/isolate/api/v1/stats/minutes', StatsGvizMinutesHandler),
]