From 8b8dc4a3756b1d343c2f10b143dc77598f28914b Mon Sep 17 00:00:00 2001 From: prakhar katiyar Date: Mon, 12 Aug 2024 16:20:34 +0530 Subject: [PATCH] Sql query optimization for application group app status listing --- .../pipelineConfig/CdWorfkflowRepository.go | 21 ++++++++++++++++--- pkg/pipeline/CdHandler.go | 4 ++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go b/internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go index c839a6ecdb..0187274fea 100644 --- a/internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go +++ b/internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go @@ -73,7 +73,7 @@ type CdWorkflowRepository interface { FetchAllCdStagesLatestEntity(pipelineIds []int) ([]*CdWorkflowStatus, error) FetchAllCdStagesLatestEntityStatus(wfrIds []int) ([]*CdWorkflowRunner, error) ExistsByStatus(status string) (bool, error) - + FetchEnvAllCdStagesLatestEntityStatus(wfrIds []int, envID int) ([]*CdWorkflowRunner, error) FetchArtifactsByCdPipelineId(pipelineId int, runnerType apiBean.WorkflowType, offset, limit int, searchString string) ([]CdWorkflowRunner, error) GetLatestTriggersOfHelmPipelinesStuckInNonTerminalStatuses(getPipelineDeployedWithinHours int) ([]*CdWorkflowRunner, error) FindLatestRunnerByPipelineIdsAndRunnerType(ctx context.Context, pipelineIds []int, runnerType apiBean.WorkflowType) ([]CdWorkflowRunner, error) @@ -685,8 +685,23 @@ func (impl *CdWorkflowRepositoryImpl) FetchAllCdStagesLatestEntity(pipelineIds [ func (impl *CdWorkflowRepositoryImpl) FetchAllCdStagesLatestEntityStatus(wfrIds []int) ([]*CdWorkflowRunner, error) { var wfrList []*CdWorkflowRunner - err := impl.dbConnection.Model(&wfrList).Column("cd_workflow_runner.*"). - Where("cd_workflow_runner.id in (?)", pg.In(wfrIds)).Select() + err := impl.dbConnection.Model(&wfrList). + Column("cd_workflow_runner.id", "cd_workflow_runner.status"). + Where("cd_workflow_runner.id in (?)", pg.In(wfrIds)). + Select() + return wfrList, err +} + +func (impl *CdWorkflowRepositoryImpl) FetchEnvAllCdStagesLatestEntityStatus(wfrIds []int, envID int) ([]*CdWorkflowRunner, error) { + var wfrList []*CdWorkflowRunner + query := ` + select wfr.id, wfr.status + from cd_workflow_runner wfr + inner join cd_workflow wf on wf.id = wfr.cd_workflow_id + inner join pipeline p on p.id = wf.pipeline_id + where p.environment_id = ? and wfr.id in (?) + ` + _, err := impl.dbConnection.Query(&wfrList, query, envID, pg.In(wfrIds)) return wfrList, err } diff --git a/pkg/pipeline/CdHandler.go b/pkg/pipeline/CdHandler.go index 914b434c4f..791c32da73 100644 --- a/pkg/pipeline/CdHandler.go +++ b/pkg/pipeline/CdHandler.go @@ -984,7 +984,7 @@ func (impl *CdHandlerImpl) FetchAppWorkflowStatusForTriggerViewForEnvironment(re statusMap := make(map[int]string) if len(wfrIds) > 0 { - cdWorkflowRunners, err := impl.cdWorkflowRepository.FetchAllCdStagesLatestEntityStatus(wfrIds) + cdWorkflowRunners, err := impl.cdWorkflowRepository.FetchEnvAllCdStagesLatestEntityStatus(wfrIds, request.ParentResourceId) if err != nil && !util.IsErrNoRows(err) { return cdWorkflowStatus, err } @@ -1130,7 +1130,7 @@ func (impl *CdHandlerImpl) FetchAppDeploymentStatusForEnvironments(request resou } if len(wfrIds) > 0 { _, span = otel.Tracer("orchestrator").Start(request.Ctx, "pipelineBuilder.FetchAllCdStagesLatestEntityStatus") - wfrList, err := impl.cdWorkflowRepository.FetchAllCdStagesLatestEntityStatus(wfrIds) + wfrList, err := impl.cdWorkflowRepository.FetchEnvAllCdStagesLatestEntityStatus(wfrIds, request.ParentResourceId) span.End() if err != nil && !util.IsErrNoRows(err) { return deploymentStatuses, err