-
Notifications
You must be signed in to change notification settings - Fork 117
/
metrics_views.go
150 lines (128 loc) · 5.14 KB
/
metrics_views.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
package metricsviews
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/services/catalog/migrator"
)
func init() {
migrator.Register(drivers.ObjectTypeMetricsView, &metricsViewMigrator{})
}
const (
SourceNotSelected = "metrics view source not selected"
SourceNotFound = "metrics view source not found"
TimestampNotSelected = "metrics view timestamp not selected"
TimestampNotFound = "metrics view selected timestamp not found"
MissingDimension = "at least one dimension should be present"
MissingMeasure = "at least one measure should be present"
)
type metricsViewMigrator struct{}
func (m *metricsViewMigrator) Create(ctx context.Context, olap drivers.OLAPStore, repo drivers.RepoStore, opts migrator.Options, catalogObj *drivers.CatalogEntry) error {
return nil
}
func (m *metricsViewMigrator) Update(ctx context.Context, olap drivers.OLAPStore, repo drivers.RepoStore, opts migrator.Options, oldCatalogObj, newCatalogObj *drivers.CatalogEntry) error {
return nil
}
func (m *metricsViewMigrator) Rename(ctx context.Context, olap drivers.OLAPStore, from string, catalogObj *drivers.CatalogEntry) error {
return nil
}
func (m *metricsViewMigrator) Delete(ctx context.Context, olap drivers.OLAPStore, catalogObj *drivers.CatalogEntry) error {
return nil
}
func (m *metricsViewMigrator) GetDependencies(ctx context.Context, olap drivers.OLAPStore, catalog *drivers.CatalogEntry) ([]string, []*drivers.CatalogEntry) {
return []string{catalog.GetMetricsView().Model}, nil
}
func (m *metricsViewMigrator) Validate(ctx context.Context, olap drivers.OLAPStore, catalog *drivers.CatalogEntry) []*runtimev1.ReconcileError {
mv := catalog.GetMetricsView()
if mv.Model == "" {
return migrator.CreateValidationError(catalog.Path, SourceNotSelected)
}
model, err := olap.InformationSchema().Lookup(ctx, mv.Model)
if err != nil {
if errors.Is(err, drivers.ErrNotFound) {
return migrator.CreateValidationError(catalog.Path, SourceNotFound)
}
return migrator.CreateValidationError(catalog.Path, err.Error())
}
fieldsMap := make(map[string]*runtimev1.StructType_Field)
for _, field := range model.Schema.Fields {
fieldsMap[strings.ToLower(field.Name)] = field
}
// if a time dimension is selected it should exist
if mv.TimeDimension != "" {
if _, ok := fieldsMap[strings.ToLower(mv.TimeDimension)]; !ok {
return migrator.CreateValidationError(catalog.Path, TimestampNotFound)
}
}
var validationErrors []*runtimev1.ReconcileError
dimensionNames := make(map[string]bool)
for i, dimension := range mv.Dimensions {
if _, ok := dimensionNames[strings.ToLower(dimension.Name)]; ok {
validationErrors = append(validationErrors, &runtimev1.ReconcileError{
Code: runtimev1.ReconcileError_CODE_VALIDATION,
FilePath: catalog.Path,
Message: "duplicate dimension name",
PropertyPath: []string{"Dimensions", strconv.Itoa(i)},
})
continue
}
dimensionNames[strings.ToLower(dimension.Name)] = true
if _, ok := fieldsMap[strings.ToLower(dimension.Column)]; !ok {
validationErrors = append(validationErrors, &runtimev1.ReconcileError{
Code: runtimev1.ReconcileError_CODE_VALIDATION,
FilePath: catalog.Path,
Message: fmt.Sprintf("dimension not found: %s", dimension.Column),
PropertyPath: []string{"Dimensions", strconv.Itoa(i)},
})
}
}
measureNames := make(map[string]bool)
for i, measure := range mv.Measures {
if _, ok := measureNames[strings.ToLower(measure.Name)]; ok {
validationErrors = append(validationErrors, &runtimev1.ReconcileError{
Code: runtimev1.ReconcileError_CODE_VALIDATION,
FilePath: catalog.Path,
Message: "duplicate measure name",
PropertyPath: []string{"Measures", strconv.Itoa(i)},
})
continue
}
measureNames[strings.ToLower(measure.Name)] = true
err := validateMeasure(ctx, olap, model, measure)
if err != nil {
validationErrors = append(validationErrors, &runtimev1.ReconcileError{
Code: runtimev1.ReconcileError_CODE_VALIDATION,
FilePath: catalog.Path,
Message: err.Error(),
PropertyPath: []string{"Measures", strconv.Itoa(i)},
})
}
}
// at least one measure has to be there in the metrics view
if len(mv.Measures) == 0 {
validationErrors = append(validationErrors, &runtimev1.ReconcileError{
Code: runtimev1.ReconcileError_CODE_VALIDATION,
FilePath: catalog.Path,
Message: MissingMeasure,
})
}
return validationErrors
}
func (m *metricsViewMigrator) IsEqual(ctx context.Context, cat1, cat2 *drivers.CatalogEntry) bool {
// TODO: do we need a deep check here?
return false
}
func (m *metricsViewMigrator) ExistsInOlap(ctx context.Context, olap drivers.OLAPStore, catalog *drivers.CatalogEntry) (bool, error) {
return true, nil
}
func validateMeasure(ctx context.Context, olap drivers.OLAPStore, model *drivers.Table, measure *runtimev1.MetricsView_Measure) error {
err := olap.Exec(ctx, &drivers.Statement{
Query: fmt.Sprintf("SELECT %s from \"%s\"", measure.Expression, model.Name),
DryRun: true,
})
return err
}