forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcreate_combined_ci_report.py
More file actions
executable file
·295 lines (250 loc) · 9.59 KB
/
create_combined_ci_report.py
File metadata and controls
executable file
·295 lines (250 loc) · 9.59 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
import argparse
import os
from pathlib import Path
from itertools import combinations
import json
import requests
from clickhouse_driver import Client
import boto3
from botocore.exceptions import NoCredentialsError
DATABASE_HOST_VAR = "CHECKS_DATABASE_HOST"
DATABASE_USER_VAR = "CHECKS_DATABASE_USER"
DATABASE_PASSWORD_VAR = "CHECKS_DATABASE_PASSWORD"
S3_BUCKET = "altinity-build-artifacts"
def get_checks_fails(client: Client, job_url: str):
"""
Get tests that did not succeed for the given job URL.
Exclude checks that have status 'error' as they are counted in get_checks_errors.
"""
columns = (
"check_status, check_name, test_status, test_name, report_url as results_link"
)
query = f"""SELECT {columns} FROM `gh-data`.checks
WHERE task_url='{job_url}'
AND test_status IN ('FAIL', 'ERROR')
AND check_status!='error'
ORDER BY check_name, test_name
"""
return client.query_dataframe(query)
def get_checks_known_fails(client: Client, job_url: str, known_fails: dict):
"""
Get tests that are known to fail for the given job URL.
"""
assert len(known_fails) > 0, "cannot query the database with empty known fails"
columns = (
"check_status, check_name, test_status, test_name, report_url as results_link"
)
query = f"""SELECT {columns} FROM `gh-data`.checks
WHERE task_url='{job_url}'
AND test_status='BROKEN'
AND test_name IN ({','.join(f"'{test}'" for test in known_fails.keys())})
ORDER BY test_name, check_name
"""
df = client.query_dataframe(query)
df.insert(
len(df.columns) - 1,
"reason",
df["test_name"]
.astype(str)
.apply(
lambda test_name: known_fails[test_name].get("reason", "No reason given")
),
)
return df
def get_checks_errors(client: Client, job_url: str):
"""
Get checks that have status 'error' for the given job URL.
"""
columns = (
"check_status, check_name, test_status, test_name, report_url as results_link"
)
query = f"""SELECT {columns} FROM `gh-data`.checks
WHERE task_url='{job_url}'
AND check_status=='error'
ORDER BY check_name, test_name
"""
return client.query_dataframe(query)
def drop_prefix_rows(df, column_to_clean):
"""
Drop rows from the dataframe if:
- the row matches another row completely except for the specified column
- the specified column of that row is a prefix of the same column in another row
"""
to_drop = set()
reference_columns = [col for col in df.columns if col != column_to_clean]
for (i, row_1), (j, row_2) in combinations(df.iterrows(), 2):
if all(row_1[col] == row_2[col] for col in reference_columns):
if row_2[column_to_clean].startswith(row_1[column_to_clean]):
to_drop.add(i)
elif row_1[column_to_clean].startswith(row_2[column_to_clean]):
to_drop.add(j)
return df.drop(to_drop)
def get_regression_fails(client: Client, job_url: str):
"""
Get regression tests that did not succeed for the given job URL.
"""
# If you rename the alias for report_url, also update the formatters in format_results_as_html_table
# Nested SELECT handles test reruns
query = f"""SELECT arch, job_name, status, test_name, results_link
FROM (
SELECT
architecture as arch,
test_name,
argMax(result, start_time) AS status,
job_url,
job_name,
report_url as results_link
FROM `gh-data`.clickhouse_regression_results
GROUP BY architecture, test_name, job_url, job_name, report_url
ORDER BY length(test_name) DESC
)
WHERE job_url='{job_url}'
AND status IN ('Fail', 'Error')
"""
df = client.query_dataframe(query)
df = drop_prefix_rows(df, "test_name")
df["job_name"] = df["job_name"].str.title()
return df
def url_to_html_link(url: str) -> str:
if not url:
return ""
text = url.split("/")[-1]
if not text:
text = "results"
return f'<a href="{url}">{text}</a>'
def format_test_name_for_linewrap(text: str) -> str:
"""Tweak the test name to improve line wrapping."""
return text.replace(".py::", "/")
def format_results_as_html_table(results) -> str:
if len(results) == 0:
return "<p>Nothing to report</p>"
results.columns = [col.replace("_", " ").title() for col in results.columns]
html = (
results.to_html(
index=False,
formatters={
"Results Link": url_to_html_link,
"Test Name": format_test_name_for_linewrap,
},
escape=False,
) # tbody/thead tags interfere with the table sorting script
.replace("<tbody>\n", "")
.replace("</tbody>\n", "")
.replace("<thead>\n", "")
.replace("</thead>\n", "")
.replace('<table border="1"', '<table style="min-width: min(900px, 98vw);"')
)
return html
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Create a combined CI report.")
parser.add_argument(
"--actions-run-url", required=True, help="URL of the actions run"
)
parser.add_argument(
"--pr-number", required=True, help="Pull request number for the S3 path"
)
parser.add_argument(
"--commit-sha", required=True, help="Commit SHA for the S3 path"
)
parser.add_argument(
"--no-upload", action="store_true", help="Do not upload the report"
)
parser.add_argument(
"--known-fails", type=str, help="Path to the file with known fails"
)
parser.add_argument(
"--mark-preview", action="store_true", help="Mark the report as a preview"
)
return parser.parse_args()
def main():
args = parse_args()
db_client = Client(
host=os.getenv(DATABASE_HOST_VAR),
user=os.getenv(DATABASE_USER_VAR),
password=os.getenv(DATABASE_PASSWORD_VAR),
port=9440,
secure="y",
verify=False,
settings={"use_numpy": True},
)
s3_path = (
f"https://s3.amazonaws.com/{S3_BUCKET}/{args.pr_number}/{args.commit_sha}/"
)
report_destination_url = s3_path + "combined_report.html"
ci_running_report_url = s3_path + "ci_running.html"
response = requests.get(ci_running_report_url)
if response.status_code == 200:
ci_running_report: str = response.text
else:
print(
f"Failed to download CI running report. Status code: {response.status_code}, Response: {response.text}"
)
exit(1)
fail_results = {
"checks_fails": get_checks_fails(db_client, args.actions_run_url),
"checks_known_fails": [],
"checks_errors": get_checks_errors(db_client, args.actions_run_url),
"regression_fails": get_regression_fails(db_client, args.actions_run_url),
}
if args.known_fails:
if not os.path.exists(args.known_fails):
print(f"Known fails file {args.known_fails} not found.")
exit(1)
with open(args.known_fails) as f:
known_fails = json.load(f)
if known_fails:
fail_results["checks_known_fails"] = get_checks_known_fails(
db_client, args.actions_run_url, known_fails
)
combined_report = (
ci_running_report.replace("ClickHouse CI running for", "Combined CI Report for")
.replace(
"<table>",
f"""<h2>Table of Contents</h2>
{'<p style="font-weight: bold;color: #F00;">This is a preview. FinishCheck has not completed.</p>' if args.mark_preview else ""}
<ul>
<li><a href="#ci-jobs-status">CI Jobs Status</a></li>
<li><a href="#checks-errors">Checks Errors</a> ({len(fail_results['checks_errors'])})</li>
<li><a href="#checks-fails">Checks New Fails</a> ({len(fail_results['checks_fails'])})</li>
<li><a href="#regression-fails">Regression New Fails</a> ({len(fail_results['regression_fails'])})</li>
<li><a href="#checks-known-fails">Checks Known Fails</a> ({len(fail_results['checks_known_fails'])})</li>
</ul>
<h2 id="ci-jobs-status">CI Jobs Status</h2>
<table>""",
1,
)
.replace(
"</table>",
f"""</table>
<h2 id="checks-errors">Checks Errors</h2>
{format_results_as_html_table(fail_results['checks_errors'])}
<h2 id="checks-fails">Checks New Fails</h2>
{format_results_as_html_table(fail_results['checks_fails'])}
<h2 id="regression-fails">Regression New Fails</h2>
{format_results_as_html_table(fail_results['regression_fails'])}
<h2 id="checks-known-fails">Checks Known Fails</h2>
{format_results_as_html_table(fail_results['checks_known_fails'])}
""",
1,
)
)
report_path = Path("combined_report.html")
report_path.write_text(combined_report, encoding="utf-8")
if args.no_upload:
print(f"Report saved to {report_path}")
exit(0)
# Upload the report to S3
s3_client = boto3.client("s3")
try:
s3_client.put_object(
Bucket=S3_BUCKET,
Key=f"{args.pr_number}/{args.commit_sha}/combined_report.html",
Body=combined_report,
ContentType="text/html; charset=utf-8",
)
except NoCredentialsError:
print("Credentials not available for S3 upload.")
print(report_destination_url)
if __name__ == "__main__":
main()