Skip to content

Commit

Permalink
fix: handle same max_created_at scenario which return multiple job hi…
Browse files Browse the repository at this point in the history
…stories with status
  • Loading branch information
yashmehrotra authored and moshloop committed Aug 1, 2024
1 parent b68d001 commit 40adb55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
4 changes: 4 additions & 0 deletions db/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 17 additions & 9 deletions views/015_job_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
LEFT JOIN agents ON job_history.agent_id = agents.id;

0 comments on commit 40adb55

Please sign in to comment.