Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit aadbae1

Browse files
committed
feat: create testruns model in timeseries app
1 parent 3aea532 commit aadbae1

File tree

6 files changed

+439
-0
lines changed

6 files changed

+439
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Generated by Django 4.2.16 on 2025-02-06 15:02
2+
3+
import django.contrib.postgres.fields
4+
import django_prometheus.models
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
dependencies = [
10+
(
11+
"timeseries",
12+
"0014_remove_measurement_timeseries_measurement_flag_unique_and_more",
13+
),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name="Testrun",
19+
fields=[
20+
("timestamp", models.DateTimeField(primary_key=True, serialize=False)),
21+
("repo_id", models.BigIntegerField()),
22+
("test_id", models.BinaryField()),
23+
("testsuite", models.TextField(null=True)),
24+
("classname", models.TextField(null=True)),
25+
("name", models.TextField(null=True)),
26+
("computed_name", models.TextField(null=True)),
27+
("outcome", models.TextField()),
28+
("duration_seconds", models.FloatField(null=True)),
29+
("failure_message", models.TextField(null=True)),
30+
("framework", models.TextField(null=True)),
31+
("filename", models.TextField(null=True)),
32+
("commit_sha", models.TextField(null=True)),
33+
("branch", models.TextField(null=True)),
34+
(
35+
"flags",
36+
django.contrib.postgres.fields.ArrayField(
37+
base_field=models.TextField(), null=True, size=None
38+
),
39+
),
40+
("upload_id", models.BigIntegerField(null=True)),
41+
],
42+
bases=(
43+
django_prometheus.models.ExportModelOperationsMixin(
44+
"timeseries.testrun"
45+
),
46+
models.Model,
47+
),
48+
),
49+
migrations.RunSQL(
50+
"ALTER TABLE timeseries_testrun DROP CONSTRAINT timeseries_testrun_pkey;",
51+
reverse_sql="",
52+
),
53+
migrations.RunSQL(
54+
"SELECT create_hypertable('timeseries_testrun', 'timestamp');",
55+
reverse_sql="",
56+
),
57+
migrations.AddIndex(
58+
model_name="testrun",
59+
index=models.Index(
60+
fields=[
61+
"repo_id",
62+
"branch",
63+
"timestamp",
64+
],
65+
name="ts__repo_branch_time_i",
66+
),
67+
),
68+
migrations.AddIndex(
69+
model_name="testrun",
70+
index=models.Index(
71+
fields=["repo_id", "branch", "test_id", "timestamp"],
72+
name="ts__repo_branch_test_time_i",
73+
),
74+
),
75+
migrations.AddIndex(
76+
model_name="testrun",
77+
index=models.Index(
78+
fields=["repo_id", "test_id", "timestamp"],
79+
name="ts__repo_test_time_i",
80+
),
81+
),
82+
migrations.AddIndex(
83+
model_name="testrun",
84+
index=models.Index(
85+
fields=["repo_id", "commit_sha", "timestamp"],
86+
name="ts__repo_commit_time_i",
87+
),
88+
),
89+
migrations.RunSQL(
90+
"""
91+
CREATE OR REPLACE FUNCTION array_merge_dedup(anyarray, anyarray)
92+
RETURNS anyarray LANGUAGE sql IMMUTABLE AS $$
93+
SELECT array_agg(DISTINCT x)
94+
FROM (
95+
SELECT unnest($1) as x
96+
UNION
97+
SELECT unnest($2)
98+
) s;
99+
$$;
100+
CREATE AGGREGATE array_merge_dedup_agg(anyarray) (
101+
SFUNC = array_merge_dedup,
102+
STYPE = anyarray,
103+
INITCOND = '{}'
104+
);
105+
""",
106+
reverse_sql="""
107+
DROP AGGREGATE array_merge_dedup_agg(anyarray);
108+
DROP FUNCTION array_merge_dedup(anyarray, anyarray);
109+
""",
110+
),
111+
]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Generated by Django 4.2.16 on 2025-02-06 16:56
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
# cant create this views in a transaction
8+
atomic = False
9+
10+
dependencies = [
11+
("timeseries", "0015_testrun_model"),
12+
]
13+
14+
operations = [
15+
migrations.RunSQL(
16+
"""
17+
create materialized view timeseries_testrun_summary_1day
18+
with (timescaledb.continuous) as
19+
select
20+
repo_id,
21+
testsuite,
22+
classname,
23+
name,
24+
time_bucket(interval '1 days', timestamp) as timestamp_bin,
25+
26+
min(computed_name) as computed_name,
27+
COUNT(DISTINCT CASE WHEN outcome = 'failure' OR outcome = 'flaky_fail' THEN commit_sha ELSE NULL END) AS failing_commits,
28+
last(duration_seconds, timestamp) as last_duration_seconds,
29+
avg(duration_seconds) as avg_duration_seconds,
30+
COUNT(*) FILTER (WHERE outcome = 'pass') AS pass_count,
31+
COUNT(*) FILTER (WHERE outcome = 'failure') AS fail_count,
32+
COUNT(*) FILTER (WHERE outcome = 'skip') AS skip_count,
33+
COUNT(*) FILTER (WHERE outcome = 'flaky_fail') AS flaky_fail_count,
34+
MAX(timestamp) AS updated_at,
35+
array_merge_dedup_agg(flags) as flags
36+
from timeseries_testrun
37+
group by
38+
repo_id, testsuite, classname, name, timestamp_bin;
39+
""",
40+
reverse_sql="drop materialized view timeseries_testrun_summary_1day;",
41+
),
42+
migrations.RunSQL(
43+
"""
44+
create materialized view timeseries_testrun_branch_summary_1day
45+
with (timescaledb.continuous) as
46+
select
47+
repo_id,
48+
branch,
49+
testsuite,
50+
classname,
51+
name,
52+
time_bucket(interval '1 days', timestamp) as timestamp_bin,
53+
54+
min(computed_name) as computed_name,
55+
COUNT(DISTINCT CASE WHEN outcome = 'failure' OR outcome = 'flaky_fail' THEN commit_sha ELSE NULL END) AS failing_commits,
56+
last(duration_seconds, timestamp) as last_duration_seconds,
57+
avg(duration_seconds) as avg_duration_seconds,
58+
COUNT(*) FILTER (WHERE outcome = 'pass') AS pass_count,
59+
COUNT(*) FILTER (WHERE outcome = 'failure') AS fail_count,
60+
COUNT(*) FILTER (WHERE outcome = 'skip') AS skip_count,
61+
COUNT(*) FILTER (WHERE outcome = 'flaky_fail') AS flaky_fail_count,
62+
MAX(timestamp) AS updated_at,
63+
array_merge_dedup_agg(flags) as flags
64+
from timeseries_testrun
65+
where branch in ('main', 'master', 'develop')
66+
group by
67+
repo_id, branch, testsuite, classname, name, timestamp_bin;
68+
""",
69+
reverse_sql="drop materialized view timeseries_testrun_branch_summary_1day;",
70+
),
71+
]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Generated by Django 4.2.16 on 2025-02-06 16:57
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("timeseries", "0016_testrun_summary_1day"),
9+
]
10+
11+
operations = [
12+
migrations.RunSQL(
13+
"""
14+
select add_continuous_aggregate_policy(
15+
'timeseries_testrun_summary_1day',
16+
start_offset => '7 days',
17+
end_offset => '1 days',
18+
schedule_interval => INTERVAL '1 days'
19+
);
20+
""",
21+
reverse_sql="select remove_continuous_aggregate_policy('timeseries_testrun_summary_1day');",
22+
),
23+
migrations.RunSQL(
24+
"""
25+
select add_continuous_aggregate_policy(
26+
'timeseries_testrun_branch_summary_1day',
27+
start_offset => '7 days',
28+
end_offset => '1 days',
29+
schedule_interval => INTERVAL '1 days'
30+
);
31+
""",
32+
reverse_sql="select remove_continuous_aggregate_policy('timeseries_testrun_branch_summary_1day');",
33+
),
34+
]
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Generated by Django 4.2.16 on 2025-02-06 16:57
2+
import django.contrib.postgres.fields
3+
import django_prometheus.models
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("timeseries", "0017_testrun_cagg_policy"),
10+
]
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name="TestrunSummary",
15+
fields=[
16+
(
17+
"timestamp_bin",
18+
models.DateTimeField(primary_key=True, serialize=False),
19+
),
20+
("repo_id", models.IntegerField()),
21+
("name", models.TextField()),
22+
("classname", models.TextField()),
23+
("testsuite", models.TextField()),
24+
("computed_name", models.TextField()),
25+
("failing_commits", models.IntegerField()),
26+
("avg_duration_seconds", models.FloatField()),
27+
("last_duration_seconds", models.FloatField()),
28+
("pass_count", models.IntegerField()),
29+
("fail_count", models.IntegerField()),
30+
("skip_count", models.IntegerField()),
31+
("flaky_fail_count", models.IntegerField()),
32+
("updated_at", models.DateTimeField()),
33+
(
34+
"flags",
35+
django.contrib.postgres.fields.ArrayField(
36+
base_field=models.TextField(), null=True, size=None
37+
),
38+
),
39+
],
40+
options={
41+
"db_table": "timeseries_testrun_summary_1day",
42+
"managed": False,
43+
},
44+
bases=(
45+
django_prometheus.models.ExportModelOperationsMixin(
46+
"timeseries.testrun_continuous_aggregate"
47+
),
48+
models.Model,
49+
),
50+
),
51+
migrations.CreateModel(
52+
name="TestrunBranchSummary",
53+
fields=[
54+
(
55+
"timestamp_bin",
56+
models.DateTimeField(primary_key=True, serialize=False),
57+
),
58+
("repo_id", models.IntegerField()),
59+
("branch", models.TextField()),
60+
("name", models.TextField()),
61+
("classname", models.TextField()),
62+
("testsuite", models.TextField()),
63+
("computed_name", models.TextField()),
64+
("failing_commits", models.IntegerField()),
65+
("avg_duration_seconds", models.FloatField()),
66+
("last_duration_seconds", models.FloatField()),
67+
("pass_count", models.IntegerField()),
68+
("fail_count", models.IntegerField()),
69+
("skip_count", models.IntegerField()),
70+
("flaky_fail_count", models.IntegerField()),
71+
("updated_at", models.DateTimeField()),
72+
(
73+
"flags",
74+
django.contrib.postgres.fields.ArrayField(
75+
base_field=models.TextField(), null=True, size=None
76+
),
77+
),
78+
],
79+
options={
80+
"db_table": "timeseries_testrun_summary_1day",
81+
"managed": False,
82+
},
83+
bases=(
84+
django_prometheus.models.ExportModelOperationsMixin(
85+
"timeseries.testrun_continuous_aggregate"
86+
),
87+
models.Model,
88+
),
89+
),
90+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Generated by Django 4.2.16 on 2025-02-12 15:58
2+
3+
from django.conf import settings
4+
from django.db import migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("timeseries", "0018_testrun_summary_model"),
10+
]
11+
12+
operations = [
13+
migrations.RunSQL(
14+
"""
15+
alter materialized view timeseries_testrun_summary_1day set (timescaledb.materialized_only = true);
16+
"""
17+
)
18+
if not settings.TIMESERIES_REAL_TIME_AGGREGATES
19+
else migrations.RunSQL(
20+
"""
21+
alter materialized view timeseries_testrun_summary_1day set (timescaledb.materialized_only = false);
22+
"""
23+
),
24+
migrations.RunSQL(
25+
"""
26+
alter materialized view timeseries_testrun_branch_summary_1day set (timescaledb.materialized_only = true);
27+
"""
28+
)
29+
if not settings.TIMESERIES_REAL_TIME_AGGREGATES
30+
else migrations.RunSQL(
31+
"""
32+
alter materialized view timeseries_testrun_branch_summary_1day set (timescaledb.materialized_only = false);
33+
"""
34+
),
35+
]

0 commit comments

Comments
 (0)