Skip to main content

stormchaser_api/db/
reports.rs

1use crate::{TestReportSummary, TestSummaryResponse};
2use sqlx::PgPool;
3use stormchaser_model::RunId;
4use stormchaser_model::TestCase;
5use stormchaser_model::TestReportId;
6
7/// Retrieves test reports for a specific workflow run.
8/// List run test reports.
9pub async fn list_run_test_reports(
10    pool: &PgPool,
11    run_id: RunId,
12) -> Result<Vec<TestReportSummary>, sqlx::Error> {
13    sqlx::query_as(
14        r#"
15            WITH combined_reports AS (
16                SELECT id, run_id, report_name, file_name, format, checksum, created_at, backend_id, remote_path FROM step_test_reports
17                UNION ALL
18                SELECT id, run_id, report_name, file_name, format, checksum, created_at, backend_id, remote_path FROM archived_step_test_reports
19            )
20            SELECT id, report_name, file_name, format, checksum, created_at, backend_id, remote_path FROM combined_reports WHERE run_id = $1 ORDER BY created_at ASC
21            "#,
22    )
23    .bind(run_id)
24    .fetch_all(pool)
25    .await
26}
27
28/// Retrieves test summaries for a specific workflow run.
29/// List run test summaries.
30pub async fn list_run_test_summaries(
31    pool: &PgPool,
32    run_id: RunId,
33) -> Result<Vec<TestSummaryResponse>, sqlx::Error> {
34    sqlx::query_as(
35        r#"
36            WITH combined_summaries AS (
37                SELECT id, run_id, step_instance_id, report_name, total_tests, passed, failed, skipped, errors, duration_ms, created_at FROM step_test_summaries
38                UNION ALL
39                SELECT id, run_id, step_instance_id, report_name, total_tests, passed, failed, skipped, errors, duration_ms, created_at FROM archived_step_test_summaries
40            )
41            SELECT id, run_id, step_instance_id, report_name, total_tests, passed, failed, skipped, errors, duration_ms, created_at FROM combined_summaries WHERE run_id = $1 ORDER BY created_at ASC
42            "#,
43    )
44    .bind(run_id)
45    .fetch_all(pool)
46    .await
47}
48
49/// Retrieves individual test cases for a workflow run.
50/// List run test cases.
51pub async fn list_run_test_cases(
52    pool: &PgPool,
53    run_id: RunId,
54) -> Result<Vec<TestCase>, sqlx::Error> {
55    sqlx::query_as(
56        r#"
57            WITH combined_cases AS (
58                SELECT id, run_id, step_instance_id, report_name, test_suite, test_case, status::test_case_status as "status", duration_ms, message, created_at FROM step_test_cases
59                UNION ALL
60                SELECT id, run_id, step_instance_id, report_name, test_suite, test_case, status::test_case_status as "status", duration_ms, message, created_at FROM archived_step_test_cases
61            )
62            SELECT * FROM combined_cases WHERE run_id = $1 ORDER BY created_at ASC
63            "#,
64    )
65    .bind(run_id)
66    .fetch_all(pool)
67    .await
68}
69
70/// Retrieves a test report by ID.
71/// Get test report.
72pub async fn get_test_report(
73    pool: &PgPool,
74    report_id: TestReportId,
75) -> Result<Option<String>, sqlx::Error> {
76    sqlx::query_scalar(
77        r#"
78            WITH combined_reports AS (
79                SELECT id, content FROM step_test_reports
80                UNION ALL
81                SELECT id, content FROM archived_step_test_reports
82            )
83            SELECT content FROM combined_reports WHERE id = $1
84            "#,
85    )
86    .bind(report_id)
87    .fetch_optional(pool)
88    .await
89}