]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blame - www/webapp/stasy.py
Import of new website.
[people/shoehn/ipfire.org.git] / www / webapp / stasy.py
CommitLineData
940227cb
MT
1#!/usr/bin/python
2
3import logging
4import pymongo
5
6DATABASE_HOST = ["irma.ipfire.org", "madeye.ipfire.org"]
7DATABASE_NAME = "stasy"
8
9class ProfileDict(object):
10 def __init__(self, data):
11 self._data = data
12
13 logging.debug("New: %s" % self._data)
14
15 def __repr__(self):
16 return self.__str__()
17
18 def __str__(self):
19 return "<%s %s>" % (self.__class__.__name__, self.public_id)
20
21 def __getattr__(self, key):
22 try:
23 return self._data[key]
24 except KeyError:
25 raise AttributeError, key
26
27 def __setattr(self, key, val):
28 self._data[key] = val
29
30
31class ProfileCPU(ProfileDict):
32 @property
33 def capable_64bit(self):
34 return "lm" in self.flags
35
36 @property
37 def capable_pae(self):
38 return "pae" in self.flags
39
40
41class ProfileHypervisor(ProfileDict):
42 pass
43
44
45class ProfileDevice(ProfileDict):
46 @property
47 def model_string(self):
48 return "XXX"
49
50 @property
51 def vendor_string(self):
52 return "XXX"
53
54
55class Profile(ProfileDict):
56 def __repr__(self):
57 return "<%s %s>" % (self.__class__.__name__, self.public_id)
58
59 @property
60 def cpu(self):
61 return ProfileCPU(self._data["cpu"])
62
63 @property
64 def hypervisor(self):
65 return ProfileHypervisor(self._data["hypervisor"])
66
67 @property
68 def devices(self):
69 return [ProfileDevice(d) for d in self._data["devices"]]
70
71
72class StasyDatabase(object):
73 def __init__(self):
74 # Initialize database connection
75 self._conn = pymongo.Connection(DATABASE_HOST)
76 self._db = self._conn[DATABASE_NAME]
77
78 def get_profile_count(self):
79 # XXX need to implement something to get profiles updated since
80 # a given date
81
82 # All distinct profiles (based on public_id)
83 c = self._db.profiles.find().distinct("public_id")
84
85 return c.count()
86
87 def _get_profile_cursor(self, public_id):
88 c = self._db.profiles.find({ "public_id" : public_id })
89 c.sort("updated", pymongo.ASCENDING)
90
91 return c
92
93 def get_latest_profile(self, public_id):
94 # XXX still finds first one
95 for p in self._get_profile_cursor(public_id).limit(1):
96 return Profile(p)
97
98 def get_profiles(self):
99 # XXX needs nicer database query
100 profiles = []
101 for p in self._db.profiles.find():
102 p = Profile(p)
103 if not p.public_id in profiles:
104 profiles.append(p.public_id)
105
106 return profiles
107
108 @property
109 def cpus(self):
110 return self._db.profiles.distinct("profile.cpu")
111
112 @property
113 def cpu_vendors(self):
114 return self._db.profiles.distinct("profile.cpu.vendor")
115
116 @property
117 def cpu_map(self):
118 cpus = {}
119
120 for vendor in self.cpu_vendors:
121 cpus[vendor] = \
122 self._db.profiles.find({ "profile.cpu.vendor" : vendor }).count()
123
124 return cpus
125
126 @property
127 def hypervisor_vendors(self):
128 return self._db.profiles.distinct("profile.hypervisor.vendor")
129
130 @property
131 def hypervisor_models(self):
132 return self._db.profiles.distinct("profile.hypervisor.model")
133
134 @property
135 def secret_ids(self):
136 return self._db.profiles.distinct("secret_id")
137
138 @property
139 def languages(self):
140 return self._db.profiles.distinct("profile.system.language")
141
142 @property
143 def vendors(self):
144 return self._db.profiles.distinct("profile.system.vendor")
145
146 @property
147 def models(self):
148 return self._db.profiles.distinct("profile.system.model")
149
150
151class Stasy(object):
152 def __init__(self):
153 self.db = StasyDatabase()
154
155 def get_profile(self, public_id):
156 return self.db.get_latest_profile(public_id)
157
158 def get_profiles(self):
159 return self.db.get_profiles()
160
161
162if __name__ == "__main__":
163 s = Stasy()
164
165 print s.get_profile("0" * 40)
166 print s.db.cpu_vendors
167 for id in s.db.secret_ids:
168 print "\t", id
169
170 for p in s.db._db.profiles.find():
171 print p
172
173 print s.db.cpu_map
174 print s.db.hypervisor_vendors
175 print s.db.hypervisor_models
176 print s.db.languages
177 print s.db.vendors
178 print s.db.models
179 print s.db.cpus