|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright (c) 2024. Devtron Inc. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | + | 
|  | 17 | +package pipelineConfig | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"github.com/devtron-labs/devtron/pkg/sql" | 
|  | 21 | +	"github.com/go-pg/pg" | 
|  | 22 | +	"github.com/go-pg/pg/orm" | 
|  | 23 | +	"go.uber.org/zap" | 
|  | 24 | +) | 
|  | 25 | + | 
|  | 26 | +type WorkflowStatusLatestRepository interface { | 
|  | 27 | +	// CI Workflow Status Latest methods | 
|  | 28 | +	SaveCiWorkflowStatusLatest(tx *pg.Tx, model *CiWorkflowStatusLatest) error | 
|  | 29 | +	UpdateCiWorkflowStatusLatest(tx *pg.Tx, model *CiWorkflowStatusLatest) error | 
|  | 30 | +	GetCiWorkflowStatusLatestByPipelineId(pipelineId int) (*CiWorkflowStatusLatest, error) | 
|  | 31 | +	GetCiWorkflowStatusLatestByPipelineIds(pipelineIds []int) ([]*CiWorkflowStatusLatest, error) | 
|  | 32 | + | 
|  | 33 | +	// CD Workflow Status Latest methods | 
|  | 34 | +	SaveCdWorkflowStatusLatest(tx *pg.Tx, model *CdWorkflowStatusLatest) error | 
|  | 35 | +	UpdateCdWorkflowStatusLatest(tx *pg.Tx, model *CdWorkflowStatusLatest) error | 
|  | 36 | +	GetCdWorkflowStatusLatestByPipelineIdAndWorkflowType(tx *pg.Tx, pipelineId int, workflowType string) (*CdWorkflowStatusLatest, error) | 
|  | 37 | +	GetCdWorkflowStatusLatestByPipelineIds(pipelineIds []int) ([]*CdWorkflowStatusLatest, error) | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +type WorkflowStatusLatestRepositoryImpl struct { | 
|  | 41 | +	dbConnection *pg.DB | 
|  | 42 | +	logger       *zap.SugaredLogger | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +func NewWorkflowStatusLatestRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *WorkflowStatusLatestRepositoryImpl { | 
|  | 46 | +	return &WorkflowStatusLatestRepositoryImpl{ | 
|  | 47 | +		dbConnection: dbConnection, | 
|  | 48 | +		logger:       logger, | 
|  | 49 | +	} | 
|  | 50 | +} | 
|  | 51 | + | 
|  | 52 | +// CI Workflow Status Latest model | 
|  | 53 | +type CiWorkflowStatusLatest struct { | 
|  | 54 | +	tableName    struct{} `sql:"ci_workflow_status_latest" pg:",discard_unknown_columns"` | 
|  | 55 | +	Id           int      `sql:"id,pk"` | 
|  | 56 | +	PipelineId   int      `sql:"pipeline_id"` | 
|  | 57 | +	AppId        int      `sql:"app_id"` | 
|  | 58 | +	CiWorkflowId int      `sql:"ci_workflow_id"` | 
|  | 59 | +	sql.AuditLog | 
|  | 60 | +} | 
|  | 61 | + | 
|  | 62 | +// CD Workflow Status Latest model | 
|  | 63 | +type CdWorkflowStatusLatest struct { | 
|  | 64 | +	tableName        struct{} `sql:"cd_workflow_status_latest" pg:",discard_unknown_columns"` | 
|  | 65 | +	Id               int      `sql:"id,pk"` | 
|  | 66 | +	PipelineId       int      `sql:"pipeline_id"` | 
|  | 67 | +	AppId            int      `sql:"app_id"` | 
|  | 68 | +	EnvironmentId    int      `sql:"environment_id"` | 
|  | 69 | +	WorkflowType     string   `sql:"workflow_type"` | 
|  | 70 | +	WorkflowRunnerId int      `sql:"workflow_runner_id"` | 
|  | 71 | +	sql.AuditLog | 
|  | 72 | +} | 
|  | 73 | + | 
|  | 74 | +// CI Workflow Status Latest methods implementation | 
|  | 75 | +func (impl *WorkflowStatusLatestRepositoryImpl) SaveCiWorkflowStatusLatest(tx *pg.Tx, model *CiWorkflowStatusLatest) error { | 
|  | 76 | +	var connection orm.DB | 
|  | 77 | +	if tx != nil { | 
|  | 78 | +		connection = tx | 
|  | 79 | +	} else { | 
|  | 80 | +		connection = impl.dbConnection | 
|  | 81 | +	} | 
|  | 82 | +	err := connection.Insert(model) | 
|  | 83 | +	if err != nil { | 
|  | 84 | +		impl.logger.Errorw("error in saving ci workflow status latest", "err", err, "model", model) | 
|  | 85 | +		return err | 
|  | 86 | +	} | 
|  | 87 | +	return nil | 
|  | 88 | +} | 
|  | 89 | + | 
|  | 90 | +func (impl *WorkflowStatusLatestRepositoryImpl) GetCiWorkflowStatusLatestByPipelineIds(pipelineIds []int) ([]*CiWorkflowStatusLatest, error) { | 
|  | 91 | +	if len(pipelineIds) == 0 { | 
|  | 92 | +		return []*CiWorkflowStatusLatest{}, nil | 
|  | 93 | +	} | 
|  | 94 | + | 
|  | 95 | +	var models []*CiWorkflowStatusLatest | 
|  | 96 | +	err := impl.dbConnection.Model(&models). | 
|  | 97 | +		Where("pipeline_id IN (?)", pg.In(pipelineIds)). | 
|  | 98 | +		Select() | 
|  | 99 | +	if err != nil { | 
|  | 100 | +		impl.logger.Errorw("error in getting ci workflow status latest by pipeline ids", "err", err, "pipelineIds", pipelineIds) | 
|  | 101 | +		return nil, err | 
|  | 102 | +	} | 
|  | 103 | +	return models, nil | 
|  | 104 | +} | 
|  | 105 | + | 
|  | 106 | +func (impl *WorkflowStatusLatestRepositoryImpl) UpdateCiWorkflowStatusLatest(tx *pg.Tx, model *CiWorkflowStatusLatest) error { | 
|  | 107 | +	var connection orm.DB | 
|  | 108 | +	if tx != nil { | 
|  | 109 | +		connection = tx | 
|  | 110 | +	} else { | 
|  | 111 | +		connection = impl.dbConnection | 
|  | 112 | +	} | 
|  | 113 | +	_, err := connection.Model(model).WherePK().Update() | 
|  | 114 | +	if err != nil { | 
|  | 115 | +		impl.logger.Errorw("error in updating ci workflow status latest", "err", err, "model", model) | 
|  | 116 | +		return err | 
|  | 117 | +	} | 
|  | 118 | +	return nil | 
|  | 119 | +} | 
|  | 120 | + | 
|  | 121 | +func (impl *WorkflowStatusLatestRepositoryImpl) GetCiWorkflowStatusLatestByPipelineId(pipelineId int) (*CiWorkflowStatusLatest, error) { | 
|  | 122 | +	var model CiWorkflowStatusLatest | 
|  | 123 | +	err := impl.dbConnection.Model(&model). | 
|  | 124 | +		Where("pipeline_id = ?", pipelineId). | 
|  | 125 | +		Select() | 
|  | 126 | +	if err != nil { | 
|  | 127 | +		impl.logger.Errorw("error in getting ci workflow status latest by pipeline id", "err", err, "pipelineId", pipelineId) | 
|  | 128 | +		return nil, err | 
|  | 129 | +	} | 
|  | 130 | +	return &model, nil | 
|  | 131 | +} | 
|  | 132 | + | 
|  | 133 | +// CD Workflow Status Latest methods implementation | 
|  | 134 | +func (impl *WorkflowStatusLatestRepositoryImpl) SaveCdWorkflowStatusLatest(tx *pg.Tx, model *CdWorkflowStatusLatest) error { | 
|  | 135 | +	var connection orm.DB | 
|  | 136 | +	if tx != nil { | 
|  | 137 | +		connection = tx | 
|  | 138 | +	} else { | 
|  | 139 | +		connection = impl.dbConnection | 
|  | 140 | +	} | 
|  | 141 | +	err := connection.Insert(model) | 
|  | 142 | +	if err != nil { | 
|  | 143 | +		impl.logger.Errorw("error in saving cd workflow status latest", "err", err, "model", model) | 
|  | 144 | +		return err | 
|  | 145 | +	} | 
|  | 146 | +	return nil | 
|  | 147 | +} | 
|  | 148 | + | 
|  | 149 | +func (impl *WorkflowStatusLatestRepositoryImpl) GetCdWorkflowStatusLatestByPipelineIds(pipelineIds []int) ([]*CdWorkflowStatusLatest, error) { | 
|  | 150 | +	var models []*CdWorkflowStatusLatest | 
|  | 151 | +	err := impl.dbConnection.Model(&models). | 
|  | 152 | +		Where("pipeline_id IN (?)", pg.In(pipelineIds)). | 
|  | 153 | +		Select() | 
|  | 154 | +	if err != nil { | 
|  | 155 | +		impl.logger.Errorw("error in getting cd workflow status latest by pipeline ids", "err", err, "pipelineIds", pipelineIds) | 
|  | 156 | +		return nil, err | 
|  | 157 | +	} | 
|  | 158 | +	return models, nil | 
|  | 159 | +} | 
|  | 160 | + | 
|  | 161 | +func (impl *WorkflowStatusLatestRepositoryImpl) UpdateCdWorkflowStatusLatest(tx *pg.Tx, model *CdWorkflowStatusLatest) error { | 
|  | 162 | +	var connection orm.DB | 
|  | 163 | +	if tx != nil { | 
|  | 164 | +		connection = tx | 
|  | 165 | +	} else { | 
|  | 166 | +		connection = impl.dbConnection | 
|  | 167 | +	} | 
|  | 168 | +	_, err := connection.Model(model).WherePK().Update() | 
|  | 169 | +	if err != nil { | 
|  | 170 | +		impl.logger.Errorw("error in updating cd workflow status latest", "err", err, "model", model) | 
|  | 171 | +		return err | 
|  | 172 | +	} | 
|  | 173 | +	return nil | 
|  | 174 | +} | 
|  | 175 | + | 
|  | 176 | +func (impl *WorkflowStatusLatestRepositoryImpl) GetCdWorkflowStatusLatestByPipelineIdAndWorkflowType(tx *pg.Tx, pipelineId int, workflowType string) (*CdWorkflowStatusLatest, error) { | 
|  | 177 | +	var connection orm.DB | 
|  | 178 | +	if tx != nil { | 
|  | 179 | +		connection = tx | 
|  | 180 | +	} else { | 
|  | 181 | +		connection = impl.dbConnection | 
|  | 182 | +	} | 
|  | 183 | + | 
|  | 184 | +	var model CdWorkflowStatusLatest | 
|  | 185 | +	err := connection.Model(&model). | 
|  | 186 | +		Where("pipeline_id = ? AND workflow_type = ?", pipelineId, workflowType). | 
|  | 187 | +		Select() | 
|  | 188 | +	if err != nil { | 
|  | 189 | +		impl.logger.Errorw("error in getting cd workflow status latest by pipeline id and workflow type", "err", err, "pipelineId", pipelineId, "workflowType", workflowType) | 
|  | 190 | +		return nil, err | 
|  | 191 | +	} | 
|  | 192 | +	return &model, nil | 
|  | 193 | +} | 
0 commit comments