]>
Commit | Line | Data |
---|---|---|
66862195 MT |
1 | #!/usr/bin/python |
2 | ||
66862195 MT |
3 | import datetime |
4 | import hwdata | |
5 | import logging | |
6 | import re | |
a95c2f97 | 7 | import json |
66862195 MT |
8 | import tornado.web |
9 | ||
a95c2f97 | 10 | from .. import fireinfo |
66862195 | 11 | |
124a8404 | 12 | from . import base |
96c9bb79 | 13 | from . import ui_modules |
66862195 | 14 | |
96c9bb79 | 15 | class BaseHandler(base.BaseHandler): |
66862195 MT |
16 | @property |
17 | def when(self): | |
18 | return self.get_argument_date("when", None) | |
19 | ||
20 | ||
21 | MIN_PROFILE_VERSION = 0 | |
22 | MAX_PROFILE_VERSION = 0 | |
23 | ||
24 | class Profile(dict): | |
25 | def __getattr__(self, key): | |
26 | try: | |
27 | return self[key] | |
28 | except KeyError: | |
11347e46 | 29 | raise AttributeError(key) |
66862195 MT |
30 | |
31 | def __setattr__(self, key, val): | |
32 | self[key] = val | |
33 | ||
34 | ||
96c9bb79 | 35 | class ProfileSendHandler(BaseHandler): |
66862195 MT |
36 | def check_xsrf_cookie(self): |
37 | # This cookie is not required here. | |
38 | pass | |
39 | ||
40 | def prepare(self): | |
41 | # Create an empty profile. | |
42 | self.profile = Profile() | |
43 | ||
44 | def __check_attributes(self, profile): | |
45 | """ | |
46 | Check for attributes that must be provided, | |
47 | """ | |
48 | attributes = ( | |
49 | "private_id", | |
50 | "profile_version", | |
51 | "public_id", | |
52 | ) | |
53 | for attr in attributes: | |
11347e46 | 54 | if attr not in profile: |
66862195 MT |
55 | raise tornado.web.HTTPError(400, "Profile lacks '%s' attribute: %s" % (attr, profile)) |
56 | ||
57 | def __check_valid_ids(self, profile): | |
58 | """ | |
59 | Check if IDs contain valid data. | |
60 | """ | |
61 | for id in ("public_id", "private_id"): | |
62 | if re.match(r"^([a-f0-9]{40})$", "%s" % profile[id]) is None: | |
63 | raise tornado.web.HTTPError(400, "ID '%s' has wrong format: %s" % (id, profile)) | |
64 | ||
65 | def __check_equal_ids(self, profile): | |
66 | """ | |
67 | Check if public_id and private_id are equal. | |
68 | """ | |
69 | if profile.public_id == profile.private_id: | |
70 | raise tornado.web.HTTPError(400, "Public and private IDs are equal: %s" % profile) | |
71 | ||
72 | def __check_matching_ids(self, profile): | |
73 | """ | |
74 | Check if a profile with the given public_id is already in the | |
75 | database. If so we need to check if the private_id matches. | |
76 | """ | |
77 | p = self.profiles.find_one({ "public_id" : profile["public_id"]}) | |
78 | if not p: | |
79 | return | |
80 | ||
81 | p = Profile(p) | |
82 | if p.private_id != profile.private_id: | |
83 | raise tornado.web.HTTPError(400, "Mismatch of private_id: %s" % profile) | |
84 | ||
85 | def __check_profile_version(self, profile): | |
86 | """ | |
87 | Check if this version of the server software does support the | |
88 | received profile. | |
89 | """ | |
90 | version = profile.profile_version | |
91 | ||
92 | if version < MIN_PROFILE_VERSION or version > MAX_PROFILE_VERSION: | |
93 | raise tornado.web.HTTPError(400, | |
94 | "Profile version is not supported: %s" % version) | |
95 | ||
96 | def check_profile_blob(self, profile): | |
97 | """ | |
98 | This method checks if the blob is sane. | |
99 | """ | |
100 | checks = ( | |
101 | self.__check_attributes, | |
102 | self.__check_valid_ids, | |
103 | self.__check_equal_ids, | |
104 | self.__check_profile_version, | |
105 | # These checks require at least one database query and should be done | |
106 | # at last. | |
107 | self.__check_matching_ids, | |
108 | ) | |
109 | ||
110 | for check in checks: | |
111 | check(profile) | |
112 | ||
113 | # If we got here, everything is okay and we can go on... | |
114 | ||
115 | def get_profile_blob(self): | |
116 | profile = self.get_argument("profile", None) | |
117 | ||
118 | # Send "400 bad request" if no profile was provided | |
119 | if not profile: | |
120 | raise tornado.web.HTTPError(400, "No profile received") | |
121 | ||
122 | # Try to decode the profile. | |
123 | try: | |
a95c2f97 | 124 | return json.loads(profile) |
11347e46 | 125 | except json.decoder.JSONDecodeError as e: |
66862195 MT |
126 | raise tornado.web.HTTPError(400, "Profile could not be decoded: %s" % e) |
127 | ||
128 | # The GET method is only allowed in debugging mode. | |
129 | def get(self, public_id): | |
130 | if not self.application.settings["debug"]: | |
fe947d95 | 131 | raise tornado.web.HTTPError(405) |
66862195 MT |
132 | |
133 | return self.post(public_id) | |
134 | ||
135 | def post(self, public_id): | |
136 | profile_blob = self.get_profile_blob() | |
137 | #self.check_profile_blob(profile_blob) | |
138 | ||
139 | # Get location | |
140 | location = self.get_remote_location() | |
141 | if location: | |
142 | location = location.country | |
143 | ||
144 | # Handle the profile. | |
a69e87a1 MT |
145 | with self.db.transaction(): |
146 | try: | |
147 | self.fireinfo.handle_profile(public_id, profile_blob, location=location) | |
66862195 | 148 | |
a69e87a1 MT |
149 | except fireinfo.ProfileParserError: |
150 | raise tornado.web.HTTPError(400) | |
66862195 MT |
151 | |
152 | self.finish("Your profile was successfully saved to the database.") | |
153 | ||
154 | ||
96c9bb79 | 155 | class IndexHandler(BaseHandler): |
66862195 | 156 | def get(self): |
c4099434 | 157 | data = { |
11ee2139 MT |
158 | # Release |
159 | "latest_release" : self.backend.releases.get_latest(), | |
160 | ||
84604476 | 161 | # Hardware |
5a34028b MT |
162 | "arches" : self.fireinfo.get_arch_map(when=self.when), |
163 | "cpu_vendors" : self.fireinfo.get_cpu_vendors_map(when=self.when), | |
84604476 MT |
164 | "memory_avg" : self.backend.fireinfo.get_average_memory_amount(when=self.when), |
165 | ||
11ee2139 MT |
166 | # Virtualization |
167 | "hypervisors" : self.fireinfo.get_hypervisor_map(when=self.when), | |
168 | "virtual_ratio" : self.fireinfo.get_virtual_ratio(when=self.when), | |
574a88c7 MT |
169 | |
170 | # Location | |
171 | "locations" : self.fireinfo.get_geo_location_map(when=self.when), | |
c4099434 MT |
172 | } |
173 | ||
8eec8811 MT |
174 | # Cache for 1h |
175 | self.set_expires(3600) | |
176 | ||
c4099434 | 177 | self.render("fireinfo/index.html", **data) |
66862195 | 178 | |
66862195 | 179 | |
1e3b2aad MT |
180 | class DriverDetail(BaseHandler): |
181 | def get(self, driver): | |
8eec8811 MT |
182 | # Cache for 1h |
183 | self.set_expires(3600) | |
184 | ||
1e3b2aad MT |
185 | self.render("fireinfo/driver.html", driver=driver, |
186 | driver_map=self.fireinfo.get_driver_map(driver, when=self.when)) | |
187 | ||
188 | ||
b84b407f | 189 | class ProfileHandler(BaseHandler): |
66862195 MT |
190 | def get(self, profile_id): |
191 | profile = self.fireinfo.get_profile(profile_id, when=self.when) | |
192 | ||
193 | if not profile or not profile.is_showable(): | |
b84b407f | 194 | raise tornado.web.HTTPError(404) |
66862195 | 195 | |
8eec8811 MT |
196 | # Cache for 1h |
197 | self.set_expires(3600) | |
198 | ||
b84b407f | 199 | self.render("fireinfo/profile.html", profile=profile) |
66862195 MT |
200 | |
201 | ||
96c9bb79 | 202 | class RandomProfileHandler(BaseHandler): |
66862195 MT |
203 | def get(self): |
204 | profile_id = self.fireinfo.get_random_profile(when=self.when) | |
205 | if profile_id is None: | |
206 | raise tornado.web.HTTPError(404) | |
207 | ||
208 | self.redirect("/profile/%s" % profile_id) | |
209 | ||
210 | ||
ed2e3c1f MT |
211 | class ReleasesHandler(BaseHandler): |
212 | def get(self): | |
213 | data = { | |
214 | "releases" : self.fireinfo.get_releases_map(when=self.when), | |
215 | "kernels" : self.fireinfo.get_kernels_map(when=self.when), | |
216 | } | |
217 | ||
218 | # Cache for 1h | |
219 | self.set_expires(3600) | |
220 | ||
221 | return self.render("fireinfo/releases.html", **data) | |
222 | ||
223 | ||
96c9bb79 | 224 | class StatsHandler(BaseHandler): |
66862195 MT |
225 | def get(self): |
226 | self.render("fireinfo/stats.html") | |
227 | ||
228 | ||
96c9bb79 | 229 | class StatsProcessorsHandler(BaseHandler): |
66862195 MT |
230 | def get(self): |
231 | avg, stddev, min, max = self.fireinfo.get_cpu_clock_speeds(when=self.when) | |
232 | arch_map = self.fireinfo.get_arch_map(when=self.when) | |
233 | ||
234 | data = { | |
235 | "arch_map" : arch_map, | |
236 | "cpu_vendors" : self.fireinfo.get_cpu_vendors_map(when=self.when), | |
237 | "clock_speed_avg" : avg, | |
238 | "clock_speed_stddev" : stddev, | |
239 | "clock_speed_min" : min, | |
240 | "clock_speed_max" : max, | |
241 | } | |
242 | ||
243 | return self.render("fireinfo/stats-cpus.html", **data) | |
244 | ||
245 | ||
96c9bb79 | 246 | class StatsProcessorDetailHandler(BaseHandler): |
66862195 MT |
247 | def get(self, platform): |
248 | assert platform in ("arm", "x86") | |
249 | ||
250 | flags = self.fireinfo.get_common_cpu_flags_by_platform(platform, when=self.when) | |
251 | ||
252 | return self.render("fireinfo/stats-cpus-detail.html", | |
253 | platform=platform, flags=flags) | |
254 | ||
255 | ||
8ab37e0b MT |
256 | class VendorsHandler(BaseHandler): |
257 | def get(self): | |
258 | vendors = self.fireinfo.get_vendor_list(when=self.when) | |
259 | ||
8eec8811 MT |
260 | # Cache for 1h |
261 | self.set_expires(3600) | |
262 | ||
8ab37e0b MT |
263 | self.render("fireinfo/vendors.html", vendors=vendors) |
264 | ||
265 | ||
266 | class VendorHandler(BaseHandler): | |
66862195 | 267 | def get(self, subsystem, vendor_id): |
8ab37e0b MT |
268 | devices = self.fireinfo.get_devices_by_vendor(subsystem, vendor_id, when=self.when) |
269 | if not devices: | |
270 | raise tornado.web.HTTPError(404) | |
66862195 MT |
271 | |
272 | vendor_name = self.fireinfo.get_vendor_string(subsystem, vendor_id) | |
273 | ||
8eec8811 MT |
274 | # Cache for 1h |
275 | self.set_expires(3600) | |
276 | ||
851a00bc | 277 | self.render("fireinfo/vendor.html", vendor_name=vendor_name, devices=devices) |
66862195 MT |
278 | |
279 | ||
96c9bb79 | 280 | class DeviceTableModule(ui_modules.UIModule): |
3697181e | 281 | def render(self, devices, show_group=True, embedded=False): |
96c9bb79 | 282 | return self.render_string("fireinfo/modules/table-devices.html", |
3697181e | 283 | devices=devices, show_group=show_group, embedded=embedded) |
96c9bb79 MT |
284 | |
285 | ||
286 | class DeviceAndGroupsTableModule(ui_modules.UIModule): | |
287 | def render(self, devices): | |
288 | _ = self.locale.translate | |
289 | ||
290 | groups = {} | |
291 | ||
292 | for device in devices: | |
293 | if device.cls not in groups: | |
294 | groups[device.cls] = [] | |
295 | ||
296 | groups[device.cls].append(device) | |
297 | ||
298 | # Sort all devices | |
299 | for key in list(groups.keys()): | |
300 | groups[key].sort() | |
301 | ||
302 | # Order the groups by their name | |
303 | groups = list(groups.items()) | |
304 | groups.sort() | |
305 | ||
306 | return self.render_string("fireinfo/modules/table-devices-and-groups.html", | |
307 | groups=groups) | |
308 | ||
309 | ||
310 | class GeoTableModule(ui_modules.UIModule): | |
311 | def render(self, items): | |
312 | countries = [] | |
313 | other_countries = [] | |
314 | for code, value in items: | |
315 | # Skip the satellite providers in this ranking | |
316 | if code in (None, "A1", "A2"): | |
317 | continue | |
318 | ||
319 | name = self.backend.geoip.get_country_name(code) | |
320 | ||
321 | # Don't add countries with a small share on the list | |
322 | if value < 0.01: | |
323 | other_countries.append(name) | |
324 | continue | |
325 | ||
326 | country = database.Row({ | |
327 | "code" : code, | |
328 | "name" : name, | |
329 | "value" : value, | |
330 | }) | |
331 | countries.append(country) | |
332 | ||
333 | return self.render_string("fireinfo/modules/table-geo.html", | |
334 | countries=countries, other_countries=other_countries) |