Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdn): deduplicate run-result on getItems #5841

Merged
merged 1 commit into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}