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 63
/
task_repo.go
136 lines (119 loc) · 3.76 KB
/
task_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package gormimpl
import (
"context"
"errors"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytestdlib/promutils"
flyteAdminDbErrors "github.com/flyteorg/flyteadmin/pkg/repositories/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"gorm.io/gorm"
)
// Implementation of TaskRepoInterface.
type TaskRepo struct {
db *gorm.DB
errorTransformer flyteAdminDbErrors.ErrorTransformer
metrics gormMetrics
}
func (r *TaskRepo) Create(ctx context.Context, input models.Task) error {
timer := r.metrics.CreateDuration.Start()
tx := r.db.Omit("id").Create(&input)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func (r *TaskRepo) Get(ctx context.Context, input interfaces.Identifier) (models.Task, error) {
var task models.Task
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.Task{
TaskKey: models.TaskKey{
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
Version: input.Version,
},
}).Take(&task)
timer.Stop()
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return models.Task{}, flyteAdminDbErrors.GetMissingEntityError(core.ResourceType_TASK.String(), &core.Identifier{
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
Version: input.Version,
})
}
if tx.Error != nil {
return models.Task{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return task, nil
}
func (r *TaskRepo) List(
ctx context.Context, input interfaces.ListResourceInput) (interfaces.TaskCollectionOutput, error) {
// First validate input.
if err := ValidateListInput(input); err != nil {
return interfaces.TaskCollectionOutput{}, err
}
var tasks []models.Task
tx := r.db.Limit(input.Limit).Offset(input.Offset)
// Apply filters
tx, err := applyFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.TaskCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
timer := r.metrics.ListDuration.Start()
tx.Find(&tasks)
timer.Stop()
if tx.Error != nil {
return interfaces.TaskCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.TaskCollectionOutput{
Tasks: tasks,
}, nil
}
func (r *TaskRepo) ListTaskIdentifiers(ctx context.Context, input interfaces.ListResourceInput) (
interfaces.TaskCollectionOutput, error) {
// Validate input.
if err := ValidateListInput(input); err != nil {
return interfaces.TaskCollectionOutput{}, err
}
tx := r.db.Model(models.Task{}).Limit(input.Limit).Offset(input.Offset)
// Apply filters
tx, err := applyFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.TaskCollectionOutput{}, err
}
for _, mapFilter := range input.MapFilters {
tx = tx.Where(mapFilter.GetFilter())
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
// Scan the results into a list of tasks
var tasks []models.Task
timer := r.metrics.ListIdentifiersDuration.Start()
tx.Select([]string{Project, Domain, Name}).Group(identifierGroupBy).Scan(&tasks)
timer.Stop()
if tx.Error != nil {
return interfaces.TaskCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.TaskCollectionOutput{
Tasks: tasks,
}, nil
}
// Returns an instance of TaskRepoInterface
func NewTaskRepo(
db *gorm.DB, errorTransformer flyteAdminDbErrors.ErrorTransformer, scope promutils.Scope) interfaces.TaskRepoInterface {
metrics := newMetrics(scope)
return &TaskRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}