Commit 9998a1e27ee1c70ae8a8764129065ba0b03b592d
1 parent
810ff586
Exists in
master
filtering computers classes and props
Showing
1 changed file
with
81 additions
and
63 deletions
Show diff stats
wscserver/view/restfulview.py
@@ -2,6 +2,7 @@ from pyramid_restler.view import RESTfulView | @@ -2,6 +2,7 @@ from pyramid_restler.view import RESTfulView | ||
2 | from collections import defaultdict | 2 | from collections import defaultdict |
3 | import json | 3 | import json |
4 | import hashlib | 4 | import hashlib |
5 | +from itertools import groupby | ||
5 | 6 | ||
6 | class CustomRESTfulView(RESTfulView): | 7 | class CustomRESTfulView(RESTfulView): |
7 | 8 | ||
@@ -10,70 +11,87 @@ class CustomRESTfulView(RESTfulView): | @@ -10,70 +11,87 @@ class CustomRESTfulView(RESTfulView): | ||
10 | para as classes WMI | 11 | para as classes WMI |
11 | """ | 12 | """ |
12 | 13 | ||
13 | - def render_json(self, value): | ||
14 | - | ||
15 | - dados_banco = json.loads(self.context.to_json(value, self.fields, | ||
16 | - wrap=False)) | ||
17 | - | ||
18 | - saida = {} | ||
19 | - results = [] | ||
20 | - | ||
21 | - for computador in dados_banco: | ||
22 | - # Cria as chaves dos computadores | ||
23 | - saida[computador['id_computador']] = {} | ||
24 | - | ||
25 | - for computador in dados_banco: | ||
26 | - # Cria as chaves de classes nos computadores | ||
27 | - saida[computador['id_computador']][computador['nm_class_name']] = {} | ||
28 | - | ||
29 | - for x in dados_banco: | ||
30 | - # cria as chaves de propriedades nas classes | ||
31 | - class_name = x['nm_class_name'] | ||
32 | - saida[x['id_computador']][x['nm_class_name']][ | ||
33 | - class_name +'_'+ x['nm_property_name']] = x['te_class_property_value'] | ||
34 | - | ||
35 | - # Ordena os objetos json | ||
36 | - for computador in saida: | ||
37 | - classes = {} | ||
38 | - | ||
39 | - # Gera um hash para o id_computador | ||
40 | - salt = str('salthere').encode('utf-8') | ||
41 | - id_reg = str(computador).encode('utf-8') | ||
42 | - id_reg = hashlib.sha512(id_reg) | ||
43 | - id_reg.update(salt) | ||
44 | - id_reg = id_reg.hexdigest() | ||
45 | - classes['id_reg'] = id_reg | ||
46 | - | ||
47 | - for classe in saida[computador]: | ||
48 | - if classe != 'data_coleta': | ||
49 | - # Aqui estou nas classes | ||
50 | - classes[classe] = saida[computador][classe] | ||
51 | - | ||
52 | - results.append(classes) | ||
53 | - | ||
54 | - valores = { | ||
55 | - 'results': results, | ||
56 | - 'result_count': len(results) | 14 | + # Computer class and properties filters |
15 | + computer_filter = { | ||
16 | + "OperatingSystem": [ | ||
17 | + "Caption", | ||
18 | + "Version", | ||
19 | + "InstallDate", | ||
20 | + # quantidade ??? | ||
21 | + ], | ||
22 | + "Win32_Processor": [ | ||
23 | + "Manufacturer", | ||
24 | + "Caption", | ||
25 | + "NumberOfLogicalProcessors", | ||
26 | + # idade ?? | ||
27 | + ], | ||
28 | + "Win32_BIOS": [ | ||
29 | + "Manufacturer", | ||
30 | + ], | ||
31 | + } | ||
32 | + | ||
33 | + def render_json(self, collection): | ||
34 | + | ||
35 | + """ | ||
36 | + :param collection: List of <wscserver.model.Coleta.Coleta object> | ||
37 | + collection element attributes example: | ||
38 | + | ||
39 | + "id_class": 3, | ||
40 | + "id_computador_coleta_historico": 1, | ||
41 | + "nm_class_name": "NetworkAdapterConfiguration", | ||
42 | + "nm_property_name": "Description", | ||
43 | + "id_computador_coleta": 1, | ||
44 | + "id_class_property": 6, | ||
45 | + "dt_hr_inclusao": "2014-07-18 12:34:43", | ||
46 | + "id_computador": 3, | ||
47 | + "te_class_property_value": " "Intel(R) 82579V Gigabit Network Connection" | ||
48 | + """ | ||
49 | + | ||
50 | + def keyfunc(obj): | ||
51 | + # Will group by computer ID | ||
52 | + return int(obj.id_computador) | ||
53 | + | ||
54 | + # Sort list before grouping | ||
55 | + collection = sorted(collection, key=keyfunc) | ||
56 | + | ||
57 | + # Create result list | ||
58 | + computers = [ ] | ||
59 | + | ||
60 | + # Navigate groups | ||
61 | + for id_computador, computer_group in groupby(collection, keyfunc): | ||
62 | + | ||
63 | + computer = { } | ||
64 | + | ||
65 | + for collection_element in list(computer_group): | ||
66 | + | ||
67 | + class_ = collection_element.nm_class_name | ||
68 | + property_ = collection_element.nm_property_name | ||
69 | + property_value = collection_element.te_class_property_value | ||
70 | + | ||
71 | + if class_ not in self.computer_filter: | ||
72 | + # Filter classes | ||
73 | + continue | ||
74 | + | ||
75 | + if property_ not in self.computer_filter[class_]: | ||
76 | + # Filter properties | ||
77 | + continue | ||
78 | + | ||
79 | + if computer.get(class_) is None: | ||
80 | + computer[class_] = { } | ||
81 | + else: | ||
82 | + prefixed_property = class_ + '_' + property_ | ||
83 | + computer[class_][prefixed_property] = property_value | ||
84 | + | ||
85 | + computers.append(computer) | ||
86 | + | ||
87 | + response = { | ||
88 | + 'results': computers, | ||
89 | + 'result_count': len(computers) | ||
57 | } | 90 | } |
58 | 91 | ||
59 | - results = valores['results'] | ||
60 | - for computador in results: | ||
61 | - | ||
62 | - SoftwareList = computador['SoftwareList'] | ||
63 | - new_SoftwareList = [ ] | ||
64 | - | ||
65 | - for soft_key, soft_value in SoftwareList.items(): | ||
66 | - | ||
67 | - new_SoftwareList_element = { | ||
68 | - 'softwarelist_name': soft_value | ||
69 | - } | ||
70 | - new_SoftwareList.append(new_SoftwareList_element) | ||
71 | - | ||
72 | - computador['SoftwareList'] = new_SoftwareList | ||
73 | - | ||
74 | - | ||
75 | - response_data = dict( | ||
76 | - body=json.dumps(valores), | 92 | + return dict( |
93 | + body=json.dumps(response), | ||
77 | content_type='application/json' | 94 | content_type='application/json' |
78 | ) | 95 | ) |
79 | - return response_data | 96 | + |
97 | + |