-
Notifications
You must be signed in to change notification settings - Fork 0
/
results_parser.py
46 lines (33 loc) · 1.23 KB
/
results_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from datetime import datetime
from typing import List
class ReportMetrics:
def __init__(self, date: str):
self.date = date
self.metrics = {}
def parse(report: str, metrics: List[str]) -> ReportMetrics:
if report is None:
return None
split = report.split("----------------------------------------------------------\n")
metrics_split = split[2].split("\n")
date = convert_to_desired_format(split[1].split("Run at ")[1].split("\n")[0])
report_metrics = ReportMetrics(date=date)
try:
for line in metrics_split:
for metric in metrics:
if line.startswith(metric):
values = line.split(":")
report_metrics.metrics[metric] = float(values[1].split()[1])
except IndexError:
return None
return report_metrics
def convert_to_desired_format(date_str):
# Define the input and output date formats
input_format = "%a %b %d %H:%M:%S UTC %Y"
output_format = "%Y-%m-%d"
try:
parsed_date = datetime.strptime(date_str, input_format)
formatted_date = parsed_date.strftime(output_format)
return formatted_date
except ValueError:
print("Invalid date format")
return None