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 62
/
description_entity_manager.go
120 lines (108 loc) · 3.88 KB
/
description_entity_manager.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
package impl
import (
"context"
"strconv"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteadmin/pkg/common"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/util"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/validation"
"github.com/flyteorg/flyteadmin/pkg/manager/interfaces"
repoInterfaces "github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/transformers"
runtimeInterfaces "github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flytestdlib/contextutils"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/promutils"
"google.golang.org/grpc/codes"
)
type DescriptionEntityMetrics struct {
Scope promutils.Scope
}
type DescriptionEntityManager struct {
db repoInterfaces.Repository
config runtimeInterfaces.Configuration
metrics DescriptionEntityMetrics
}
func (d *DescriptionEntityManager) GetDescriptionEntity(ctx context.Context, request admin.ObjectGetRequest) (
*admin.DescriptionEntity, error) {
if err := validation.ValidateDescriptionEntityGetRequest(request); err != nil {
logger.Errorf(ctx, "invalid request [%+v]: %v", request, err)
return nil, err
}
ctx = contextutils.WithProjectDomain(ctx, request.Id.Project, request.Id.Domain)
return util.GetDescriptionEntity(ctx, d.db, *request.Id)
}
func (d *DescriptionEntityManager) ListDescriptionEntity(ctx context.Context, request admin.DescriptionEntityListRequest) (*admin.DescriptionEntityList, error) {
// Check required fields
if err := validation.ValidateDescriptionEntityListRequest(request); err != nil {
return nil, err
}
ctx = contextutils.WithProjectDomain(ctx, request.Id.Project, request.Id.Domain)
if request.ResourceType == core.ResourceType_WORKFLOW {
ctx = contextutils.WithWorkflowID(ctx, request.Id.Name)
} else {
ctx = contextutils.WithTaskID(ctx, request.Id.Name)
}
filters, err := util.GetDbFilters(util.FilterSpec{
Project: request.Id.Project,
Domain: request.Id.Domain,
Name: request.Id.Name,
RequestFilters: request.Filters,
}, common.ResourceTypeToEntity[request.ResourceType])
if err != nil {
logger.Error(ctx, "failed to get database filter")
return nil, err
}
var sortParameter common.SortParameter
if request.SortBy != nil {
sortParameter, err = common.NewSortParameter(*request.SortBy)
if err != nil {
return nil, err
}
}
offset, err := validation.ValidateToken(request.Token)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"invalid pagination token %s for ListWorkflows", request.Token)
}
listDescriptionEntitiesInput := repoInterfaces.ListResourceInput{
Limit: int(request.Limit),
Offset: offset,
InlineFilters: filters,
SortParameter: sortParameter,
}
output, err := d.db.DescriptionEntityRepo().List(ctx, listDescriptionEntitiesInput)
if err != nil {
logger.Debugf(ctx, "Failed to list workflows with [%+v] with err %v", request.Id, err)
return nil, err
}
descriptionEntityList, err := transformers.FromDescriptionEntityModels(output.Entities)
if err != nil {
logger.Errorf(ctx,
"Failed to transform workflow models [%+v] with err: %v", output.Entities, err)
return nil, err
}
var token string
if len(output.Entities) == int(request.Limit) {
token = strconv.Itoa(offset + len(output.Entities))
}
return &admin.DescriptionEntityList{
DescriptionEntities: descriptionEntityList,
Token: token,
}, nil
}
func NewDescriptionEntityManager(
db repoInterfaces.Repository,
config runtimeInterfaces.Configuration,
scope promutils.Scope) interfaces.DescriptionEntityInterface {
metrics := DescriptionEntityMetrics{
Scope: scope,
}
return &DescriptionEntityManager{
db: db,
config: config,
metrics: metrics,
}
}