3 from __future__
import division
11 from misc
import Object
16 "AMDisbetter!" : "AMD",
17 "AuthenticAMD" : "AMD",
18 "CentaurHauls" : "VIA",
19 "CyrixInstead" : "Cyrix",
20 "GenuineIntel" : "Intel",
21 "TransmetaCPU" : "Transmeta",
22 "GenuineTMx86" : "Transmeta",
23 "Geode by NSC" : "NSC",
24 "NexGenDriven" : "NexGen",
25 "RiseRiseRise" : "Rise",
26 "SiS SiS SiS" : "SiS",
27 "SiS SiS SiS " : "SiS",
28 "UMC UMC UMC " : "UMC",
29 "VIA VIA VIA " : "VIA",
30 "Vortex86 SoC" : "Vortex86",
36 (r
"AMD ([\w\-]+) APU with Radeon\(tm\) HD Graphics", r
"AMD \1 APU"),
38 (r
"AMD Athlon.* II X2 ([a-z0-9]+).*", r
"AMD Athlon X2 \1"),
39 (r
"AMD Athlon\(tm\) 64 Processor (\w+)", r
"AMD Athlon64 \1"),
40 (r
"AMD Athlon\(tm\) 64 X2 Dual Core Processor (\w+)", r
"AMD Athlon64 X2 \1"),
41 (r
"(AMD Athlon).*(XP).*", r
"\1 \2"),
42 (r
"(AMD Phenom).* ([0-9]+) .*", r
"\1 \2"),
43 (r
"(AMD Phenom).*", r
"\1"),
44 (r
"(AMD Sempron).*", r
"\1"),
46 (r
"Geode\(TM\) Integrated Processor by AMD PCS", r
"AMD Geode"),
47 (r
"(Geode).*", r
"\1"),
50 (r
"Intel\(R\) (Atom|Celeron).*CPU\s*([A-Z0-9]+) .*", r
"Intel \1 \2"),
51 (r
"(Intel).*(Celeron).*", r
"\1 \2"),
52 (r
"Intel\(R\)? Core\(TM\)?2 Duo *CPU .* ([A-Z0-9]+) .*", r
"Intel C2D \1"),
53 (r
"Intel\(R\)? Core\(TM\)?2 Duo CPU (\w+)", r
"Intel C2D \1"),
54 (r
"Intel\(R\)? Core\(TM\)?2 CPU .* ([A-Z0-9]+) .*", r
"Intel C2 \1"),
55 (r
"Intel\(R\)? Core\(TM\)?2 Quad *CPU .* ([A-Z0-9]+) .*", r
"Intel C2Q \1"),
56 (r
"Intel\(R\)? Core\(TM\)? (i[753]\-\w+) CPU", r
"Intel Core \1"),
57 (r
"Intel\(R\)? Xeon\(R\)? CPU (\w+) (0|v\d+)", r
"Intel Xeon \1 \2"),
58 (r
"Intel\(R\)? Xeon\(R\)? CPU\s+(\w+)", r
"Intel Xeon \1"),
59 (r
"(Intel).*(Xeon).*", r
"\1 \2"),
60 (r
"Intel.* Pentium.* (D|4) .*", r
"Intel Pentium \1"),
61 (r
"Intel.* Pentium.* Dual .* ([A-Z0-9]+) .*", r
"Intel Pentium Dual \1"),
62 (r
"Pentium.* Dual-Core .* ([A-Z0-9]+) .*", r
"Intel Pentium Dual \1"),
63 (r
"(Pentium I{2,3}).*", r
"Intel \1"),
64 (r
"(Celeron \(Coppermine\))", r
"Intel Celeron"),
67 (r
"Geode\(TM\) Integrated Processor by National Semi", r
"NSC Geode"),
70 (r
"(VIA \w*).*", r
"\1"),
73 (r
"QEMU Virtual CPU version .*", r
"QEMU CPU"),
76 (r
"Feroceon .*", r
"ARM Feroceon"),
79 IGNORED_DEVICES
= ["usb",]
81 class ProfileDict(object):
82 def __init__(self
, data
):
86 class ProfileNetwork(ProfileDict
):
87 def __eq__(self
, other
):
91 if not self
.has_red
== other
.has_red
:
94 if not self
.has_green
== other
.has_green
:
97 if not self
.has_orange
== other
.has_orange
:
100 if not self
.has_blue
== other
.has_blue
:
108 for zone
in ("red", "green", "orange", "blue"):
109 if self
.has_zone(zone
):
114 def has_zone(self
, name
):
115 return self
._data
.get("has_%s" % name
)
119 return self
._data
.get("has_red", False)
123 return self
._data
.get("has_green", False)
126 def has_orange(self
):
127 return self
._data
.get("has_orange", False)
131 return self
._data
.get("has_blue", False)
134 class Processor(Object
):
135 def __init__(self
, backend
, id, data
=None, clock_speed
=None, bogomips
=None):
136 Object
.__init
__(self
, backend
)
140 self
.__clock
_speed
= clock_speed
141 self
.__bogomips
= bogomips
146 if not self
.model_string
.startswith(self
.vendor
):
147 s
.append(self
.vendor
)
150 s
.append(self
.model_string
)
152 if self
.core_count
> 1:
153 s
.append("x%s" % self
.core_count
)
159 if self
.__data
is None:
160 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_processors \
161 WHERE id = %s", self
.id)
168 return CPU_VENDORS
[self
.data
.vendor
]
170 return self
.data
.vendor
174 return self
.data
.family
178 return self
.data
.model
182 return self
.data
.stepping
185 def model_string(self
):
186 s
= self
.data
.model_string
.split()
188 return " ".join((e
for e
in s
if e
))
192 return self
.data
.flags
194 def has_flag(self
, flag
):
195 return flag
in self
.flags
198 if self
.family
== 6 and self
.model
in (55, 77):
201 return self
.has_flag("ht")
204 def core_count(self
):
205 return self
.data
.core_count
210 return self
.core_count
// 2
212 return self
.core_count
215 def clock_speed(self
):
216 return self
.__clock
_speed
218 def format_clock_speed(self
):
219 if not self
.clock_speed
:
222 if self
.clock_speed
< 1000:
223 return "%dMHz" % self
.clock_speed
225 return "%.2fGHz" % round(self
.clock_speed
/ 1000, 2)
229 return self
.__bogomips
232 def capabilities(self
):
234 ("64bit", self
.has_flag("lm")),
235 ("aes", self
.has_flag("aes")),
236 ("nx", self
.has_flag("nx")),
237 ("pae", self
.has_flag("pae") or self
.has_flag("lpae")),
238 ("rdrand", self
.has_flag("rdrand")),
241 # If the system is already running in a virtual environment,
242 # we cannot correctly detect if the CPU supports svm or vmx
243 if self
.has_flag("hypervisor"):
244 caps
.append(("virt", None))
246 caps
.append(("virt", self
.has_flag("vmx") or self
.has_flag("svm")))
250 def format_model(self
):
251 s
= self
.model_string
253 # Remove everything after the @: Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
254 s
, sep
, rest
= s
.partition("@")
256 for pattern
, repl
in CPU_STRINGS
:
257 if re
.match(pattern
, s
) is None:
260 s
= re
.sub(pattern
, repl
, s
)
263 # Otherwise remove the symbols
264 for i
in ("C", "R", "TM", "tm"):
265 s
= s
.replace("(%s)" % i
, "")
267 # Replace too long strings with shorter ones
269 ("Quad-Core Processor", ""),
270 ("Dual-Core Processor", ""),
271 ("Processor", "CPU"),
272 ("processor", "CPU"),
277 # Remove too many spaces
278 s
= " ".join((e
for e
in s
.split() if e
))
283 def friendly_string(self
):
286 model
= self
.format_model()
289 clock_speed
= self
.format_clock_speed()
291 s
.append("@ %s" % clock_speed
)
294 s
.append("x%s" % self
.count
)
299 class Device(Object
):
302 "00" : N_("Unclassified"),
303 "01" : N_("Mass storage"),
304 "02" : N_("Network"),
305 "03" : N_("Display"),
306 "04" : N_("Multimedia"),
307 "05" : N_("Memory controller"),
309 "07" : N_("Communication"),
310 "08" : N_("Generic system peripheral"),
311 "09" : N_("Input device"),
312 "0a" : N_("Docking station"),
313 "0b" : N_("Processor"),
314 "0c" : N_("Serial bus"),
315 "0d" : N_("Wireless"),
316 "0e" : N_("Intelligent controller"),
317 "0f" : N_("Satellite communications controller"),
318 "10" : N_("Encryption"),
319 "11" : N_("Signal processing controller"),
320 "ff" : N_("Unassigned class"),
324 "00" : N_("Unclassified"),
325 "01" : N_("Multimedia"),
326 "02" : N_("Communication"),
327 "03" : N_("Input device"),
328 "05" : N_("Generic system peripheral"),
330 "07" : N_("Printer"),
331 "08" : N_("Mass storage"),
333 "0a" : N_("Communication"),
334 "0b" : N_("Smart card"),
335 "0d" : N_("Encryption"),
336 "0e" : N_("Display"),
337 "0f" : N_("Personal Healthcare"),
338 "dc" : N_("Diagnostic Device"),
339 "e0" : N_("Wireless"),
340 "ef" : N_("Unclassified"),
341 "fe" : N_("Unclassified"),
342 "ff" : N_("Unclassified"),
346 def __init__(self
, backend
, id, data
=None):
347 Object
.__init
__(self
, backend
)
353 return "<%s vendor=%s model=%s>" % (self
.__class
__.__name
__,
354 self
.vendor_string
, self
.model_string
)
356 def __cmp__(self
, other
):
357 if self
.id and self
.id == other
.id:
361 cmp(self
.subsystem
, other
.subsystem
) or \
362 cmp(self
.vendor_string
, other
.vendor_string
) or \
363 cmp(self
.vendor
, other
.vendor
) or \
364 cmp(self
.model_string
, other
.model_string
) or \
365 cmp(self
.model
, other
.model
) or \
366 cmp(self
.driver
, other
.driver
)
370 if self
.__data
is None:
373 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_devices \
374 WHERE id = %s", self
.id)
378 def is_showable(self
):
379 if self
.driver
in IGNORED_DEVICES
:
382 if self
.driver
in ("pcieport", "hub"):
389 return self
.data
.subsystem
393 return self
.data
.model
396 def model_string(self
):
397 return self
.fireinfo
.get_model_string(self
.subsystem
,
398 self
.vendor
, self
.model
)
402 return self
.data
.vendor
405 def vendor_string(self
):
406 return self
.fireinfo
.get_vendor_string(self
.subsystem
, self
.vendor
)
410 return self
.data
.driver
414 classid
= self
.data
.deviceclass
416 if self
.subsystem
== "pci":
417 classid
= classid
[:-4]
418 if len(classid
) == 1:
419 classid
= "0%s" % classid
421 elif self
.subsystem
== "usb" and classid
:
422 classid
= classid
.split("/")[0]
423 classid
= "%02x" % int(classid
)
426 return self
.classid2name
[self
.subsystem
][classid
]
431 def percentage(self
):
432 return self
.data
.get("percentage", None)
435 class Profile(Object
):
436 def __init__(self
, backend
, id, data
=None):
437 Object
.__init
__(self
, backend
)
443 return "<%s %s>" % (self
.__class
__.__name
__, self
.public_id
)
445 def __cmp__(self
, other
):
446 return cmp(self
.id, other
.id)
448 def is_showable(self
):
456 if self
.__data
is None:
457 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_profiles \
458 WHERE id = %s", self
.id)
464 return self
.data
.public_id
467 def private_id(self
):
468 raise NotImplementedError
471 def time_created(self
):
472 return self
.data
.time_created
475 def time_updated(self
):
476 return self
.data
.time_updated
478 def updated(self
, profile_parser
=None, location
=None, when
=None):
479 valid
= self
.settings
.get_int("fireinfo_profile_days_valid", 14)
481 self
.db
.execute("UPDATE fireinfo_profiles \
483 time_updated = then_or_now(%s), \
484 time_valid = then_or_now(%s) + INTERVAL '%s days', \
485 updates = updates + 1 \
486 WHERE id = %s", when
, when
, valid
, self
.id)
489 self
.set_processor_speeds(
490 profile_parser
.processor_clock_speed
,
491 profile_parser
.processor_bogomips
,
495 self
.set_location(location
)
497 def expired(self
, when
=None):
498 self
.db
.execute("UPDATE fireinfo_profiles \
499 SET time_valid = then_or_now(%s) WHERE id = %s", when
, self
.id)
501 def parse(self
, parser
):
503 self
.processor
= parser
.processor
504 self
.set_processor_speeds(parser
.processor_clock_speed
, parser
.processor_bogomips
)
507 self
.devices
= parser
.devices
510 self
.system_id
= parser
.system_id
513 self
.memory
= parser
.memory
516 self
.storage
= parser
.storage
519 self
.kernel_id
= parser
.kernel_id
522 self
.arch_id
= parser
.arch_id
525 self
.release_id
= parser
.release_id
528 self
.language
= parser
.language
532 self
.hypervisor_id
= parser
.hypervisor_id
535 self
.network
= parser
.network
539 def get_location(self
):
540 if not hasattr(self
, "_location"):
541 res
= self
.db
.get("SELECT location FROM fireinfo_profiles_locations \
542 WHERE profile_id = %s", self
.id)
545 self
._location
= res
.location
547 self
._location
= None
549 return self
._location
551 def set_location(self
, location
):
552 if self
.location
== location
:
555 self
.db
.execute("DELETE FROM fireinfo_profiles_locations \
556 WHERE profile_id = %s", self
.id)
557 self
.db
.execute("INSERT INTO fireinfo_profiles_locations(profile_id, location) \
558 VALUES(%s, %s)", self
.id, location
)
560 self
._location
= location
562 location
= property(get_location
, set_location
)
565 def location_string(self
):
566 return self
.geoip
.get_country_name(self
.location
)
571 def device_ids(self
):
572 if not hasattr(self
, "_device_ids"):
573 res
= self
.db
.query("SELECT device_id FROM fireinfo_profiles_devices \
574 WHERE profile_id = %s", self
.id)
576 self
._device
_ids
= sorted([r
.device_id
for r
in res
])
578 return self
._device
_ids
580 def get_devices(self
):
581 if not hasattr(self
, "_devices"):
582 res
= self
.db
.query("SELECT * FROM fireinfo_devices \
583 LEFT JOIN fireinfo_profiles_devices ON \
584 fireinfo_devices.id = fireinfo_profiles_devices.device_id \
585 WHERE fireinfo_profiles_devices.profile_id = %s", self
.id)
589 device
= Device(self
.backend
, row
.id, row
)
590 self
._devices
.append(device
)
594 def set_devices(self
, devices
):
595 device_ids
= [d
.id for d
in devices
]
597 self
.db
.execute("DELETE FROM fireinfo_profiles_devices WHERE profile_id = %s", self
.id)
598 self
.db
.executemany("INSERT INTO fireinfo_profiles_devices(profile_id, device_id) \
599 VALUES(%s, %s)", ((self
.id, d
) for d
in device_ids
))
601 self
._devices
= devices
602 self
._device
_ids
= device_ids
604 devices
= property(get_devices
, set_devices
)
606 def count_device(self
, subsystem
, vendor
, model
):
609 for dev
in self
.devices
:
610 if dev
.subsystem
== subsystem
and dev
.vendor
== vendor
and dev
.model
== model
:
617 def get_system_id(self
):
618 if not hasattr(self
, "_system_id"):
619 res
= self
.db
.get("SELECT system_id AS id FROM fireinfo_profiles_systems \
620 WHERE profile_id = %s", self
.id)
623 self
._system
_id
= res
.id
625 self
._system
_id
= None
627 return self
._system
_id
629 def set_system_id(self
, system_id
):
630 self
.db
.execute("DELETE FROM fireinfo_profiles_systems WHERE profile_id = %s", self
.id)
633 self
.db
.execute("INSERT INTO fireinfo_profiles_systems(profile_id, system_id) \
634 VALUES(%s, %s)", self
.id, system_id
)
636 self
._system
_id
= None
637 if hasattr(self
, "_system"):
640 system_id
= property(get_system_id
, set_system_id
)
644 if not hasattr(self
, "_system"):
645 res
= self
.db
.get("SELECT fireinfo_systems.vendor AS vendor, fireinfo_systems.model AS model \
646 FROM fireinfo_profiles_systems \
647 LEFT JOIN fireinfo_systems ON fireinfo_profiles_systems.system_id = fireinfo_systems.id \
648 WHERE fireinfo_profiles_systems.profile_id = %s", self
.id)
651 self
._system
= (res
.vendor
, res
.model
)
653 self
._system
= (None, None)
658 def system_vendor(self
):
666 def system_model(self
):
674 def appliance_id(self
):
675 if not hasattr(self
, "_appliance_id"):
677 ("fountainnetworks-prime", self
._appliance
_test
_fountainnetworks
_prime
),
678 ("lightningwirelabs-eco", self
._appliance
_test
_lightningwirelabs
_eco
),
681 self
._appliance
_id
= None
682 for name
, test_func
in appliances
:
686 self
._appliance
_id
= name
689 return self
._appliance
_id
693 if self
.appliance_id
== "fountainnetworks-prime":
694 return "Fountain Networks - IPFire Prime Box"
696 elif self
.appliance_id
== "lightningwirelabs-eco":
697 return "Lightning Wire Labs - IPFire Eco Appliance"
699 def _appliance_test_fountainnetworks_prime(self
):
700 if not self
.system
in (("SECO", None), ("SECO", "0949")):
703 # Must have a wireless device
704 if self
.count_device("usb", "148f", "5572") < 1:
709 def _appliance_test_lightningwirelabs_eco(self
):
710 if not self
.system
== ("MSI", "MS-9877"):
713 # Must have four Intel network adapters
714 network_adapters_count
= self
.count_device("pci", "8086", "10d3")
715 if not network_adapters_count
== 4:
719 if not self
.memory
>= 4230823936 * 0.95:
727 def processor_id(self
):
728 if hasattr(self
, "_processor"):
729 return self
._processor
.id
731 if not hasattr(self
, "_processor_id"):
732 res
= self
.db
.get("SELECT processor_id FROM fireinfo_profiles_processors \
733 WHERE profile_id = %s", self
.id)
736 self
._processor
_id
= res
.processor_id
738 self
._processor
_id
= None
740 return self
._processor
_id
742 def get_processor(self
):
743 if not self
.processor_id
:
746 if not hasattr(self
, "_processor"):
747 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_processors \
748 WHERE profile_id = %s", self
.id)
751 self
._processor
= self
.fireinfo
.get_processor_by_id(res
.processor_id
,
752 clock_speed
=res
.clock_speed
, bogomips
=res
.bogomips
)
754 self
._processor
= None
756 return self
._processor
758 def set_processor(self
, processor
):
759 self
.db
.execute("DELETE FROM fireinfo_profiles_processors \
760 WHERE profile_id = %s", self
.id)
763 self
.db
.execute("INSERT INTO fireinfo_profiles_processors(profile_id, processor_id) \
764 VALUES(%s, %s)", self
.id, processor
.id)
766 self
._processor
= processor
768 processor
= property(get_processor
, set_processor
)
770 def set_processor_speeds(self
, clock_speed
, bogomips
):
771 self
.db
.execute("UPDATE fireinfo_profiles_processors \
772 SET clock_speed = %s, bogomips = %s WHERE profile_id = %s",
773 clock_speed
, bogomips
, self
.id)
778 return self
.processor
782 def get_memory(self
):
783 if not hasattr(self
, "_memory"):
784 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_memory \
785 WHERE profile_id = %s", self
.id)
788 self
._memory
= res
.amount
* 1024
794 def set_memory(self
, amount
):
795 if self
.memory
== amount
:
800 self
.db
.execute("DELETE FROM fireinfo_profiles_memory WHERE profile_id = %s", self
.id)
802 self
.db
.execute("INSERT INTO fireinfo_profiles_memory(profile_id, amount) \
803 VALUES(%s, %s)", self
.id, amount
)
805 self
._memory
= amount
* 1024
807 memory
= property(get_memory
, set_memory
)
810 def friendly_memory(self
):
811 return util
.format_size(self
.memory
or 0)
815 def get_storage(self
):
816 if not hasattr(self
, "_storage"):
817 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_storage \
818 WHERE profile_id = %s", self
.id)
821 self
._storage
= res
.amount
* 1024
827 def set_storage(self
, amount
):
828 if self
.storage
== amount
:
833 self
.db
.execute("DELETE FROM fireinfo_profiles_storage WHERE profile_id = %s", self
.id)
835 self
.db
.execute("INSERT INTO fireinfo_profiles_storage(profile_id, amount) \
836 VALUES(%s, %s)", self
.id, amount
)
838 self
._storage
= amount
* 1024
840 storage
= property(get_storage
, set_storage
)
843 def friendly_storage(self
):
844 return util
.format_size(self
.storage
)
848 def get_kernel_id(self
):
849 if not hasattr(self
, "_kernel_id"):
850 res
= self
.db
.get("SELECT fireinfo_profiles_kernels.kernel_id AS id FROM fireinfo_profiles \
851 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
852 WHERE fireinfo_profiles.id = %s", self
.id)
855 self
._kernel
_id
= res
.id
857 self
._kernel
_id
= None
859 return self
._kernel
_id
861 def set_kernel_id(self
, kernel_id
):
862 if self
.kernel_id
== kernel_id
:
865 self
.db
.execute("DELETE FROM fireinfo_profiles_kernels WHERE profile_id = %s", self
.id)
867 self
.db
.execute("INSERT INTO fireinfo_profiles_kernels(profile_id, kernel_id) \
868 VALUES(%s, %s)", self
.id, kernel_id
)
870 self
._kernel
_id
= kernel_id
871 if hasattr(self
, "_kernel"):
874 kernel_id
= property(get_kernel_id
, set_kernel_id
)
878 if not hasattr(self
, "_kernel"):
879 res
= self
.db
.get("SELECT fireinfo_kernels.name AS name FROM fireinfo_profiles \
880 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
881 LEFT JOIN fireinfo_kernels ON fireinfo_kernels.id = fireinfo_profiles_kernels.kernel_id \
882 WHERE fireinfo_profiles.id = %s", self
.id)
885 self
._kernel
= res
.name
893 def get_arch_id(self
):
894 if not hasattr(self
, "_arch_id"):
895 res
= self
.db
.get("SELECT fireinfo_profiles_arches.arch_id AS id FROM fireinfo_profiles \
896 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
897 WHERE fireinfo_profiles.id = %s", self
.id)
900 self
._arch
_id
= res
.id
906 def set_arch_id(self
, arch_id
):
907 if self
.arch_id
== arch_id
:
910 self
.db
.execute("DELETE FROM fireinfo_profiles_arches WHERE profile_id = %s", self
.id)
912 self
.db
.execute("INSERT INTO fireinfo_profiles_arches(profile_id, arch_id) \
913 VALUES(%s, %s)", self
.id, arch_id
)
916 if hasattr(self
, "_arch"):
919 arch_id
= property(get_arch_id
, set_arch_id
)
923 if not hasattr(self
, "_arch"):
924 res
= self
.db
.get("SELECT fireinfo_arches.name AS name FROM fireinfo_profiles \
925 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
926 LEFT JOIN fireinfo_arches ON fireinfo_arches.id = fireinfo_profiles_arches.arch_id \
927 WHERE fireinfo_profiles.id = %s", self
.id)
930 self
._arch
= res
.name
938 def get_release_id(self
):
939 if not hasattr(self
, "_release_id"):
940 res
= self
.db
.get("SELECT fireinfo_profiles_releases.release_id AS id FROM fireinfo_profiles \
941 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
942 WHERE fireinfo_profiles.id = %s", self
.id)
945 self
._release
_id
= res
.id
947 self
._release
_id
= None
949 return self
._release
_id
951 def set_release_id(self
, release_id
):
952 if self
.release_id
== release_id
:
955 self
.db
.execute("DELETE FROM fireinfo_profiles_releases WHERE profile_id = %s", self
.id)
957 self
.db
.execute("INSERT INTO fireinfo_profiles_releases(profile_id, release_id) \
958 VALUES(%s, %s)", self
.id, release_id
)
960 self
._release
_id
= release_id
961 if hasattr(self
, "_release"):
964 release_id
= property(get_release_id
, set_release_id
)
968 if not hasattr(self
, "_release"):
969 res
= self
.db
.get("SELECT fireinfo_releases.name AS name FROM fireinfo_profiles \
970 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
971 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
972 WHERE fireinfo_profiles.id = %s", self
.id)
975 self
._release
= self
._format
_release
(res
.name
)
982 def _format_release(r
):
986 # Remove the development header
987 r
= r
.replace("Development Build: ", "")
990 ("-beta", " - Beta "),
991 ("-rc", " - Release Candidate "),
992 ("rc", "Release Candidate "),
993 ("core", "Core Update "),
1003 def release_short(self
):
1005 (r
"Release Candidate (\d+)", r
"RC\1"),
1009 for pattern
, repl
in pairs
:
1010 if re
.search(pattern
, s
) is None:
1013 s
= re
.sub(pattern
, repl
, s
)
1021 if not hasattr(self
, "_virtual"):
1022 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles_virtual \
1023 WHERE profile_id = %s", self
.id)
1026 self
._virtual
= True
1028 self
._virtual
= False
1030 return self
._virtual
1032 def get_hypervisor_id(self
):
1033 if not hasattr(self
, "_hypervisor_id"):
1034 res
= self
.db
.get("SELECT fireinfo_profiles_virtual.hypervisor_id AS id FROM fireinfo_profiles \
1035 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
1036 WHERE fireinfo_profiles.id = %s", self
.id)
1039 self
._hypervisor
_id
= res
.id
1041 self
._hypervisor
_id
= None
1043 return self
._hypervisor
_id
1045 def set_hypervisor_id(self
, hypervisor_id
):
1046 self
.db
.execute("DELETE FROM fireinfo_profiles_virtual WHERE profile_id = %s", self
.id)
1047 self
.db
.execute("INSERT INTO fireinfo_profiles_virtual(profile_id, hypervisor_id) \
1048 VALUES(%s, %s)", self
.id, hypervisor_id
)
1050 self
._hypervisor
_id
= hypervisor_id
1052 hypervisor_id
= property(get_hypervisor_id
, set_hypervisor_id
)
1055 def hypervisor(self
):
1056 if not hasattr(self
, "_hypervisor"):
1057 res
= self
.db
.get("SELECT fireinfo_hypervisors.name AS hypervisor FROM fireinfo_profiles \
1058 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
1059 LEFT JOIN fireinfo_hypervisors ON fireinfo_profiles_virtual.hypervisor_id = fireinfo_hypervisors.id \
1060 WHERE fireinfo_profiles.id = %s", self
.id)
1063 self
._hypervisor
= res
.hypervisor
1065 self
._hypervisor
= None
1067 return self
._hypervisor
1071 def get_language(self
):
1072 if not hasattr(self
, "_language"):
1073 res
= self
.db
.get("SELECT language FROM fireinfo_profiles_languages \
1074 WHERE profile_id = %s", self
.id)
1077 self
._language
= res
.language
1079 self
._language
= None
1081 return self
._language
1083 def set_language(self
, language
):
1084 self
.db
.execute("DELETE FROM fireinfo_profiles_languages WHERE profile_id = %s", self
.id)
1087 self
.db
.execute("INSERT INTO fireinfo_profiles_languages(profile_id, language) \
1088 VALUES(%s, %s)", self
.id, language
)
1090 self
._language
= language
1092 language
= property(get_language
, set_language
)
1096 def get_network(self
):
1097 if not hasattr(self
, "_network"):
1098 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_networks \
1099 WHERE profile_id = %s", self
.id)
1104 self
._network
= ProfileNetwork(res
)
1106 return self
._network
1108 def set_network(self
, network
):
1109 self
.db
.execute("DELETE FROM fireinfo_profiles_networks WHERE profile_id = %s", self
.id)
1112 self
.db
.execute("INSERT INTO fireinfo_profiles_networks(profile_id, \
1113 has_red, has_green, has_orange, has_blue) VALUES(%s, %s, %s, %s, %s)",
1114 self
.id, network
.has_red
, network
.has_green
, network
.has_orange
, network
.has_blue
)
1116 self
._network
= network
1118 network
= property(get_network
, set_network
)
1121 class ProfileData(Object
):
1122 def __init__(self
, backend
, id, data
=None, profile
=None):
1123 Object
.__init
__(self
, backend
)
1127 self
._profile
= profile
1131 if self
._data
is None:
1132 self
._data
= self
.db
.get("SELECT * FROM fireinfo_profile_data \
1133 WHERE id = %s", self
.id)
1139 if not self
._profile
:
1140 self
._profile
= self
.fireinfo
.get_profile_by_id(self
.profile_id
)
1142 return self
._profile
1145 def profile_id(self
):
1146 return self
.data
.profile_id
1149 class ProfileParserError(Exception):
1153 class ProfileParser(Object
):
1164 __processor_args
= (
1174 __processor_args_mandatory
= (
1179 def __init__(self
, backend
, public_id
, blob
=None):
1180 Object
.__init
__(self
, backend
)
1182 self
.public_id
= public_id
1183 self
.private_id
= None
1185 self
.processor
= None
1186 self
.processor_clock_speed
= None
1187 self
.processor_bogomips
= None
1188 self
.system_id
= None
1192 self
.kernel_id
= None
1196 self
.release_id
= None
1197 self
.language
= None
1199 self
.hypervisor_id
= None
1202 self
.__parse
_blob
(blob
)
1204 def equals(self
, other
):
1205 if not self
.processor_id
== other
.processor_id
:
1208 if not self
.device_ids
== other
.device_ids
:
1211 if not self
.system_id
== other
.system_id
:
1214 if not self
.memory
== other
.memory
:
1217 if not self
.storage
== other
.storage
:
1220 if not self
.kernel_id
== other
.kernel_id
:
1223 if not self
.arch_id
== other
.arch_id
:
1226 if not self
.release_id
== other
.release_id
:
1229 if not self
.language
== other
.language
:
1232 if not self
.virtual
== other
.virtual
:
1236 if not self
.hypervisor_id
== other
.hypervisor_id
:
1239 if not self
.network
== other
.network
:
1244 def __parse_blob(self
, blob
):
1245 _profile
= blob
.get("profile", {})
1246 self
.private_id
= blob
.get("private_id")
1248 # Do not try to parse an empty profile
1253 _processor
= _profile
.get("cpu", {})
1254 self
.__parse
_processor
(_processor
)
1257 _devices
= _profile
.get("devices", [])
1258 self
.__parse
_devices
(_devices
)
1261 _system
= _profile
.get("system")
1263 self
.__parse
_system
(_system
)
1265 # Memory (convert to bytes)
1266 memory
= _system
.get("memory", None)
1268 self
.memory
= memory
* 1024
1270 # Storage size (convert to bytes)
1271 storage
= _system
.get("root_size", None)
1273 self
.storage
= storage
* 1024
1276 kernel
= _system
.get("kernel_release", None)
1278 self
.__parse
_kernel
(kernel
)
1281 release
= _system
.get("release", None)
1283 self
.__parse
_release
(release
)
1286 language
= _system
.get("language", None)
1288 self
.__parse
_language
(language
)
1291 self
.virtual
= _system
.get("virtual", False)
1293 hypervisor
= _profile
.get("hypervisor")
1294 self
.__parse
_hypervisor
(hypervisor
)
1297 _network
= _profile
.get("network")
1299 self
.__parse
_network
(_network
)
1302 def device_ids(self
):
1303 return sorted([d
.id for d
in self
.devices
])
1305 def __parse_devices(self
, _devices
):
1308 for _device
in _devices
:
1310 for arg
in self
.__device
_args
:
1311 args
[arg
] = _device
.get(arg
, None)
1313 # Skip if the subsystem is not set
1314 if not args
.get("subsystem", None):
1317 # Find the device or create a new one.
1318 device
= self
.fireinfo
.get_device(**args
)
1320 device
= self
.fireinfo
.create_device(**args
)
1322 self
.devices
.append(device
)
1324 def __parse_system(self
, system
):
1325 vendor
= system
.get("vendor", None)
1329 model
= system
.get("model", None)
1333 self
.system_id
= self
.fireinfo
.get_system(vendor
, model
)
1334 if not self
.system_id
:
1335 self
.system_id
= self
.fireinfo
.create_system(vendor
, model
)
1338 def processor_id(self
):
1339 if not self
.processor
:
1342 return self
.processor
.id
1344 def __parse_processor(self
, _processor
):
1346 for arg
in self
.__processor
_args
:
1347 if arg
== "core_count":
1352 args
[arg
] = _processor
.get(_arg
, None)
1354 for arg
in self
.__processor
_args
_mandatory
:
1355 if not args
.get(arg
, None):
1356 raise ProfileParserError
, "Mandatory argument missing: %s" % arg
1358 self
.processor
= self
.fireinfo
.get_processor(**args
)
1359 if not self
.processor
:
1360 self
.processor
= self
.fireinfo
.create_processor(**args
)
1362 self
.processor_clock_speed
= _processor
.get("speed", None)
1363 self
.processor_bogomips
= _processor
.get("bogomips", None)
1365 arch
= _processor
.get("arch", None)
1367 self
.__parse
_arch
(arch
)
1369 def __parse_kernel(self
, kernel
):
1370 self
.kernel_id
= self
.fireinfo
.get_kernel(kernel
)
1371 if not self
.kernel_id
:
1372 self
.kernel_id
= self
.fireinfo
.create_kernel(kernel
)
1373 assert self
.kernel_id
1375 self
.kernel
= kernel
1377 def __parse_arch(self
, arch
):
1378 self
.arch_id
= self
.fireinfo
.get_arch(arch
)
1379 if not self
.arch_id
:
1380 self
.arch_id
= self
.fireinfo
.create_arch(arch
)
1384 def __parse_release(self
, release
):
1385 # Remove the arch bit
1387 r
= [e
for e
in release
.split() if e
]
1388 for s
in ("(i586)", "(armv5tel)"):
1395 release
= " ".join(r
)
1397 self
.release_id
= self
.fireinfo
.get_release(release
)
1398 if not self
.release_id
:
1399 self
.release_id
= self
.fireinfo
.create_release(release
)
1400 assert self
.release_id
1402 self
.release
= release
1404 def __parse_language(self
, language
):
1405 self
.language
= language
1406 self
.language
, delim
, rest
= self
.language
.partition(".")
1407 self
.language
, delim
, rest
= self
.language
.partition("_")
1409 def __parse_hypervisor(self
, hypervisor
):
1410 vendor
= hypervisor
.get("vendor", "other")
1412 if vendor
in ("other", "unknown"):
1413 self
.hypervisor_id
= None
1416 self
.hypervisor_id
= self
.fireinfo
.get_hypervisor(vendor
)
1417 if not self
.hypervisor_id
:
1418 self
.hypervisor_id
= self
.fireinfo
.create_hypervisor(vendor
)
1420 def __parse_network(self
, network
):
1421 self
.network
= ProfileNetwork({
1422 "has_red" : network
.get("red", False),
1423 "has_green" : network
.get("green", False),
1424 "has_orange" : network
.get("orange", False),
1425 "has_blue" : network
.get("blue", False),
1429 class Fireinfo(Object
):
1430 def get_profile_count(self
, when
=None):
1431 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1432 WHERE then_or_now(%s) BETWEEN time_created AND time_valid", when
)
1437 def get_total_updates_count(self
, when
=None):
1438 res
= self
.db
.get("SELECT COUNT(*) + SUM(updates) AS count \
1439 FROM fireinfo_profiles WHERE time_created <= then_or_now(%s)", when
)
1446 def parse_profile(self
, public_id
, blob
):
1447 return ProfileParser(self
.backend
, public_id
, blob
)
1451 def profile_exists(self
, public_id
):
1452 res
= self
.db
.get("SELECT id FROM fireinfo_profiles \
1453 WHERE public_id = %s LIMIT 1", public_id
)
1460 def can_update_profile(self
, public_id
, private_id
, when
=None):
1461 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles \
1462 WHERE public_id = %s AND private_id = %s \
1463 AND time_updated + INTERVAL '60 minutes' <= then_or_now(%s) \
1464 AND time_valid >= then_or_now(%s) ORDER BY time_updated DESC LIMIT 1",
1465 public_id
, private_id
, when
, when
)
1472 def get_profile(self
, public_id
, private_id
=None, when
=None):
1473 res
= self
.db
.get("SELECT * FROM fireinfo_profiles \
1474 WHERE public_id = %s AND \
1475 (CASE WHEN %s IS NULL THEN TRUE ELSE private_id = %s END) AND \
1476 (CASE WHEN %s IS NULL THEN TRUE ELSE \
1477 then_or_now(%s) BETWEEN time_created AND time_valid END) \
1478 ORDER BY time_updated DESC LIMIT 1", public_id
,
1479 private_id
, private_id
, when
, when
)
1482 return Profile(self
.backend
, res
.id, res
)
1484 def get_profiles(self
, public_id
):
1485 res
= self
.db
.query("SELECT * FROM fireinfo_profiles \
1486 WHERE public_id = %s ORDER BY time_created DESC", public_id
)
1490 profile
= Profile(self
.backend
, row
.id, row
)
1491 profiles
.append(profile
)
1495 def create_profile(self
, public_id
, private_id
, when
=None):
1496 valid
= self
.settings
.get_int("fireinfo_profile_days_valid", 14)
1498 res
= self
.db
.get("INSERT INTO fireinfo_profiles(public_id, private_id, \
1499 time_created, time_updated, time_valid) VALUES(%s, %s, then_or_now(%s), \
1500 then_or_now(%s), then_or_now(%s) + INTERVAL '%s days') RETURNING id",
1501 public_id
, private_id
, when
, when
, when
, valid
)
1504 return Profile(self
.backend
, res
.id)
1508 def create_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1509 driver
=None, deviceclass
=None):
1510 res
= self
.db
.get("INSERT INTO fireinfo_devices(subsystem, vendor, model, \
1511 sub_vendor, sub_model, driver, deviceclass) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1512 RETURNING id", subsystem
, vendor
, model
, sub_vendor
, sub_model
, driver
, deviceclass
)
1515 return Device(self
.backend
, res
.id)
1517 def get_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1518 driver
=None, deviceclass
=None):
1519 res
= self
.db
.get("SELECT * FROM fireinfo_devices \
1520 WHERE subsystem = %s AND vendor = %s AND model = %s \
1521 AND sub_vendor IS NOT DISTINCT FROM %s \
1522 AND sub_model IS NOT DISTINCT FROM %s \
1523 AND driver IS NOT DISTINCT FROM %s \
1524 AND deviceclass IS NOT DISTINCT FROM %s \
1525 LIMIT 1", subsystem
, vendor
, model
, sub_vendor
,
1526 sub_model
, driver
, deviceclass
)
1529 return Device(self
.backend
, res
.id, res
)
1533 def create_system(self
, vendor
, model
):
1534 res
= self
.db
.get("INSERT INTO fireinfo_systems(vendor, model) \
1535 VALUES(%s, %s) RETURNING id", vendor
, model
)
1540 def get_system(self
, vendor
, model
):
1541 res
= self
.db
.get("SELECT id FROM fireinfo_systems WHERE vendor IS NOT DISTINCT FROM %s \
1542 AND model IS NOT DISTINCT FROM %s LIMIT 1", vendor
, model
)
1549 def create_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1550 res
= self
.db
.get("INSERT INTO fireinfo_processors(vendor, model_string, \
1551 family, model, stepping, core_count, flags) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1552 RETURNING id", vendor
, model_string
, family
, model
, stepping
, core_count
, flags
)
1555 return Processor(self
.backend
, res
.id)
1557 def get_processor_by_id(self
, processor_id
, **kwargs
):
1558 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1559 WHERE id = %s", processor_id
)
1562 return Processor(self
.backend
, res
.id, data
=res
, **kwargs
)
1564 def get_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1568 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1569 WHERE vendor = %s AND model_string = %s \
1570 AND family IS NOT DISTINCT FROM %s AND model IS NOT DISTINCT FROM %s \
1571 AND stepping IS NOT DISTINCT FROM %s AND core_count = %s \
1572 AND flags <@ %s AND flags @> %s", vendor
, model_string
, family
, model
,
1573 stepping
, core_count
, flags
, flags
)
1576 return Processor(self
.backend
, res
.id, res
)
1580 def create_kernel(self
, kernel
):
1581 res
= self
.db
.get("INSERT INTO fireinfo_kernels(name) VALUES(%s) \
1582 RETURNING id", kernel
)
1587 def get_kernel(self
, kernel
):
1588 res
= self
.db
.get("SELECT id FROM fireinfo_kernels WHERE name = %s", kernel
)
1595 def create_arch(self
, arch
):
1596 res
= self
.db
.get("INSERT INTO fireinfo_arches(name) VALUES(%s) \
1597 RETURNING id", arch
)
1602 def get_arch(self
, arch
):
1603 res
= self
.db
.get("SELECT id FROM fireinfo_arches WHERE name = %s", arch
)
1610 def create_release(self
, release
):
1611 res
= self
.db
.get("INSERT INTO fireinfo_releases(name) VALUES(%s) \
1612 RETURNING id", release
)
1617 def get_release(self
, release
):
1618 res
= self
.db
.get("SELECT id FROM fireinfo_releases WHERE name = %s", release
)
1625 def create_hypervisor(self
, hypervisor
):
1626 res
= self
.db
.get("INSERT INTO fireinfo_hypervisors(name) VALUES(%s) \
1627 RETURNING id", hypervisor
)
1632 def get_hypervisor(self
, hypervisor
):
1633 res
= self
.db
.get("SELECT id FROM fireinfo_hypervisors WHERE name = %s",
1641 def handle_profile(self
, *args
, **kwargs
):
1642 self
.db
.execute("START TRANSACTION")
1644 # Wrap all the handling of the profile in a huge transaction.
1646 self
._handle
_profile
(*args
, **kwargs
)
1649 self
.db
.execute("ROLLBACK")
1653 self
.db
.execute("COMMIT")
1655 def _handle_profile(self
, public_id
, profile_blob
, location
=None, when
=None):
1656 private_id
= profile_blob
.get("private_id", None)
1659 # Check if the profile already exists in the database.
1660 profile
= self
.fireinfo
.get_profile(public_id
, private_id
=private_id
, when
=when
)
1662 # Check if the update can actually be updated
1663 if profile
and not self
.fireinfo
.can_update_profile(public_id
, private_id
, when
=when
):
1664 logging
.warning("Profile not updated because private ID does not"
1665 " match or last update is too recent: %s" % public_id
)
1669 profile_parser
= self
.parse_profile(public_id
, profile_blob
)
1671 # If a profile exists, check if it matches and if so, just update the
1674 # Check if the profile has changed. If so, update the data.
1675 if profile_parser
.equals(profile
):
1676 profile
.updated(profile_parser
, location
=location
, when
=when
)
1679 # If it does not match, we assume that it is expired and
1680 # create a new profile.
1681 profile
.expired(when
=when
)
1683 # Replace the old profile with a new one
1684 profile
= self
.fireinfo
.create_profile(public_id
, private_id
, when
=when
)
1685 profile
.parse(profile_parser
)
1688 profile
.set_location(location
)
1694 def get_random_profile(self
, when
=None):
1695 # Check if the architecture exists so that we pick a profile with some data
1696 res
= self
.db
.get("SELECT public_id FROM fireinfo_profiles \
1697 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
1698 WHERE fireinfo_profiles_arches.profile_id IS NOT NULL \
1699 AND then_or_now(%s) BETWEEN time_created AND time_valid ORDER BY RANDOM() LIMIT 1", when
)
1702 return res
.public_id
1704 def get_active_profiles(self
, when
=None):
1705 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1706 SELECT COUNT(*) AS with_data, (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1707 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1708 WHERE fireinfo_profiles_releases.profile_id IS NOT NULL", when
)
1711 return res
.with_data
, res
.count
1713 def get_archive_size(self
, when
=None):
1714 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1715 WHERE time_created <= then_or_now(%s)", when
)
1720 def get_geo_location_map(self
, when
=None):
1721 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1722 SELECT location, COUNT(location)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1723 LEFT JOIN fireinfo_profiles_locations ON profiles.id = fireinfo_profiles_locations.profile_id \
1724 WHERE fireinfo_profiles_locations.location IS NOT NULL GROUP BY location ORDER BY count DESC", when
)
1726 return ((r
.location
, r
.count
) for r
in res
)
1728 def get_language_map(self
, when
=None):
1729 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1730 SELECT language, COUNT(language)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1731 LEFT JOIN fireinfo_profiles_languages ON profiles.id = fireinfo_profiles_languages.profile_id \
1732 WHERE fireinfo_profiles_languages.language IS NOT NULL GROUP BY language ORDER BY count DESC", when
)
1734 return ((r
.language
, r
.count
) for r
in res
)
1737 def cpu_vendors(self
):
1738 res
= self
.db
.query("SELECT DISTINCT vendor FROM fireinfo_processors ORDER BY vendor")
1740 return (CPU_VENDORS
.get(r
.vendor
, r
.vendor
) for r
in res
)
1742 def get_cpu_vendors_map(self
, when
=None):
1743 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1744 SELECT vendor, COUNT(vendor)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1745 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1746 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1747 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL GROUP BY vendor ORDER BY count DESC", when
)
1749 return ((CPU_VENDORS
.get(r
.vendor
, r
.vendor
), r
.count
) for r
in res
)
1751 def get_cpu_clock_speeds(self
, when
=None):
1752 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1753 SELECT AVG(fireinfo_profiles_processors.clock_speed) AS avg, \
1754 STDDEV(fireinfo_profiles_processors.clock_speed) AS stddev, \
1755 MIN(fireinfo_profiles_processors.clock_speed) AS min, \
1756 MAX(fireinfo_profiles_processors.clock_speed) AS max FROM profiles \
1757 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1758 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1759 AND fireinfo_profiles_processors.clock_speed > 0", when
)
1762 return (res
.avg
or 0, res
.stddev
or 0, res
.min or 0, res
.max or 0)
1764 def get_cpus_with_platform_and_flag(self
, platform
, flag
, when
=None):
1765 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1766 processors AS (SELECT fireinfo_processors.id AS id, fireinfo_processors.flags AS flags FROM profiles \
1767 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1768 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1769 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1770 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1771 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1772 AND fireinfo_arches.platform = %s AND NOT 'hypervisor' = ANY(fireinfo_processors.flags)) \
1773 SELECT (COUNT(*)::float / (SELECT NULLIF(COUNT(*), 0) FROM processors)) AS count FROM processors \
1774 WHERE %s = ANY(processors.flags)", when
, platform
, flag
)
1776 return res
.count
or 0
1778 def get_common_cpu_flags_by_platform(self
, platform
, when
=None):
1779 if platform
== "arm":
1781 "lpae", "neon", "thumb", "thumb2", "thumbee", "vfpv3", "vfpv4",
1783 elif platform
== "x86":
1785 "aes", "avx", "avx2", "lm", "mmx", "mmxext", "nx", "pae",
1786 "pni", "popcnt", "sse", "sse2", "rdrand", "ssse3", "sse4a",
1794 ret
.append((flag
, self
.get_cpus_with_platform_and_flag(platform
, flag
, when
=when
)))
1796 # Add virtual CPU flag "virt" for virtualization support
1797 if platform
== "x86":
1799 self
.get_cpus_with_platform_and_flag(platform
, "vmx", when
=when
) + \
1800 self
.get_cpus_with_platform_and_flag(platform
, "svm", when
=when
)))
1802 return sorted(ret
, key
=lambda x
: x
[1], reverse
=True)
1804 def get_common_memory_amounts(self
, when
=None):
1805 amounts
= (128, 256, 512, 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024, 32 * 1024, 64 * 1024)
1808 for amount
in amounts
:
1809 ret
.append((amount
, self
.get_memory_amount(amount
* 1024 * 0.95, when
=when
)))
1813 def get_memory_amount(self
, greather_than
, when
=None):
1814 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1815 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles \
1816 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \
1817 WHERE NOT fireinfo_profiles_memory.amount IS NULL) AS percentage FROM profiles \
1818 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \
1819 WHERE fireinfo_profiles_memory.amount >= %s", when
, greather_than
)
1822 return res
.percentage
1824 def get_memory_amounts(self
, when
=None):
1825 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1826 SELECT AVG(fireinfo_profiles_memory.amount) AS avg, \
1827 STDDEV(fireinfo_profiles_memory.amount) AS stddev, \
1828 MIN(fireinfo_profiles_memory.amount) AS min, \
1829 MAX(fireinfo_profiles_memory.amount) AS max FROM profiles \
1830 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id", when
)
1833 return (res
.avg
or 0, res
.stddev
or 0, res
.min or 0, res
.max or 0)
1835 def get_arch_map(self
, when
=None):
1836 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1837 SELECT fireinfo_arches.name AS arch, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count \
1839 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1840 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1841 WHERE NOT fireinfo_profiles_arches.profile_id IS NULL \
1842 GROUP BY fireinfo_arches.id ORDER BY count DESC", when
)
1844 return ((r
.arch
, r
.count
) for r
in res
)
1848 def get_hypervisor_map(self
, when
=None):
1849 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1850 virtual_profiles AS (SELECT profiles.id AS profile_id, fireinfo_profiles_virtual.hypervisor_id FROM profiles \
1851 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1852 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL) \
1853 SELECT COALESCE(fireinfo_hypervisors.name, %s) AS name, \
1854 COUNT(*)::float / (SELECT COUNT(*) FROM virtual_profiles) AS count FROM virtual_profiles \
1855 LEFT JOIN fireinfo_hypervisors ON virtual_profiles.hypervisor_id = fireinfo_hypervisors.id \
1856 GROUP BY fireinfo_hypervisors.name ORDER BY count DESC", when
, "unknown")
1858 return ((r
.name
, r
.count
) for r
in res
)
1860 def get_virtual_ratio(self
, when
=None):
1861 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1862 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1863 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1864 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL", when
)
1871 def get_releases_map(self
, when
=None):
1872 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1873 SELECT fireinfo_releases.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1874 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1875 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
1876 GROUP BY fireinfo_releases.name ORDER BY count DESC", when
)
1878 return ((r
.name
, r
.count
) for r
in res
)
1880 def get_kernels_map(self
, when
=None):
1881 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1882 SELECT fireinfo_kernels.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1883 LEFT JOIN fireinfo_profiles_kernels ON profiles.id = fireinfo_profiles_kernels.profile_id \
1884 LEFT JOIN fireinfo_kernels ON fireinfo_profiles_kernels.kernel_id = fireinfo_kernels.id \
1885 GROUP BY fireinfo_kernels.name ORDER BY count DESC", when
)
1887 return ((r
.name
, r
.count
) for r
in res
)
1889 def _process_devices(self
, devices
):
1893 dev
= Device(self
.backend
, dev
.get("id", None), dev
)
1898 def get_driver_map(self
, driver
, when
=None):
1899 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1900 devices AS (SELECT * FROM profiles \
1901 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1902 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1903 WHERE driver = %s) \
1904 SELECT subsystem, model, vendor, driver, deviceclass, \
1905 COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \
1906 GROUP BY subsystem, model, vendor, driver, deviceclass \
1907 ORDER BY percentage DESC", when
, driver
)
1909 return self
._process
_devices
(res
)
1912 "pci" : hwdata
.PCI(),
1913 "usb" : hwdata
.USB(),
1916 def get_vendor_string(self
, subsystem
, vendor_id
):
1918 cls
= self
.subsystem2class
[subsystem
]
1922 return cls
.get_vendor(vendor_id
)
1924 def get_model_string(self
, subsystem
, vendor_id
, model_id
):
1926 cls
= self
.subsystem2class
[subsystem
]
1930 return cls
.get_device(vendor_id
, model_id
)
1932 def get_vendor_list(self
, when
=None):
1933 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1934 SELECT DISTINCT fireinfo_devices.subsystem AS subsystem, fireinfo_devices.vendor AS vendor FROM profiles \
1935 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1936 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1937 WHERE NOT fireinfo_devices.driver = ANY(%s)", when
, IGNORED_DEVICES
)
1941 vendor
= self
.get_vendor_string(row
.subsystem
, row
.vendor
)
1943 # Drop if vendor could not be determined
1948 vendors
[vendor
].append((row
.subsystem
, row
.vendor
))
1950 vendors
[vendor
] = [(row
.subsystem
, row
.vendor
)]
1952 vendors
= vendors
.items()
1953 return sorted(vendors
)
1955 def get_devices_by_vendor(self
, subsystem
, vendor
, when
=None):
1956 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1957 devices AS (SELECT * FROM profiles \
1958 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1959 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1960 WHERE NOT fireinfo_devices.driver = ANY(%s)), \
1961 vendor_devices AS (SELECT * FROM devices WHERE devices.subsystem = %s AND devices.vendor = %s) \
1962 SELECT subsystem, model, vendor, driver, deviceclass FROM vendor_devices \
1963 GROUP BY subsystem, model, vendor, driver, deviceclass", when
, IGNORED_DEVICES
, subsystem
, vendor
)
1965 return self
._process
_devices
(res
)
1967 def get_device_percentage(self
, subsystem
, vendor
, model
, when
=None):
1968 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1969 devices AS (SELECT * FROM profiles \
1970 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1971 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id) \
1972 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \
1973 WHERE devices.subsystem = %s AND devices.vendor = %s AND devices.model = %s",
1974 when
, subsystem
, vendor
, model
)
1977 return res
.percentage
1979 def get_device_in_profile(self
, subsystem
, vendor
, model
, limit
=10, when
=None):
1980 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1981 profiles_with_device AS (SELECT DISTINCT fireinfo_profiles.public_id FROM profiles \
1982 LEFT JOIN fireinfo_profiles ON profiles.id = fireinfo_profiles.id \
1983 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1984 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1985 WHERE fireinfo_devices.subsystem = %s AND fireinfo_devices.vendor = %s \
1986 AND fireinfo_devices.model = %s) \
1987 SELECT * FROM profiles_with_device ORDER BY RANDOM() LIMIT %s",
1988 when
, subsystem
, vendor
, model
, limit
)
1990 return (r
.public_id
for r
in res
)
1992 def get_network_zones_map(self
, when
=None):
1993 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1994 SELECT COUNT(NULLIF(has_red, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_red, \
1995 COUNT(NULLIF(has_green, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_green, \
1996 COUNT(NULLIF(has_orange, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_orange, \
1997 COUNT(NULLIF(has_blue, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_blue FROM profiles \
1998 LEFT JOIN fireinfo_profiles_networks ON profiles.id = fireinfo_profiles_networks.profile_id \
1999 WHERE fireinfo_profiles_networks.profile_id IS NOT NULL", when
)