@@ -28,87 +28,73 @@ def __init__(
28
28
verify_certs : bool = True ,
29
29
):
30
30
self .index = index
31
- self .es_url = ES_URL
32
31
self .search_size = 10000
33
32
self .logger = SingletonLogger (debug = level , name = "Matcher" )
34
- self .es = Elasticsearch ([self .es_url ], timeout = 30 , verify_certs = verify_certs )
35
- self .data = None
33
+ self .es = Elasticsearch ([ES_URL ], timeout = 30 , verify_certs = verify_certs )
36
34
37
- def get_metadata_by_uuid (self , uuid : str , index : str = None ) -> dict :
35
+ def get_metadata_by_uuid (self , uuid : str ) -> dict :
38
36
"""Returns back metadata when uuid is given
39
37
40
38
Args:
41
39
uuid (str): uuid of the run
42
- index (str, optional): index to be searched in. Defaults to None.
43
40
44
41
Returns:
45
42
_type_: _description_
46
43
"""
47
- if index is None :
48
- index = self .index
49
44
query = Q ("match" , uuid = uuid )
50
45
result = {}
51
- s = Search (using = self .es , index = index ).query (query )
52
- res = self .query_index (index , s )
46
+ s = Search (using = self .es , index = self . index ).query (query )
47
+ res = self .query_index (s )
53
48
hits = res .hits .hits
54
49
if hits :
55
50
result = dict (hits [0 ].to_dict ()["_source" ])
56
51
return result
57
52
58
- def query_index (self , index : str , search : Search ) -> Response :
53
+ def query_index (self , search : Search ) -> Response :
59
54
"""generic query function
60
55
61
56
Args:
62
- index (str): _description_
63
57
search (Search) : Search object with query
64
58
"""
65
- self .logger .info ("Executing query against index= %s" , index )
59
+ self .logger .info ("Executing query against index: %s" , self . index )
66
60
self .logger .debug ("Executing query \r \n %s" , search .to_dict ())
67
61
return search .execute ()
68
62
69
63
def get_uuid_by_metadata (
70
64
self ,
71
65
meta : Dict [str , Any ],
72
- index : str = None ,
73
66
lookback_date : datetime = None ,
74
67
lookback_size : int = 10000 ,
75
68
) -> List [Dict [str , str ]]:
76
69
"""gets uuid by metadata
77
70
78
71
Args:
79
72
meta (Dict[str, Any]): metadata of the runs
80
- index (str, optional): Index to search. Defaults to None.
81
- lookback_date (datetime, optional):
73
+ lookback_date (datetime, optional):
82
74
The cutoff date to get the uuids from. Defaults to None.
83
- lookback_size (int, optional):
75
+ lookback_size (int, optional):
84
76
Maximum number of runs to get, gets the latest. Defaults to 10000.
85
77
86
- lookback_size and lookback_date get the data on the
78
+ lookback_size and lookback_date get the data on the
87
79
precedency of whichever cutoff comes first.
88
80
Similar to a car manufacturer's warranty limits.
89
81
90
82
Returns:
91
83
List[Dict[str, str]]: _description_
92
84
"""
85
+ must_clause = []
93
86
must_not_clause = []
94
- if index is None :
95
- index = self .index
96
87
97
88
version = meta ["ocpVersion" ][:4 ]
98
89
99
- must_clause = [
100
- (
101
- Q ("match" , ** {field : str (value )})
102
- if isinstance (value , str )
103
- else Q ("match" , ** {field : value })
104
- )
105
- for field , value in meta .items ()
106
- if field not in "ocpVersion"
107
- if field not in "ocpMajorVersion"
108
- ]
109
-
110
- for field , value in meta .get ("not" , {}).items ():
111
- must_not_clause .append (Q ("match" , ** {field : str (value )}))
90
+ for field , value in meta .items ():
91
+ if field in ["ocpVersion" , "ocpMajorVersion" ]:
92
+ continue
93
+ if field != "not" :
94
+ must_clause .append (Q ("match" , ** {field : str (value )}))
95
+ else :
96
+ for not_field , not_value in meta ["not" ].items ():
97
+ must_not_clause .append (Q ("match" , ** {not_field : str (not_value )}))
112
98
113
99
if "ocpMajorVersion" in meta :
114
100
version = meta ["ocpMajorVersion" ]
@@ -130,26 +116,32 @@ def get_uuid_by_metadata(
130
116
filter = filter_clause ,
131
117
)
132
118
s = (
133
- Search (using = self .es , index = index )
119
+ Search (using = self .es , index = self . index )
134
120
.query (query )
135
121
.sort ({"timestamp" : {"order" : "desc" }})
136
122
.extra (size = lookback_size )
137
123
)
138
- result = self .query_index (index , s )
124
+ result = self .query_index (s )
139
125
hits = result .hits .hits
140
126
uuids_docs = []
141
127
for hit in hits :
142
128
if "buildUrl" in hit ["_source" ]:
143
- uuids_docs .append ({
129
+ uuids_docs .append (
130
+ {
144
131
"uuid" : hit .to_dict ()["_source" ]["uuid" ],
145
- "buildUrl" : hit .to_dict ()["_source" ]["buildUrl" ]})
132
+ "buildUrl" : hit .to_dict ()["_source" ]["buildUrl" ],
133
+ }
134
+ )
146
135
else :
147
- uuids_docs .append ({
136
+ uuids_docs .append (
137
+ {
148
138
"uuid" : hit .to_dict ()["_source" ]["uuid" ],
149
- "buildUrl" : "http://bogus-url" })
139
+ "buildUrl" : "http://bogus-url" ,
140
+ }
141
+ )
150
142
return uuids_docs
151
143
152
- def match_kube_burner (self , uuids : List [str ], index : str ) -> List [Dict [str , Any ]]:
144
+ def match_kube_burner (self , uuids : List [str ]) -> List [Dict [str , Any ]]:
153
145
"""match kube burner runs
154
146
Args:
155
147
uuids (list): list of uuids
@@ -165,9 +157,11 @@ def match_kube_burner(self, uuids: List[str], index: str) -> List[Dict[str, Any]
165
157
],
166
158
)
167
159
search = (
168
- Search (using = self .es , index = index ).query (query ).extra (size = self .search_size )
160
+ Search (using = self .es , index = self .index )
161
+ .query (query )
162
+ .extra (size = self .search_size )
169
163
)
170
- result = self .query_index (index , search )
164
+ result = self .query_index (search )
171
165
runs = [item .to_dict ()["_source" ] for item in result .hits .hits ]
172
166
return runs
173
167
@@ -188,16 +182,15 @@ def filter_runs(self, pdata: Dict[Any, Any], data: Dict[Any, Any]) -> List[str]:
188
182
ids_df = ndf .loc [df ["jobConfig.jobIterations" ] == iterations ]
189
183
return ids_df ["uuid" ].to_list ()
190
184
191
- def getResults (
192
- self , uuid : str , uuids : List [str ], index_str : str , metrics : Dict [str , Any ]
185
+ def get_results (
186
+ self , uuid : str , uuids : List [str ], metrics : Dict [str , Any ]
193
187
) -> Dict [Any , Any ]:
194
188
"""
195
189
Get results of elasticsearch data query based on uuid(s) and defined metrics
196
190
197
191
Args:
198
192
uuid (str): _description_
199
193
uuids (list): _description_
200
- index_str (str): _description_
201
194
metrics (dict): _description_
202
195
203
196
Returns:
@@ -224,22 +217,19 @@ def getResults(
224
217
],
225
218
)
226
219
search = (
227
- Search (using = self .es , index = index_str )
220
+ Search (using = self .es , index = self . index )
228
221
.query (query )
229
222
.extra (size = self .search_size )
230
223
)
231
- result = self .query_index (index_str , search )
224
+ result = self .query_index (search )
232
225
runs = [item .to_dict ()["_source" ] for item in result .hits .hits ]
233
226
return runs
234
227
235
- def get_agg_metric_query (
236
- self , uuids : List [str ], index : str , metrics : Dict [str , Any ]
237
- ):
228
+ def get_agg_metric_query (self , uuids : List [str ], metrics : Dict [str , Any ]):
238
229
"""burner_metric_query will query for specific metrics data.
239
230
240
231
Args:
241
232
uuids (list): List of uuids
242
- index (str): ES/OS Index to query from
243
233
metrics (dict): metrics defined in es index metrics
244
234
"""
245
235
metric_queries = []
@@ -261,7 +251,9 @@ def get_agg_metric_query(
261
251
],
262
252
)
263
253
search = (
264
- Search (using = self .es , index = index ).query (query ).extra (size = self .search_size )
254
+ Search (using = self .es , index = self .index )
255
+ .query (query )
256
+ .extra (size = self .search_size )
265
257
)
266
258
agg_value = metrics ["agg" ]["value" ]
267
259
agg_type = metrics ["agg" ]["agg_type" ]
@@ -271,7 +263,7 @@ def get_agg_metric_query(
271
263
search .aggs .bucket (
272
264
"uuid" , "terms" , field = "uuid.keyword" , size = self .search_size
273
265
).metric (agg_value , agg_type , field = metrics ["metric_of_interest" ])
274
- result = self .query_index (index , search )
266
+ result = self .query_index (search )
275
267
data = self .parse_agg_results (result , agg_value , agg_type )
276
268
return data
277
269
0 commit comments