forked from raystack/compass
/
service.go
273 lines (219 loc) · 6.75 KB
/
service.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package asset
import (
"context"
"fmt"
"github.com/google/uuid"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
type Service struct {
assetRepository Repository
discoveryRepository DiscoveryRepository
lineageRepository LineageRepository
worker Worker
assetOpCounter metric.Int64Counter
}
//go:generate mockery --name=Worker -r --case underscore --with-expecter --structname Worker --filename worker_mock.go --output=./mocks
type Worker interface {
EnqueueIndexAssetJob(ctx context.Context, ast Asset) error
EnqueueDeleteAssetJob(ctx context.Context, urn string) error
EnqueueSyncAssetJob(ctx context.Context, service string) error
Close() error
}
type ServiceDeps struct {
AssetRepo Repository
DiscoveryRepo DiscoveryRepository
LineageRepo LineageRepository
Worker Worker
}
func NewService(deps ServiceDeps) *Service {
assetOpCounter, err := otel.Meter("github.com/goto/compass/core/asset").
Int64Counter("compass.asset.operation")
if err != nil {
otel.Handle(err)
}
return &Service{
assetRepository: deps.AssetRepo,
discoveryRepository: deps.DiscoveryRepo,
lineageRepository: deps.LineageRepo,
worker: deps.Worker,
assetOpCounter: assetOpCounter,
}
}
func (s *Service) GetAllAssets(ctx context.Context, flt Filter, withTotal bool) ([]Asset, uint32, error) {
var totalCount uint32 = 0
assets, err := s.assetRepository.GetAll(ctx, flt)
if err != nil {
return nil, totalCount, err
}
if withTotal {
total, err := s.assetRepository.GetCount(ctx, flt)
if err != nil {
return nil, totalCount, err
}
totalCount = uint32(total)
}
return assets, totalCount, nil
}
func (s *Service) UpsertAsset(ctx context.Context, ast *Asset, upstreams, downstreams []string) (string, error) {
assetID, err := s.UpsertAssetWithoutLineage(ctx, ast)
if err != nil {
return "", err
}
if err := s.lineageRepository.Upsert(ctx, ast.URN, upstreams, downstreams); err != nil {
return "", err
}
return assetID, nil
}
func (s *Service) UpsertAssetWithoutLineage(ctx context.Context, ast *Asset) (string, error) {
assetID, err := s.assetRepository.Upsert(ctx, ast)
if err != nil {
return "", err
}
ast.ID = assetID
if err := s.worker.EnqueueIndexAssetJob(ctx, *ast); err != nil {
return "", err
}
return assetID, nil
}
func (s *Service) DeleteAsset(ctx context.Context, id string) (err error) {
defer func() {
s.instrumentAssetOp(ctx, "DeleteAsset", id, err)
}()
urn := id
if isValidUUID(id) {
asset, err := s.assetRepository.GetByID(ctx, id)
if err != nil {
return err
}
urn = asset.URN
}
if err := s.assetRepository.DeleteByURN(ctx, urn); err != nil {
return err
}
if err := s.worker.EnqueueDeleteAssetJob(ctx, urn); err != nil {
return err
}
return s.lineageRepository.DeleteByURN(ctx, urn)
}
func (s *Service) GetAssetByID(ctx context.Context, id string) (Asset, error) {
ast, err := s.assetByIDWithoutProbes(ctx, "GetAssetByID", id)
if err != nil {
return Asset{}, err
}
probes, err := s.assetRepository.GetProbes(ctx, ast.URN)
if err != nil {
return Asset{}, fmt.Errorf("get probes: %w", err)
}
ast.Probes = probes
return ast, nil
}
func (s *Service) GetAssetByIDWithoutProbes(ctx context.Context, id string) (Asset, error) {
return s.assetByIDWithoutProbes(ctx, "GetAssetByIDWithoutProbes", id)
}
func (s *Service) assetByIDWithoutProbes(ctx context.Context, op, id string) (ast Asset, err error) {
defer func() {
s.instrumentAssetOp(ctx, op, id, err)
}()
if isValidUUID(id) {
ast, err = s.assetRepository.GetByID(ctx, id)
if err != nil {
return Asset{}, fmt.Errorf("get asset by id: %w", err)
}
return ast, nil
}
ast, err = s.assetRepository.GetByURN(ctx, id)
if err != nil {
return Asset{}, fmt.Errorf("get asset by urn: %w", err)
}
return ast, nil
}
func (s *Service) GetAssetByVersion(ctx context.Context, id, version string) (ast Asset, err error) {
defer func() {
s.instrumentAssetOp(ctx, "GetAssetByVersion", id, err)
}()
if isValidUUID(id) {
return s.assetRepository.GetByVersionWithID(ctx, id, version)
}
return s.assetRepository.GetByVersionWithURN(ctx, id, version)
}
func (s *Service) GetAssetVersionHistory(ctx context.Context, flt Filter, id string) ([]Asset, error) {
return s.assetRepository.GetVersionHistory(ctx, flt, id)
}
func (s *Service) AddProbe(ctx context.Context, assetURN string, probe *Probe) error {
return s.assetRepository.AddProbe(ctx, assetURN, probe)
}
func (s *Service) GetLineage(ctx context.Context, urn string, query LineageQuery) (Lineage, error) {
edges, err := s.lineageRepository.GetGraph(ctx, urn, query)
if err != nil {
return Lineage{}, fmt.Errorf("get lineage: get graph edges: %w", err)
}
urns := newUniqueStrings(len(edges))
urns.add(urn)
for _, edge := range edges {
urns.add(edge.Source, edge.Target)
}
assetProbes, err := s.assetRepository.GetProbesWithFilter(ctx, ProbesFilter{
AssetURNs: urns.list(),
MaxRows: 1,
})
if err != nil {
return Lineage{}, fmt.Errorf("get lineage: get latest probes: %w", err)
}
return Lineage{
Edges: edges,
NodeAttrs: buildNodeAttrs(assetProbes),
}, nil
}
func (s *Service) GetTypes(ctx context.Context, flt Filter) (map[Type]int, error) {
result, err := s.assetRepository.GetTypes(ctx, flt)
if err != nil {
return nil, err
}
return result, nil
}
func (s *Service) SearchAssets(ctx context.Context, cfg SearchConfig) (results []SearchResult, err error) {
return s.discoveryRepository.Search(ctx, cfg)
}
func (s *Service) GroupAssets(ctx context.Context, cfg GroupConfig) (results []GroupResult, err error) {
return s.discoveryRepository.GroupAssets(ctx, cfg)
}
func (s *Service) SuggestAssets(ctx context.Context, cfg SearchConfig) (suggestions []string, err error) {
return s.discoveryRepository.Suggest(ctx, cfg)
}
func (s *Service) SyncAssets(ctx context.Context, services []string) error {
for _, service := range services {
if err := s.worker.EnqueueSyncAssetJob(ctx, service); err != nil {
return err
}
}
return nil
}
func (s *Service) instrumentAssetOp(ctx context.Context, op, id string, err error) {
identifier := "URN"
if isValidUUID(id) {
identifier = "ID"
}
s.assetOpCounter.Add(ctx, 1, metric.WithAttributes(
attribute.String("compass.asset_operation", op),
attribute.String("asset.identifier", identifier),
attribute.Bool("operation.success", err == nil),
))
}
func isValidUUID(u string) bool {
_, err := uuid.Parse(u)
return err == nil
}
func buildNodeAttrs(assetProbes map[string][]Probe) map[string]NodeAttributes {
nodeAttrs := make(map[string]NodeAttributes, len(assetProbes))
for urn, probes := range assetProbes {
if len(probes) == 0 {
continue
}
nodeAttrs[urn] = NodeAttributes{
Probes: ProbesInfo{Latest: probes[0]},
}
}
return nodeAttrs
}