forked from raystack/optimus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bq_upstream_identifier.go
163 lines (141 loc) · 4.81 KB
/
bq_upstream_identifier.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
package upstreamidentifier
import (
"context"
"fmt"
"github.com/goto/salt/log"
"github.com/goto/optimus/core/resource"
"github.com/goto/optimus/ext/store/bigquery"
"github.com/goto/optimus/internal/errors"
)
type (
BQExtractorFunc func(context.Context, []bigquery.ResourceURN) (map[bigquery.ResourceURN]string, error)
extractorFunc func(ctx context.Context, resourceURNs []string) (map[string]string, error)
)
type BQUpstreamIdentifier struct {
logger log.Logger
parserFunc ParserFunc
extractorFunc extractorFunc
evaluatorFuncs []EvalAssetFunc
}
func (g BQUpstreamIdentifier) IdentifyResources(ctx context.Context, assets map[string]string) ([]resource.URN, error) {
resourcesAccumulation := []*bigquery.ResourceURNWithUpstreams{}
// generate resource urn with upstream from each evaluator
me := errors.NewMultiError("identify resource errors")
for _, evaluatorFunc := range g.evaluatorFuncs {
query := evaluatorFunc(assets)
if query == "" {
continue
}
visited := map[string][]*bigquery.ResourceURNWithUpstreams{}
paths := map[string]bool{}
resources, err := g.identifyResources(ctx, query, visited, paths)
if err != nil {
me.Append(err)
continue
}
resourcesAccumulation = append(resourcesAccumulation, resources...)
}
// compiled all collected resources and extract its urns
flattenedResources := bigquery.ResourceURNWithUpstreamsList(resourcesAccumulation).FlattenUnique()
var resourceURNs []resource.URN
for _, r := range flattenedResources {
rawURN := r.ResourceURN.URN()
urn, err := resource.ParseURN(rawURN)
if err != nil {
me.Append(err)
continue
}
resourceURNs = append(resourceURNs, urn)
}
return resourceURNs, me.ToErr()
}
func (g BQUpstreamIdentifier) identifyResources(ctx context.Context, query string, visited map[string][]*bigquery.ResourceURNWithUpstreams, paths map[string]bool) ([]*bigquery.ResourceURNWithUpstreams, error) {
me := errors.NewMultiError("identify resources errors")
resourceURNs := g.parserFunc(query)
resources := []*bigquery.ResourceURNWithUpstreams{}
if len(resourceURNs) == 0 {
return resources, nil
}
urnToQuery, err := g.extractorFunc(ctx, resourceURNs)
if err != nil {
g.logger.Error(fmt.Sprintf("error when extract ddl resource: %s", err.Error()))
return nil, err
}
for _, resourceURN := range resourceURNs {
bqResourceURN, err := bigquery.NewResourceURNFromString(resourceURN)
if err != nil {
g.logger.Error(err.Error())
continue
}
resource := &bigquery.ResourceURNWithUpstreams{ResourceURN: bqResourceURN}
if paths[resourceURN] {
me.Append(fmt.Errorf("circular reference is detected"))
continue
}
if _, ok := visited[resourceURN]; !ok {
query := urnToQuery[resourceURN]
paths[resourceURN] = true
upstreamResources, err := g.identifyResources(ctx, query, visited, paths)
visited[resourceURN] = upstreamResources
me.Append(err)
delete(paths, resourceURN)
}
resource.Upstreams = visited[resourceURN]
resources = append(resources, resource)
}
return resources, me.ToErr()
}
func NewBQUpstreamIdentifier(logger log.Logger, parserFunc ParserFunc, bqExtractorFunc BQExtractorFunc, evaluatorFuncs ...EvalAssetFunc) (*BQUpstreamIdentifier, error) {
me := errors.NewMultiError("create bq upstream generator errors")
if logger == nil {
me.Append(fmt.Errorf("logger is nil"))
}
if parserFunc == nil {
me.Append(fmt.Errorf("parserFunc is nil"))
}
if bqExtractorFunc == nil {
me.Append(fmt.Errorf("bqExtractorFunc is nil"))
}
sanitizedEvaluatorFuncs := []EvalAssetFunc{}
for _, evaluatorFunc := range evaluatorFuncs {
if evaluatorFunc != nil {
sanitizedEvaluatorFuncs = append(sanitizedEvaluatorFuncs, evaluatorFunc)
}
}
if len(sanitizedEvaluatorFuncs) == 0 {
me.Append(fmt.Errorf("non-nil evaluatorFuncs is needed"))
}
if me.ToErr() != nil {
return nil, me.ToErr()
}
return &BQUpstreamIdentifier{
logger: logger,
parserFunc: parserFunc,
extractorFunc: bqExtractorDecorator(logger, bqExtractorFunc),
evaluatorFuncs: sanitizedEvaluatorFuncs,
}, nil
}
// bqExtractorDecorator to convert bigquery resource urn to plain resource urn string
func bqExtractorDecorator(logger log.Logger, fn BQExtractorFunc) extractorFunc {
return func(ctx context.Context, resourceURNs []string) (map[string]string, error) {
urnToQuery := make(map[string]string, len(resourceURNs))
bqURNs := []bigquery.ResourceURN{}
for _, resourceURN := range resourceURNs {
urnToQuery[resourceURN] = "" // initialization
bqURN, err := bigquery.NewResourceURNFromString(resourceURN)
if err != nil {
logger.Error(err.Error())
continue
}
bqURNs = append(bqURNs, bqURN)
}
extractedBqURNToQuery, err := fn(ctx, bqURNs)
if err != nil {
return nil, err
}
for bqURN, query := range extractedBqURNToQuery {
urnToQuery[bqURN.URN()] = query
}
return urnToQuery, nil
}
}