Skip to content

Commit 9eff016

Browse files
committed
Add capability to query more than 1 test id and retries
1 parent 2b7eee6 commit 9eff016

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

backend/scripts/test_failure_reason.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,58 @@
22
import csv
33
import json
44
import string
5+
import time
56
import urllib.request
67
from multiprocessing import Pool
78
from argparse import ArgumentParser
89

10+
RETRY = 10
11+
912
access_token = os.getenv("C3_TOKEN")
13+
if not access_token:
14+
raise SystemExit("C3_TOKEN required")
1015

1116

1217
def parse_args():
1318
ap = ArgumentParser()
1419
ap.add_argument("--filter", choices=("id", "artifact_name"), default="id")
1520
ap.add_argument("csvs", nargs="+")
16-
ap.add_argument("test_id")
21+
ap.add_argument("--test-ids", nargs="+", required=True)
1722
return ap.parse_args()
1823

1924

2025
def get_summary(sub_id):
2126
"""
2227
Get the validation results of a given list of submissions from the C3 API.
2328
"""
24-
api_url = (
25-
f"https://certification.canonical.com/api/v2/reports/summary/{sub_id}"
26-
)
27-
headers = {
28-
"Content-Type": "application/json",
29-
"Authorization": f"Bearer {access_token}",
30-
}
31-
# Convert the payload to a JSON string
32-
# Create the request object with the data and headers
33-
req = urllib.request.Request(api_url, headers=headers, method="GET")
34-
# Send the request and get the response
35-
with urllib.request.urlopen(req) as response:
36-
result = json.loads(response.read().decode("utf-8"))
29+
for i in range(RETRY):
30+
try:
31+
api_url = f"https://certification.canonical.com/api/v2/reports/summary/{sub_id}"
32+
headers = {
33+
"Content-Type": "application/json",
34+
"Authorization": f"Bearer {access_token}",
35+
}
36+
# Convert the payload to a JSON string
37+
# Create the request object with the data and headers
38+
req = urllib.request.Request(
39+
api_url, headers=headers, method="GET"
40+
)
41+
# Send the request and get the response
42+
with urllib.request.urlopen(req, timeout=60) as response:
43+
result = json.loads(response.read().decode("utf-8"))
3744

38-
return (sub_id, result)
45+
return (sub_id, result)
46+
except Exception as e:
47+
if i == RETRY - 1:
48+
raise
49+
print(f"C3 failed with exception: {e}", flush=True)
50+
time.sleep(min(i * 10, 60))
3951

4052

4153
def slugify(_string: str):
4254
if not _string:
4355
return _string
56+
_string = _string.replace("com.canonical.certification::", "")
4457

4558
valid_chars = frozenset(f"_{string.ascii_letters}{string.digits}")
4659
# Python identifiers cannot start with a digit
@@ -75,12 +88,11 @@ def _f(x: dict) -> bool:
7588
]
7689

7790

78-
def main():
79-
args = parse_args()
91+
def do_test_id(filter_arg, csvs, test_id):
8092
lines_of_interest = []
81-
filter_f = get_filter(args.filter, args.test_id)
93+
filter_f = get_filter(filter_arg, test_id)
8294

83-
for f_path in args.csvs:
95+
for f_path in csvs:
8496
with open(f_path) as f:
8597
reader = csv.DictReader(f, delimiter=",")
8698
lines_of_interest += list(filter(filter_f, reader))
@@ -109,7 +121,7 @@ def main():
109121
} | relevant_template_id
110122
relevant_ids = relevant_ids - {None, ""}
111123

112-
file_name = slugify(args.test_id)
124+
file_name = slugify(test_id)
113125

114126
with open(f"{file_name}.url", "w+") as f:
115127
f.writelines("\n".join(sub_links))
@@ -193,5 +205,13 @@ def main():
193205
writer.writerows(result_rows)
194206

195207

208+
def main():
209+
args = parse_args()
210+
tot = len(args.test_ids)
211+
for i, test_id in enumerate(args.test_ids, 1):
212+
print(f"Doing [{i}/{tot}]: {test_id}")
213+
do_test_id(args.filter, args.csvs, test_id)
214+
215+
196216
if __name__ == "__main__":
197217
main()

0 commit comments

Comments
 (0)