1#!/usr/bin/env python3
2
3# Copyright (c) 2022 Intel Corporation
4# SPDX-License-Identifier: Apache-2.0
5
6
7# This script upload test ci results to the zephyr ES instance for reporting and analysis.
8# see https://kibana.zephyrproject.io/
9
10from elasticsearch import Elasticsearch
11from elasticsearch.helpers import bulk
12import sys
13import os
14import json
15import argparse
16
17def gendata(f, index, run_date=None):
18    with open(f, "r") as j:
19        data = json.load(j)
20        for t in data['testsuites']:
21            name = t['name']
22            _grouping = name.split("/")[-1]
23            main_group = _grouping.split(".")[0]
24            sub_group = _grouping.split(".")[1]
25            env = data['environment']
26            if run_date:
27                env['run_date'] =  run_date
28            t['environment'] = env
29            t['component'] = main_group
30            t['sub_component'] = sub_group
31            yield {
32                    "_index": index,
33                    "_source": t
34                    }
35
36def main():
37    args = parse_args()
38
39    if args.index:
40        index_name = args.index
41    else:
42        index_name = 'tests-zephyr-1'
43
44    settings = {
45            "index": {
46                "number_of_shards": 4
47                }
48            }
49    mappings = {
50            "properties": {
51                "execution_time": {"type": "float"},
52                "retries": {"type": "integer"},
53                "testcases.execution_time": {"type": "float"},
54                }
55            }
56
57    if args.dry_run:
58        xx = None
59        for f in args.files:
60            xx = gendata(f, index_name)
61        for x in xx:
62            print(x)
63        sys.exit(0)
64
65    es = Elasticsearch(
66        [os.environ['ELASTICSEARCH_SERVER']],
67        api_key=os.environ['ELASTICSEARCH_KEY'],
68        verify_certs=False
69        )
70
71    if args.create_index:
72        es.indices.create(index=index_name, mappings=mappings, settings=settings)
73    else:
74        if args.run_date:
75            print(f"Setting run date from command line: {args.run_date}")
76        for f in args.files:
77            bulk(es, gendata(f, index_name, args.run_date))
78
79
80def parse_args():
81    parser = argparse.ArgumentParser(allow_abbrev=False)
82    parser.add_argument('-y','--dry-run', action="store_true", help='Dry run.')
83    parser.add_argument('-c','--create-index', action="store_true", help='Create index.')
84    parser.add_argument('-i', '--index', help='index to push to.', required=True)
85    parser.add_argument('-r', '--run-date', help='Run date in ISO format', required=False)
86    parser.add_argument('files', metavar='FILE', nargs='+', help='file with test data.')
87
88    args = parser.parse_args()
89
90    return args
91
92
93if __name__ == '__main__':
94    main()
95