Commit c452bde33acf674f10a808dac9d913b434aea802
1 parent
ce03424b
Exists in
master
build computers json and writing to file
Showing
2 changed files
with
141 additions
and
88 deletions
Show diff stats
wscserver/config/routing.py
1 | from wscserver.model.Coleta import ColetaContextFactory | 1 | from wscserver.model.Coleta import ColetaContextFactory |
2 | -from wscserver.view.restfulview import CustomRESTfulView | 2 | +#from wscserver.view.restfulview import CustomRESTfulView |
3 | from wscserver.model.So import SoContextFactory | 3 | from wscserver.model.So import SoContextFactory |
4 | 4 | ||
5 | +from wscserver.view.restfulview import viewcoleta | ||
6 | + | ||
5 | def make_routes(config): | 7 | def make_routes(config): |
6 | """ | 8 | """ |
7 | Cria rotas | 9 | Cria rotas |
8 | """ | 10 | """ |
11 | + | ||
12 | + | ||
13 | + config.add_route('the_big_colect', 'rest/coleta', request_method='GET') | ||
14 | + config.add_view(view=viewcoleta, route_name='the_big_colect', request_method='GET') | ||
15 | + | ||
9 | config.add_static_view('static', 'static', cache_max_age=3600) | 16 | config.add_static_view('static', 'static', cache_max_age=3600) |
10 | - config.add_restful_routes( | ||
11 | - 'rest/coleta', ColetaContextFactory, view=CustomRESTfulView) | 17 | + #config.add_restful_routes( |
18 | + # 'rest/coleta', ColetaContextFactory, view=CustomRESTfulView) | ||
12 | config.add_restful_routes('rest/coleta2', ColetaContextFactory) | 19 | config.add_restful_routes('rest/coleta2', ColetaContextFactory) |
13 | config.add_route('download','zip/coleta') | 20 | config.add_route('download','zip/coleta') |
14 | config.add_restful_routes('rest/so', SoContextFactory) | 21 | config.add_restful_routes('rest/so', SoContextFactory) |
wscserver/view/restfulview.py
1 | -from pyramid_restler.view import RESTfulView | ||
2 | -from collections import defaultdict | ||
3 | import json | 1 | import json |
4 | import hashlib | 2 | import hashlib |
5 | -from itertools import groupby | ||
6 | - | ||
7 | -class CustomRESTfulView(RESTfulView): | ||
8 | - | ||
9 | - """ | ||
10 | - Classe com parametros personalizados de view dos registros | ||
11 | - para as classes WMI | ||
12 | - """ | ||
13 | - | ||
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 | 3 | ||
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" | 4 | +from pyramid.response import Response |
5 | +from wscserver.model import session | ||
6 | + | ||
7 | + | ||
8 | +FILE_BEGIN = '{"results":[' | ||
9 | +FILE_END = '], "result_count": %s}' | ||
10 | + | ||
11 | +# Computer class and properties filters | ||
12 | +COMPUTER_FILTER = { | ||
13 | + "OperatingSystem": [ | ||
14 | + "Caption", | ||
15 | + "Version", | ||
16 | + "InstallDate", | ||
17 | + # quantidade ??? | ||
18 | + ], | ||
19 | + "Win32_Processor": [ | ||
20 | + "Manufacturer", | ||
21 | + "Caption", | ||
22 | + "NumberOfLogicalProcessors", | ||
23 | + # idade ?? | ||
24 | + ], | ||
25 | + "Win32_BIOS": [ | ||
26 | + "Manufacturer", | ||
27 | + ], | ||
28 | + "SoftwareList": [] | ||
29 | +} | ||
30 | + | ||
31 | +FILTER_KEYS = str(tuple(COMPUTER_FILTER.keys())) | ||
32 | + | ||
33 | + | ||
34 | +def viewcoleta(request): | ||
35 | + | ||
36 | + #create index idx_id_computador on computador_coleta(id_computador); | ||
37 | + | ||
38 | + limit = request.params.get('limit', 'NULL') | ||
39 | + | ||
40 | + stmt1 = """ | ||
41 | + SELECT | ||
42 | + id_computador | ||
43 | + FROM computador_coleta | ||
44 | + GROUP BY id_computador | ||
45 | + LIMIT {}; | ||
46 | + """.format(limit) | ||
47 | + | ||
48 | + computer_ids = session.execute(stmt1) | ||
49 | + | ||
50 | + | ||
51 | + stmt2 = """ | ||
52 | + SELECT classe.nm_class_name, | ||
53 | + cp.nm_property_name, | ||
54 | + cc.te_class_property_value | ||
55 | + FROM computador_coleta AS cc | ||
56 | + INNER JOIN class_property as cp ON (cc.id_class_property = | ||
57 | + cp.id_class_property) | ||
58 | + INNER JOIN classe ON (classe.id_class = cp.id_class) | ||
59 | + WHERE cc.id_computador = {} AND | ||
60 | + classe.nm_class_name IN {}; | ||
48 | """ | 61 | """ |
49 | 62 | ||
50 | - def keyfunc(obj): | ||
51 | - # Will group by computer ID | ||
52 | - return int(obj.id_computador) | 63 | + with open('/tmp/coleta.json', 'w') as f: |
53 | 64 | ||
54 | - # Sort list before grouping | ||
55 | - collection = sorted(collection, key=keyfunc) | 65 | + f.write(FILE_BEGIN) |
56 | 66 | ||
57 | - # Create result list | ||
58 | - computers = [ ] | 67 | + count = computer_ids.rowcount |
59 | 68 | ||
60 | - # Navigate groups | ||
61 | - for id_computador, computer_group in groupby(collection, keyfunc): | 69 | + for (id_computador, ) in computer_ids.fetchall(): |
62 | 70 | ||
63 | - computer = { } | 71 | + computer_attributes = session.execute( |
72 | + stmt2.format(id_computador, FILTER_KEYS)).fetchall() | ||
73 | + computer_json = build_computer_json(computer_attributes) | ||
64 | 74 | ||
65 | - for collection_element in list(computer_group): | 75 | + f.write(computer_json) |
76 | + count -= 1 | ||
66 | 77 | ||
67 | - class_ = collection_element.nm_class_name | ||
68 | - property_ = collection_element.nm_property_name | ||
69 | - property_value = collection_element.te_class_property_value | 78 | + if count is not 0: |
79 | + f.write(',') | ||
70 | 80 | ||
71 | - if class_ == 'SoftwareList': | ||
72 | - if computer.get(class_) is None: | ||
73 | - computer[class_] = [ ] | ||
74 | - elif property_value.lower().find('office') > -1: | ||
75 | - computer[class_].append(property_value) | ||
76 | - elif property_value.lower().find('microsoft') > -1: | ||
77 | - computer[class_].append(property_value) | ||
78 | - continue | 81 | + f.write(FILE_END % computer_ids.rowcount) |
79 | 82 | ||
80 | - elif class_ not in self.computer_filter \ | ||
81 | - or property_ not in self.computer_filter[class_]: | ||
82 | - # Filter classes and properties | ||
83 | - continue | 83 | + return Response('ok') |
84 | + #return Response( | ||
85 | + # content_type='application/json', | ||
86 | + # content_disposition='filename=coleta.json', | ||
87 | + # app_iter=[open('/var/antony/coleta.json', 'rb').read()] | ||
88 | + #) | ||
84 | 89 | ||
85 | - elif computer.get(class_) is None: | ||
86 | - computer[class_] = { } | ||
87 | - else: | ||
88 | - prefixed_property = class_ + '_' + property_ | ||
89 | - computer[class_][prefixed_property] = property_value | ||
90 | 90 | ||
91 | - computers.append(computer) | ||
92 | 91 | ||
93 | - response = { | ||
94 | - 'results': computers, | ||
95 | - 'result_count': len(computers) | ||
96 | - } | ||
97 | - | ||
98 | - return dict( | ||
99 | - body=json.dumps(response), | ||
100 | - content_type='application/json' | ||
101 | - ) | 92 | +def build_computer_json(computer_group): |
102 | 93 | ||
94 | + computer = { | ||
95 | + "OperatingSystem": {}, | ||
96 | + "Win32_Processor": {}, | ||
97 | + "Win32_BIOS": {}, | ||
98 | + "SoftwareList": [] | ||
99 | + } | ||
103 | 100 | ||
101 | + for class_, property_, property_value in computer_group: | ||
102 | + | ||
103 | + if class_ == 'SoftwareList': | ||
104 | + if property_value.lower().find('office') > -1: | ||
105 | + computer[class_].append(property_value) | ||
106 | + elif property_value.lower().find('microsoft') > -1: | ||
107 | + computer[class_].append(property_value) | ||
108 | + continue | ||
109 | + | ||
110 | + elif property_ not in COMPUTER_FILTER[class_]: | ||
111 | + continue | ||
112 | + | ||
113 | + else: | ||
114 | + prefixed_property = class_ + '_' + property_ | ||
115 | + computer[class_][prefixed_property] = property_value | ||
116 | + | ||
117 | + return json.dumps(computer) | ||
118 | + | ||
119 | +def zipcoleta(): | ||
120 | + """Recebe o json da coleta em um arquivo e zipa""" | ||
121 | + coleta = requests.get('http://localhost/wscserver/rest/coleta') | ||
122 | + | ||
123 | + tmpdir = tempfile.gettempdir() | ||
124 | + os.chdir(tmpdir) | ||
125 | + f = open('coleta.json','r') | ||
126 | + arquivozip = 'coleta.zip' | ||
127 | + with zipfile.ZipFile(arquivozip, 'w') as myzip: | ||
128 | + myzip.write(f.name) | ||
129 | + f.close() | ||
130 | + os.remove('coleta.json') | ||
131 | + filepath = os.path.abspath(arquivozip) | ||
132 | + return filepath | ||
133 | +''' | ||
134 | +def zipcoleta(): | ||
135 | + """Recebe o json da coleta em um arquivo e zipa""" | ||
136 | + coleta = requests.get('http://localhost/wscserver/rest/coleta') | ||
137 | + | ||
138 | + path = tempfile.gettempdir() | ||
139 | + os.chdir(path) | ||
140 | + f = open('coleta.json','w') | ||
141 | + f.write(coleta.text) | ||
142 | + arquivozip = 'coleta.zip' | ||
143 | + with zipfile.ZipFile(arquivozip, 'w') as myzip: | ||
144 | + myzip.write(f.name) | ||
145 | + f.close() | ||
146 | + os.remove('coleta.json') | ||
147 | + filepath = os.path.abspath(arquivozip) | ||
148 | + return filepath | ||
149 | +''' |