Skip to content

Commit cb6ce49

Browse files
committed
Integrate optimized CI workflow fetching in CiHandlerImpl using FindLastTriggeredWorkflowByCiIdsOptimized. Add fallback to legacy method for error handling. Update repository with the optimized query method.
1 parent 17c1dae commit cb6ce49

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

internal/sql/repository/pipelineConfig/CiWorkflowRepository.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type CiWorkflowRepository interface {
4040
FindByName(name string) (*CiWorkflow, error)
4141

4242
FindLastTriggeredWorkflowByCiIds(pipelineId []int) (ciWorkflow []*CiWorkflow, err error)
43+
FindLastTriggeredWorkflowByCiIdsOptimized(pipelineId []int) (ciWorkflow []*CiWorkflow, err error)
4344
FindLastTriggeredWorkflowByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error)
4445
FindAllLastTriggeredWorkflowByArtifactId(ciArtifactId []int) (ciWorkflow []*CiWorkflow, err error)
4546
FindAllTriggeredWorkflowCountInLast24Hour() (ciWorkflowCount int, err error)
@@ -290,6 +291,23 @@ func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowByCiIds(pipelineI
290291
return ciWorkflow, err
291292
}
292293

294+
// FindLastTriggeredWorkflowByCiIdsOptimized uses the ci_workflow_status_latest table for better performance
295+
func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowByCiIdsOptimized(pipelineId []int) (ciWorkflow []*CiWorkflow, err error) {
296+
err = impl.dbConnection.Model(&ciWorkflow).
297+
Column("ci_workflow.*", "CiPipeline").
298+
Join("INNER JOIN ci_workflow_status_latest cwsl ON cwsl.ci_workflow_id = ci_workflow.id").
299+
Where("cwsl.pipeline_id IN (?)", pg.In(pipelineId)).
300+
Select()
301+
302+
if err != nil {
303+
impl.logger.Errorw("error in optimized query, falling back to old method", "err", err, "pipelineIds", pipelineId)
304+
// Fallback to the old method if optimized query fails
305+
return impl.FindLastTriggeredWorkflowByCiIds(pipelineId)
306+
}
307+
308+
return ciWorkflow, err
309+
}
310+
293311
func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error) {
294312
workflow := &CiWorkflow{}
295313
err = impl.dbConnection.Model(workflow).

pkg/pipeline/CiHandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,9 @@ func (impl *CiHandlerImpl) FetchCiStatusForTriggerViewForEnvironment(request res
870870
if len(ciPipelineIds) == 0 {
871871
return ciWorkflowStatuses, nil
872872
}
873-
latestCiWorkflows, err := impl.ciWorkflowRepository.FindLastTriggeredWorkflowByCiIds(ciPipelineIds)
873+
latestCiWorkflows, err := impl.ciWorkflowRepository.FindLastTriggeredWorkflowByCiIdsOptimized(ciPipelineIds)
874874
if err != nil && !util.IsErrNoRows(err) {
875-
impl.Logger.Errorw("err", "ciPipelineIds", ciPipelineIds, "err", err)
875+
impl.Logger.Errorw("err in optimized ci workflow fetch", "ciPipelineIds", ciPipelineIds, "err", err)
876876
return ciWorkflowStatuses, err
877877
}
878878

0 commit comments

Comments
 (0)