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