Skip to content

Commit b571de9

Browse files
committed
Refactor and formatting
Signed-off-by: Raul Sevilla <[email protected]>
1 parent 9b0b87f commit b571de9

File tree

5 files changed

+131
-113
lines changed

5 files changed

+131
-113
lines changed

fmatch/logrus.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ class SingletonLogger:
1717

1818
def __new__(cls, debug: int, name: str):
1919
if (not cls.instance) or name not in cls.instance:
20-
cls.instance[name] = cls._initialize_logger(debug,name)
20+
cls.instance[name] = cls._initialize_logger(debug, name)
2121
return cls.instance[name]
2222

2323
@staticmethod
2424
def _initialize_logger(debug: int, name: str) -> logging.Logger:
2525
level = debug # if debug else logging.INFO
2626
logger = logging.getLogger(name)
27-
logger.propagate=False
27+
logger.propagate = False
2828
if not logger.hasHandlers():
2929
logger.setLevel(level)
3030
handler = logging.StreamHandler(sys.stdout)
3131
handler.setLevel(level)
3232
formatter = logging.Formatter(
33-
"%(asctime)s - %(name)-10s - %(levelname)s - file: %(filename)s - line: %(lineno)d - %(message)s" # pylint: disable = line-too-long
33+
"%(asctime)s - %(name)-10s - %(levelname)s - file: %(filename)s - line: %(lineno)d - %(message)s" # pylint: disable = line-too-long
3434
)
3535
handler.setFormatter(formatter)
3636
logger.addHandler(handler)
3737
return logger
3838

3939
@classmethod
40-
def getLogger(cls, name:str) -> logging.Logger:
40+
def getLogger(cls, name: str) -> logging.Logger:
4141
"""Return logger in instance
4242
4343
Args:

fmatch/matcher.py

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,87 +28,73 @@ def __init__(
2828
verify_certs: bool = True,
2929
):
3030
self.index = index
31-
self.es_url = ES_URL
3231
self.search_size = 10000
3332
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)
3634

37-
def get_metadata_by_uuid(self, uuid: str, index: str = None) -> dict:
35+
def get_metadata_by_uuid(self, uuid: str) -> dict:
3836
"""Returns back metadata when uuid is given
3937
4038
Args:
4139
uuid (str): uuid of the run
42-
index (str, optional): index to be searched in. Defaults to None.
4340
4441
Returns:
4542
_type_: _description_
4643
"""
47-
if index is None:
48-
index = self.index
4944
query = Q("match", uuid=uuid)
5045
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)
5348
hits = res.hits.hits
5449
if hits:
5550
result = dict(hits[0].to_dict()["_source"])
5651
return result
5752

58-
def query_index(self, index: str, search: Search) -> Response:
53+
def query_index(self, search: Search) -> Response:
5954
"""generic query function
6055
6156
Args:
62-
index (str): _description_
6357
search (Search) : Search object with query
6458
"""
65-
self.logger.info("Executing query against index=%s", index)
59+
self.logger.info("Executing query against index: %s", self.index)
6660
self.logger.debug("Executing query \r\n%s", search.to_dict())
6761
return search.execute()
6862

6963
def get_uuid_by_metadata(
7064
self,
7165
meta: Dict[str, Any],
72-
index: str = None,
7366
lookback_date: datetime = None,
7467
lookback_size: int = 10000,
7568
) -> List[Dict[str, str]]:
7669
"""gets uuid by metadata
7770
7871
Args:
7972
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):
8274
The cutoff date to get the uuids from. Defaults to None.
83-
lookback_size (int, optional):
75+
lookback_size (int, optional):
8476
Maximum number of runs to get, gets the latest. Defaults to 10000.
8577
86-
lookback_size and lookback_date get the data on the
78+
lookback_size and lookback_date get the data on the
8779
precedency of whichever cutoff comes first.
8880
Similar to a car manufacturer's warranty limits.
8981
9082
Returns:
9183
List[Dict[str, str]]: _description_
9284
"""
85+
must_clause = []
9386
must_not_clause = []
94-
if index is None:
95-
index = self.index
9687

9788
version = meta["ocpVersion"][:4]
9889

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)}))
11298

11399
if "ocpMajorVersion" in meta:
114100
version = meta["ocpMajorVersion"]
@@ -130,26 +116,32 @@ def get_uuid_by_metadata(
130116
filter=filter_clause,
131117
)
132118
s = (
133-
Search(using=self.es, index=index)
119+
Search(using=self.es, index=self.index)
134120
.query(query)
135121
.sort({"timestamp": {"order": "desc"}})
136122
.extra(size=lookback_size)
137123
)
138-
result = self.query_index(index, s)
124+
result = self.query_index(s)
139125
hits = result.hits.hits
140126
uuids_docs = []
141127
for hit in hits:
142128
if "buildUrl" in hit["_source"]:
143-
uuids_docs.append({
129+
uuids_docs.append(
130+
{
144131
"uuid": hit.to_dict()["_source"]["uuid"],
145-
"buildUrl": hit.to_dict()["_source"]["buildUrl"]})
132+
"buildUrl": hit.to_dict()["_source"]["buildUrl"],
133+
}
134+
)
146135
else:
147-
uuids_docs.append({
136+
uuids_docs.append(
137+
{
148138
"uuid": hit.to_dict()["_source"]["uuid"],
149-
"buildUrl": "http://bogus-url"})
139+
"buildUrl": "http://bogus-url",
140+
}
141+
)
150142
return uuids_docs
151143

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]]:
153145
"""match kube burner runs
154146
Args:
155147
uuids (list): list of uuids
@@ -165,9 +157,11 @@ def match_kube_burner(self, uuids: List[str], index: str) -> List[Dict[str, Any]
165157
],
166158
)
167159
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)
169163
)
170-
result = self.query_index(index, search)
164+
result = self.query_index(search)
171165
runs = [item.to_dict()["_source"] for item in result.hits.hits]
172166
return runs
173167

@@ -188,16 +182,15 @@ def filter_runs(self, pdata: Dict[Any, Any], data: Dict[Any, Any]) -> List[str]:
188182
ids_df = ndf.loc[df["jobConfig.jobIterations"] == iterations]
189183
return ids_df["uuid"].to_list()
190184

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]
193187
) -> Dict[Any, Any]:
194188
"""
195189
Get results of elasticsearch data query based on uuid(s) and defined metrics
196190
197191
Args:
198192
uuid (str): _description_
199193
uuids (list): _description_
200-
index_str (str): _description_
201194
metrics (dict): _description_
202195
203196
Returns:
@@ -224,22 +217,19 @@ def getResults(
224217
],
225218
)
226219
search = (
227-
Search(using=self.es, index=index_str)
220+
Search(using=self.es, index=self.index)
228221
.query(query)
229222
.extra(size=self.search_size)
230223
)
231-
result = self.query_index(index_str, search)
224+
result = self.query_index(search)
232225
runs = [item.to_dict()["_source"] for item in result.hits.hits]
233226
return runs
234227

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]):
238229
"""burner_metric_query will query for specific metrics data.
239230
240231
Args:
241232
uuids (list): List of uuids
242-
index (str): ES/OS Index to query from
243233
metrics (dict): metrics defined in es index metrics
244234
"""
245235
metric_queries = []
@@ -261,7 +251,9 @@ def get_agg_metric_query(
261251
],
262252
)
263253
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)
265257
)
266258
agg_value = metrics["agg"]["value"]
267259
agg_type = metrics["agg"]["agg_type"]
@@ -271,7 +263,7 @@ def get_agg_metric_query(
271263
search.aggs.bucket(
272264
"uuid", "terms", field="uuid.keyword", size=self.search_size
273265
).metric(agg_value, agg_type, field=metrics["metric_of_interest"])
274-
result = self.query_index(index, search)
266+
result = self.query_index(search)
275267
data = self.parse_agg_results(result, agg_value, agg_type)
276268
return data
277269

fmatch/test_fmatch.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from datetime import datetime
66
import sys
77
import warnings
8+
89
# pylint: disable=import-error
910
import pandas as pd
1011

@@ -17,7 +18,7 @@
1718
)
1819

1920
match = Matcher(index="perf_scale_ci*", verify_certs=False)
20-
res=match.get_metadata_by_uuid("b4afc724-f175-44d1-81ff-a8255fea034f",'perf_scale_ci*')
21+
res = match.get_metadata_by_uuid("b4afc724-f175-44d1-81ff-a8255fea034f")
2122

2223
meta = {}
2324
meta["masterNodesType"] = "m6a.xlarge"
@@ -34,15 +35,16 @@
3435
# meta['fips'] = "false"
3536

3637
uuids = match.get_uuid_by_metadata(meta)
37-
print("All uuids",len(uuids))
38-
date= datetime.strptime("2024-07-01T13:46:24Z","%Y-%m-%dT%H:%M:%SZ")
39-
uuids2= match.get_uuid_by_metadata(meta,lookback_date=date)
40-
print("lookback uuids",len(uuids2))
38+
print("All uuids", len(uuids))
39+
date = datetime.strptime("2024-07-01T13:46:24Z", "%Y-%m-%dT%H:%M:%SZ")
40+
uuids2 = match.get_uuid_by_metadata(meta, lookback_date=date)
41+
print("lookback uuids", len(uuids2))
4142
uuids2 = match.get_uuid_by_metadata(meta)
4243
if len(uuids) == 0:
4344
print("No UUID present for given metadata")
4445
sys.exit()
45-
runs = match.match_kube_burner(uuids,"ripsaw-kube-burner*")
46+
match = Matcher(index="ripsaw-kube-burner*", verify_certs=False)
47+
runs = match.match_kube_burner(uuids)
4648

4749
ids = match.filter_runs(runs, runs)
4850
podl_metrics = {
@@ -52,25 +54,25 @@
5254
"metric_of_interest": "P99",
5355
"not": {"jobConfig.name": "garbage-collection"},
5456
}
55-
podl = match.getResults("", ids, "ripsaw-kube-burner*",metrics=podl_metrics)
57+
podl = match.get_results("", ids, metrics=podl_metrics)
5658
kapi_metrics = {
5759
"name": "apiserverCPU",
5860
"metricName": "containerCPU",
5961
"labels.namespace.keyword": "openshift-kube-apiserver",
6062
"metric_of_interest": "value",
6163
"agg": {"value": "cpu", "agg_type": "avg"},
6264
}
63-
kapi_cpu = match.get_agg_metric_query(ids, "ripsaw-kube-burner*", metrics=kapi_metrics)
65+
kapi_cpu = match.get_agg_metric_query(ids, metrics=kapi_metrics)
6466
podl_df = match.convert_to_df(
65-
podl, columns=['uuid', 'timestamp', 'quantileName', 'P99'])
67+
podl, columns=["uuid", "timestamp", "quantileName", "P99"]
68+
)
6669
kapi_cpu_df = match.convert_to_df(kapi_cpu)
6770
merge_df = pd.merge(kapi_cpu_df, podl_df, on="uuid")
68-
match.save_results(merge_df, "merged.csv", [
69-
"uuid", "timestamp_x", "cpu_avg", "P99"])
71+
match.save_results(merge_df, "merged.csv", ["uuid", "timestamp_x", "cpu_avg", "P99"])
7072

7173
df = pd.read_csv("merged.csv")
7274
ls = df["uuid"].to_list()
7375
# Check merged csv data - Debug
7476
for i in ls:
7577
# Debug - Ensure they are all using the same networkType
76-
print(match.get_metadata_by_uuid(i)['networkType'])
78+
print(match.get_metadata_by_uuid(i)["networkType"])

0 commit comments

Comments
 (0)