Skip to content

Commit 01b8bc6

Browse files
committed
Refactor CI status fetching in CiHandlerImpl by renaming methods for clarity, introducing GetCiWorkflowStatusFromCiWorkflow in adapter, and improving log consistency.
1 parent 19f6886 commit 01b8bc6

File tree

2 files changed

+29
-53
lines changed

2 files changed

+29
-53
lines changed

pkg/pipeline/CiHandler.go

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
buildBean "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
2828
repository2 "github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
2929
eventProcessorBean "github.com/devtron-labs/devtron/pkg/eventProcessor/bean"
30+
"github.com/devtron-labs/devtron/pkg/pipeline/adapter"
3031
"github.com/devtron-labs/devtron/pkg/pipeline/constants"
3132
"github.com/devtron-labs/devtron/pkg/pipeline/workflowStatus"
3233
"regexp"
@@ -647,100 +648,79 @@ func (impl *CiHandlerImpl) stateChanged(status string, podStatus string, msg str
647648
}
648649

649650
func (impl *CiHandlerImpl) FetchCiStatusForTriggerViewV1(appId int) ([]*pipelineConfig.CiWorkflowStatus, error) {
650-
// Get all CI pipeline IDs for this app
651651
allPipelineIds, err := impl.ciWorkflowRepository.FindCiPipelineIdsByAppId(appId)
652652
if err != nil {
653-
impl.Logger.Errorw("error in getting ci pipeline ids for app, falling back to old method", "err", err, "appId", appId)
653+
impl.Logger.Errorw("error in getting ci pipeline ids for app, falling back to old method", "appId", appId, "err", err)
654654
return impl.ciWorkflowRepository.FIndCiWorkflowStatusesByAppId(appId)
655655
}
656656

657657
if len(allPipelineIds) == 0 {
658658
return []*pipelineConfig.CiWorkflowStatus{}, nil
659659
}
660660

661-
// Find which pipeline IDs have entries in latest status table
662661
pipelinesInLatestTable, err := impl.workflowStatusLatestRepository.GetByPipelineIds(allPipelineIds)
663662
if err != nil {
664-
impl.Logger.Errorw("error in checking latest status table, falling back to old method", "err", err, "appId", appId)
663+
impl.Logger.Errorw("error in checking latest status table, falling back to old method", "appId", appId, "err", err)
665664
return impl.ciWorkflowRepository.FIndCiWorkflowStatusesByAppId(appId)
666665
}
667666

668667
var allStatuses []*pipelineConfig.CiWorkflowStatus
669668

670-
// Fetch from latest status table for available pipelines
671669
if len(pipelinesInLatestTable) > 0 {
672670
statusesFromLatestTable, err := impl.fetchCiStatusFromLatestTable(pipelinesInLatestTable)
673671
if err != nil {
674-
impl.Logger.Errorw("error in fetching from latest status table", "err", err, "pipelineIds", pipelinesInLatestTable)
672+
impl.Logger.Errorw("error in fetching from latest status table", "pipelineIds", pipelinesInLatestTable, "err", err)
675673
return nil, err
676674
} else {
677675
allStatuses = append(allStatuses, statusesFromLatestTable...)
678676
}
679677
}
680678

681-
// Find pipeline IDs that are NOT in latest status table
682-
pipelinesNotInLatestTable := impl.findMissingPipelineIds(allPipelineIds, pipelinesInLatestTable)
679+
pipelinesNotInLatestTable := impl.getPipelineIdsNotInLatestTable(allPipelineIds, pipelinesInLatestTable)
683680

684-
// Fetch using complex query for missing pipeline IDs
685681
if len(pipelinesNotInLatestTable) > 0 {
686-
statusesFromComplexQuery, err := impl.fetchCiStatusUsingComplexQuery(pipelinesNotInLatestTable)
682+
statusesFromComplexQuery, err := impl.fetchCiStatusUsingFallbackMethod(pipelinesNotInLatestTable)
687683
if err != nil {
688-
impl.Logger.Errorw("error in fetching using complex query", "err", err, "pipelineIds", pipelinesNotInLatestTable)
684+
impl.Logger.Errorw("error in fetching using complex query", "pipelineIds", pipelinesNotInLatestTable, "err", err)
689685
return nil, err
690686
} else {
691687
allStatuses = append(allStatuses, statusesFromComplexQuery...)
692688
}
693689
}
694690

695-
impl.Logger.Debugw("hybrid ci status fetch completed",
696-
"appId", appId,
697-
"totalPipelines", len(allPipelineIds),
698-
"pipelinesFromLatestTable", len(pipelinesInLatestTable),
699-
"pipelinesFromComplexQuery", len(pipelinesNotInLatestTable),
700-
"totalResults", len(allStatuses))
691+
impl.Logger.Debugw("hybrid ci status fetch completed", "appId", appId, "totalPipelines", len(allPipelineIds), "pipelinesFromLatestTable", len(pipelinesInLatestTable), "pipelinesFromOldQuery", len(pipelinesNotInLatestTable))
701692

702693
return allStatuses, nil
703694
}
704695

705696
// fetchCiStatusFromLatestTable fetches CI status from ci_workflow_status_latest table
706697
func (impl *CiHandlerImpl) fetchCiStatusFromLatestTable(pipelineIds []int) ([]*pipelineConfig.CiWorkflowStatus, error) {
707-
// Get entries from latest status table
708698
latestStatusEntries, err := impl.workflowStatusLatestRepository.GetCiWorkflowStatusLatestByPipelineIds(pipelineIds)
709699
if err != nil {
710700
return nil, err
711701
}
712702

713-
// Extract workflow IDs
714703
var workflowIds []int
715704
for _, entry := range latestStatusEntries {
716705
workflowIds = append(workflowIds, entry.CiWorkflowId)
717706
}
718707

719-
// Get workflows by IDs
720708
workflows, err := impl.ciWorkflowRepository.FindWorkflowsByCiWorkflowIds(workflowIds)
721709
if err != nil {
722710
return nil, err
723711
}
724712

725-
// Convert to CiWorkflowStatus format
726713
var statuses []*pipelineConfig.CiWorkflowStatus
727714
for _, workflow := range workflows {
728-
status := &pipelineConfig.CiWorkflowStatus{
729-
CiWorkflowId: workflow.Id,
730-
CiPipelineName: workflow.CiPipeline.Name,
731-
CiPipelineId: workflow.CiPipelineId,
732-
CiStatus: workflow.Status,
733-
StorageConfigured: workflow.BlobStorageEnabled,
734-
}
715+
status := adapter.GetCiWorkflowStatusFromCiWorkflow(workflow)
735716
statuses = append(statuses, status)
736717
}
737718

738719
return statuses, nil
739720
}
740721

741-
// fetchCiStatusUsingComplexQuery fetches CI status using complex query for specific pipeline IDs
742-
func (impl *CiHandlerImpl) fetchCiStatusUsingComplexQuery(pipelineIds []int) ([]*pipelineConfig.CiWorkflowStatus, error) {
743-
// Get latest workflows for these pipelines
722+
// fetchCiStatusUsingFallbackMethod fetches CI status directly from workflow table having multiple joins
723+
func (impl *CiHandlerImpl) fetchCiStatusUsingFallbackMethod(pipelineIds []int) ([]*pipelineConfig.CiWorkflowStatus, error) {
744724
workflows, err := impl.ciWorkflowRepository.FindLastTriggeredWorkflowByCiIds(pipelineIds)
745725
if err != nil {
746726
return nil, err
@@ -749,13 +729,7 @@ func (impl *CiHandlerImpl) fetchCiStatusUsingComplexQuery(pipelineIds []int) ([]
749729
// Convert to CiWorkflowStatus format
750730
var statuses []*pipelineConfig.CiWorkflowStatus
751731
for _, workflow := range workflows {
752-
status := &pipelineConfig.CiWorkflowStatus{
753-
CiWorkflowId: workflow.Id,
754-
CiPipelineName: workflow.CiPipeline.Name,
755-
CiPipelineId: workflow.CiPipelineId,
756-
CiStatus: workflow.Status,
757-
StorageConfigured: workflow.BlobStorageEnabled,
758-
}
732+
status := adapter.GetCiWorkflowStatusFromCiWorkflow(workflow)
759733
statuses = append(statuses, status)
760734
}
761735

@@ -787,50 +761,43 @@ func (impl *CiHandlerImpl) fetchLastTriggeredWorkflowsHybrid(pipelineIds []int)
787761
return []*pipelineConfig.CiWorkflow{}, nil
788762
}
789763

790-
// Find which pipeline IDs have entries in latest status table
791764
pipelinesInLatestTable, err := impl.workflowStatusLatestRepository.GetByPipelineIds(pipelineIds)
792765
if err != nil {
793-
impl.Logger.Errorw("error in checking latest status table, falling back to complex query", "err", err, "pipelineIds", pipelineIds)
766+
impl.Logger.Errorw("error in checking latest status table, falling back to complex query", "pipelineIds", pipelineIds, "err", err)
794767
return impl.ciWorkflowRepository.FindLastTriggeredWorkflowByCiIds(pipelineIds)
795768
}
796769

797770
var allWorkflows []*pipelineConfig.CiWorkflow
798771

799-
// Fetch from latest status table for available pipelines
800772
if len(pipelinesInLatestTable) > 0 {
801773
workflowsFromLatestTable, err := impl.fetchWorkflowsFromLatestTable(pipelinesInLatestTable)
802774
if err != nil {
803-
impl.Logger.Errorw("error in fetching from latest status table", "err", err, "pipelineIds", pipelinesInLatestTable)
775+
impl.Logger.Errorw("error in fetching from latest status table", "pipelineIds", pipelinesInLatestTable, "err", err)
804776
return nil, err
805777
} else {
806778
allWorkflows = append(allWorkflows, workflowsFromLatestTable...)
807779
}
808780
}
809781

810-
// Find pipeline IDs that are NOT in latest status table
811-
pipelinesNotInLatestTable := impl.findMissingPipelineIds(pipelineIds, pipelinesInLatestTable)
782+
pipelinesNotInLatestTable := impl.getPipelineIdsNotInLatestTable(pipelineIds, pipelinesInLatestTable)
812783

813-
// Fetch using complex query for missing pipeline IDs
814784
if len(pipelinesNotInLatestTable) > 0 {
815785
workflowsFromComplexQuery, err := impl.ciWorkflowRepository.FindLastTriggeredWorkflowByCiIds(pipelinesNotInLatestTable)
816786
if err != nil {
817-
impl.Logger.Errorw("error in fetching using complex query", "err", err, "pipelineIds", pipelinesNotInLatestTable)
787+
impl.Logger.Errorw("error in fetching using complex query", "pipelineIds", pipelinesNotInLatestTable, "err", err)
818788
return nil, err
819789
} else {
820790
allWorkflows = append(allWorkflows, workflowsFromComplexQuery...)
821791
}
822792
}
823793

824-
impl.Logger.Debugw("hybrid workflow fetch completed",
825-
"totalPipelines", len(pipelineIds),
826-
"pipelinesFromLatestTable", len(pipelinesInLatestTable),
827-
"pipelinesFromOldQuery", len(pipelinesNotInLatestTable))
794+
impl.Logger.Debugw("hybrid workflow fetch completed", "totalPipelines", len(pipelineIds), "pipelinesFromLatestTable", len(pipelinesInLatestTable), "pipelinesFromOldQuery", len(pipelinesNotInLatestTable))
828795

829796
return allWorkflows, nil
830797
}
831798

832-
// findMissingPipelineIds finds pipeline IDs that are NOT in the latest status table
833-
func (impl *CiHandlerImpl) findMissingPipelineIds(allPipelineIds, pipelinesInLatestTable []int) []int {
799+
// getPipelineIdsNotInLatestTable finds pipeline IDs that are NOT in the latest status table
800+
func (impl *CiHandlerImpl) getPipelineIdsNotInLatestTable(allPipelineIds, pipelinesInLatestTable []int) []int {
834801
pipelineIdMap := make(map[int]bool)
835802
for _, id := range pipelinesInLatestTable {
836803
pipelineIdMap[id] = true
@@ -1053,7 +1020,6 @@ func (impl *CiHandlerImpl) FetchCiStatusForTriggerViewForEnvironment(request res
10531020
if len(ciPipelineIds) == 0 {
10541021
return ciWorkflowStatuses, nil
10551022
}
1056-
// Hybrid approach: Use latest status table for available pipelines, fallback to complex old query for others
10571023
latestCiWorkflows, err := impl.fetchLastTriggeredWorkflowsHybrid(ciPipelineIds)
10581024
if err != nil && !util.IsErrNoRows(err) {
10591025
impl.Logger.Errorw("err in hybrid ci workflow fetch", "ciPipelineIds", ciPipelineIds, "err", err)

pkg/pipeline/adapter/adapter.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,13 @@ func NewMigrateExternalAppValidationRequest(pipeline *bean.CDPipelineConfigObjec
406406
}
407407
return request
408408
}
409+
410+
func GetCiWorkflowStatusFromCiWorkflow(ciWorkflow *pipelineConfig.CiWorkflow) *pipelineConfig.CiWorkflowStatus {
411+
return &pipelineConfig.CiWorkflowStatus{
412+
CiPipelineId: ciWorkflow.CiPipelineId,
413+
CiPipelineName: ciWorkflow.CiPipeline.Name,
414+
CiStatus: ciWorkflow.Status,
415+
StorageConfigured: ciWorkflow.BlobStorageEnabled,
416+
CiWorkflowId: ciWorkflow.Id,
417+
}
418+
}

0 commit comments

Comments
 (0)