-
Notifications
You must be signed in to change notification settings - Fork 117
/
metricsview_rows.go
142 lines (119 loc) · 3.63 KB
/
metricsview_rows.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
package queries
import (
"context"
"encoding/json"
"fmt"
"strings"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/drivers"
"google.golang.org/protobuf/types/known/timestamppb"
)
type MetricsViewRows struct {
MetricsViewName string `json:"metrics_view_name,omitempty"`
TimeStart *timestamppb.Timestamp `json:"time_start,omitempty"`
TimeEnd *timestamppb.Timestamp `json:"time_end,omitempty"`
Filter *runtimev1.MetricsViewFilter `json:"filter,omitempty"`
Sort []*runtimev1.MetricsViewSort `json:"sort,omitempty"`
Limit int32 `json:"limit,omitempty"`
Offset int64 `json:"offset,omitempty"`
Result *runtimev1.MetricsViewRowsResponse `json:"-"`
}
var _ runtime.Query = &MetricsViewRows{}
func (q *MetricsViewRows) Key() string {
r, err := json.Marshal(q)
if err != nil {
panic(err)
}
return fmt.Sprintf("MetricsViewRows:%s", string(r))
}
func (q *MetricsViewRows) Deps() []string {
return []string{q.MetricsViewName}
}
func (q *MetricsViewRows) MarshalResult() any {
return q.Result
}
func (q *MetricsViewRows) UnmarshalResult(v any) error {
res, ok := v.(*runtimev1.MetricsViewRowsResponse)
if !ok {
return fmt.Errorf("MetricsViewRows: mismatched unmarshal input")
}
q.Result = res
return nil
}
func (q *MetricsViewRows) Resolve(ctx context.Context, rt *runtime.Runtime, instanceID string, priority int) error {
olap, err := rt.OLAP(ctx, instanceID)
if err != nil {
return err
}
if olap.Dialect() != drivers.DialectDuckDB && olap.Dialect() != drivers.DialectDruid {
return fmt.Errorf("not available for dialect '%s'", olap.Dialect())
}
mv, err := lookupMetricsView(ctx, rt, instanceID, q.MetricsViewName)
if err != nil {
return err
}
if mv.TimeDimension == "" && (q.TimeStart != nil || q.TimeEnd != nil) {
return fmt.Errorf("metrics view '%s' does not have a time dimension", q.MetricsViewName)
}
ql, args, err := q.buildMetricsRowsSQL(mv, olap.Dialect())
if err != nil {
return fmt.Errorf("error building query: %w", err)
}
meta, data, err := metricsQuery(ctx, olap, priority, ql, args)
if err != nil {
return err
}
q.Result = &runtimev1.MetricsViewRowsResponse{
Meta: meta,
Data: data,
}
return nil
}
func (q *MetricsViewRows) buildMetricsRowsSQL(mv *runtimev1.MetricsView, dialect drivers.Dialect) (string, []any, error) {
whereClause := "1=1"
args := []any{}
if mv.TimeDimension != "" {
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, dialect)
if err != nil {
return "", nil, err
}
whereClause += " " + clause
args = append(args, clauseArgs...)
}
sortingCriteria := make([]string, 0, len(q.Sort))
for _, s := range q.Sort {
sortCriterion := safeName(s.Name)
if !s.Ascending {
sortCriterion += " DESC"
}
if dialect == drivers.DialectDuckDB {
sortCriterion += " NULLS LAST"
}
sortingCriteria = append(sortingCriteria, sortCriterion)
}
orderClause := ""
if len(sortingCriteria) > 0 {
orderClause = "ORDER BY " + strings.Join(sortingCriteria, ", ")
}
if q.Limit == 0 {
q.Limit = 100
}
sql := fmt.Sprintf("SELECT * FROM %q WHERE %s %s LIMIT %d",
mv.Model,
whereClause,
orderClause,
q.Limit,
)
return sql, args, nil
}