Remove old script to push test stats
[integration/test.git] / csit / scripts / generate_searchSourceJSON.py
1 from copy import deepcopy as dc
2
3 # Template for search source format
4 SEARCH_SOURCE_FORMAT = {
5     "index": None,
6     "filter": [],
7     "query": {"language": "lucene", "query": ""},
8 }
9
10 # Template for filter format
11 FILTER_FORMAT = {
12     "query": {
13         "match": {"placeholder_field": {"query": "query_phrase", "type": "phrase"}}
14     }
15 }
16
17
18 def generate(dash_config, viz_config, index_pattern):
19
20     search_source = dc(SEARCH_SOURCE_FORMAT)
21
22     # Search for 'match-with' and 'field' for each keys in 'filter' either
23     # in viz_config or dash_config
24     #
25     # ex:- filter:
26     #        1:
27     #           field: my_field
28     #           match-with: pattern
29
30     try:
31         filters = dash_config["filter"]
32         for _, value in filters.items():
33             try:
34                 temp = dc(FILTER_FORMAT)
35                 temp["query"]["match"][value["field"]] = temp["query"]["match"][
36                     "placeholder_field"
37                 ]
38                 temp["query"]["match"][value["field"]]["query"] = value["match-with"]
39                 del temp["query"]["match"]["placeholder_field"]
40                 search_source["filter"].append(temp)
41             except KeyError:
42                 continue
43     except KeyError:
44         pass
45
46     try:
47         filters = viz_config["filter"]
48         for _, value in filters.items():
49             try:
50                 temp = dc(FILTER_FORMAT)
51                 temp["query"]["match"][value["field"]] = temp["query"]["match"][
52                     "placeholder_field"
53                 ]
54                 temp["query"]["match"][value["field"]]["query"] = value["match-with"]
55                 del temp["query"]["match"]["placeholder_field"]
56                 search_source["filter"].append(temp)
57             except KeyError:
58                 continue
59     except KeyError:
60         pass
61
62     search_source["index"] = index_pattern
63
64     return search_source