-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathquery.go
256 lines (224 loc) · 7.75 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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, backend.PluginError(err)
}
if setHeaders {
applyHeaders(model, headers)
}
return model, nil
}
type DBQuery struct {
DB Connection
fillMode *data.FillMissing
Settings backend.DataSourceInstanceSettings
metrics Metrics
DSName string
converters []sqlutil.Converter
}
func NewQuery(db Connection, settings backend.DataSourceInstanceSettings, converters []sqlutil.Converter, fillMode *data.FillMissing) *DBQuery {
return &DBQuery{
DB: db,
DSName: settings.Name,
converters: converters,
fillMode: fillMode,
metrics: NewMetrics(settings.Name, settings.Type, EndpointQuery),
}
}
// Run sends the query to the connection and converts the rows to a dataframe.
func (q *DBQuery) Run(ctx context.Context, query *Query, args ...interface{}) (data.Frames, error) {
start := time.Now()
rows, err := q.DB.QueryContext(ctx, query.RawSQL, args...)
if err != nil {
errType := ErrorQuery
if errors.Is(err, context.Canceled) {
errType = context.Canceled
}
errWithSource := backend.DownstreamError(fmt.Errorf("%w: %s", errType, err.Error()))
q.metrics.CollectDuration(SourceDownstream, StatusError, time.Since(start).Seconds())
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}
q.metrics.CollectDuration(SourceDownstream, StatusOK, time.Since(start).Seconds())
// 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"
errWithSource := backend.DownstreamError(fmt.Errorf("%s: %w", "No results from query", err))
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}
errWithSource := backend.DownstreamError(fmt.Errorf("%s: %w", "Error response from database", err))
q.metrics.CollectDuration(SourceDownstream, StatusError, time.Since(start).Seconds())
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}
defer func() {
if err := rows.Close(); err != nil {
backend.Logger.Error(err.Error())
}
}()
start = time.Now()
// Convert the response to frames
res, err := getFrames(rows, -1, q.converters, q.fillMode, query)
if err != nil {
// We default to plugin error source
errSource := backend.ErrorSourcePlugin
if backend.IsDownstreamHTTPError(err) || isProcessingDownstreamError(err) {
errSource = backend.ErrorSourceDownstream
}
errWithSource := backend.NewErrorWithSource(fmt.Errorf("%w: %s", err, "Could not process SQL results"), errSource)
q.metrics.CollectDuration(Source(errSource), StatusError, time.Since(start).Seconds())
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}
q.metrics.CollectDuration(SourcePlugin, StatusOK, time.Since(start).Seconds())
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
}
// the handling of zero-rows differs between various "format"s.
zeroRows := count == 0
frame.Meta.ExecutedQueryString = query.RawSQL
frame.Meta.PreferredVisualization = data.VisTypeGraph
switch query.Format {
case FormatOptionMulti:
if zeroRows {
return nil, ErrorNoResults
}
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 zeroRows {
return nil, ErrorNoResults
}
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
}
func isProcessingDownstreamError(err error) bool {
downstreamErrors := []error{
data.ErrorInputFieldsWithoutRows,
data.ErrorSeriesUnsorted,
data.ErrorNullTimeValues,
}
for _, e := range downstreamErrors {
if errors.Is(err, e) {
return true
}
}
return false
}