From 40adb558052d3f9ccca0f01e6641cea4a484ce30 Mon Sep 17 00:00:00 2001 From: Yash Mehrotra Date: Thu, 1 Aug 2024 10:00:26 +0530 Subject: [PATCH] fix: handle same max_created_at scenario which return multiple job histories with status --- db/utils.go | 4 ++++ views/015_job_history.sql | 26 +++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/db/utils.go b/db/utils.go index 3f5e850a..b3329302 100644 --- a/db/utils.go +++ b/db/utils.go @@ -10,6 +10,10 @@ import ( ) func ErrorDetails(err error) error { + if err == nil { + return nil + } + var pgErr *pgconn.PgError if errors.As(err, &pgErr) { var errString []string diff --git a/views/015_job_history.sql b/views/015_job_history.sql index 69dd2f3a..5a16aa98 100644 --- a/views/015_job_history.sql +++ b/views/015_job_history.sql @@ -5,21 +5,29 @@ DROP VIEW IF EXISTS integrations_with_status; CREATE OR REPLACE VIEW job_history_latest_status AS WITH - latest_job_history AS ( + max_created_at_job_history AS ( SELECT - job_history.resource_id, - MAX(job_history.created_at) AS max_created_at + resource_id, + MAX(created_at) AS max_created_at FROM job_history GROUP BY - job_history.resource_id + resource_id + ), + latest_job_history AS ( + SELECT + jh.*, + ROW_NUMBER() OVER (PARTITION BY jh.resource_id ORDER BY jh.created_at DESC, jh.id DESC) AS rn + FROM + job_history jh + JOIN max_created_at_job_history mc ON jh.resource_id = mc.resource_id AND jh.created_at = mc.max_created_at ) SELECT - job_history.* + latest_job_history.* FROM - job_history - JOIN latest_job_history ON job_history.resource_id = latest_job_history.resource_id - AND job_history.created_at = latest_job_history.max_created_at; + latest_job_history +WHERE + rn = 1; -- Topologies with job status DROP VIEW IF EXISTS topologies_with_status; @@ -341,4 +349,4 @@ LEFT JOIN components ON job_history.resource_id = components.id::TEXT AND job_hi LEFT JOIN config_scrapers ON job_history.resource_id = config_scrapers.id::TEXT AND job_history.resource_type = 'config_scraper' LEFT JOIN canaries ON job_history.resource_id = canaries.id::TEXT AND job_history.resource_type = 'canary' LEFT JOIN topologies ON job_history.resource_id = topologies.id::TEXT AND job_history.resource_type = 'topology' -LEFT JOIN agents ON job_history.agent_id = agents.id; \ No newline at end of file +LEFT JOIN agents ON job_history.agent_id = agents.id;