This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
schedule_entities_snapshot_repo.go
57 lines (49 loc) · 1.99 KB
/
schedule_entities_snapshot_repo.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
package gormimpl
import (
"context"
"errors"
flyteSchedulerDbErrors "github.com/flyteorg/flyteadmin/pkg/repositories/errors"
interfaces2 "github.com/flyteorg/flyteadmin/scheduler/repositories/interfaces"
"github.com/flyteorg/flyteadmin/scheduler/repositories/models"
"github.com/flyteorg/flytestdlib/promutils"
"gorm.io/gorm"
)
// ScheduleEntitiesSnapshotRepo Implementation of ScheduleEntitiesSnapshotRepoInterface.
type ScheduleEntitiesSnapshotRepo struct {
db *gorm.DB
errorTransformer flyteSchedulerDbErrors.ErrorTransformer
metrics gormMetrics
}
// TODO : always overwrite the exisiting snapshot instead of creating new rows
func (r *ScheduleEntitiesSnapshotRepo) Write(ctx context.Context, input models.ScheduleEntitiesSnapshot) error {
timer := r.metrics.GetDuration.Start()
tx := r.db.Omit("id").Create(&input)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func (r *ScheduleEntitiesSnapshotRepo) Read(ctx context.Context) (models.ScheduleEntitiesSnapshot, error) {
var schedulableEntitiesSnapshot models.ScheduleEntitiesSnapshot
timer := r.metrics.GetDuration.Start()
tx := r.db.Last(&schedulableEntitiesSnapshot)
timer.Stop()
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return models.ScheduleEntitiesSnapshot{}, flyteSchedulerDbErrors.GetSingletonMissingEntityError("schedule_entities_snapshots")
}
return models.ScheduleEntitiesSnapshot{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return schedulableEntitiesSnapshot, nil
}
// NewScheduleEntitiesSnapshotRepo Returns an instance of ScheduleEntitiesSnapshotRepoInterface
func NewScheduleEntitiesSnapshotRepo(
db *gorm.DB, errorTransformer flyteSchedulerDbErrors.ErrorTransformer, scope promutils.Scope) interfaces2.ScheduleEntitiesSnapShotRepoInterface {
metrics := newMetrics(scope)
return &ScheduleEntitiesSnapshotRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}