Skip to content

Commit f6f6457

Browse files
fix: Optimize app grouping apis (#3125)
* workflow status api optimized * optimzation on db queries for app grouping api's * app workflow listing for app grouping optimized * ci pipelines fetch for build n deploy for app grouping * fixing db query * fix for rbac objects for app grouping api * fix * fix * ci workflow status fix * added otel tracer for app grouping api's * review changes * fix * review changes
1 parent 56af646 commit f6f6457

18 files changed

+356
-148
lines changed

api/restHandler/AppWorkflowRestHandler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/devtron-labs/devtron/pkg/user/casbin"
3030
"github.com/devtron-labs/devtron/util/rbac"
3131
"github.com/gorilla/mux"
32+
"go.opentelemetry.io/otel"
3233
"go.uber.org/zap"
3334
"net/http"
3435
"strconv"
@@ -239,7 +240,9 @@ func (impl AppWorkflowRestHandlerImpl) FindAppWorkflowByEnvironment(w http.Respo
239240
return
240241
}
241242
workflows := make(map[string]interface{})
243+
_, span := otel.Tracer("orchestrator").Start(r.Context(), "ciHandler.FetchAppWorkflowsInAppGrouping")
242244
workflowsList, err := impl.appWorkflowService.FindAppWorkflowsByEnvironmentId(envId, userEmailId, impl.checkAuthBatch)
245+
span.End()
243246
if err != nil {
244247
impl.Logger.Errorw("error in fetching workflows for app", "err", err)
245248
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)

api/restHandler/app/BuildPipelineRestHandler.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
bean1 "github.com/devtron-labs/devtron/pkg/pipeline/bean"
1717
"github.com/devtron-labs/devtron/pkg/user/casbin"
1818
"github.com/gorilla/mux"
19+
"go.opentelemetry.io/otel"
1920
"io"
2021
"net/http"
2122
"strconv"
@@ -1333,7 +1334,9 @@ func (handler PipelineConfigRestHandlerImpl) GetCiPipelineByEnvironment(w http.R
13331334
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
13341335
return
13351336
}
1336-
ciConf, err := handler.pipelineBuilder.GetCiPipelineByEnvironment(envId, userEmailId, handler.checkAuthBatch)
1337+
_, span := otel.Tracer("orchestrator").Start(r.Context(), "ciHandler.FetchCiPipelinesForAppGrouping")
1338+
ciConf, err := handler.pipelineBuilder.GetCiPipelineByEnvironment(envId, userEmailId, handler.checkAuthBatch, r.Context())
1339+
span.End()
13371340
if err != nil {
13381341
handler.Logger.Errorw("service err, GetCiPipeline", "err", err, "envId", envId)
13391342
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)

api/restHandler/app/DeploymentPipelineRestHandler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,9 @@ func (handler PipelineConfigRestHandlerImpl) GetCdPipelinesByEnvironment(w http.
19611961
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
19621962
return
19631963
}
1964+
_, span := otel.Tracer("orchestrator").Start(context.Background(), "ciHandler.FetchCdPipelinesForAppGrouping")
19641965
results, err := handler.pipelineBuilder.GetCdPipelinesByEnvironment(envId, userEmailId, handler.checkAuthBatch)
1966+
span.End()
19651967
if err != nil {
19661968
handler.Logger.Errorw("service err, GetCdPipelines", "err", err, "envId", envId)
19671969
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)

api/restHandler/app/PipelineConfigRestHandler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/devtron-labs/devtron/internal/sql/repository/helper"
2626
"github.com/devtron-labs/devtron/pkg/chart"
2727
"github.com/devtron-labs/devtron/util/argo"
28+
"go.opentelemetry.io/otel"
2829
"io"
2930
"net/http"
3031
"strconv"
@@ -593,7 +594,9 @@ func (handler PipelineConfigRestHandlerImpl) FetchAppWorkflowStatusForTriggerVie
593594
return
594595
}
595596
triggerWorkflowStatus := pipelineConfig.TriggerWorkflowStatus{}
597+
_, span := otel.Tracer("orchestrator").Start(r.Context(), "ciHandler.FetchCiStatusForBuildAndDeployInAppGrouping")
596598
ciWorkflowStatus, err := handler.ciHandler.FetchCiStatusForTriggerViewForEnvironment(envId, userEmailId, handler.checkAuthBatch)
599+
span.End()
597600
if err != nil {
598601
handler.Logger.Errorw("service err", "err", err)
599602
if util.IsErrNoRows(err) {
@@ -605,7 +608,9 @@ func (handler PipelineConfigRestHandlerImpl) FetchAppWorkflowStatusForTriggerVie
605608
return
606609
}
607610

611+
_, span = otel.Tracer("orchestrator").Start(r.Context(), "ciHandler.FetchCdStatusForBuildAndDeployInAppGrouping")
608612
cdWorkflowStatus, err := handler.cdHandler.FetchAppWorkflowStatusForTriggerViewForEnvironment(envId, userEmailId, handler.checkAuthBatch)
613+
span.End()
609614
if err != nil {
610615
handler.Logger.Errorw("service err, FetchAppWorkflowStatusForTriggerView", "err", err)
611616
if util.IsErrNoRows(err) {

cmd/external-app/wire_gen.go

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/sql/repository/appWorkflow/AppWorkflowRepository.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type AppWorkflowRepository interface {
5656
FindAllWfsHavingCdPipelinesFromSpecificEnvsOnly(envIds []int, appIds []int) ([]*AppWorkflowMapping, error)
5757
FindCiPipelineIdsFromAppWfIds(appWfIds []int) ([]int, error)
5858
FindChildCDIdsByParentCDPipelineId(cdPipelineId int) ([]int, error)
59+
FindByCDPipelineIds(cdPipelineIds []int) ([]*AppWorkflowMapping, error)
60+
FindByWorkflowIds(workflowIds []int) ([]*AppWorkflowMapping, error)
61+
FindMappingByAppIds(appIds []int) ([]*AppWorkflowMapping, error)
5962
}
6063

6164
type AppWorkflowRepositoryImpl struct {
@@ -323,6 +326,7 @@ func (impl AppWorkflowRepositoryImpl) FindWFCDMappingByParentCDPipelineId(cdPipe
323326
Select()
324327
return appWorkflowsMapping, err
325328
}
329+
326330
func (impl AppWorkflowRepositoryImpl) DeleteAppWorkflowMapping(appWorkflow *AppWorkflowMapping, tx *pg.Tx) error {
327331
appWorkflow.Active = false
328332
err := tx.Update(appWorkflow)
@@ -400,3 +404,32 @@ func (impl AppWorkflowRepositoryImpl) FindChildCDIdsByParentCDPipelineId(cdPipel
400404
_, err := impl.dbConnection.Query(&ids, query, cdPipelineId, CDPIPELINE, CDPIPELINE, true)
401405
return ids, err
402406
}
407+
408+
func (impl AppWorkflowRepositoryImpl) FindByCDPipelineIds(cdPipelineIds []int) ([]*AppWorkflowMapping, error) {
409+
var appWorkflowsMapping []*AppWorkflowMapping
410+
err := impl.dbConnection.Model(&appWorkflowsMapping).
411+
Where("component_id in (?)", pg.In(cdPipelineIds)).
412+
Where("type = ?", CDPIPELINE).
413+
Where("active = ?", true).
414+
Select()
415+
return appWorkflowsMapping, err
416+
}
417+
418+
func (impl AppWorkflowRepositoryImpl) FindByWorkflowIds(workflowIds []int) ([]*AppWorkflowMapping, error) {
419+
var appWorkflowsMapping []*AppWorkflowMapping
420+
err := impl.dbConnection.Model(&appWorkflowsMapping).
421+
Where("app_workflow_id in (?)", pg.In(workflowIds)).
422+
Where("active = ?", true).
423+
Select()
424+
return appWorkflowsMapping, err
425+
}
426+
427+
func (impl AppWorkflowRepositoryImpl) FindMappingByAppIds(appIds []int) ([]*AppWorkflowMapping, error) {
428+
var appWorkflowsMapping []*AppWorkflowMapping
429+
err := impl.dbConnection.Model(&appWorkflowsMapping).Column("app_workflow_mapping.*", "AppWorkflow").
430+
Where("app_workflow.app_id in (?)", pg.In(appIds)).
431+
Where("app_workflow.active = ?", true).
432+
Where("app_workflow_mapping.active = ?", true).
433+
Select()
434+
return appWorkflowsMapping, err
435+
}

internal/sql/repository/chartConfig/PipelineConfigRepository.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type PipelineConfigRepository interface {
4444
GetAllStrategyByPipelineId(pipelineId int) ([]*PipelineStrategy, error)
4545
GetDefaultStrategyByPipelineId(pipelineId int) (pipelineStrategy *PipelineStrategy, err error)
4646
Delete(pipelineStrategy *PipelineStrategy, tx *pg.Tx) error
47+
GetAllStrategyByPipelineIds(pipelineIds []int) ([]*PipelineStrategy, error)
4748
}
4849

4950
type PipelineConfigRepositoryImpl struct {
@@ -117,3 +118,16 @@ func (impl PipelineConfigRepositoryImpl) GetDefaultStrategyByPipelineId(pipeline
117118
func (impl PipelineConfigRepositoryImpl) Delete(pipelineStrategy *PipelineStrategy, tx *pg.Tx) error {
118119
return tx.Delete(pipelineStrategy)
119120
}
121+
122+
func (impl PipelineConfigRepositoryImpl) GetAllStrategyByPipelineIds(pipelineIds []int) ([]*PipelineStrategy, error) {
123+
var pipelineStrategies []*PipelineStrategy
124+
err := impl.dbConnection.
125+
Model(&pipelineStrategies).
126+
Where("pipeline_id in (?)", pg.In(pipelineIds)).
127+
Where("deleted = ?", false).
128+
Select()
129+
if pg.ErrNoRows == err {
130+
return nil, errors.NotFoundf(err.Error())
131+
}
132+
return pipelineStrategies, err
133+
}

internal/sql/repository/pipelineConfig/CiPipelineRepository.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type CiPipelineRepository interface {
9898
FinDByParentCiPipelineAndAppId(parentCiPipeline int, appIds []int) ([]*CiPipeline, error)
9999
FindAllPipelineInLast24Hour() (pipelines []*CiPipeline, err error)
100100
FindNumberOfAppsWithCiPipeline(appIds []int) (count int, err error)
101+
FindAppAndProjectByCiPipelineIds(ciPipelineIds []int) ([]*CiPipeline, error)
101102
}
102103
type CiPipelineRepositoryImpl struct {
103104
dbConnection *pg.DB
@@ -377,3 +378,12 @@ func (impl CiPipelineRepositoryImpl) FindNumberOfAppsWithCiPipeline(appIds []int
377378

378379
return count, nil
379380
}
381+
382+
func (impl CiPipelineRepositoryImpl) FindAppAndProjectByCiPipelineIds(ciPipelineIds []int) ([]*CiPipeline, error) {
383+
var ciPipelines []*CiPipeline
384+
err := impl.dbConnection.Model(&ciPipelines).Column("ci_pipeline.*", "App", "App.Team").
385+
Where("ci_pipeline.id in(?)", pg.In(ciPipelineIds)).
386+
Where("ci_pipeline.deleted = ?", false).
387+
Select()
388+
return ciPipelines, err
389+
}

internal/sql/repository/pipelineConfig/CiWorkflowRepository.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,12 @@ func (impl *CiWorkflowRepositoryImpl) UpdateWorkFlow(wf *CiWorkflow) error {
207207
}
208208

209209
func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowByCiIds(pipelineId []int) (ciWorkflow []*CiWorkflow, err error) {
210-
var workflow []*CiWorkflow
211-
err = impl.dbConnection.Model(workflow).
210+
err = impl.dbConnection.Model(&ciWorkflow).
212211
Column("ci_workflow.*", "CiPipeline").
213-
Where("ci_workflow.ci_pipeline_id = ? ", pg.In(pipelineId)).
212+
Where("ci_workflow.ci_pipeline_id in (?) ", pg.In(pipelineId)).
214213
Order("ci_workflow.started_on Desc").
215214
Select()
216-
return workflow, err
215+
return ciWorkflow, err
217216
}
218217

219218
func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error) {

internal/sql/repository/pipelineConfig/PipelineRepository.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type PipelineRepository interface {
104104
GetArgoPipelineByArgoAppName(argoAppName string) (Pipeline, error)
105105
GetPartiallyDeletedPipelineByStatus(appId int, envId int) (Pipeline, error)
106106
FindActiveByAppIds(appIds []int) (pipelines []*Pipeline, err error)
107+
FindAppAndEnvironmentAndProjectByPipelineIds(pipelineIds []int) (pipelines []*Pipeline, err error)
107108
}
108109

109110
type CiArtifactDTO struct {
@@ -602,3 +603,11 @@ func (impl PipelineRepositoryImpl) FindActiveByAppIds(appIds []int) (pipelines [
602603
Select()
603604
return pipelines, err
604605
}
606+
607+
func (impl PipelineRepositoryImpl) FindAppAndEnvironmentAndProjectByPipelineIds(pipelineIds []int) (pipelines []*Pipeline, err error) {
608+
err = impl.dbConnection.Model(&pipelines).Column("pipeline.*", "App", "Environment", "App.Team").
609+
Where("pipeline.id in(?)", pg.In(pipelineIds)).
610+
Where("pipeline.deleted = ?", false).
611+
Select()
612+
return pipelines, err
613+
}

0 commit comments

Comments
 (0)