no more output elastic docs to stdout/console
[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(url, creds=None, body=None):
26     result = _post(url, creds=creds, body=(json.dumps(body)))
27     return result.status, result.data
28
29
30 def _get_docs_nr(url, creds=None, body=None):
31     res_data = _get('{}/_search?size=0'.format(url), creds=creds, body=body)
32     print type(res_data), res_data
33     return res_data['hits']['total']
34
35
36 def get_docs(url, creds=None, body=None, field='_source'):
37
38     docs_nr = _get_docs_nr(url, creds=creds, body=body)
39     res_data = _get('{}/_search?size={}'.format(url, docs_nr),
40                     creds=creds, body=body)
41
42     docs = []
43     for hit in res_data['hits']['hits']:
44         docs.append(hit[field])
45     return docs
46
47
48 def get_elastic_docs_by_days(elastic_url, creds, days):
49     if days == 0:
50         body = '''{
51             "query": {
52                 "match_all": {}
53             }
54         }'''
55     elif days > 0:
56         body = '''{{
57             "query" : {{
58                 "range" : {{
59                     "start_date" : {{
60                         "gte" : "now-{}d"
61                     }}
62                 }}
63             }}
64         }}'''.format(days)
65     else:
66         raise Exception('Update days must be non-negative')
67     return get_docs(elastic_url, creds, body)