Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: optimize pipeline composition by batching task check runs (#4438)

Danny Xub0caae89612a
diff --git a/backend/store/pipeline.go b/backend/store/pipeline.go
index d4c808ca93..9ea8918d98 100644
--- a/backend/store/pipeline.go
+++ b/backend/store/pipeline.go
@@ -34,10 +34,96 @@ func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage)
Name: pipeline.Name,
}
- tasks, err := s.FindTask(ctx, &api.TaskFind{PipelineID: &pipeline.ID})
+ tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &pipeline.ID})
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to find Task list for pipeline %v", pipeline.ID)
+ }
+ taskRuns, err := s.listTaskRun(ctx, &TaskRunFind{PipelineID: &pipeline.ID})
+ if err != nil {
+ return nil, err
+ }
+ taskCheckRuns, err := s.ListTaskCheckRuns(ctx, &TaskCheckRunFind{PipelineID: &pipeline.ID})
+ if err != nil {
+ return nil, err
+ }
+ dags, err := s.ListTaskDags(ctx, &TaskDAGFind{PipelineID: &pipeline.ID})
if err != nil {
return nil, err
}
+ var composedTasks []*api.Task
+ for _, task := range tasks {
+ composedTask := task.toTask()
+ creator, err := s.GetPrincipalByID(ctx, task.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ composedTask.Creator = creator
+ updater, err := s.GetPrincipalByID(ctx, task.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ composedTask.Updater = updater
+
+ for _, taskRun := range taskRuns {
+ if taskRun.TaskID == task.ID {
+ composedTaskRun := taskRun.toTaskRun()
+ creator, err := s.GetPrincipalByID(ctx, composedTaskRun.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ composedTaskRun.Creator = creator
+ updater, err := s.GetPrincipalByID(ctx, composedTaskRun.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ composedTaskRun.Updater = updater
+ composedTask.TaskRunList = append(composedTask.TaskRunList, composedTaskRun)
+ }
+ }
+ for _, taskCheckRun := range taskCheckRuns {
+ if taskCheckRun.TaskID == task.ID {
+ taskCheckRun := taskCheckRun.toTaskCheckRun()
+ creator, err := s.GetPrincipalByID(ctx, taskCheckRun.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ taskCheckRun.Creator = creator
+ updater, err := s.GetPrincipalByID(ctx, taskCheckRun.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ taskCheckRun.Updater = updater
+ composedTask.TaskCheckRunList = append(composedTask.TaskCheckRunList, taskCheckRun)
+ }
+ }
+
+ for _, dag := range dags {
+ if dag.ToTaskID == task.ID {
+ composedTask.BlockedBy = append(composedTask.BlockedBy, fmt.Sprintf("%d", dag.FromTaskID))
+ }
+ }
+
+ instance, err := s.GetInstanceByID(ctx, task.InstanceID)
+ if err != nil {
+ return nil, err
+ }
+ if instance == nil {
+ return nil, errors.Errorf("instance not found with ID %v", task.InstanceID)
+ }
+ composedTask.Instance = instance
+ if task.DatabaseID != nil {
+ database, err := s.GetDatabase(ctx, &api.DatabaseFind{ID: task.DatabaseID})
+ if err != nil {
+ return nil, err
+ }
+ if database == nil {
+ return nil, errors.Errorf("database not found with ID %v", task.DatabaseID)
+ }
+ composedTask.Database = database
+ }
+
+ composedTasks = append(composedTasks, composedTask)
+ }
stages, err := s.ListStageV2(ctx, pipeline.ID)
if err != nil {
@@ -56,9 +142,9 @@ func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage)
Environment: environment,
Name: stage.Name,
}
- for _, task := range tasks {
- if task.StageID == stage.ID {
- composedStage.TaskList = append(composedStage.TaskList, task)
+ for _, composedTask := range composedTasks {
+ if composedTask.StageID == stage.ID {
+ composedStage.TaskList = append(composedStage.TaskList, composedTask)
}
}
diff --git a/backend/store/task_check_run.go b/backend/store/task_check_run.go
index b6b946bff1..9763ca8c2f 100644
--- a/backend/store/task_check_run.go
+++ b/backend/store/task_check_run.go
@@ -385,7 +385,6 @@ func (s *Store) ListTaskCheckRuns(ctx context.Context, find *TaskCheckRunFind) (
}
func (*Store) findTaskCheckRunImpl(ctx context.Context, tx *Tx, find *TaskCheckRunFind) ([]*TaskCheckRunMessage, error) {
- // Build WHERE clause.
joinClause := ""
where, args := []string{"TRUE"}, []interface{}{}
if v := find.TaskID; v != nil {
diff --git a/backend/store/task_dag.go b/backend/store/task_dag.go
index ce8ec201ce..53cc7a4b8f 100644
--- a/backend/store/task_dag.go
+++ b/backend/store/task_dag.go
@@ -17,6 +17,8 @@ type TaskDAGMessage struct {
type TaskDAGFind struct {
FromTaskID *int
ToTaskID *int
+ StageID *int
+ PipelineID *int
}
// CreateTaskDAGV2 creates a task DAG.
@@ -56,6 +58,7 @@ func (s *Store) CreateTaskDAGV2(ctx context.Context, create *TaskDAGMessage) err
// ListTaskDags lists task dags.
func (s *Store) ListTaskDags(ctx context.Context, find *TaskDAGFind) ([]*TaskDAGMessage, error) {
+ joinClause := ""
where, args := []string{"TRUE"}, []interface{}{}
if v := find.FromTaskID; v != nil {
where, args = append(where, fmt.Sprintf("from_task_id = $%d", len(args)+1)), append(args, *v)
@@ -63,6 +66,16 @@ func (s *Store) ListTaskDags(ctx context.Context, find *TaskDAGFind) ([]*TaskDAG
if v := find.ToTaskID; v != nil {
where, args = append(where, fmt.Sprintf("to_task_id = $%d", len(args)+1)), append(args, *v)
}
+ if v := find.StageID; v != nil {
+ where, args = append(where, fmt.Sprintf("task.stage_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.PipelineID; v != nil {
+ where, args = append(where, fmt.Sprintf("task.pipeline_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if find.StageID != nil || find.PipelineID != nil {
+ // FROM and TO tasks are from the same pipeline and same stage.
+ joinClause = "JOIN task ON task.id = task_dag.from_task_id"
+ }
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
@@ -70,12 +83,13 @@ func (s *Store) ListTaskDags(ctx context.Context, find *TaskDAGFind) ([]*TaskDAG
}
defer tx.Rollback()
- rows, err := tx.QueryContext(ctx, `
+ rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT
- from_task_id,
- to_task_id
+ task_dag.from_task_id,
+ task_dag.to_task_id
FROM task_dag
- WHERE `+strings.Join(where, " AND "),
+ %s
+ WHERE %s`, joinClause, strings.Join(where, " AND ")),
args...,
)
if err != nil {
diff --git a/backend/store/task_run.go b/backend/store/task_run.go
index 1b907c0eab..89b4fc94eb 100644
--- a/backend/store/task_run.go
+++ b/backend/store/task_run.go
@@ -43,7 +43,9 @@ type TaskRunCreate struct {
// TaskRunFind is the API message for finding task runs.
type TaskRunFind struct {
// Related fields
- TaskID *int
+ TaskID *int
+ StageID *int
+ PipelineID *int
// Domain specific fields
StatusList *[]api.TaskRunStatus
@@ -206,9 +208,19 @@ func (s *Store) listTaskRun(ctx context.Context, find *TaskRunFind) ([]*TaskRunM
}
func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *TaskRunFind) ([]*TaskRunMessage, error) {
+ joinClause := ""
where, args := []string{"TRUE"}, []interface{}{}
if v := find.TaskID; v != nil {
- where, args = append(where, fmt.Sprintf("task_id = $%d", len(args)+1)), append(args, *v)
+ where, args = append(where, fmt.Sprintf("task_run.task_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.StageID; v != nil {
+ where, args = append(where, fmt.Sprintf("task.stage_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.PipelineID; v != nil {
+ where, args = append(where, fmt.Sprintf("task.pipeline_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if find.StageID != nil || find.PipelineID != nil {
+ joinClause = "JOIN task ON task.id = task_run.task_id"
}
if v := find.StatusList; v != nil {
@@ -220,23 +232,24 @@ func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *TaskRunFind) ([
where = append(where, fmt.Sprintf("status in (%s)", strings.Join(list, ",")))
}
- rows, err := tx.QueryContext(ctx, `
+ rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT
- id,
- creator_id,
- created_ts,
- updater_id,
- updated_ts,
- task_id,
- name,
- status,
- type,
- code,
- comment,
- result,
- payload
+ task_run.id,
+ task_run.creator_id,
+ task_run.created_ts,
+ task_run.updater_id,
+ task_run.updated_ts,
+ task_run.task_id,
+ task_run.name,
+ task_run.status,
+ task_run.type,
+ task_run.code,
+ task_run.comment,
+ task_run.result,
+ task_run.payload
FROM task_run
- WHERE `+strings.Join(where, " AND "),
+ %s
+ WHERE %s`, joinClause, strings.Join(where, " AND ")),
args...,
)
if err != nil {