-
Notifications
You must be signed in to change notification settings - Fork 117
/
metricsview_toplist.go
165 lines (140 loc) · 4.41 KB
/
metricsview_toplist.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
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 MetricsViewToplist struct {
MetricsViewName string `json:"metrics_view_name,omitempty"`
DimensionName string `json:"dimension_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"`
Result *runtimev1.MetricsViewToplistResponse `json:"-"`
}
var _ runtime.Query = &MetricsViewToplist{}
func (q *MetricsViewToplist) Key() string {
r, err := json.Marshal(q)
if err != nil {
panic(err)
}
return fmt.Sprintf("MetricsViewToplist:%s", string(r))
}
func (q *MetricsViewToplist) Deps() []string {
return []string{q.MetricsViewName}
}
func (q *MetricsViewToplist) MarshalResult() *runtime.QueryResult {
return &runtime.QueryResult{
Value: q.Result,
Bytes: sizeProtoMessage(q.Result),
}
}
func (q *MetricsViewToplist) UnmarshalResult(v any) error {
res, ok := v.(*runtimev1.MetricsViewToplistResponse)
if !ok {
return fmt.Errorf("MetricsViewToplist: mismatched unmarshal input")
}
q.Result = res
return nil
}
func (q *MetricsViewToplist) 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)
}
// Build query
sql, args, err := q.buildMetricsTopListSQL(mv, olap.Dialect())
if err != nil {
return fmt.Errorf("error building query: %w", err)
}
// Execute
meta, data, err := metricsQuery(ctx, olap, priority, sql, args)
if err != nil {
return err
}
q.Result = &runtimev1.MetricsViewToplistResponse{
Meta: meta,
Data: data,
}
return nil
}
func (q *MetricsViewToplist) buildMetricsTopListSQL(mv *runtimev1.MetricsView, dialect drivers.Dialect) (string, []any, error) {
ms, err := resolveMeasures(mv, q.InlineMeasures, q.MeasureNames)
if err != nil {
return "", nil, err
}
dimName := safeName(q.DimensionName)
selectCols := []string{dimName}
for _, m := range ms {
expr := fmt.Sprintf(`%s as "%s"`, m.Expression, m.Name)
selectCols = append(selectCols, expr)
}
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 %s FROM %q WHERE %s GROUP BY %s %s LIMIT %d OFFSET %d",
strings.Join(selectCols, ", "),
mv.Model,
whereClause,
dimName,
orderClause,
q.Limit,
q.Offset,
)
return sql, args, nil
}