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
/
Copy pathexecution_repo.go
159 lines (144 loc) · 4.8 KB
/
execution_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gormimpl
import (
"context"
"fmt"
"github.com/lyft/flyteadmin/pkg/common"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/jinzhu/gorm"
"github.com/lyft/flyteadmin/pkg/repositories/errors"
"github.com/lyft/flyteadmin/pkg/repositories/interfaces"
"github.com/lyft/flyteadmin/pkg/repositories/models"
"github.com/lyft/flytestdlib/promutils"
)
// Implementation of ExecutionInterface.
type ExecutionRepo struct {
db *gorm.DB
errorTransformer errors.ErrorTransformer
metrics gormMetrics
}
func (r *ExecutionRepo) Create(ctx context.Context, input models.Execution) error {
timer := r.metrics.CreateDuration.Start()
tx := r.db.Create(&input)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func (r *ExecutionRepo) Get(ctx context.Context, input interfaces.GetResourceInput) (models.Execution, error) {
var execution models.Execution
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.Execution{
ExecutionKey: models.ExecutionKey{
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
},
}).First(&execution)
timer.Stop()
if tx.Error != nil {
return models.Execution{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
if tx.RecordNotFound() {
return models.Execution{}, errors.GetMissingEntityError("execution", &core.Identifier{
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
})
}
return execution, nil
}
func (r *ExecutionRepo) GetByID(ctx context.Context, id uint) (models.Execution, error) {
var execution models.Execution
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.Execution{
BaseModel: models.BaseModel{
ID: id,
},
}).First(&execution)
timer.Stop()
if tx.Error != nil {
return models.Execution{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
if tx.RecordNotFound() {
return models.Execution{}, errors.GetMissingEntityByIDError("execution")
}
return execution, nil
}
func (r *ExecutionRepo) Update(ctx context.Context, event models.ExecutionEvent, execution models.Execution) error {
timer := r.metrics.UpdateDuration.Start()
defer timer.Stop()
// Use a transaction to guarantee no partial updates.
tx := r.db.Begin()
if err := tx.Create(&event).Error; err != nil {
tx.Rollback()
return r.errorTransformer.ToFlyteAdminError(err)
}
if err := r.db.Model(&execution).Updates(execution).Error; err != nil {
tx.Rollback()
return r.errorTransformer.ToFlyteAdminError(err)
}
if err := tx.Commit().Error; err != nil {
return r.errorTransformer.ToFlyteAdminError(err)
}
return nil
}
func (r *ExecutionRepo) UpdateExecution(ctx context.Context, execution models.Execution) error {
timer := r.metrics.UpdateDuration.Start()
tx := r.db.Model(&execution).Updates(execution)
timer.Stop()
if err := tx.Error; err != nil {
return r.errorTransformer.ToFlyteAdminError(err)
}
return nil
}
func (r *ExecutionRepo) List(ctx context.Context, input interfaces.ListResourceInput) (
interfaces.ExecutionCollectionOutput, error) {
// First validate input.
if err := ValidateListInput(input); err != nil {
return interfaces.ExecutionCollectionOutput{}, err
}
var executions []models.Execution
tx := r.db.Limit(input.Limit).Offset(input.Offset)
// And add join condition as required by user-specified filters (which can potentially include join table attrs).
if ok := input.JoinTableEntities[common.LaunchPlan]; ok {
tx = tx.Joins(fmt.Sprintf("INNER JOIN %s ON %s.launch_plan_id = %s.id",
launchPlanTableName, executionTableName, launchPlanTableName))
}
if ok := input.JoinTableEntities[common.Workflow]; ok {
tx = tx.Joins(fmt.Sprintf("INNER JOIN %s ON %s.workflow_id = %s.id",
workflowTableName, executionTableName, workflowTableName))
}
if ok := input.JoinTableEntities[common.Task]; ok {
tx = tx.Joins(fmt.Sprintf("INNER JOIN %s ON %s.task_id = %s.id",
taskTableName, executionTableName, taskTableName))
}
// Apply filters
tx, err := applyScopedFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.ExecutionCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
timer := r.metrics.ListDuration.Start()
tx = tx.Find(&executions)
timer.Stop()
if tx.Error != nil {
return interfaces.ExecutionCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.ExecutionCollectionOutput{
Executions: executions,
}, nil
}
// Returns an instance of ExecutionRepoInterface
func NewExecutionRepo(
db *gorm.DB, errorTransformer errors.ErrorTransformer, scope promutils.Scope) interfaces.ExecutionRepoInterface {
metrics := newMetrics(scope)
return &ExecutionRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}