| 
 | 1 | +package security  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	serverBean "github.com/devtron-labs/devtron/pkg/server/bean"  | 
 | 5 | +	"github.com/devtron-labs/devtron/pkg/sql"  | 
 | 6 | +	"github.com/go-pg/pg"  | 
 | 7 | +	"go.uber.org/zap"  | 
 | 8 | +	"time"  | 
 | 9 | +)  | 
 | 10 | + | 
 | 11 | +type ScanToolExecutionHistoryMapping struct {  | 
 | 12 | +	tableName                   struct{}                             `sql:"scan_tool_execution_history_mapping" pg:",discard_unknown_columns"`  | 
 | 13 | +	Id                          int                                  `sql:"id,pk"`  | 
 | 14 | +	ImageScanExecutionHistoryId int                                  `sql:"image_scan_execution_history_id"`  | 
 | 15 | +	ScanToolId                  int                                  `sql:"scan_tool_id"`  | 
 | 16 | +	ExecutionStartTime          time.Time                            `sql:"execution_start_time,notnull"`  | 
 | 17 | +	ExecutionFinishTime         time.Time                            `sql:"execution_finish_time,notnull"`  | 
 | 18 | +	State                       serverBean.ScanExecutionProcessState `sql:"state"`  | 
 | 19 | +	TryCount                    int                                  `sql:"try_count"`  | 
 | 20 | +	sql.AuditLog  | 
 | 21 | +}  | 
 | 22 | + | 
 | 23 | +type ScanToolExecutionHistoryMappingRepository interface {  | 
 | 24 | +	Save(model *ScanToolExecutionHistoryMapping) error  | 
 | 25 | +	SaveInBatch(models []*ScanToolExecutionHistoryMapping) error  | 
 | 26 | +	UpdateStateByToolAndExecutionHistoryId(executionHistoryId, toolId int, state serverBean.ScanExecutionProcessState, executionFinishTime time.Time) error  | 
 | 27 | +	MarkAllRunningStateAsFailedHavingTryCountReachedLimit(tryCount int) error  | 
 | 28 | +	GetAllScanHistoriesByState(state serverBean.ScanExecutionProcessState) ([]*ScanToolExecutionHistoryMapping, error)  | 
 | 29 | +	GetAllScanHistoriesByExecutionHistoryIdAndStates(executionHistoryId int, states []serverBean.ScanExecutionProcessState) ([]*ScanToolExecutionHistoryMapping, error)  | 
 | 30 | +	GetAllScanHistoriesByExecutionHistoryIds(ids []int) ([]*ScanToolExecutionHistoryMapping, error)  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +type ScanToolExecutionHistoryMappingRepositoryImpl struct {  | 
 | 34 | +	dbConnection *pg.DB  | 
 | 35 | +	logger       *zap.SugaredLogger  | 
 | 36 | +}  | 
 | 37 | + | 
 | 38 | +func NewScanToolExecutionHistoryMappingRepositoryImpl(dbConnection *pg.DB,  | 
 | 39 | +	logger *zap.SugaredLogger) *ScanToolExecutionHistoryMappingRepositoryImpl {  | 
 | 40 | +	return &ScanToolExecutionHistoryMappingRepositoryImpl{  | 
 | 41 | +		dbConnection: dbConnection,  | 
 | 42 | +		logger:       logger,  | 
 | 43 | +	}  | 
 | 44 | +}  | 
 | 45 | + | 
 | 46 | +func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) Save(model *ScanToolExecutionHistoryMapping) error {  | 
 | 47 | +	err := repo.dbConnection.Insert(model)  | 
 | 48 | +	if err != nil {  | 
 | 49 | +		repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, Save", "model", model, "err", err)  | 
 | 50 | +		return err  | 
 | 51 | +	}  | 
 | 52 | +	return nil  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) SaveInBatch(models []*ScanToolExecutionHistoryMapping) error {  | 
 | 56 | +	err := repo.dbConnection.Insert(&models)  | 
 | 57 | +	if err != nil {  | 
 | 58 | +		repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, SaveInBatch", "err", err, "models", models)  | 
 | 59 | +		return err  | 
 | 60 | +	}  | 
 | 61 | +	return nil  | 
 | 62 | +}  | 
 | 63 | + | 
 | 64 | +func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) UpdateStateByToolAndExecutionHistoryId(executionHistoryId, toolId int,  | 
 | 65 | +	state serverBean.ScanExecutionProcessState, executionFinishTime time.Time) error {  | 
 | 66 | +	model := &ScanToolExecutionHistoryMapping{}  | 
 | 67 | +	_, err := repo.dbConnection.Model(model).Set("state = ?", state).  | 
 | 68 | +		Set("execution_finish_time  = ?", executionFinishTime).  | 
 | 69 | +		Set("updated_on = ?", time.Now()).  | 
 | 70 | +		Set("updated_by =?", time.Now()).  | 
 | 71 | +		Where("image_scan_execution_history_id = ?", executionHistoryId).  | 
 | 72 | +		Where("scan_tool_id = ?", toolId).Update()  | 
 | 73 | +	if err != nil {  | 
 | 74 | +		repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, SaveInBatch", "err", err, "model", model)  | 
 | 75 | +		return err  | 
 | 76 | +	}  | 
 | 77 | +	return nil  | 
 | 78 | +}  | 
 | 79 | + | 
 | 80 | +func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) MarkAllRunningStateAsFailedHavingTryCountReachedLimit(tryCount int) error {  | 
 | 81 | +	var models []*ScanToolExecutionHistoryMapping  | 
 | 82 | +	_, err := repo.dbConnection.Model(&models).  | 
 | 83 | +		Set("state = ?", serverBean.ScanExecutionProcessStateFailed).  | 
 | 84 | +		Set("updated_on = ?", time.Now()).  | 
 | 85 | +		Set("updated_by =?", time.Now()).  | 
 | 86 | +		Where("state = ?", serverBean.ScanExecutionProcessStateRunning).  | 
 | 87 | +		Where("try_count > ?", tryCount).Update()  | 
 | 88 | +	if err != nil {  | 
 | 89 | +		repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, MarkAllRunningStateAsFailedHavingTryCountReachedLimit", "err", err)  | 
 | 90 | +		return err  | 
 | 91 | +	}  | 
 | 92 | +	return nil  | 
 | 93 | +}  | 
 | 94 | + | 
 | 95 | +func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) GetAllScanHistoriesByState(state serverBean.ScanExecutionProcessState) ([]*ScanToolExecutionHistoryMapping, error) {  | 
 | 96 | +	var models []*ScanToolExecutionHistoryMapping  | 
 | 97 | +	err := repo.dbConnection.Model(&models).Column("scan_tool_execution_history_mapping.*").  | 
 | 98 | +		Where("state = ?", state).Select()  | 
 | 99 | +	if err != nil {  | 
 | 100 | +		repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, GetAllScanHistoriesByState", "err", err)  | 
 | 101 | +		return nil, err  | 
 | 102 | +	}  | 
 | 103 | +	return models, nil  | 
 | 104 | +}  | 
 | 105 | + | 
 | 106 | +func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) GetAllScanHistoriesByExecutionHistoryIdAndStates(executionHistoryId int, states []serverBean.ScanExecutionProcessState) ([]*ScanToolExecutionHistoryMapping, error) {  | 
 | 107 | +	var models []*ScanToolExecutionHistoryMapping  | 
 | 108 | +	err := repo.dbConnection.Model(&models).Column("scan_tool_execution_history_mapping.*").  | 
 | 109 | +		Where("image_scan_execution_history_id = ?", executionHistoryId).  | 
 | 110 | +		Where("state in (?)", pg.In(states)).Select()  | 
 | 111 | +	if err != nil {  | 
 | 112 | +		repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, GetAllScanHistoriesByState", "err", err)  | 
 | 113 | +		return nil, err  | 
 | 114 | +	}  | 
 | 115 | +	return models, nil  | 
 | 116 | +}  | 
 | 117 | +func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) GetAllScanHistoriesByExecutionHistoryIds(ids []int) ([]*ScanToolExecutionHistoryMapping, error) {  | 
 | 118 | +	var models []*ScanToolExecutionHistoryMapping  | 
 | 119 | +	err := repo.dbConnection.Model(&models).Column("scan_tool_execution_history_mapping.*").  | 
 | 120 | +		Where("image_scan_execution_history_id in (?)", pg.In(ids)).  | 
 | 121 | +		Select()  | 
 | 122 | +	if err != nil {  | 
 | 123 | +		repo.logger.Errorw("error in getting ScanToolExecutionHistoryMappingRepository, GetAllScanHistoriesByState", "err", err)  | 
 | 124 | +		return nil, err  | 
 | 125 | +	}  | 
 | 126 | +	return models, nil  | 
 | 127 | +}  | 
0 commit comments