This repository was archived by the owner on Jun 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_results.py
More file actions
262 lines (214 loc) · 8.29 KB
/
test_results.py
File metadata and controls
262 lines (214 loc) · 8.29 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
import tempfile
from datetime import date, timedelta
import polars as pl
from django.conf import settings
from shared.helpers.redis import get_redis_connection
from shared.metrics import Summary
from shared.storage import get_appropriate_storage_service
from shared.storage.exceptions import FileNotInStorageError
from rollouts import READ_NEW_TA
from services.task import TaskService
get_results_summary = Summary(
"test_results_get_results", "Time it takes to download results from GCS", ["impl"]
)
def redis_key(
repoid: int,
branch: str,
interval_start: int,
interval_end: int | None = None,
) -> str:
key = f"test_results:{repoid}:{branch}:{interval_start}"
if interval_end is not None:
key = f"{key}:{interval_end}"
return key
def storage_key(
repoid: int, branch: str, interval_start: int, interval_end: int | None = None
) -> str:
key = f"test_results/rollups/{repoid}/{branch}/{interval_start}"
if interval_end is not None:
key = f"{key}_{interval_end}"
return key
def dedup_table(table: pl.DataFrame) -> pl.DataFrame:
failure_rate_expr = (
pl.col("failure_rate")
* (pl.col("total_fail_count") + pl.col("total_pass_count"))
).sum() / (pl.col("total_fail_count") + pl.col("total_pass_count")).sum()
flake_rate_expr = (
pl.col("flake_rate") * (pl.col("total_fail_count") + pl.col("total_pass_count"))
).sum() / (pl.col("total_fail_count") + pl.col("total_pass_count")).sum()
avg_duration_expr = (
pl.col("avg_duration")
* (pl.col("total_pass_count") + pl.col("total_fail_count"))
).sum() / (pl.col("total_pass_count") + pl.col("total_fail_count")).sum()
# dedup
table = (
table.group_by("name")
.agg(
pl.col("testsuite").alias("testsuite"),
pl.col("flags").explode().unique().alias("flags"),
failure_rate_expr.fill_nan(0).alias("failure_rate"),
flake_rate_expr.fill_nan(0).alias("flake_rate"),
pl.col("updated_at").max().alias("updated_at"),
avg_duration_expr.fill_nan(0).alias("avg_duration"),
pl.col("total_fail_count").sum().alias("total_fail_count"),
pl.col("total_flaky_fail_count").sum().alias("total_flaky_fail_count"),
pl.col("total_pass_count").sum().alias("total_pass_count"),
pl.col("total_skip_count").sum().alias("total_skip_count"),
pl.col("commits_where_fail")
.sum()
.alias("commits_where_fail"), # TODO: this is wrong
pl.col("last_duration").max().alias("last_duration"),
)
.sort("name")
)
return table
def get_results(
repoid: int,
branch: str,
interval_start: int,
interval_end: int | None = None,
) -> pl.DataFrame | None:
"""
try redis
if redis is empty
try storage
if storage is empty
return None
else
cache to redis
deserialize
"""
# try redis
if READ_NEW_TA.check_value(repoid):
func = new_get_results
label = "new"
else:
func = old_get_results
label = "old"
with get_results_summary.labels(label).time():
return func(repoid, branch, interval_start, interval_end)
def old_get_results(
repoid: int,
branch: str,
interval_start: int,
interval_end: int | None = None,
) -> pl.DataFrame | None:
redis_conn = get_redis_connection()
key = redis_key(repoid, branch, interval_start, interval_end)
result: bytes | None = redis_conn.get(key)
if result is None:
# try storage
storage_service = get_appropriate_storage_service(repoid)
key = storage_key(repoid, branch, interval_start, interval_end)
try:
result = storage_service.read_file(
bucket_name=settings.GCS_BUCKET_NAME, path=key
)
# cache to redis
TaskService().cache_test_results_redis(repoid, branch)
except FileNotInStorageError:
# give up
return None
# deserialize
table = pl.read_ipc(result)
if table.height == 0:
return None
table = dedup_table(table)
return table
def rollup_blob_path(repoid: int, branch: str | None = None) -> str:
return (
f"test_analytics/branch_rollups/{repoid}/{branch}.arrow"
if branch
else f"test_analytics/repo_rollups/{repoid}.arrow"
)
def no_version_agg_table(table: pl.LazyFrame) -> pl.LazyFrame:
failure_rate_expr = (pl.col("fail_count")).sum() / (
pl.col("fail_count") + pl.col("pass_count")
).sum()
flake_rate_expr = (pl.col("flaky_fail_count")).sum() / (
pl.col("fail_count") + pl.col("pass_count")
).sum()
avg_duration_expr = (
pl.col("avg_duration") * (pl.col("pass_count") + pl.col("fail_count"))
).sum() / (pl.col("pass_count") + pl.col("fail_count")).sum()
table = table.group_by(pl.col("computed_name").alias("name")).agg(
pl.col("flags")
.explode()
.unique()
.alias("flags"), # TODO: filter by this before we aggregate
pl.col("failing_commits").sum().alias("commits_where_fail"),
pl.col("last_duration").max().alias("last_duration"),
failure_rate_expr.alias("failure_rate"),
flake_rate_expr.alias("flake_rate"),
avg_duration_expr.alias("avg_duration"),
pl.col("pass_count").sum().alias("total_pass_count"),
pl.col("fail_count").sum().alias("total_fail_count"),
pl.col("flaky_fail_count").sum().alias("total_flaky_fail_count"),
pl.col("skip_count").sum().alias("total_skip_count"),
pl.col("updated_at").max().alias("updated_at"),
)
return table
def v1_agg_table(table: pl.LazyFrame) -> pl.LazyFrame:
failure_rate_expr = (pl.col("fail_count")).sum() / (
pl.col("fail_count") + pl.col("pass_count")
).sum()
flake_rate_expr = (pl.col("flaky_fail_count")).sum() / (
pl.col("fail_count") + pl.col("pass_count")
).sum()
avg_duration_expr = (
pl.col("avg_duration") * (pl.col("pass_count") + pl.col("fail_count"))
).sum() / (pl.col("pass_count") + pl.col("fail_count")).sum()
table = table.group_by(pl.col("computed_name").alias("name")).agg(
pl.col("testsuite").alias(
"testsuite"
), # TODO: filter by this before we aggregate
pl.col("flags")
.explode()
.unique()
.alias("flags"), # TODO: filter by this before we aggregate
pl.col("failing_commits").sum().alias("commits_where_fail"),
pl.col("last_duration").max().alias("last_duration"),
failure_rate_expr.alias("failure_rate"),
flake_rate_expr.alias("flake_rate"),
avg_duration_expr.alias("avg_duration"),
pl.col("pass_count").sum().alias("total_pass_count"),
pl.col("fail_count").sum().alias("total_fail_count"),
pl.col("flaky_fail_count").sum().alias("total_flaky_fail_count"),
pl.col("skip_count").sum().alias("total_skip_count"),
pl.col("updated_at").max().alias("updated_at"),
)
return table
def new_get_results(
repoid: int,
branch: str | None,
interval_start: int,
interval_end: int | None = None,
) -> pl.DataFrame | None:
storage_service = get_appropriate_storage_service(repoid)
key = rollup_blob_path(repoid, branch)
try:
with tempfile.TemporaryFile() as tmp:
metadata = {}
storage_service.read_file(
bucket_name=settings.GCS_BUCKET_NAME,
path=key,
file_obj=tmp,
metadata_container=metadata,
)
table = pl.scan_ipc(tmp)
# filter start
start_date = date.today() - timedelta(days=interval_start)
table = table.filter(pl.col("timestamp_bin") >= start_date)
# filter end
if interval_end is not None:
end_date = date.today() - timedelta(days=interval_end)
table = table.filter(pl.col("timestamp_bin") <= end_date)
# aggregate
match metadata.get("version"):
case "1":
table = v1_agg_table(table)
case _: # no version is missding
table = no_version_agg_table(table)
return table.collect()
except FileNotInStorageError:
return None