X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=utils%2Ftest%2Fscripts%2Fshared_utils.py;h=aa8a65d79305c1d9c99bc537ce50dceaae4c44b6;hb=ced0a8742f31f1b597a3c76c7dcf72d62da2bbc8;hp=15c1af8a51315559c79dcd18ea3e3ef35f074716;hpb=ae5c54cd7acb95defc0c8f0ec8a4ddeb0d85cfce;p=releng.git diff --git a/utils/test/scripts/shared_utils.py b/utils/test/scripts/shared_utils.py index 15c1af8a5..aa8a65d79 100644 --- a/utils/test/scripts/shared_utils.py +++ b/utils/test/scripts/shared_utils.py @@ -10,14 +10,14 @@ def delete_request(url, creds, body=None): http.request('DELETE', url, headers=headers, body=body) -def publish_json(json_ojb, creds, output_destination): +def publish_json(json_ojb, creds, to): json_dump = json.dumps(json_ojb) - if output_destination == 'stdout': + if to == 'stdout': print json_dump return 200, None else: headers = urllib3.make_headers(basic_auth=creds) - result = http.request('POST', output_destination, headers=headers, body=json_dump) + result = http.request('POST', to, headers=headers, body=json_dump) return result.status, result.data @@ -25,16 +25,39 @@ def _get_nr_of_hits(elastic_json): return elastic_json['hits']['total'] -def get_elastic_data(elastic_url, creds, body, field='_source'): +def get_elastic_docs(elastic_url, creds, body=None, field = '_source'): + # 1. get the number of results headers = urllib3.make_headers(basic_auth=creds) elastic_json = json.loads(http.request('GET', elastic_url + '/_search?size=0', headers=headers, body=body).data) + print elastic_json nr_of_hits = _get_nr_of_hits(elastic_json) # 2. get all results elastic_json = json.loads(http.request('GET', elastic_url + '/_search?size={}'.format(nr_of_hits), headers=headers, body=body).data) - elastic_data = [] + elastic_docs = [] for hit in elastic_json['hits']['hits']: - elastic_data.append(hit[field]) - return elastic_data + elastic_docs.append(hit[field]) + return elastic_docs + +def get_elastic_docs_by_days(elastic_url, creds, days): + if days == 0: + body = '''{ + "query": { + "match_all": {} + } + }''' + elif days > 0: + body = '''{{ + "query" : {{ + "range" : {{ + "start_date" : {{ + "gte" : "now-{}d" + }} + }} + }} + }}'''.format(days) + else: + raise Exception('Update days must be non-negative') + return get_elastic_docs(elastic_url, creds, body)