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 pathnamed_entity_repo.go
224 lines (184 loc) · 8.15 KB
/
named_entity_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package gormimpl
import (
"context"
"fmt"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"google.golang.org/grpc/codes"
"github.com/jinzhu/gorm"
"github.com/lyft/flyteadmin/pkg/common"
adminErrors "github.com/lyft/flyteadmin/pkg/errors"
"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"
)
const innerJoinTableAlias = "entities"
var resourceTypeToTableName = map[core.ResourceType]string{
core.ResourceType_LAUNCH_PLAN: launchPlanTableName,
core.ResourceType_WORKFLOW: workflowTableName,
core.ResourceType_TASK: taskTableName,
}
var joinString = "RIGHT JOIN ? AS entities ON named_entity_metadata.resource_type = %d AND " +
"named_entity_metadata.project = entities.project AND named_entity_metadata.domain = entities.domain AND " +
"named_entity_metadata.name = entities.name"
func getSubQueryJoin(db *gorm.DB, tableName string, input interfaces.ListNamedEntityInput) *gorm.DB {
tx := db.Select([]string{Project, Domain, Name}).
Table(tableName).
Where(map[string]interface{}{Project: input.Project, Domain: input.Domain}).
Limit(input.Limit).
Offset(input.Offset).
Group(identifierGroupBy)
// Apply consistent sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
return db.Joins(fmt.Sprintf(joinString, input.ResourceType), tx.SubQuery())
}
var leftJoinWorkflowNameToMetadata = fmt.Sprintf(
"LEFT JOIN %s ON %s.resource_type = %d AND %s.project = %s.project AND %s.domain = %s.domain AND %s.name = %s.name", namedEntityMetadataTableName, namedEntityMetadataTableName, core.ResourceType_WORKFLOW, namedEntityMetadataTableName, workflowTableName,
namedEntityMetadataTableName, workflowTableName,
namedEntityMetadataTableName, workflowTableName)
var leftJoinLaunchPlanNameToMetadata = fmt.Sprintf(
"LEFT JOIN %s ON %s.resource_type = %d AND %s.project = %s.project AND %s.domain = %s.domain AND %s.name = %s.name", namedEntityMetadataTableName, namedEntityMetadataTableName, core.ResourceType_LAUNCH_PLAN, namedEntityMetadataTableName, launchPlanTableName,
namedEntityMetadataTableName, launchPlanTableName,
namedEntityMetadataTableName, launchPlanTableName)
var leftJoinTaskNameToMetadata = fmt.Sprintf(
"LEFT JOIN %s ON %s.resource_type = %d AND %s.project = %s.project AND %s.domain = %s.domain AND %s.name = %s.name", namedEntityMetadataTableName, namedEntityMetadataTableName, core.ResourceType_TASK, namedEntityMetadataTableName, taskTableName,
namedEntityMetadataTableName, taskTableName,
namedEntityMetadataTableName, taskTableName)
var resourceTypeToMetadataJoin = map[core.ResourceType]string{
core.ResourceType_LAUNCH_PLAN: leftJoinLaunchPlanNameToMetadata,
core.ResourceType_WORKFLOW: leftJoinWorkflowNameToMetadata,
core.ResourceType_TASK: leftJoinTaskNameToMetadata,
}
var getGroupByForNamedEntity = fmt.Sprintf("%s.%s, %s.%s, %s.%s, %s.%s, %s.%s",
innerJoinTableAlias, Project, innerJoinTableAlias, Domain, innerJoinTableAlias, Name, namedEntityMetadataTableName,
Description,
namedEntityMetadataTableName, State)
func getSelectForNamedEntity(tableName string, resourceType core.ResourceType) []string {
return []string{
fmt.Sprintf("%s.%s", tableName, Project),
fmt.Sprintf("%s.%s", tableName, Domain),
fmt.Sprintf("%s.%s", tableName, Name),
fmt.Sprintf("'%d' AS %s", resourceType, ResourceType),
fmt.Sprintf("%s.%s", namedEntityMetadataTableName, Description),
fmt.Sprintf("%s.%s", namedEntityMetadataTableName, State),
}
}
func getNamedEntityFilters(resourceType core.ResourceType, project string, domain string, name string) ([]common.InlineFilter, error) {
entity := common.ResourceTypeToEntity[resourceType]
filters := make([]common.InlineFilter, 0)
projectFilter, err := common.NewSingleValueFilter(entity, common.Equal, Project, project)
if err != nil {
return nil, err
}
filters = append(filters, projectFilter)
domainFilter, err := common.NewSingleValueFilter(entity, common.Equal, Domain, domain)
if err != nil {
return nil, err
}
filters = append(filters, domainFilter)
nameFilter, err := common.NewSingleValueFilter(entity, common.Equal, Name, name)
if err != nil {
return nil, err
}
filters = append(filters, nameFilter)
return filters, nil
}
// Implementation of NamedEntityRepoInterface.
type NamedEntityRepo struct {
db *gorm.DB
errorTransformer errors.ErrorTransformer
metrics gormMetrics
}
func (r *NamedEntityRepo) Update(ctx context.Context, input models.NamedEntity) error {
timer := r.metrics.UpdateDuration.Start()
var metadata models.NamedEntityMetadata
tx := r.db.Where(&models.NamedEntityMetadata{
NamedEntityMetadataKey: models.NamedEntityMetadataKey{
ResourceType: input.ResourceType,
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
},
}).Assign(input.NamedEntityMetadataFields).FirstOrCreate(&metadata)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func (r *NamedEntityRepo) Get(ctx context.Context, input interfaces.GetNamedEntityInput) (models.NamedEntity, error) {
var namedEntity models.NamedEntity
filters, err := getNamedEntityFilters(input.ResourceType, input.Project, input.Domain, input.Name)
if err != nil {
return models.NamedEntity{}, err
}
tableName, tableFound := resourceTypeToTableName[input.ResourceType]
joinString, joinFound := resourceTypeToMetadataJoin[input.ResourceType]
if !tableFound || !joinFound {
return models.NamedEntity{}, adminErrors.NewFlyteAdminErrorf(codes.InvalidArgument, "Cannot get NamedEntityMetadata for resource type: %v", input.ResourceType)
}
tx := r.db.Table(tableName).Joins(joinString)
// Apply filters
tx, err = applyScopedFilters(tx, filters, nil)
if err != nil {
return models.NamedEntity{}, err
}
timer := r.metrics.GetDuration.Start()
tx = tx.Select(getSelectForNamedEntity(tableName, input.ResourceType)).First(&namedEntity)
timer.Stop()
if tx.Error != nil {
return models.NamedEntity{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return namedEntity, nil
}
func (r *NamedEntityRepo) List(ctx context.Context, input interfaces.ListNamedEntityInput) (
interfaces.NamedEntityCollectionOutput, error) {
// Validate input. Filters aren't required because they're implicit in the Project & Domain specified by the input.
if len(input.Project) == 0 {
return interfaces.NamedEntityCollectionOutput{}, errors.GetInvalidInputError(Project)
}
if len(input.Domain) == 0 {
return interfaces.NamedEntityCollectionOutput{}, errors.GetInvalidInputError(Domain)
}
if input.Limit == 0 {
return interfaces.NamedEntityCollectionOutput{}, errors.GetInvalidInputError(limit)
}
tableName, tableFound := resourceTypeToTableName[input.ResourceType]
if !tableFound {
return interfaces.NamedEntityCollectionOutput{}, adminErrors.NewFlyteAdminErrorf(codes.InvalidArgument,
"Cannot list entity names for resource type: %v", input.ResourceType)
}
tx := getSubQueryJoin(r.db, tableName, input)
// Apply filters
tx, err := applyScopedFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.NamedEntityCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
// Scan the results into a list of named entities
var entities []models.NamedEntity
timer := r.metrics.ListDuration.Start()
tx.Select(getSelectForNamedEntity(innerJoinTableAlias, input.ResourceType)).Table(namedEntityMetadataTableName).Group(getGroupByForNamedEntity).Scan(&entities)
timer.Stop()
if tx.Error != nil {
return interfaces.NamedEntityCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.NamedEntityCollectionOutput{
Entities: entities,
}, nil
}
// Returns an instance of NamedEntityRepoInterface
func NewNamedEntityRepo(
db *gorm.DB, errorTransformer errors.ErrorTransformer, scope promutils.Scope) interfaces.NamedEntityRepoInterface {
metrics := newMetrics(scope)
return &NamedEntityRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}