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 pathnode_execution_repo.go
175 lines (159 loc) · 6.08 KB
/
node_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package gormimpl
import (
"context"
"fmt"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/flyteorg/flyteadmin/pkg/repositories/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/jinzhu/gorm"
)
// Implementation of NodeExecutionInterface.
type NodeExecutionRepo struct {
db *gorm.DB
errorTransformer errors.ErrorTransformer
metrics gormMetrics
}
func (r *NodeExecutionRepo) Create(ctx context.Context, execution *models.NodeExecution) error {
timer := r.metrics.CreateDuration.Start()
tx := r.db.Create(&execution)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func (r *NodeExecutionRepo) Get(ctx context.Context, input interfaces.NodeExecutionResource) (models.NodeExecution, error) {
var nodeExecution models.NodeExecution
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.NodeExecution{
NodeExecutionKey: models.NodeExecutionKey{
NodeID: input.NodeExecutionIdentifier.NodeId,
ExecutionKey: models.ExecutionKey{
Project: input.NodeExecutionIdentifier.ExecutionId.Project,
Domain: input.NodeExecutionIdentifier.ExecutionId.Domain,
Name: input.NodeExecutionIdentifier.ExecutionId.Name,
},
},
}).Preload("ChildNodeExecutions").Take(&nodeExecution)
timer.Stop()
if tx.Error != nil {
return models.NodeExecution{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
if tx.RecordNotFound() {
return models.NodeExecution{},
errors.GetMissingEntityError("node execution", &core.NodeExecutionIdentifier{
NodeId: input.NodeExecutionIdentifier.NodeId,
ExecutionId: &core.WorkflowExecutionIdentifier{
Project: input.NodeExecutionIdentifier.ExecutionId.Project,
Domain: input.NodeExecutionIdentifier.ExecutionId.Domain,
Name: input.NodeExecutionIdentifier.ExecutionId.Name,
},
})
}
return nodeExecution, nil
}
func (r *NodeExecutionRepo) Update(ctx context.Context, nodeExecution *models.NodeExecution) error {
timer := r.metrics.UpdateDuration.Start()
tx := r.db.Model(&nodeExecution).Updates(nodeExecution)
timer.Stop()
if err := tx.Error; err != nil {
return r.errorTransformer.ToFlyteAdminError(err)
}
return nil
}
func (r *NodeExecutionRepo) List(ctx context.Context, input interfaces.ListResourceInput) (
interfaces.NodeExecutionCollectionOutput, error) {
// First validate input.
if err := ValidateListInput(input); err != nil {
return interfaces.NodeExecutionCollectionOutput{}, err
}
var nodeExecutions []models.NodeExecution
tx := r.db.Limit(input.Limit).Offset(input.Offset).Preload("ChildNodeExecutions")
// And add join condition (joining multiple tables is fine even we only filter on a subset of table attributes).
// (this query isn't called for deletes).
tx = tx.Joins(fmt.Sprintf("INNER JOIN %s ON %s.execution_project = %s.execution_project AND "+
"%s.execution_domain = %s.execution_domain AND %s.execution_name = %s.execution_name",
executionTableName, nodeExecutionTableName, executionTableName,
nodeExecutionTableName, executionTableName, nodeExecutionTableName, executionTableName))
// Apply filters
tx, err := applyScopedFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.NodeExecutionCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
timer := r.metrics.ListDuration.Start()
tx = tx.Find(&nodeExecutions)
timer.Stop()
if tx.Error != nil {
return interfaces.NodeExecutionCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.NodeExecutionCollectionOutput{
NodeExecutions: nodeExecutions,
}, nil
}
func (r *NodeExecutionRepo) ListEvents(
ctx context.Context, input interfaces.ListResourceInput) (interfaces.NodeExecutionEventCollectionOutput, error) {
// First validate input.
if err := ValidateListInput(input); err != nil {
return interfaces.NodeExecutionEventCollectionOutput{}, err
}
var nodeExecutionEvents []models.NodeExecutionEvent
tx := r.db.Limit(input.Limit).Offset(input.Offset)
// And add join condition (joining multiple tables is fine even we only filter on a subset of table attributes).
// (this query isn't called for deletes).
tx = tx.Joins(innerJoinNodeExecToNodeEvents)
tx = tx.Joins(innerJoinExecToNodeExec)
// Apply filters
tx, err := applyScopedFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.NodeExecutionEventCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
timer := r.metrics.ListDuration.Start()
tx = tx.Find(&nodeExecutionEvents)
timer.Stop()
if tx.Error != nil {
return interfaces.NodeExecutionEventCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.NodeExecutionEventCollectionOutput{
NodeExecutionEvents: nodeExecutionEvents,
}, nil
}
func (r *NodeExecutionRepo) Exists(ctx context.Context, input interfaces.NodeExecutionResource) (bool, error) {
var nodeExecution models.NodeExecution
timer := r.metrics.ExistsDuration.Start()
tx := r.db.Select(ID).Where(&models.NodeExecution{
NodeExecutionKey: models.NodeExecutionKey{
NodeID: input.NodeExecutionIdentifier.NodeId,
ExecutionKey: models.ExecutionKey{
Project: input.NodeExecutionIdentifier.ExecutionId.Project,
Domain: input.NodeExecutionIdentifier.ExecutionId.Domain,
Name: input.NodeExecutionIdentifier.ExecutionId.Name,
},
},
}).Take(&nodeExecution)
timer.Stop()
if tx.Error != nil {
return false, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return !tx.RecordNotFound(), nil
}
// Returns an instance of NodeExecutionRepoInterface
func NewNodeExecutionRepo(
db *gorm.DB, errorTransformer errors.ErrorTransformer,
scope promutils.Scope) interfaces.NodeExecutionRepoInterface {
metrics := newMetrics(scope)
return &NodeExecutionRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}