From c168bdcaf6b3bf6c8928479b0606685a06636aca Mon Sep 17 00:00:00 2001 From: Nurseit Date: Sun, 14 Dec 2025 02:05:34 +0600 Subject: [PATCH] Store duration in database --- ...4ed7cc57f40071bad913011db294e90d6859b.json | 8 ++++++++ ...f119a7835ab5fd09326265a4a1403d0d6ad19.json | 8 ++++++++ ...14433d1228427b35742760521487a9bc0be51.json | 8 ++++++++ ...615433b4a61c43f26f2ae39e68d02d609bf90.json | 8 ++++++++ ...3d945e77bbdcf50e3fcfba029f24ce7481618.json | 8 ++++++++ ...53aebbc461fed17903556e9a6dcc18faef200.json | 15 +++++++++++++++ ...0ddeb83e3a97562c6330fe72375bfc3cadfd3.json | 8 ++++++++ ...72b8721d72dead28356fbbc69f84029d08132.json | 8 ++++++++ ...51211185514_add_duration_to_build.down.sql | 2 ++ ...0251211185514_add_duration_to_build.up.sql | 2 ++ src/bors/handlers/workflow.rs | 2 ++ src/database/client.rs | 14 ++++++++++++-- src/database/operations.rs | 19 +++++++++++++++++++ .../20251211185514_add_duration_to_build.sql | 17 +++++++++++++++++ 14 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 .sqlx/query-98bb13a50aa2b9a07086ee8065253aebbc461fed17903556e9a6dcc18faef200.json create mode 100644 migrations/20251211185514_add_duration_to_build.down.sql create mode 100644 migrations/20251211185514_add_duration_to_build.up.sql create mode 100644 tests/data/migrations/20251211185514_add_duration_to_build.sql diff --git a/.sqlx/query-3107d965f4602e40a60f856dda74ed7cc57f40071bad913011db294e90d6859b.json b/.sqlx/query-3107d965f4602e40a60f856dda74ed7cc57f40071bad913011db294e90d6859b.json index bae4c013..060b5308 100644 --- a/.sqlx/query-3107d965f4602e40a60f856dda74ed7cc57f40071bad913011db294e90d6859b.json +++ b/.sqlx/query-3107d965f4602e40a60f856dda74ed7cc57f40071bad913011db294e90d6859b.json @@ -112,6 +112,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } @@ -157,6 +161,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } diff --git a/.sqlx/query-367bf2de86a8a92b56b2be296cef119a7835ab5fd09326265a4a1403d0d6ad19.json b/.sqlx/query-367bf2de86a8a92b56b2be296cef119a7835ab5fd09326265a4a1403d0d6ad19.json index 04c9f32f..fef33f1e 100644 --- a/.sqlx/query-367bf2de86a8a92b56b2be296cef119a7835ab5fd09326265a4a1403d0d6ad19.json +++ b/.sqlx/query-367bf2de86a8a92b56b2be296cef119a7835ab5fd09326265a4a1403d0d6ad19.json @@ -112,6 +112,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } @@ -157,6 +161,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } diff --git a/.sqlx/query-483efa3070e78c109d493533ca114433d1228427b35742760521487a9bc0be51.json b/.sqlx/query-483efa3070e78c109d493533ca114433d1228427b35742760521487a9bc0be51.json index 978ef817..d1e68eeb 100644 --- a/.sqlx/query-483efa3070e78c109d493533ca114433d1228427b35742760521487a9bc0be51.json +++ b/.sqlx/query-483efa3070e78c109d493533ca114433d1228427b35742760521487a9bc0be51.json @@ -112,6 +112,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } @@ -157,6 +161,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } diff --git a/.sqlx/query-5c3c81357e7329a9db4b2bf05c2615433b4a61c43f26f2ae39e68d02d609bf90.json b/.sqlx/query-5c3c81357e7329a9db4b2bf05c2615433b4a61c43f26f2ae39e68d02d609bf90.json index 5c20b49e..fbe41f85 100644 --- a/.sqlx/query-5c3c81357e7329a9db4b2bf05c2615433b4a61c43f26f2ae39e68d02d609bf90.json +++ b/.sqlx/query-5c3c81357e7329a9db4b2bf05c2615433b4a61c43f26f2ae39e68d02d609bf90.json @@ -112,6 +112,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } @@ -157,6 +161,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } diff --git a/.sqlx/query-62763d313a8278147d0f6fea3dd3d945e77bbdcf50e3fcfba029f24ce7481618.json b/.sqlx/query-62763d313a8278147d0f6fea3dd3d945e77bbdcf50e3fcfba029f24ce7481618.json index 285dbd46..94de6712 100644 --- a/.sqlx/query-62763d313a8278147d0f6fea3dd3d945e77bbdcf50e3fcfba029f24ce7481618.json +++ b/.sqlx/query-62763d313a8278147d0f6fea3dd3d945e77bbdcf50e3fcfba029f24ce7481618.json @@ -112,6 +112,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } @@ -157,6 +161,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } diff --git a/.sqlx/query-98bb13a50aa2b9a07086ee8065253aebbc461fed17903556e9a6dcc18faef200.json b/.sqlx/query-98bb13a50aa2b9a07086ee8065253aebbc461fed17903556e9a6dcc18faef200.json new file mode 100644 index 00000000..5db9d0ba --- /dev/null +++ b/.sqlx/query-98bb13a50aa2b9a07086ee8065253aebbc461fed17903556e9a6dcc18faef200.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE build SET duration = $1 WHERE id = $2", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Interval", + "Int4" + ] + }, + "nullable": [] + }, + "hash": "98bb13a50aa2b9a07086ee8065253aebbc461fed17903556e9a6dcc18faef200" +} diff --git a/.sqlx/query-a146064228559bca97e795319590ddeb83e3a97562c6330fe72375bfc3cadfd3.json b/.sqlx/query-a146064228559bca97e795319590ddeb83e3a97562c6330fe72375bfc3cadfd3.json index 0c5105d7..5422abe0 100644 --- a/.sqlx/query-a146064228559bca97e795319590ddeb83e3a97562c6330fe72375bfc3cadfd3.json +++ b/.sqlx/query-a146064228559bca97e795319590ddeb83e3a97562c6330fe72375bfc3cadfd3.json @@ -112,6 +112,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } @@ -157,6 +161,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } diff --git a/.sqlx/query-d16ca5631fcdd4216b1bf3a7b1672b8721d72dead28356fbbc69f84029d08132.json b/.sqlx/query-d16ca5631fcdd4216b1bf3a7b1672b8721d72dead28356fbbc69f84029d08132.json index 34cc35a1..3aeb99bb 100644 --- a/.sqlx/query-d16ca5631fcdd4216b1bf3a7b1672b8721d72dead28356fbbc69f84029d08132.json +++ b/.sqlx/query-d16ca5631fcdd4216b1bf3a7b1672b8721d72dead28356fbbc69f84029d08132.json @@ -112,6 +112,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } @@ -157,6 +161,10 @@ [ "check_run_id", "Int8" + ], + [ + "duration", + "Interval" ] ] } diff --git a/migrations/20251211185514_add_duration_to_build.down.sql b/migrations/20251211185514_add_duration_to_build.down.sql new file mode 100644 index 00000000..f9219dcb --- /dev/null +++ b/migrations/20251211185514_add_duration_to_build.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +ALTER TABLE build DROP COLUMN duration; diff --git a/migrations/20251211185514_add_duration_to_build.up.sql b/migrations/20251211185514_add_duration_to_build.up.sql new file mode 100644 index 00000000..7c1860f3 --- /dev/null +++ b/migrations/20251211185514_add_duration_to_build.up.sql @@ -0,0 +1,2 @@ +-- Add up migration script here +ALTER TABLE build ADD COLUMN duration INTERVAL; diff --git a/src/bors/handlers/workflow.rs b/src/bors/handlers/workflow.rs index 4559518c..538c632c 100644 --- a/src/bors/handlers/workflow.rs +++ b/src/bors/handlers/workflow.rs @@ -269,6 +269,8 @@ async fn maybe_complete_build( }; db.update_build_status(&build, status).await?; + db.update_build_duration(&build, payload.running_time) + .await?; if let Some(trigger) = trigger { handle_label_trigger(repo, pr_num, trigger).await?; } diff --git a/src/database/client.rs b/src/database/client.rs index 4ab9e768..8026824b 100644 --- a/src/database/client.rs +++ b/src/database/client.rs @@ -1,3 +1,4 @@ +use chrono::Duration; use sqlx::PgPool; use crate::bors::comment::CommentTag; @@ -18,8 +19,9 @@ use super::operations::{ get_workflow_urls_for_build, get_workflows_for_build, insert_repo_if_not_exists, record_tagged_bot_comment, set_pr_assignees, set_pr_mergeability_state, set_pr_priority, set_pr_rollup, set_pr_status, unapprove_pull_request, undelegate_pull_request, - update_build_check_run_id, update_build_status, update_mergeable_states_by_base_branch, - update_pr_try_build_id, update_workflow_status, upsert_pull_request, upsert_repository, + update_build_check_run_id, update_build_duration, update_build_status, + update_mergeable_states_by_base_branch, update_pr_try_build_id, update_workflow_status, + upsert_pull_request, upsert_repository, }; use super::{ApprovalInfo, DelegatedPermission, MergeableState, RunId, UpsertPullRequestParams}; @@ -233,6 +235,14 @@ impl PgDbClient { update_build_status(&self.pool, build.id, status).await } + pub async fn update_build_duration( + &self, + build: &BuildModel, + duration: Option, + ) -> anyhow::Result<()> { + update_build_duration(&self.pool, build.id, duration).await + } + pub async fn update_build_check_run_id( &self, build_id: i32, diff --git a/src/database/operations.rs b/src/database/operations.rs index 94949fdb..667145be 100644 --- a/src/database/operations.rs +++ b/src/database/operations.rs @@ -1,4 +1,5 @@ use chrono::DateTime; +use chrono::Duration; use chrono::Utc; use sqlx::postgres::PgExecutor; @@ -631,6 +632,24 @@ pub(crate) async fn update_build_status( .await } +pub(crate) async fn update_build_duration( + executor: impl PgExecutor<'_>, + build_id: i32, + duration: Option, +) -> anyhow::Result<()> { + measure_db_query("update_build_duration", || async { + sqlx::query!( + "UPDATE build SET duration = $1 WHERE id = $2", + duration as _, + build_id + ) + .execute(executor) + .await?; + Ok(()) + }) + .await +} + pub(crate) async fn create_workflow( executor: impl PgExecutor<'_>, build_id: i32, diff --git a/tests/data/migrations/20251211185514_add_duration_to_build.sql b/tests/data/migrations/20251211185514_add_duration_to_build.sql new file mode 100644 index 00000000..1b1a90b2 --- /dev/null +++ b/tests/data/migrations/20251211185514_add_duration_to_build.sql @@ -0,0 +1,17 @@ +UPDATE build +SET + duration = '3h 8m 37s' +WHERE + id = 1; + +UPDATE build +SET + duration = '3h 11m 36s' +WHERE + id = 2; + +UPDATE build +SET + duration = '3h 12m 15s' +WHERE + id = 3;