Merge "Add a trend line to Functest reporting And use d3js lib rather than static...
[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