Skip to content

Commit

Permalink
fix(cdn): deduplicate run-result on getItems (#5841)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Jun 11, 2021
1 parent a28bde4 commit 8d4f75e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
16 changes: 14 additions & 2 deletions engine/cdn/item/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
2 changes: 1 addition & 1 deletion engine/cdn/item_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 12 additions & 1 deletion engine/cdn/item_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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)
}

0 comments on commit 8d4f75e

Please sign in to comment.