Skip to content

Commit

Permalink
fix(api,ui): remove node run sidebar + remove data from getRuns (#5510)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Oct 19, 2020
1 parent 2a272d5 commit 4b4b725
Show file tree
Hide file tree
Showing 36 changed files with 297 additions and 1,199 deletions.
4 changes: 2 additions & 2 deletions cli/cdsctl/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ var workflowRunManualCmd = cli.Command{

func workflowRunManualRun(v cli.Values) error {
if v.GetBool("sync") && v.GetString("run-number") == "" {
return fmt.Errorf("Could not use flag --sync without flag --run-number")
return fmt.Errorf("could not use flag --sync without flag --run-number")
}

manual := sdk.WorkflowNodeRunManual{}
if strings.TrimSpace(v.GetString("data")) != "" {
data := map[string]interface{}{}
if err := json.Unmarshal([]byte(v.GetString("data")), &data); err != nil {
return fmt.Errorf("Error payload isn't a valid json")
return fmt.Errorf("error payload isn't a valid json")
}
manual.Payload = data
} else {
Expand Down
1 change: 0 additions & 1 deletion engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ func (api *API) InitRouter() {
r.Handle("/project/{permProjectKey}/push/workflows", Scope(sdk.AuthConsumerScopeProject), r.POST(api.postWorkflowPushHandler))

// Workflows run
r.Handle("/project/{permProjectKey}/runs", Scope(sdk.AuthConsumerScopeProject), r.GET(api.getWorkflowAllRunsHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/artifact/{artifactId}", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getDownloadArtifactHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getWorkflowRunsHandler), r.POSTEXECUTE(api.postWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/branch/{branch}", Scope(sdk.AuthConsumerScopeRun), r.DELETE(api.deleteWorkflowRunsBranchHandler))
Expand Down
7 changes: 4 additions & 3 deletions engine/api/purge/purge_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func ApplyRetentionPolicyOnWorkflow(ctx context.Context, store cache.Store, db *
limit := 50
offset := 0
for {
wfRuns, _, _, count, err := workflow.LoadRuns(db, wf.ProjectKey, wf.Name, offset, limit, nil)
wfRuns, _, _, count, err := workflow.LoadRunsSummaries(db, wf.ProjectKey, wf.Name, offset, limit, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func ApplyRetentionPolicyOnWorkflow(ctx context.Context, store cache.Store, db *
return nil
}

func applyRetentionPolicyOnRun(ctx context.Context, db *gorp.DbMap, wf sdk.Workflow, run sdk.WorkflowRun, branchesMap map[string]struct{}, app sdk.Application, vcsClient sdk.VCSAuthorizedClientService, opts MarkAsDeleteOptions) (bool, error) {
func applyRetentionPolicyOnRun(ctx context.Context, db *gorp.DbMap, wf sdk.Workflow, run sdk.WorkflowRunSummary, branchesMap map[string]struct{}, app sdk.Application, vcsClient sdk.VCSAuthorizedClientService, opts MarkAsDeleteOptions) (bool, error) {
if wf.ToDelete && !opts.DryRun {
if err := workflow.MarkWorkflowRunsAsDelete(db, []int64{run.ID}); err != nil {
return true, sdk.WithStack(err)
Expand Down Expand Up @@ -163,7 +163,8 @@ func applyRetentionPolicyOnRun(ctx context.Context, db *gorp.DbMap, wf sdk.Workf
return false, nil
}

func purgeComputeVariables(ctx context.Context, luaCheck *luascript.Check, run sdk.WorkflowRun, branchesMap map[string]struct{}, app sdk.Application, vcsClient sdk.VCSAuthorizedClientService) error {
func purgeComputeVariables(ctx context.Context, luaCheck *luascript.Check, run sdk.WorkflowRunSummary, branchesMap map[string]struct{}, app sdk.Application, vcsClient sdk.VCSAuthorizedClientService) error {

vars := make(map[string]string)
varsFloats := make(map[string]float64)

Expand Down
4 changes: 2 additions & 2 deletions engine/api/purge/purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ func Test_applyRetentionPolicyOnRun(t *testing.T) {
RetentionPolicy: "return run_days_before < 2",
}
now := time.Now()
run1 := sdk.WorkflowRun{
run1 := sdk.WorkflowRunSummary{
LastModified: now.Add(-49 * time.Hour),
}
keep, err := applyRetentionPolicyOnRun(context.TODO(), db.DbMap, wf, run1, nil, sdk.Application{}, nil, MarkAsDeleteOptions{DryRun: true})
require.NoError(t, err)
require.False(t, keep)

run2 := sdk.WorkflowRun{
run2 := sdk.WorkflowRunSummary{
LastModified: now.Add(-47 * time.Hour),
}
keep, err = applyRetentionPolicyOnRun(context.TODO(), db.DbMap, wf, run2, nil, sdk.Application{}, nil, MarkAsDeleteOptions{DryRun: true})
Expand Down
4 changes: 2 additions & 2 deletions engine/api/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (api *API) getApplicationOverviewHandler() service.Handler {

appOverview := sdk.ApplicationOverview{
Graphs: make([]sdk.ApplicationOverviewGraph, 0, 3),
History: make(map[string][]sdk.WorkflowRun, len(app.Usage.Workflows)),
History: make(map[string][]sdk.WorkflowRunSummary, len(app.Usage.Workflows)),
}

// Get metrics
Expand Down Expand Up @@ -114,7 +114,7 @@ func (api *API) getApplicationOverviewHandler() service.Handler {
tagFilter := make(map[string]string, 1)
tagFilter["git.branch"] = defaultBranch.DisplayID
for _, w := range app.Usage.Workflows {
runs, _, _, _, err := workflow.LoadRuns(tx, projectKey, w.Name, 0, 5, tagFilter)
runs, _, _, _, err := workflow.LoadRunsSummaries(tx, projectKey, w.Name, 0, 5, tagFilter)
if err != nil {
return sdk.WrapError(err, "unable to load runs")
}
Expand Down
2 changes: 1 addition & 1 deletion engine/api/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (api *API) postWorkflowRetentionPolicyDryRun() service.Handler {
wf.RetentionPolicy = request.RetentionPolicy

// Get the number of runs to analyze
_, _, _, count, err := workflow.LoadRuns(api.mustDB(), wf.ProjectKey, wf.Name, 0, 1, nil)
_, _, _, count, err := workflow.LoadRunsSummaries(api.mustDB(), wf.ProjectKey, wf.Name, 0, 1, nil)
if err != nil {
return err
}
Expand Down
99 changes: 45 additions & 54 deletions engine/api/workflow/dao_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,11 @@ func loadRuns(db gorp.SqlExecutor, query string, args ...interface{}) ([]sdk.Wor
wruns := make([]sdk.WorkflowRun, len(runs))
for i := range runs {
wr := sdk.WorkflowRun(runs[i])
if err := loadRunTags(db, &wr); err != nil {
return nil, sdk.WrapError(err, "Unable to load tags")
tags, err := loadRunTags(db, wr.ID)
if err != nil {
return nil, err
}
wr.Tags = tags
wruns[i] = wr
}
return wruns, nil
Expand All @@ -283,63 +285,44 @@ func LoadRunsIDsToDelete(db gorp.SqlExecutor, offset int64, limit int64) ([]int6
return ids, offset, limit, count, nil
}

//LoadRuns loads all runs
//LoadRunsSummaries loads a short version of workflow runs
//It returns runs, offset, limit count and an error
func LoadRuns(db gorp.SqlExecutor, projectkey, workflowname string, offset, limit int, tagFilter map[string]string) ([]sdk.WorkflowRun, int, int, int, error) {
var args = []interface{}{projectkey}
func LoadRunsSummaries(db gorp.SqlExecutor, projectkey, workflowname string, offset, limit int, tagFilter map[string]string) ([]sdk.WorkflowRunSummary, int, int, int, error) {
queryCount := `select count(workflow_run.id)
from workflow_run
join project on workflow_run.project_id = project.id
join workflow on workflow_run.workflow_id = workflow.id
where project.projectkey = $1 AND workflow_run.to_delete = false`

if workflowname != "" {
args = []interface{}{projectkey, workflowname}
queryCount = `select count(workflow_run.id)
from workflow_run
join project on workflow_run.project_id = project.id
join workflow on workflow_run.workflow_id = workflow.id
where project.projectkey = $1
and workflow.name = $2
AND workflow_run.to_delete = false`
}

count, errc := db.SelectInt(queryCount, args...)
count, errc := db.SelectInt(queryCount, projectkey, workflowname)
if errc != nil {
return nil, 0, 0, 0, sdk.WrapError(errc, "Unable to load runs")
return nil, 0, 0, 0, sdk.WrapError(errc, "unable to load short runs")
}
if count == 0 {
return nil, 0, 0, 0, nil
}

args = []interface{}{projectkey, limit, offset}
query := fmt.Sprintf(`select %s
from workflow_run
join project on workflow_run.project_id = project.id
join workflow on workflow_run.workflow_id = workflow.id
where project.projectkey = $1 AND workflow_run.to_delete = false
order by workflow_run.start desc
limit $2 offset $3`, wfRunfields)

if workflowname != "" {
args = []interface{}{projectkey, workflowname, limit, offset}
query = fmt.Sprintf(`select %s
from workflow_run
join project on workflow_run.project_id = project.id
join workflow on workflow_run.workflow_id = workflow.id
where project.projectkey = $1
and workflow.name = $2
AND workflow_run.to_delete = false
order by workflow_run.start desc
limit $3 offset $4`, wfRunfields)
}
selectedColumn := "wr.id, wr.num, wr.status, wr.start, wr.last_modified, wr.last_sub_num, wr.last_execution, wr.version, wr.to_craft_opts"
args := []interface{}{projectkey, workflowname, limit, offset}
query := fmt.Sprintf(`
SELECT %s
FROM workflow_run wr
JOIN project ON wr.project_id = project.id
JOIN workflow ON wr.workflow_id = workflow.id
WHERE project.projectkey = $1
AND workflow.name = $2
AND wr.to_delete = false
ORDER BY wr.start desc
LIMIT $3 OFFSET $4`, selectedColumn)

if len(tagFilter) > 0 {
// Posgres operator: '<@' means 'is contained by' eg. 'ARRAY[2,7] <@ ARRAY[1,7,4,2,6]' ==> returns true
query = fmt.Sprintf(`select %s
from workflow_run
join project on workflow_run.project_id = project.id
join workflow on workflow_run.workflow_id = workflow.id
from workflow_run wr
join project on wr.project_id = project.id
join workflow on wr.workflow_id = workflow.id
join (
select workflow_run_id, string_agg(all_tags, ',') as tags
from (
Expand All @@ -348,13 +331,13 @@ func LoadRuns(db gorp.SqlExecutor, projectkey, workflowname string, offset, limi
order by tag
) as all_wr_tags
group by workflow_run_id
) as tags on workflow_run.id = tags.workflow_run_id
) as tags on wr.id = tags.workflow_run_id
where project.projectkey = $1
and workflow.name = $2
AND workflow_run.to_delete = false
AND wr.to_delete = false
and string_to_array($5, ',') <@ string_to_array(tags.tags, ',')
order by workflow_run.start desc
limit $3 offset $4`, wfRunfields)
order by wr.start desc
limit $3 offset $4`, selectedColumn)

var tags []string
for k, v := range tagFilter {
Expand All @@ -366,12 +349,20 @@ func LoadRuns(db gorp.SqlExecutor, projectkey, workflowname string, offset, limi
args = append(args, strings.Join(tags, ","))
}

wruns, err := loadRuns(db, query, args...)
var shortRuns []sdk.WorkflowRunSummary
_, err := db.Select(&shortRuns, query, args...)
if err != nil {
return nil, 0, 0, 0, err
return nil, 0, 0, 0, sdk.WithStack(err)
}

return wruns, offset, limit, int(count), nil
for i := range shortRuns {
run := &shortRuns[i]
tags, err := loadRunTags(db, run.ID)
if err != nil {
return nil, 0, 0, 0, err
}
run.Tags = tags
}
return shortRuns, offset, limit, int(count), nil
}

// LoadRunsIDByTag load workflow run ids for given tag and his value
Expand Down Expand Up @@ -412,17 +403,17 @@ func LoadRunsIDByTag(db gorp.SqlExecutor, projectKey, workflowName, tag, tagValu
return ids, nil
}

func loadRunTags(db gorp.SqlExecutor, run *sdk.WorkflowRun) error {
func loadRunTags(db gorp.SqlExecutor, runID int64) ([]sdk.WorkflowRunTag, error) {
dbRunTags := []RunTag{}
if _, err := db.Select(&dbRunTags, "SELECT * from workflow_run_tag WHERE workflow_run_id=$1", run.ID); err != nil {
return sdk.WithStack(err)
if _, err := db.Select(&dbRunTags, "SELECT * from workflow_run_tag WHERE workflow_run_id=$1", runID); err != nil {
return nil, sdk.WithStack(err)
}

run.Tags = make([]sdk.WorkflowRunTag, len(dbRunTags))
tags := make([]sdk.WorkflowRunTag, 0, len(dbRunTags))
for i := range dbRunTags {
run.Tags[i] = sdk.WorkflowRunTag(dbRunTags[i])
tags = append(tags, sdk.WorkflowRunTag(dbRunTags[i]))
}
return nil
return tags, nil
}

func loadRun(db gorp.SqlExecutor, loadOpts LoadRunOptions, query string, args ...interface{}) (*sdk.WorkflowRun, error) {
Expand Down
30 changes: 6 additions & 24 deletions engine/api/workflow/dao_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ vcs_ssh_key: proj-blabla
errP := workflow.PurgeWorkflowRun(context.TODO(), db, *w1)
test.NoError(t, errP)

_, _, _, count, errRuns := workflow.LoadRuns(db, proj.Key, w1.Name, 0, 10, nil)
_, _, _, count, errRuns := workflow.LoadRunsSummaries(db, proj.Key, w1.Name, 0, 10, nil)
test.NoError(t, errRuns)
test.Equal(t, 2, count, "Number of workflow runs isn't correct")
}
Expand Down Expand Up @@ -338,18 +338,9 @@ func TestPurgeWorkflowRunWithRunningStatus(t *testing.T) {
errP := workflow.PurgeWorkflowRun(context.TODO(), db, *w1)
test.NoError(t, errP)

wruns, _, _, count, errRuns := workflow.LoadRuns(db, proj.Key, w1.Name, 0, 10, nil)
_, _, _, count, errRuns := workflow.LoadRunsSummaries(db, proj.Key, w1.Name, 0, 10, nil)
test.NoError(t, errRuns)
test.Equal(t, 5, count, "Number of workflow runs isn't correct")

toDeleteNb := 0
for _, wfRun := range wruns {
if wfRun.ToDelete {
toDeleteNb++
}
}

test.Equal(t, 0, toDeleteNb, "Number of workflow runs to be purged isn't correct")
}

func TestPurgeWorkflowRunWithOneSuccessWorkflowRun(t *testing.T) {
Expand Down Expand Up @@ -541,7 +532,7 @@ vcs_ssh_key: proj-blabla
errP := workflow.PurgeWorkflowRun(context.TODO(), db, *w1)
test.NoError(t, errP)

wruns, _, _, count, errRuns := workflow.LoadRuns(db, proj.Key, w1.Name, 0, 10, nil)
wruns, _, _, count, errRuns := workflow.LoadRunsSummaries(db, proj.Key, w1.Name, 0, 10, nil)
test.NoError(t, errRuns)
test.Equal(t, 3, count, "Number of workflow runs isn't correct")
wfInSuccess := false
Expand Down Expand Up @@ -721,7 +712,7 @@ vcs_ssh_key: proj-blabla
n := workflow.CountWorkflowRunsMarkToDelete(context.TODO(), db, nil)
assert.True(t, n >= 3, "At least 3 runs must be mark to delete")

_, _, _, count, errRuns := workflow.LoadRuns(db, proj.Key, w1.Name, 0, 10, nil)
_, _, _, count, errRuns := workflow.LoadRunsSummaries(db, proj.Key, w1.Name, 0, 10, nil)
test.NoError(t, errRuns)
test.Equal(t, 2, count, "Number of workflow runs isn't correct")
}
Expand Down Expand Up @@ -807,7 +798,7 @@ func TestPurgeWorkflowRunWithoutTags(t *testing.T) {
errP := workflow.PurgeWorkflowRun(context.TODO(), db, *w1)
test.NoError(t, errP)

_, _, _, count, errRuns := workflow.LoadRuns(db, proj.Key, w1.Name, 0, 10, nil)
_, _, _, count, errRuns := workflow.LoadRunsSummaries(db, proj.Key, w1.Name, 0, 10, nil)
test.NoError(t, errRuns)
test.Equal(t, 3, count, "Number of workflow runs isn't correct")
}
Expand Down Expand Up @@ -893,18 +884,9 @@ func TestPurgeWorkflowRunWithoutTagsBiggerHistoryLength(t *testing.T) {
errP := workflow.PurgeWorkflowRun(context.TODO(), db, *w1)
test.NoError(t, errP)

wruns, _, _, count, errRuns := workflow.LoadRuns(db, proj.Key, w1.Name, 0, 10, nil)
_, _, _, count, errRuns := workflow.LoadRunsSummaries(db, proj.Key, w1.Name, 0, 10, nil)
test.NoError(t, errRuns)
test.Equal(t, 10, count, "Number of workflow runs isn't correct")

toDeleteNb := 0
for _, wfRun := range wruns {
if wfRun.ToDelete {
toDeleteNb++
}
}

test.Equal(t, 0, toDeleteNb, "Number of workflow runs to be purged isn't correct (because it should keep at least one in success)")
}

func TestLoadRunsIDsToDelete(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion engine/api/workflow/run_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestManualRun1(t *testing.T) {
require.NoError(t, errS)

//TestLoadRuns
runs, offset, limit, count, err := workflow.LoadRuns(db, proj.Key, w1.Name, 0, 50, nil)
runs, offset, limit, count, err := workflow.LoadRunsSummaries(db, proj.Key, w1.Name, 0, 50, nil)
require.NoError(t, err)
assert.Equal(t, 0, offset)
assert.Equal(t, 50, limit)
Expand Down
Loading

0 comments on commit 4b4b725

Please sign in to comment.