forked from grafana/sqlds
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
205 lines (178 loc) · 6.12 KB
/
query.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
package sqlds
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/grafana/dataplane/sdata/timeseries"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
)
// FormatQueryOption defines how the user has chosen to represent the data
// Deprecated: use sqlutil.FormatQueryOption directly instead
type FormatQueryOption = sqlutil.FormatQueryOption
// Deprecated: use the values in sqlutil directly instead
const (
// FormatOptionTimeSeries formats the query results as a timeseries using "LongToWide"
FormatOptionTimeSeries = sqlutil.FormatOptionTimeSeries
// FormatOptionTable formats the query results as a table using "LongToWide"
FormatOptionTable = sqlutil.FormatOptionTable
// FormatOptionLogs sets the preferred visualization to logs
FormatOptionLogs = sqlutil.FormatOptionLogs
// FormatOptionsTrace sets the preferred visualization to trace
FormatOptionTrace = sqlutil.FormatOptionTrace
// FormatOptionMulti formats the query results as a timeseries using "LongToMulti"
FormatOptionMulti = sqlutil.FormatOptionMulti
)
// Deprecated: use sqlutil.Query directly instead
type Query = sqlutil.Query
// GetQuery wraps sqlutil's GetQuery to add headers if needed
func GetQuery(query backend.DataQuery, headers http.Header, setHeaders bool) (*Query, error) {
model, err := sqlutil.GetQuery(query)
if err != nil {
return nil, PluginError(err)
}
if setHeaders {
applyHeaders(model, headers)
}
return model, nil
}
// QueryDB sends the query to the connection and converts the rows to a dataframe.
func QueryDB(ctx context.Context, db Connection, converters []sqlutil.Converter, fillMode *data.FillMissing, query *Query, args ...interface{}) (data.Frames, error) {
// Query the rows from the database
rows, err := db.QueryContext(ctx, query.RawSQL, args...)
if err != nil {
errType := ErrorQuery
if errors.Is(err, context.Canceled) {
errType = context.Canceled
}
err := DownstreamError(fmt.Errorf("%w: %s", errType, err.Error()))
return sqlutil.ErrorFrameFromQuery(query), err
}
// Check for an error response
if err := rows.Err(); err != nil {
if errors.Is(err, sql.ErrNoRows) {
// Should we even response with an error here?
// The panel will simply show "no data"
err := DownstreamError(fmt.Errorf("%s: %w", "No results from query", err))
return sqlutil.ErrorFrameFromQuery(query), err
}
err := DownstreamError(fmt.Errorf("%s: %w", "Error response from database", err))
return sqlutil.ErrorFrameFromQuery(query), err
}
defer func() {
if err := rows.Close(); err != nil {
backend.Logger.Error(err.Error())
}
}()
// Convert the response to frames
res, err := getFrames(rows, -1, converters, fillMode, query)
if err != nil {
err := PluginError(fmt.Errorf("%w: %s", err, "Could not process SQL results"))
return sqlutil.ErrorFrameFromQuery(query), err
}
return res, nil
}
func getFrames(rows *sql.Rows, limit int64, converters []sqlutil.Converter, fillMode *data.FillMissing, query *Query) (data.Frames, error) {
frame, err := sqlutil.FrameFromRows(rows, limit, converters...)
if err != nil {
return nil, err
}
frame.Name = query.RefID
if frame.Meta == nil {
frame.Meta = &data.FrameMeta{}
}
count, err := frame.RowLen()
if err != nil {
return nil, err
}
if count == 0 {
return nil, ErrorNoResults
}
frame.Meta.ExecutedQueryString = query.RawSQL
frame.Meta.PreferredVisualization = data.VisTypeGraph
switch query.Format {
case FormatOptionMulti:
if frame.TimeSeriesSchema().Type == data.TimeSeriesTypeLong {
err = fixFrameForLongToMulti(frame)
if err != nil {
return nil, err
}
frames, err := timeseries.LongToMulti(×eries.LongFrame{frame})
if err != nil {
return nil, err
}
return frames.Frames(), nil
}
case FormatOptionTable:
frame.Meta.PreferredVisualization = data.VisTypeTable
case FormatOptionLogs:
frame.Meta.PreferredVisualization = data.VisTypeLogs
case FormatOptionTrace:
frame.Meta.PreferredVisualization = data.VisTypeTrace
// Format as timeSeries
default:
if frame.TimeSeriesSchema().Type == data.TimeSeriesTypeLong {
frame, err = data.LongToWide(frame, fillMode)
if err != nil {
return nil, err
}
}
}
return data.Frames{frame}, nil
}
// fixFrameForLongToMulti edits the passed in frame so that it's first time field isn't nullable and has the correct meta
func fixFrameForLongToMulti(frame *data.Frame) error {
if frame == nil {
return fmt.Errorf("can not convert to wide series, input is nil")
}
timeFields := frame.TypeIndices(data.FieldTypeTime, data.FieldTypeNullableTime)
if len(timeFields) == 0 {
return fmt.Errorf("can not convert to wide series, input is missing a time field")
}
// the timeseries package expects the first time field in the frame to be non-nullable and ignores the rest
timeField := frame.Fields[timeFields[0]]
if timeField.Type() == data.FieldTypeNullableTime {
newValues := []time.Time{}
for i := 0; i < timeField.Len(); i++ {
val, ok := timeField.ConcreteAt(i)
if !ok {
return fmt.Errorf("can not convert to wide series, input has null time values")
}
newValues = append(newValues, val.(time.Time))
}
newField := data.NewField(timeField.Name, timeField.Labels, newValues)
newField.Config = timeField.Config
frame.Fields[timeFields[0]] = newField
// LongToMulti requires the meta to be set for the frame
if frame.Meta == nil {
frame.Meta = &data.FrameMeta{}
}
frame.Meta.Type = data.FrameTypeTimeSeriesLong
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
}
return nil
}
func applyHeaders(query *Query, headers http.Header) *Query {
var args map[string]interface{}
if query.ConnectionArgs == nil {
query.ConnectionArgs = []byte("{}")
}
err := json.Unmarshal(query.ConnectionArgs, &args)
if err != nil {
backend.Logger.Warn(fmt.Sprintf("Failed to apply headers: %s", err.Error()))
return query
}
args[HeaderKey] = headers
raw, err := json.Marshal(args)
if err != nil {
backend.Logger.Warn(fmt.Sprintf("Failed to apply headers: %s", err.Error()))
return query
}
query.ConnectionArgs = raw
return query
}