]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/fireinfo.py
10 from .. import fireinfo
13 from . import ui_modules
15 class BaseHandler(base
.BaseHandler
):
18 return self
.get_argument_date("when", None)
21 MIN_PROFILE_VERSION
= 0
22 MAX_PROFILE_VERSION
= 0
25 def __getattr__(self
, key
):
29 raise AttributeError(key
)
31 def __setattr__(self
, key
, val
):
35 class ProfileSendHandler(BaseHandler
):
36 def check_xsrf_cookie(self
):
37 # This cookie is not required here.
41 # Create an empty profile.
42 self
.profile
= Profile()
44 def __check_attributes(self
, profile
):
46 Check for attributes that must be provided,
53 for attr
in attributes
:
54 if attr
not in profile
:
55 raise tornado
.web
.HTTPError(400, "Profile lacks '%s' attribute: %s" % (attr
, profile
))
57 def __check_valid_ids(self
, profile
):
59 Check if IDs contain valid data.
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
))
65 def __check_equal_ids(self
, profile
):
67 Check if public_id and private_id are equal.
69 if profile
.public_id
== profile
.private_id
:
70 raise tornado
.web
.HTTPError(400, "Public and private IDs are equal: %s" % profile
)
72 def __check_matching_ids(self
, profile
):
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.
77 p
= self
.profiles
.find_one({ "public_id" : profile
["public_id"]})
82 if p
.private_id
!= profile
.private_id
:
83 raise tornado
.web
.HTTPError(400, "Mismatch of private_id: %s" % profile
)
85 def __check_profile_version(self
, profile
):
87 Check if this version of the server software does support the
90 version
= profile
.profile_version
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
)
96 def check_profile_blob(self
, profile
):
98 This method checks if the blob is sane.
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
107 self
.__check
_matching
_ids
,
113 # If we got here, everything is okay and we can go on...
115 def get_profile_blob(self
):
116 profile
= self
.get_argument("profile", None)
118 # Send "400 bad request" if no profile was provided
120 raise tornado
.web
.HTTPError(400, "No profile received")
122 # Try to decode the profile.
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
)
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)
133 return self
.post(public_id
)
135 def post(self
, public_id
):
136 profile_blob
= self
.get_profile_blob()
137 #self.check_profile_blob(profile_blob)
140 location
= self
.get_remote_location()
142 location
= location
.country
144 # Handle the profile.
145 with self
.db
.transaction():
147 self
.fireinfo
.handle_profile(public_id
, profile_blob
, location
=location
)
149 except fireinfo
.ProfileParserError
:
150 raise tornado
.web
.HTTPError(400)
152 self
.finish("Your profile was successfully saved to the database.")
155 class IndexHandler(BaseHandler
):
159 "latest_release" : self
.backend
.releases
.get_latest(),
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
),
167 "hypervisors" : self
.fireinfo
.get_hypervisor_map(when
=self
.when
),
168 "virtual_ratio" : self
.fireinfo
.get_virtual_ratio(when
=self
.when
),
171 "locations" : self
.fireinfo
.get_geo_location_map(when
=self
.when
),
175 self
.set_expires(3600)
177 self
.render("fireinfo/index.html", **data
)
180 class DriverDetail(BaseHandler
):
181 def get(self
, driver
):
183 self
.set_expires(3600)
185 self
.render("fireinfo/driver.html", driver
=driver
,
186 driver_map
=self
.fireinfo
.get_driver_map(driver
, when
=self
.when
))
189 class ProfileHandler(BaseHandler
):
190 def get(self
, profile_id
):
191 profile
= self
.fireinfo
.get_profile(profile_id
, when
=self
.when
)
193 if not profile
or not profile
.is_showable():
194 raise tornado
.web
.HTTPError(404)
197 self
.set_expires(3600)
199 self
.render("fireinfo/profile.html", profile
=profile
)
202 class RandomProfileHandler(BaseHandler
):
204 profile_id
= self
.fireinfo
.get_random_profile(when
=self
.when
)
205 if profile_id
is None:
206 raise tornado
.web
.HTTPError(404)
208 self
.redirect("/profile/%s" % profile_id
)
211 class ReleasesHandler(BaseHandler
):
214 "releases" : self
.fireinfo
.get_releases_map(when
=self
.when
),
215 "kernels" : self
.fireinfo
.get_kernels_map(when
=self
.when
),
219 self
.set_expires(3600)
221 return self
.render("fireinfo/releases.html", **data
)
224 class ProcessorsHandler(BaseHandler
):
228 for platform
in ("arm", "x86"):
230 self
.fireinfo
.get_common_cpu_flags_by_platform(platform
, when
=self
.when
)
233 self
.set_expires(3600)
235 return self
.render("fireinfo/processors.html", flags
=flags
)
238 class VendorsHandler(BaseHandler
):
240 vendors
= self
.fireinfo
.get_vendor_list(when
=self
.when
)
243 self
.set_expires(3600)
245 self
.render("fireinfo/vendors.html", vendors
=vendors
)
248 class VendorHandler(BaseHandler
):
249 def get(self
, subsystem
, vendor_id
):
250 devices
= self
.fireinfo
.get_devices_by_vendor(subsystem
, vendor_id
, when
=self
.when
)
252 raise tornado
.web
.HTTPError(404)
254 vendor_name
= self
.fireinfo
.get_vendor_string(subsystem
, vendor_id
)
257 self
.set_expires(3600)
259 self
.render("fireinfo/vendor.html", vendor_name
=vendor_name
, devices
=devices
)
262 class DeviceTableModule(ui_modules
.UIModule
):
263 def render(self
, devices
, show_group
=True, embedded
=False):
264 return self
.render_string("fireinfo/modules/table-devices.html",
265 devices
=devices
, show_group
=show_group
, embedded
=embedded
)
268 class DeviceAndGroupsTableModule(ui_modules
.UIModule
):
269 def render(self
, devices
):
270 _
= self
.locale
.translate
274 for device
in devices
:
275 if device
.cls
not in groups
:
276 groups
[device
.cls
] = []
278 groups
[device
.cls
].append(device
)
281 for key
in list(groups
.keys()):
284 # Order the groups by their name
285 groups
= list(groups
.items())
288 return self
.render_string("fireinfo/modules/table-devices-and-groups.html",
292 class GeoTableModule(ui_modules
.UIModule
):
293 def render(self
, items
):
296 for code
, value
in items
:
297 # Skip the satellite providers in this ranking
298 if code
in (None, "A1", "A2"):
301 name
= self
.backend
.geoip
.get_country_name(code
)
303 # Don't add countries with a small share on the list
305 other_countries
.append(name
)
308 country
= database
.Row({
313 countries
.append(country
)
315 return self
.render_string("fireinfo/modules/table-geo.html",
316 countries
=countries
, other_countries
=other_countries
)