refactor elastic_access.py
[releng.git] / utils / test / dashboard / dashboard / common / elastic_access.py
1 import json
2
3 import urllib3
4
5 http = urllib3.PoolManager()
6
7
8 def _request(method, url, creds=None, body=None):
9     headers = urllib3.make_headers(basic_auth=creds)
10     return http.request(method, url, headers=headers, body=body)
11
12
13 def _post(url, creds=None, body=None):
14     return _request('POST', url, creds=creds, body=body)
15
16
17 def _get(url, creds=None, body=None):
18     return json.loads(_request('GET', url, creds=creds, body=body).data)
19
20
21 def delete_docs(url, creds=None, body=None):
22     return _request('DELETE', url, creds=creds, body=body)
23
24
25 def publish_docs(docs, creds, to):
26     json_docs = json.dumps(docs)
27     if to == 'stdout':
28         print json_docs
29         return 200, None
30     else:
31         result = _post(to, creds=creds, body=json_docs)
32         return result.status, result.data
33
34
35 def _get_docs_nr(url, creds=None, body=None):
36     res_data = _get('{}/_search?size=0'.format(url), creds=creds, body=body)
37     print type(res_data), res_data
38     return res_data['hits']['total']
39
40
41 def get_docs(url, creds=None, body=None, field='_source'):
42
43     docs_nr = _get_docs_nr(url, creds=creds, body=body)
44     res_data = _get('{}/_search?size={}'.format(url, docs_nr),
45                     creds=creds, body=body)
46
47     docs = []
48     for hit in res_data['hits']['hits']:
49         docs.append(hit[field])
50     return docs
51
52
53 def get_elastic_docs_by_days(elastic_url, creds, days):
54     if days == 0:
55         body = '''{
56             "query": {
57                 "match_all": {}
58             }
59         }'''
60     elif days > 0:
61         body = '''{{
62             "query" : {{
63                 "range" : {{
64                     "start_date" : {{
65                         "gte" : "now-{}d"
66                     }}
67                 }}
68             }}
69         }}'''.format(days)
70     else:
71         raise Exception('Update days must be non-negative')
72     return get_docs(elastic_url, creds, body)