]> git.ipfire.org Git - ipfire.org.git/blob - src/web/fireinfo.py
3fa99547391f7355316d75f8dd6a957550850dec
[ipfire.org.git] / src / web / fireinfo.py
1 #!/usr/bin/python
2
3 import datetime
4 import hwdata
5 import logging
6 import re
7 import json
8 import tornado.web
9
10 from .. import fireinfo
11
12 from . import base
13 from . import ui_modules
14
15 class BaseHandler(base.BaseHandler):
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:
29 raise AttributeError(key)
30
31 def __setattr__(self, key, val):
32 self[key] = val
33
34
35 class ProfileSendHandler(BaseHandler):
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:
54 if attr not in profile:
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:
124 return json.loads(profile)
125 except json.decoder.JSONDecodeError as e:
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"]:
131 raise tornado.web.HTTPError(405)
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.
145 with self.db.transaction():
146 try:
147 self.fireinfo.handle_profile(public_id, profile_blob, location=location)
148
149 except fireinfo.ProfileParserError:
150 raise tornado.web.HTTPError(400)
151
152 self.finish("Your profile was successfully saved to the database.")
153
154
155 class IndexHandler(BaseHandler):
156 def get(self):
157 data = {
158 # Release
159 "latest_release" : self.backend.releases.get_latest(),
160
161 # Hardware
162 "arches" : self.fireinfo.get_arch_map(when=self.when),
163 "cpu_vendors" : self.fireinfo.get_cpu_vendors_map(when=self.when),
164 "memory_avg" : self.backend.fireinfo.get_average_memory_amount(when=self.when),
165
166 # Virtualization
167 "hypervisors" : self.fireinfo.get_hypervisor_map(when=self.when),
168 "virtual_ratio" : self.fireinfo.get_virtual_ratio(when=self.when),
169 }
170
171 # Cache for 1h
172 self.set_expires(3600)
173
174 self.render("fireinfo/index.html", **data)
175
176
177 class DriverDetail(BaseHandler):
178 def get(self, driver):
179 # Cache for 1h
180 self.set_expires(3600)
181
182 self.render("fireinfo/driver.html", driver=driver,
183 driver_map=self.fireinfo.get_driver_map(driver, when=self.when))
184
185
186 class ProfileHandler(BaseHandler):
187 def get(self, profile_id):
188 profile = self.fireinfo.get_profile(profile_id, when=self.when)
189
190 if not profile or not profile.is_showable():
191 raise tornado.web.HTTPError(404)
192
193 # Cache for 1h
194 self.set_expires(3600)
195
196 self.render("fireinfo/profile.html", profile=profile)
197
198
199 class RandomProfileHandler(BaseHandler):
200 def get(self):
201 profile_id = self.fireinfo.get_random_profile(when=self.when)
202 if profile_id is None:
203 raise tornado.web.HTTPError(404)
204
205 self.redirect("/profile/%s" % profile_id)
206
207
208 class StatsHandler(BaseHandler):
209 def get(self):
210 self.render("fireinfo/stats.html")
211
212
213 class StatsProcessorsHandler(BaseHandler):
214 def get(self):
215 avg, stddev, min, max = self.fireinfo.get_cpu_clock_speeds(when=self.when)
216 arch_map = self.fireinfo.get_arch_map(when=self.when)
217
218 data = {
219 "arch_map" : arch_map,
220 "cpu_vendors" : self.fireinfo.get_cpu_vendors_map(when=self.when),
221 "clock_speed_avg" : avg,
222 "clock_speed_stddev" : stddev,
223 "clock_speed_min" : min,
224 "clock_speed_max" : max,
225 }
226
227 return self.render("fireinfo/stats-cpus.html", **data)
228
229
230 class StatsProcessorDetailHandler(BaseHandler):
231 def get(self, platform):
232 assert platform in ("arm", "x86")
233
234 flags = self.fireinfo.get_common_cpu_flags_by_platform(platform, when=self.when)
235
236 return self.render("fireinfo/stats-cpus-detail.html",
237 platform=platform, flags=flags)
238
239
240 class StatsReleasesHandler(BaseHandler):
241 def get(self):
242 data = {
243 "releases" : self.fireinfo.get_releases_map(when=self.when),
244 "kernels" : self.fireinfo.get_kernels_map(when=self.when),
245 }
246 return self.render("fireinfo/stats-oses.html", **data)
247
248
249 class StatsGeoHandler(BaseHandler):
250 def get(self):
251 return self.render("fireinfo/stats-geo.html",
252 geo_locations = self.fireinfo.get_geo_location_map(when=self.when))
253
254
255 class VendorsHandler(BaseHandler):
256 def get(self):
257 vendors = self.fireinfo.get_vendor_list(when=self.when)
258
259 # Cache for 1h
260 self.set_expires(3600)
261
262 self.render("fireinfo/vendors.html", vendors=vendors)
263
264
265 class VendorHandler(BaseHandler):
266 def get(self, subsystem, vendor_id):
267 devices = self.fireinfo.get_devices_by_vendor(subsystem, vendor_id, when=self.when)
268 if not devices:
269 raise tornado.web.HTTPError(404)
270
271 vendor_name = self.fireinfo.get_vendor_string(subsystem, vendor_id)
272
273 # Cache for 1h
274 self.set_expires(3600)
275
276 self.render("fireinfo/vendor.html", vendor_name=vendor_name, devices=devices)
277
278
279 class DeviceTableModule(ui_modules.UIModule):
280 def render(self, devices, show_group=True, embedded=False):
281 return self.render_string("fireinfo/modules/table-devices.html",
282 devices=devices, show_group=show_group, embedded=embedded)
283
284
285 class DeviceAndGroupsTableModule(ui_modules.UIModule):
286 def render(self, devices):
287 _ = self.locale.translate
288
289 groups = {}
290
291 for device in devices:
292 if device.cls not in groups:
293 groups[device.cls] = []
294
295 groups[device.cls].append(device)
296
297 # Sort all devices
298 for key in list(groups.keys()):
299 groups[key].sort()
300
301 # Order the groups by their name
302 groups = list(groups.items())
303 groups.sort()
304
305 return self.render_string("fireinfo/modules/table-devices-and-groups.html",
306 groups=groups)
307
308
309 class GeoTableModule(ui_modules.UIModule):
310 def render(self, items):
311 countries = []
312 other_countries = []
313 for code, value in items:
314 # Skip the satellite providers in this ranking
315 if code in (None, "A1", "A2"):
316 continue
317
318 name = self.backend.geoip.get_country_name(code)
319
320 # Don't add countries with a small share on the list
321 if value < 0.01:
322 other_countries.append(name)
323 continue
324
325 country = database.Row({
326 "code" : code,
327 "name" : name,
328 "value" : value,
329 })
330 countries.append(country)
331
332 return self.render_string("fireinfo/modules/table-geo.html",
333 countries=countries, other_countries=other_countries)