-
Notifications
You must be signed in to change notification settings - Fork 100
/
proto.go
176 lines (164 loc) · 6.35 KB
/
proto.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
package queries
import (
"fmt"
"time"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/timestamppb"
)
// ProtoToQuery builds a runtime query from a proto query and security attributes.
// NOTE: Pending refactors, this implementation is replicated from handlers in runtime/server.
func ProtoToQuery(q *runtimev1.Query, attrs map[string]any) (runtime.Query, error) {
switch r := q.Query.(type) {
case *runtimev1.Query_MetricsViewAggregationRequest:
req := r.MetricsViewAggregationRequest
tr := req.TimeRange
if req.TimeStart != nil || req.TimeEnd != nil {
tr = &runtimev1.TimeRange{
Start: req.TimeStart,
End: req.TimeEnd,
}
}
return &MetricsViewAggregation{
MetricsViewName: req.MetricsView,
Dimensions: req.Dimensions,
Measures: req.Measures,
Sort: req.Sort,
TimeRange: tr,
Where: req.Where,
Having: req.Having,
Filter: req.Filter,
Offset: req.Offset,
PivotOn: req.PivotOn,
SecurityAttributes: attrs,
}, nil
case *runtimev1.Query_MetricsViewComparisonRequest:
req := r.MetricsViewComparisonRequest
return &MetricsViewComparison{
MetricsViewName: req.MetricsViewName,
DimensionName: req.Dimension.Name,
Measures: req.Measures,
ComparisonMeasures: req.ComparisonMeasures,
TimeRange: req.TimeRange,
ComparisonTimeRange: req.ComparisonTimeRange,
Limit: req.Limit,
Offset: req.Offset,
Sort: req.Sort,
Where: req.Where,
Having: req.Having,
Filter: req.Filter,
Exact: req.Exact,
SecurityAttributes: attrs,
}, nil
default:
return nil, fmt.Errorf("query type %T not supported for alerts", r)
}
}
// ProtoFromJSON builds a proto query from a query name, JSON args, and optional execution time.
func ProtoFromJSON(qryName, qryArgsJSON string, executionTime *time.Time) (*runtimev1.Query, error) {
qry := &runtimev1.Query{}
switch qryName {
case "MetricsViewAggregation":
req := &runtimev1.MetricsViewAggregationRequest{}
qry.Query = &runtimev1.Query_MetricsViewAggregationRequest{MetricsViewAggregationRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return nil, fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
if executionTime != nil {
req.TimeRange = overrideTimeRange(req.TimeRange, *executionTime)
}
case "MetricsViewToplist":
req := &runtimev1.MetricsViewToplistRequest{}
qry.Query = &runtimev1.Query_MetricsViewToplistRequest{MetricsViewToplistRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return nil, fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
case "MetricsViewRows":
req := &runtimev1.MetricsViewRowsRequest{}
qry.Query = &runtimev1.Query_MetricsViewRowsRequest{MetricsViewRowsRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return nil, fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
case "MetricsViewTimeSeries":
req := &runtimev1.MetricsViewTimeSeriesRequest{}
qry.Query = &runtimev1.Query_MetricsViewTimeSeriesRequest{MetricsViewTimeSeriesRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return nil, fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
case "MetricsViewComparison":
req := &runtimev1.MetricsViewComparisonRequest{}
qry.Query = &runtimev1.Query_MetricsViewComparisonRequest{MetricsViewComparisonRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return nil, fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
if executionTime != nil {
req.TimeRange = overrideTimeRange(req.TimeRange, *executionTime)
}
default:
return nil, fmt.Errorf("query %q not supported for reports", qryName)
}
return qry, nil
}
// MetricsViewFromQuery extracts the metrics view name from a JSON query based on the query name.
func MetricsViewFromQuery(qryName, qryArgsJSON string) (string, error) {
qry := &runtimev1.Query{}
var metricsView string
switch qryName {
case "MetricsViewAggregation":
req := &runtimev1.MetricsViewAggregationRequest{}
qry.Query = &runtimev1.Query_MetricsViewAggregationRequest{MetricsViewAggregationRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return "", fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
metricsView = req.MetricsView
case "MetricsViewToplist":
req := &runtimev1.MetricsViewToplistRequest{}
qry.Query = &runtimev1.Query_MetricsViewToplistRequest{MetricsViewToplistRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return "", fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
metricsView = req.MetricsViewName
case "MetricsViewRows":
req := &runtimev1.MetricsViewRowsRequest{}
qry.Query = &runtimev1.Query_MetricsViewRowsRequest{MetricsViewRowsRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return "", fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
metricsView = req.MetricsViewName
case "MetricsViewTimeSeries":
req := &runtimev1.MetricsViewTimeSeriesRequest{}
qry.Query = &runtimev1.Query_MetricsViewTimeSeriesRequest{MetricsViewTimeSeriesRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return "", fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
metricsView = req.MetricsViewName
case "MetricsViewComparison":
req := &runtimev1.MetricsViewComparisonRequest{}
qry.Query = &runtimev1.Query_MetricsViewComparisonRequest{MetricsViewComparisonRequest: req}
err := protojson.Unmarshal([]byte(qryArgsJSON), req)
if err != nil {
return "", fmt.Errorf("invalid properties for query %q: %w", qryName, err)
}
metricsView = req.MetricsViewName
default:
return "", fmt.Errorf("query %q not supported for reports", qryName)
}
return metricsView, nil
}
func overrideTimeRange(tr *runtimev1.TimeRange, t time.Time) *runtimev1.TimeRange {
if tr == nil {
tr = &runtimev1.TimeRange{}
}
tr.End = timestamppb.New(t)
return tr
}