From 37b84397505a617a598ca5903ad63b15aef7b0ce Mon Sep 17 00:00:00 2001 From: Steven Guiheux Date: Thu, 10 Jun 2021 10:47:20 +0200 Subject: [PATCH] fix(cdn): deduplicate run-result on getItems --- engine/cdn/item/dao.go | 16 ++++++++++++++-- engine/cdn/item_handler.go | 2 +- engine/cdn/item_handler_test.go | 13 ++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/engine/cdn/item/dao.go b/engine/cdn/item/dao.go index e0c4ef81b7..ad50514ddc 100644 --- a/engine/cdn/item/dao.go +++ b/engine/cdn/item/dao.go @@ -319,8 +319,20 @@ func LoadOldItemByStatusAndDuration(ctx context.Context, m *gorpmapper.Mapper, d return getItems(ctx, m, db, query, opts...) } -func LoadByRunID(ctx context.Context, m *gorpmapper.Mapper, db gorp.SqlExecutor, itemType sdk.CDNItemType, runID string) ([]sdk.CDNItem, error) { - query := gorpmapper.NewQuery("SELECT * FROM item WHERE api_ref->>'run_id'::text = $1 AND type = $2 AND to_delete = false").Args(runID, itemType) +func LoadRunResultByRunID(ctx context.Context, m *gorpmapper.Mapper, db gorp.SqlExecutor, runID string) ([]sdk.CDNItem, error) { + query := gorpmapper.NewQuery(` + WITH allResults AS ( + SELECT api_ref->>'artifact_name' as name, api_ref->>'run_job_id' as run_job_id, id + FROM item + WHERE api_ref->>'run_id'::text = $1 AND type = $2 AND to_delete = false + ), + deduplication AS ( + SELECT distinct on (name) * + FROM allResults + ORDER BY name, run_job_id DESC + ) + SELECT * FROM item WHERE id IN (SELECT id FROM deduplication) + `).Args(runID, sdk.CDNTypeItemRunResult) return getItems(ctx, m, db, query) } diff --git a/engine/cdn/item_handler.go b/engine/cdn/item_handler.go index 3502663250..c20da2d979 100644 --- a/engine/cdn/item_handler.go +++ b/engine/cdn/item_handler.go @@ -251,7 +251,7 @@ func (s *Service) getArtifacts(ctx context.Context, r *http.Request, w http.Resp if runID == "" { return sdk.WrapError(sdk.ErrWrongRequest, "invalid workflow run") } - items, err := item.LoadByRunID(ctx, s.Mapper, s.mustDBWithCtx(ctx), sdk.CDNTypeItemRunResult, runID) + items, err := item.LoadRunResultByRunID(ctx, s.Mapper, s.mustDBWithCtx(ctx), runID) if err != nil { return err } diff --git a/engine/cdn/item_handler_test.go b/engine/cdn/item_handler_test.go index b01e1159fc..dd5882f0e8 100644 --- a/engine/cdn/item_handler_test.go +++ b/engine/cdn/item_handler_test.go @@ -378,6 +378,17 @@ func TestGetItemsArtefactHandler(t *testing.T) { item2.APIRefHash = refhashLog require.NoError(t, item.Insert(context.Background(), s.Mapper, db, &item2)) + workerSignature.JobID += 1 + item3 := sdk.CDNItem{ + Type: sdk.CDNTypeItemRunResult, + Status: sdk.CDNStatusItemCompleted, + APIRef: sdk.NewCDNRunResultApiRef(workerSignature), + } + refhash, err = item3.APIRef.ToHash() + require.NoError(t, err) + item3.APIRefHash = refhash + require.NoError(t, item.Insert(context.Background(), s.Mapper, db, &item3)) + vars := map[string]string{ "type": string(sdk.CDNTypeItemRunResult), } @@ -397,5 +408,5 @@ func TestGetItemsArtefactHandler(t *testing.T) { require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &results)) require.Equal(t, 1, len(results)) - require.Equal(t, item1.ID, results[0].ID) + require.Equal(t, item3.ID, results[0].ID) }