]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - www/webapp/backend/stasy.py
3 from __future__
import division
9 from misc
import Singleton
11 DATABASE_HOST
= ["irma.ipfire.org", "madeye.ipfire.org"]
12 DATABASE_NAME
= "stasy"
14 CPU_SPEED_CONSTRAINTS
= (0, 500, 1000, 1500, 2000, 2500, 3000, 3500)
15 MEMORY_CONSTRAINTS
= (0, 64, 128, 256, 512, 1024, 2048, 4096, 8128, 16384)
17 class ProfileDict(object):
18 def __init__(self
, data
):
22 class ProfileCPU(ProfileDict
):
25 return self
._data
.get("arch")
29 return self
._data
.get("vendor")
33 return self
._data
.get("speed")
37 return self
._data
.get("bogomips")
41 return self
._data
.get("model")
44 def model_string(self
):
45 return self
._data
.get("model_string")
49 return self
._data
.get("flags")
53 return self
._data
.get("count")
56 def capable_64bit(self
):
57 return "lm" in self
.flags
60 def capable_pae(self
):
61 return "pae" in self
.flags
64 def capable_virt(self
):
65 return "vmx" in self
.flags
or "svm" in self
.flags
68 class ProfileHypervisor(ProfileDict
):
70 return "<%s %s-%s>" % (self
.__class
__.__name
__, self
.vendor
, self
.type)
74 return self
._data
.get("vendor")
78 return self
._data
.get("type")
81 class ProfileDevice(ProfileDict
):
89 "00" : "unclassified",
90 "01" : "Mass storage",
94 "05" : "Memory controller",
96 "07" : "Communication",
97 "08" : "Generic system peripheral",
98 "09" : "Input device",
99 "0a" : "Docking station",
103 "0e" : "Intelligent controller",
104 "0f" : "Satellite communications controller",
105 "10" : "Encryption controller",
106 "11" : "Signal processing controller",
107 "ff" : "Unassigned class",
113 "02" : "Communication",
114 "03" : "Input device",
115 "05" : "Physical ???",
118 "08" : "Mass storage",
120 "0a" : "CDC-Data ???",
121 "0b" : "Smart card ???",
122 "0d" : "Content Security ???",
124 "0f" : "Personal healthcare ???",
125 "dc" : "Diagnostic Device",
128 "fe" : "Application specific ???",
129 "ff" : "Vendor specific ???",
133 def __cmp__(self
, other
):
134 return cmp(self
.vendor
, other
.vendor
) or \
135 cmp(self
.model
, other
.model
) or \
136 cmp(self
.driver
, other
.driver
)
140 return self
._data
.get("model")
143 def model_string(self
):
144 cls
= self
.subsystem2class
[self
.subsystem
]
146 return cls().get_device(self
.vendor
, self
.model
)
150 return self
._data
.get("subsystem")
154 return self
._data
.get("vendor")
157 def vendor_string(self
):
158 cls
= self
.subsystem2class
[self
.subsystem
]
160 return cls().get_vendor(self
.vendor
)
164 return self
._data
.get("driver")
168 classid
= self
._data
.get("deviceclass")
170 if self
.subsystem
== "pci":
171 classid
= classid
[:-4]
172 if len(classid
) == 1:
173 classid
= "0%s" % classid
175 elif self
.subsystem
== "usb" and classid
:
176 classid
= classid
.split("/")[0]
177 classid
= "%02x" % int(classid
)
180 return self
.classid2name
[self
.subsystem
][classid
]
185 class Profile(ProfileDict
):
186 def __init__(self
, profile_blob
):
187 ProfileDict
.__init
__(self
, profile_blob
.get("profile"))
189 self
.public_id
= profile_blob
.get("public_id")
190 self
.updated
= profile_blob
.get("updated")
191 self
._geoip
= profile_blob
.get("geoip", None)
194 return "<%s %s>" % (self
.__class
__.__name
__, self
.public_id
)
198 return ProfileCPU(self
._data
.get("cpu"))
203 for d
in self
._data
.get("devices"):
206 if d
.driver
in ("pcieport", "usb", "hub"):
214 def hypervisor(self
):
216 return ProfileHypervisor(self
._data
.get("hypervisor"))
220 return self
.system
.get("virtual")
224 return self
._data
.get("system")
228 return self
.system
.get("release")
232 return self
.system
.get("kernel_release")
236 return self
.system
.get("memory")
240 return self
.system
.get("root_size") or 0
244 return self
.system
.get("vendor")
248 return self
.system
.get("model")
251 def country_code(self
):
253 return self
._geoip
["country_code"].lower()
259 __metaclass__
= Singleton
262 # Initialize database connection
263 self
._conn
= pymongo
.Connection(DATABASE_HOST
)
264 self
._db
= self
._conn
[DATABASE_NAME
]
266 def get_profile_count(self
):
267 # XXX need to implement something to get profiles updated since
270 # All distinct profiles (based on public_id)
271 return self
._db
.profiles
.distinct("public_id").count()
273 #def _get_profile_cursor(self, public_id):
274 # c = self._db.profiles.find({ "public_id" : public_id })
275 # c.sort("updated", pymongo.ASCENDING)
279 def profile_exists(self
, public_id
):
280 return self
._db
.profiles
.find({ "public_id" : public_id
}).count() >= 1
282 def get_profile(self
, public_id
):
283 # XXX should only find one object in the end
284 for p
in self
._db
.profiles
.find({ "public_id" : public_id
}):
289 def get_profiles(self
):
290 # XXX needs nicer database query
292 for p
in self
._db
.profiles
.find():
293 if not p
.get("public_id") in profiles
:
294 profiles
.append(p
.get("public_id"))
299 def secret_ids(self
):
300 return self
._db
.profiles
.distinct("secret_id")
304 return self
._db
.profiles
.distinct("profile.cpu")
307 def cpu_vendors(self
):
308 return self
._db
.profiles
.distinct("profile.cpu.vendor")
311 def cpu_vendors_map(self
):
314 for vendor
in self
.cpu_vendors
:
316 self
._db
.profiles
.find({
317 "profile.cpu.vendor" : vendor
323 def cpu_speed_average(self
):
326 all
= self
._db
.profiles
.find()
328 # XXX ugly. needs to be done by group()
330 if not m
.has_key("profile"):
332 speed
+= m
.get("profile").get("cpu").get("speed")
334 return (speed
/ all
.count())
337 def cpu_speed_map(self
):
340 for i
in range(len(CPU_SPEED_CONSTRAINTS
) - 1):
341 min, max = CPU_SPEED_CONSTRAINTS
[i
:i
+2]
343 cpu_speeds
[min, max] = \
344 self
._db
.profiles
.find(
345 { "profile.cpu.speed" : {
346 "$gte" : min, "$lt" : max
352 def get_memory_map(self
):
355 for i
in range(len(MEMORY_CONSTRAINTS
) - 1):
356 min, max = MEMORY_CONSTRAINTS
[i
:i
+2]
359 self
._db
.profiles
.find(
360 { "profile.system.memory" : {
361 "$gte" : min * 1024, "$lt" : max * 1024
368 def memory_average(self
):
371 all
= self
._db
.profiles
.find()
373 # XXX ugly. needs to be done by group()
375 if not m
.has_key("profile"):
377 memory
+= int(m
.get("profile").get("system").get("memory"))
379 return (memory
/ all
.count()) / 1024
382 def hypervisor_vendors(self
):
383 return self
._db
.profiles
.distinct("profile.hypervisor.vendor")
386 def hypervisor_map(self
):
389 for hypervisor
in self
.hypervisor_vendors
:
390 hypervisors
[hypervisor
] = \
391 self
._db
.profiles
.find({
392 "profile.hypervisor.vendor" : hypervisor
398 def hypervisor_models(self
):
399 return self
._db
.profiles
.distinct("profile.hypervisor.model")
402 def virtual_map(self
):
408 for k
in virtual
.keys():
410 self
._db
.profiles
.find({ "profile.system.virtual": k
}).count()
416 return self
._db
.profiles
.distinct("profile.system.language")
418 def get_language_map(self
):
421 for language
in self
.languages
:
422 languages
[language
] = \
423 self
._db
.profiles
.find({
424 "profile.system.language" : language
431 return self
._db
.profiles
.distinct("profile.system.vendor")
434 def vendor_map(self
):
437 for vendor
in self
.vendors
:
439 self
._db
.profiles
.find({
440 "profile.system.vendor" : vendor
447 return self
._db
.profiles
.distinct("profile.system.model")
453 for model
in self
.models
:
455 self
._db
.profiles
.find({
456 "profile.system.model" : model
463 return self
._db
.profiles
.distinct("profile.cpu.arch")
469 for arch
in self
.arches
:
471 self
._db
.profiles
.find({
472 "profile.cpu.arch" : arch
479 return self
._db
.profiles
.distinct("profile.system.kernel_release")
482 def kernel_map(self
):
485 for kernel
in self
.kernels
:
487 self
._db
.profiles
.find({
488 "profile.system.kernel_release" : kernel
495 return self
._db
.profiles
.distinct("profile.system.release")
498 def release_map(self
):
501 for release
in self
.releases
:
502 releases
[release
] = \
503 self
._db
.profiles
.find({
504 "profile.system.release" : release
509 def get_device_percentage(self
, bus
, vendor_id
, model_id
):
510 profiles_with_device
= self
._db
.profiles
.find({
511 "profile.devices.subsystem" : bus
,
512 "profile.devices.vendor" : vendor_id
,
513 "profile.devices.model" : model_id
,
516 # XXX must only be systems that have profile data
517 profiles_all
= self
._db
.profiles
.find()
519 if not profiles_all
.count():
522 return profiles_with_device
.count() / profiles_all
.count()
524 def get_cpu_flag_map(self
, flag
):
528 self
._db
.profiles
.find({
529 "profile.cpu.flags" : flag
,
532 # XXX must only be systems that have profile data
533 profiles_all
= self
._db
.profiles
.find()
535 flags
[False] = profiles_all
.count() - flags
[True]
540 def geo_locations(self
):
541 return [code
.lower() for code
in self
._db
.profiles
.distinct("geoip.country_code")]
543 def get_geo_location_map(self
):
547 for geo_location
in self
.geo_locations
:
548 geo_locations
[geo_location
] = \
549 self
._db
.profiles
.find({
550 "geoip.country_code" : geo_location
553 count
+= geo_locations
[geo_location
]
555 # XXX must only be systems that have profile data
556 profiles_all
= self
._db
.profiles
.find()
558 unknown_count
= profiles_all
.count() - count
560 geo_locations
["unknown"] = unknown_count
564 def get_models_by_vendor(self
, subsystem
, vendor_id
):
567 # XXX must only be systems that have profile data
568 profiles_all
= self
._db
.profiles
.find()
570 for profile
in profiles_all
:
571 if not profile
.has_key("profile"):
574 profile
= Profile(profile
)
576 for device
in profile
.devices
:
577 if not device
.vendor
== vendor_id
:
580 if not device
in devices
:
581 devices
.append(device
)
586 if __name__
== "__main__":
589 print s
.get_profile("0" * 40)
591 for id in s
.secret_ids
:
594 #for p in s._db.profiles.find():
599 print s
.memory_average
600 print s
.hypervisor_vendors
601 print s
.hypervisor_models
603 print s
.language_maps
616 p
= s
.get_profile("0b5f4fe2162fdfbfa29b632610e317078fa70d34")