-
Notifications
You must be signed in to change notification settings - Fork 117
/
metricsview_timeseries.go
236 lines (200 loc) · 6.18 KB
/
metricsview_timeseries.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
package queries
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/pbutil"
"google.golang.org/protobuf/types/known/timestamppb"
)
type MetricsViewTimeSeries struct {
MetricsViewName string `json:"metrics_view_name,omitempty"`
MeasureNames []string `json:"measure_names,omitempty"`
InlineMeasures []*runtimev1.InlineMeasure `json:"inline_measures,omitempty"`
TimeStart *timestamppb.Timestamp `json:"time_start,omitempty"`
TimeEnd *timestamppb.Timestamp `json:"time_end,omitempty"`
Limit int64 `json:"limit,omitempty"`
Offset int64 `json:"offset,omitempty"`
Sort []*runtimev1.MetricsViewSort `json:"sort,omitempty"`
Filter *runtimev1.MetricsViewFilter `json:"filter,omitempty"`
TimeGranularity runtimev1.TimeGrain `json:"time_granularity,omitempty"`
Result *runtimev1.MetricsViewTimeSeriesResponse `json:"-"`
}
var _ runtime.Query = &MetricsViewTimeSeries{}
func (q *MetricsViewTimeSeries) Key() string {
r, err := json.Marshal(q)
if err != nil {
panic(err)
}
return fmt.Sprintf("MetricsViewTimeSeries:%s", string(r))
}
func (q *MetricsViewTimeSeries) Deps() []string {
return []string{q.MetricsViewName}
}
func (q *MetricsViewTimeSeries) MarshalResult() *runtime.QueryResult {
return &runtime.QueryResult{
Value: q.Result,
Bytes: sizeProtoMessage(q.Result),
}
}
func (q *MetricsViewTimeSeries) UnmarshalResult(v any) error {
res, ok := v.(*runtimev1.MetricsViewTimeSeriesResponse)
if !ok {
return fmt.Errorf("MetricsViewTimeSeries: mismatched unmarshal input")
}
q.Result = res
return nil
}
func (q *MetricsViewTimeSeries) Resolve(ctx context.Context, rt *runtime.Runtime, instanceID string, priority int) error {
mv, err := lookupMetricsView(ctx, rt, instanceID, q.MetricsViewName)
if err != nil {
return err
}
if mv.TimeDimension == "" {
return fmt.Errorf("metrics view '%s' does not have a time dimension", q.MetricsViewName)
}
olap, err := rt.OLAP(ctx, instanceID)
if err != nil {
return err
}
switch olap.Dialect() {
case drivers.DialectDuckDB:
return q.resolveDuckDB(ctx, rt, instanceID, mv, priority)
case drivers.DialectDruid:
return q.resolveDruid(ctx, olap, mv, priority)
default:
return fmt.Errorf("not available for dialect '%s'", olap.Dialect())
}
}
func (q *MetricsViewTimeSeries) resolveDuckDB(ctx context.Context, rt *runtime.Runtime, instanceID string, mv *runtimev1.MetricsView, priority int) error {
ms, err := resolveMeasures(mv, q.InlineMeasures, q.MeasureNames)
if err != nil {
return err
}
measures, err := toColumnTimeseriesMeasures(ms)
if err != nil {
return err
}
tsq := &ColumnTimeseries{
TableName: mv.Model,
TimestampColumnName: mv.TimeDimension,
TimeRange: &runtimev1.TimeSeriesTimeRange{
Start: q.TimeStart,
End: q.TimeEnd,
Interval: q.TimeGranularity,
},
Measures: measures,
Filters: q.Filter,
}
err = rt.Query(ctx, instanceID, tsq, priority)
if err != nil {
return err
}
r := tsq.Result
q.Result = &runtimev1.MetricsViewTimeSeriesResponse{
Meta: r.Meta,
Data: r.Results,
}
return nil
}
func toColumnTimeseriesMeasures(measures []*runtimev1.MetricsView_Measure) ([]*runtimev1.ColumnTimeSeriesRequest_BasicMeasure, error) {
res := make([]*runtimev1.ColumnTimeSeriesRequest_BasicMeasure, len(measures))
for i, m := range measures {
res[i] = &runtimev1.ColumnTimeSeriesRequest_BasicMeasure{
SqlName: m.Name,
Expression: m.Expression,
}
}
return res, nil
}
func (q *MetricsViewTimeSeries) resolveDruid(ctx context.Context, olap drivers.OLAPStore, mv *runtimev1.MetricsView, priority int) error {
sql, tsAlias, args, err := q.buildDruidMetricsTimeseriesSQL(mv)
if err != nil {
return fmt.Errorf("error building query: %w", err)
}
rows, err := olap.Execute(ctx, &drivers.Statement{
Query: sql,
Args: args,
Priority: priority,
ExecutionTimeout: defaultExecutionTimeout,
})
if err != nil {
return err
}
defer rows.Close()
var data []*runtimev1.TimeSeriesValue
for rows.Next() {
rowMap := make(map[string]any)
err := rows.MapScan(rowMap)
if err != nil {
return err
}
var t time.Time
switch v := rowMap[tsAlias].(type) {
case time.Time:
t = v
default:
panic(fmt.Sprintf("unexpected type for timestamp column: %T", v))
}
delete(rowMap, tsAlias)
records, err := pbutil.ToStruct(rowMap)
if err != nil {
return err
}
data = append(data, &runtimev1.TimeSeriesValue{
Ts: timestamppb.New(t),
Records: records,
})
}
meta := structTypeToMetricsViewColumn(rows.Schema)
q.Result = &runtimev1.MetricsViewTimeSeriesResponse{
Meta: meta,
Data: data,
}
return nil
}
func (q *MetricsViewTimeSeries) buildDruidMetricsTimeseriesSQL(mv *runtimev1.MetricsView) (string, string, []any, error) {
ms, err := resolveMeasures(mv, q.InlineMeasures, q.MeasureNames)
if err != nil {
return "", "", nil, err
}
selectCols := []string{}
for _, m := range ms {
expr := fmt.Sprintf(`%s as "%s"`, m.Expression, m.Name)
selectCols = append(selectCols, expr)
}
whereClause := "1=1"
args := []any{}
if q.TimeStart != nil {
whereClause += fmt.Sprintf(" AND %s >= ?", safeName(mv.TimeDimension))
args = append(args, q.TimeStart.AsTime())
}
if q.TimeEnd != nil {
whereClause += fmt.Sprintf(" AND %s < ?", safeName(mv.TimeDimension))
args = append(args, q.TimeEnd.AsTime())
}
if q.Filter != nil {
clause, clauseArgs, err := buildFilterClauseForMetricsViewFilter(q.Filter, drivers.DialectDruid)
if err != nil {
return "", "", nil, err
}
whereClause += " " + clause
args = append(args, clauseArgs...)
}
tsAlias := tempName("_ts_")
tsSpecifier := convertToDateTruncSpecifier(q.TimeGranularity)
sql := fmt.Sprintf(
`SELECT date_trunc('%s', %s) AS %s, %s FROM %q WHERE %s GROUP BY 1 ORDER BY 1`,
tsSpecifier,
safeName(mv.TimeDimension),
tsAlias,
strings.Join(selectCols, ", "),
mv.Model,
whereClause,
)
return sql, tsAlias, args, nil
}