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
/
workflow_repo.go
133 lines (116 loc) · 3.9 KB
/
workflow_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
package gormimpl
import (
"context"
"errors"
flyteAdminDbErrors "github.com/flyteorg/flyteadmin/pkg/repositories/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytestdlib/promutils"
"gorm.io/gorm"
)
const workflowTableName = "workflows"
// Implementation of WorkflowRepoInterface.
type WorkflowRepo struct {
db *gorm.DB
errorTransformer flyteAdminDbErrors.ErrorTransformer
metrics gormMetrics
}
func (r *WorkflowRepo) Create(ctx context.Context, input models.Workflow) 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 *WorkflowRepo) Get(ctx context.Context, input interfaces.Identifier) (models.Workflow, error) {
var workflow models.Workflow
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.Workflow{
WorkflowKey: models.WorkflowKey{
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
Version: input.Version,
},
}).Take(&workflow)
timer.Stop()
if tx.Error != nil && errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return models.Workflow{}, flyteAdminDbErrors.GetMissingEntityError(core.ResourceType_WORKFLOW.String(), &core.Identifier{
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
Version: input.Version,
})
} else if tx.Error != nil {
return models.Workflow{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return workflow, nil
}
func (r *WorkflowRepo) List(
ctx context.Context, input interfaces.ListResourceInput) (interfaces.WorkflowCollectionOutput, error) {
// First validate input.
if err := ValidateListInput(input); err != nil {
return interfaces.WorkflowCollectionOutput{}, err
}
var workflows []models.Workflow
tx := r.db.Limit(input.Limit).Offset(input.Offset)
// Apply filters
tx, err := applyFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.WorkflowCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
timer := r.metrics.ListDuration.Start()
tx.Find(&workflows)
timer.Stop()
if tx.Error != nil {
return interfaces.WorkflowCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.WorkflowCollectionOutput{
Workflows: workflows,
}, nil
}
func (r *WorkflowRepo) ListIdentifiers(ctx context.Context, input interfaces.ListResourceInput) (
interfaces.WorkflowCollectionOutput, error) {
// Validate input.
if err := ValidateListInput(input); err != nil {
return interfaces.WorkflowCollectionOutput{}, err
}
tx := r.db.Model(models.Workflow{}).Limit(input.Limit).Offset(input.Offset)
// Apply filters
tx, err := applyFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.WorkflowCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
// Scan the results into a list of workflows
var workflows []models.Workflow
timer := r.metrics.ListIdentifiersDuration.Start()
tx.Select([]string{Project, Domain, Name}).Group(identifierGroupBy).Scan(&workflows)
timer.Stop()
if tx.Error != nil {
return interfaces.WorkflowCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.WorkflowCollectionOutput{
Workflows: workflows,
}, nil
}
// Returns an instance of WorkflowRepoInterface
func NewWorkflowRepo(
db *gorm.DB, errorTransformer flyteAdminDbErrors.ErrorTransformer, scope promutils.Scope) interfaces.WorkflowRepoInterface {
metrics := newMetrics(scope)
return &WorkflowRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}