Skip to content

Commit 12b6185

Browse files
authored
Sql query optimization for application group app status listing (#5672)
1 parent 71a0bfc commit 12b6185

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type CdWorkflowRepository interface {
7373
FetchAllCdStagesLatestEntity(pipelineIds []int) ([]*CdWorkflowStatus, error)
7474
FetchAllCdStagesLatestEntityStatus(wfrIds []int) ([]*CdWorkflowRunner, error)
7575
ExistsByStatus(status string) (bool, error)
76-
76+
FetchEnvAllCdStagesLatestEntityStatus(wfrIds []int, envID int) ([]*CdWorkflowRunner, error)
7777
FetchArtifactsByCdPipelineId(pipelineId int, runnerType apiBean.WorkflowType, offset, limit int, searchString string) ([]CdWorkflowRunner, error)
7878
GetLatestTriggersOfHelmPipelinesStuckInNonTerminalStatuses(getPipelineDeployedWithinHours int) ([]*CdWorkflowRunner, error)
7979
FindLatestRunnerByPipelineIdsAndRunnerType(ctx context.Context, pipelineIds []int, runnerType apiBean.WorkflowType) ([]CdWorkflowRunner, error)
@@ -685,8 +685,23 @@ func (impl *CdWorkflowRepositoryImpl) FetchAllCdStagesLatestEntity(pipelineIds [
685685

686686
func (impl *CdWorkflowRepositoryImpl) FetchAllCdStagesLatestEntityStatus(wfrIds []int) ([]*CdWorkflowRunner, error) {
687687
var wfrList []*CdWorkflowRunner
688-
err := impl.dbConnection.Model(&wfrList).Column("cd_workflow_runner.*").
689-
Where("cd_workflow_runner.id in (?)", pg.In(wfrIds)).Select()
688+
err := impl.dbConnection.Model(&wfrList).
689+
Column("cd_workflow_runner.id", "cd_workflow_runner.status").
690+
Where("cd_workflow_runner.id in (?)", pg.In(wfrIds)).
691+
Select()
692+
return wfrList, err
693+
}
694+
695+
func (impl *CdWorkflowRepositoryImpl) FetchEnvAllCdStagesLatestEntityStatus(wfrIds []int, envID int) ([]*CdWorkflowRunner, error) {
696+
var wfrList []*CdWorkflowRunner
697+
query := `
698+
select wfr.id, wfr.status
699+
from cd_workflow_runner wfr
700+
inner join cd_workflow wf on wf.id = wfr.cd_workflow_id
701+
inner join pipeline p on p.id = wf.pipeline_id
702+
where p.environment_id = ? and wfr.id in (?)
703+
`
704+
_, err := impl.dbConnection.Query(&wfrList, query, envID, pg.In(wfrIds))
690705
return wfrList, err
691706
}
692707

pkg/pipeline/CdHandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ func (impl *CdHandlerImpl) FetchAppWorkflowStatusForTriggerViewForEnvironment(re
984984

985985
statusMap := make(map[int]string)
986986
if len(wfrIds) > 0 {
987-
cdWorkflowRunners, err := impl.cdWorkflowRepository.FetchAllCdStagesLatestEntityStatus(wfrIds)
987+
cdWorkflowRunners, err := impl.cdWorkflowRepository.FetchEnvAllCdStagesLatestEntityStatus(wfrIds, request.ParentResourceId)
988988
if err != nil && !util.IsErrNoRows(err) {
989989
return cdWorkflowStatus, err
990990
}
@@ -1130,7 +1130,7 @@ func (impl *CdHandlerImpl) FetchAppDeploymentStatusForEnvironments(request resou
11301130
}
11311131
if len(wfrIds) > 0 {
11321132
_, span = otel.Tracer("orchestrator").Start(request.Ctx, "pipelineBuilder.FetchAllCdStagesLatestEntityStatus")
1133-
wfrList, err := impl.cdWorkflowRepository.FetchAllCdStagesLatestEntityStatus(wfrIds)
1133+
wfrList, err := impl.cdWorkflowRepository.FetchEnvAllCdStagesLatestEntityStatus(wfrIds, request.ParentResourceId)
11341134
span.End()
11351135
if err != nil && !util.IsErrNoRows(err) {
11361136
return deploymentStatuses, err

0 commit comments

Comments
 (0)