-
Notifications
You must be signed in to change notification settings - Fork 473
/
VariableSnapshotHistoryService.go
62 lines (56 loc) · 2.3 KB
/
VariableSnapshotHistoryService.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package variables
import (
"github.com/devtron-labs/devtron/pkg/sql"
repository2 "github.com/devtron-labs/devtron/pkg/variables/repository"
"go.uber.org/zap"
"time"
)
type VariableSnapshotHistoryService interface {
SaveVariableHistoriesForTrigger(variableHistories []*repository2.VariableSnapshotHistoryBean, userId int32) error
GetVariableHistoryForReferences(references []repository2.HistoryReference) (map[repository2.HistoryReference]*repository2.VariableSnapshotHistoryBean, error)
}
type VariableSnapshotHistoryServiceImpl struct {
logger *zap.SugaredLogger
repository repository2.VariableSnapshotHistoryRepository
}
func NewVariableSnapshotHistoryServiceImpl(repository repository2.VariableSnapshotHistoryRepository, logger *zap.SugaredLogger) *VariableSnapshotHistoryServiceImpl {
return &VariableSnapshotHistoryServiceImpl{
repository: repository,
logger: logger,
}
}
func (impl VariableSnapshotHistoryServiceImpl) SaveVariableHistoriesForTrigger(variableHistories []*repository2.VariableSnapshotHistoryBean, userId int32) error {
variableSnapshotHistoryList := make([]*repository2.VariableSnapshotHistory, 0)
for _, history := range variableHistories {
variableSnapshotHistoryList = append(variableSnapshotHistoryList, &repository2.VariableSnapshotHistory{
VariableSnapshotHistoryBean: *history,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
})
}
if len(variableSnapshotHistoryList) > 0 {
err := impl.repository.SaveVariableSnapshots(variableSnapshotHistoryList)
if err != nil {
return err
}
}
return nil
}
func (impl VariableSnapshotHistoryServiceImpl) GetVariableHistoryForReferences(references []repository2.HistoryReference) (map[repository2.HistoryReference]*repository2.VariableSnapshotHistoryBean, error) {
snapshots, err := impl.repository.GetVariableSnapshots(references)
if err != nil {
return nil, err
}
variableSnapshotHistories := make(map[repository2.HistoryReference]*repository2.VariableSnapshotHistoryBean)
for _, snapshot := range snapshots {
variableSnapshotHistories[snapshot.HistoryReference] = &repository2.VariableSnapshotHistoryBean{
VariableSnapshot: snapshot.VariableSnapshot,
HistoryReference: snapshot.HistoryReference,
}
}
return variableSnapshotHistories, nil
}