-
Notifications
You must be signed in to change notification settings - Fork 28
/
googlesheets.go
331 lines (294 loc) · 9.96 KB
/
googlesheets.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package googlesheets
import (
"context"
"errors"
"fmt"
"net"
"strings"
"time"
"github.com/grafana/google-sheets-datasource/pkg/models"
"github.com/araddon/dateparse"
"github.com/davecgh/go-spew/spew"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/patrickmn/go-cache"
"golang.org/x/oauth2"
"google.golang.org/api/googleapi"
"google.golang.org/api/sheets/v4"
)
// GoogleSheets provides an interface to the Google Sheets API.
type GoogleSheets struct {
Cache *cache.Cache
}
// Query queries a spreadsheet and returns a corresponding data frame.
func (gs *GoogleSheets) Query(ctx context.Context, refID string, qm *models.QueryModel, config models.DatasourceSettings, timeRange backend.TimeRange) (dr backend.DataResponse) {
client, err := NewGoogleClient(ctx, config)
if err != nil {
dr = errorsource.Response(err)
dr.Error = fmt.Errorf("unable to create Google API client: %w", err)
return
}
// This result may be cached
sheetData, meta, err := gs.getSheetData(ctx, client, qm)
if err != nil {
dr = errorsource.Response(err)
return
}
frame, err := gs.transformSheetToDataFrame(ctx, sheetData, meta, refID, qm)
if err != nil {
dr.Error = err
return
}
if frame == nil {
return
}
if qm.UseTimeFilter {
timeIndex := findTimeField(frame)
if timeIndex >= 0 {
frame, dr.Error = frame.FilterRowsByField(timeIndex, func(i any) (bool, error) {
val, ok := i.(*time.Time)
if !ok {
return false, fmt.Errorf("invalid time column: %s", spew.Sdump(i))
}
if val == nil || val.Before(timeRange.From) || val.After(timeRange.To) {
return false, nil
}
return true, nil
})
}
}
dr.Frames = append(dr.Frames, frame)
return
}
// GetSpreadsheets gets spreadsheets from the Google API.
func (gs *GoogleSheets) GetSpreadsheets(ctx context.Context, config models.DatasourceSettings) (map[string]string, error) {
client, err := NewGoogleClient(ctx, config)
if err != nil {
return nil, fmt.Errorf("failed to create Google API client: %w", err)
}
files, err := client.GetSpreadsheetFiles()
if err != nil {
return nil, err
}
fileNames := make(map[string]string, len(files))
for _, i := range files {
fileNames[i.Id] = i.Name
}
return fileNames, nil
}
// getSheetData gets grid data corresponding to a spreadsheet.
func (gs *GoogleSheets) getSheetData(ctx context.Context, client client, qm *models.QueryModel) (*sheets.GridData, map[string]any, error) {
logger := backend.Logger.FromContext(ctx)
cacheKey := qm.Spreadsheet + qm.Range
if item, expires, found := gs.Cache.GetWithExpiration(cacheKey); found && qm.CacheDurationSeconds > 0 {
if gridData, ok := item.(*sheets.GridData); ok {
return gridData, map[string]any{
"hit": true,
"expires": expires.Unix(),
}, nil
}
return nil, nil, errors.New("invalid cache item not type of *sheets.GridData")
}
result, err := client.GetSpreadsheet(ctx, qm.Spreadsheet, qm.Range, true)
if err != nil {
if apiErr, ok := err.(*googleapi.Error); ok {
// Handle API-specific errors
// We use ErrorSourceFromHTTPStatus to determine error source based on HTTP status code
if apiErr.Code == 404 {
errWithSource := errorsource.DownstreamError(errors.New("spreadsheet not found"), false)
return nil, nil, errWithSource
}
if apiErr.Message != "" {
logger.Warn("Google API Error: " + apiErr.Message)
errWithSource := errorsource.SourceError(backend.ErrorSourceFromHTTPStatus(apiErr.Code), fmt.Errorf("google API Error %d", apiErr.Code), false)
return nil, nil, errWithSource
}
errWithSource := errorsource.SourceError(backend.ErrorSourceFromHTTPStatus(apiErr.Code), errors.New("unknown API error"), false)
logger.Warn(apiErr.Error())
return nil, nil, errWithSource
}
netErr, neErrOk := err.(net.Error)
if neErrOk {
var retrieveErr *oauth2.RetrieveError
if errors.As(netErr, &retrieveErr) {
errWithSource := errorsource.SourceError(backend.ErrorSourceFromHTTPStatus(retrieveErr.Response.StatusCode), err, false)
return nil, nil, errWithSource
}
}
logger.Warn("unknown error", "err", err)
// This is an unknown error from the client - it might have error source middleware.
// If not, it will be handled by the default error source - plugin error.
return nil, nil, err
}
if result.Properties.TimeZone != "" {
loc, err := time.LoadLocation(result.Properties.TimeZone)
if err != nil {
logger.Warn("could not load timezone from spreadsheet: %w", err)
} else {
time.Local = loc
}
}
sheetData := result.Sheets[0].Data[0]
if qm.CacheDurationSeconds > 0 {
gs.Cache.Set(cacheKey, sheetData, time.Duration(qm.CacheDurationSeconds)*time.Second)
}
return sheetData, map[string]any{"hit": false}, nil
}
func (gs *GoogleSheets) transformSheetToDataFrame(ctx context.Context, sheet *sheets.GridData, meta map[string]any, refID string, qm *models.QueryModel) (*data.Frame, error) {
logger := backend.Logger.FromContext(ctx)
columns, start := getColumnDefinitions(sheet.RowData)
warnings := []string{}
converters := make([]data.FieldConverter, len(columns))
for i, column := range columns {
fc, ok := converterMap[column.GetType()]
if !ok {
return nil, fmt.Errorf("unknown column type: %s", column.GetType())
}
converters[i] = fc
}
inputConverter, err := data.NewFrameInputConverter(converters, len(sheet.RowData)-start)
if err != nil {
return nil, err
}
frame := inputConverter.Frame
frame.RefID = refID
frame.Name = refID // TODO: should set the name from metadata
for i, column := range columns {
field := frame.Fields[i]
field.Name = column.Header
field.Config = &data.FieldConfig{
DisplayName: column.Header,
Unit: column.GetUnit(),
}
if column.HasMixedTypes() {
warning := fmt.Sprintf("Multiple data types found in column %q. Using string data type", column.Header)
warnings = append(warnings, warning)
logger.Debug(warning)
}
if column.HasMixedUnits() {
warning := fmt.Sprintf("Multiple units found in column %q. Formatted value will be used", column.Header)
warnings = append(warnings, warning)
logger.Debug(warning)
}
}
// We want to show the warnings only once per column
warningsIncludeConverterErrorForColumns := make(map[int]bool, len(columns))
for rowIndex := start; rowIndex < len(sheet.RowData); rowIndex++ {
for columnIndex, cellData := range sheet.RowData[rowIndex].Values {
if columnIndex >= len(columns) {
continue
}
// Skip any empty values
if cellData.FormattedValue == "" {
continue
}
err := inputConverter.Set(columnIndex, rowIndex-start, cellData)
if err != nil && !warningsIncludeConverterErrorForColumns[columnIndex] {
logger.Debug("unsuccessful converting of cell data", "err", err)
warnings = append(warnings, err.Error())
warningsIncludeConverterErrorForColumns[columnIndex] = true
}
}
}
meta["warnings"] = warnings
meta["spreadsheetId"] = qm.Spreadsheet
meta["range"] = qm.Range
frame.Meta = &data.FrameMeta{Custom: meta}
return frame, nil
}
// timeConverter handles sheets TIME column types.
var timeConverter = data.FieldConverter{
OutputFieldType: data.FieldTypeNullableTime,
Converter: func(i any) (any, error) {
var t *time.Time
cellData, ok := i.(*sheets.CellData)
if !ok {
return t, fmt.Errorf("expected type *sheets.CellData, but got %T", i)
}
parsedTime, err := dateparse.ParseLocal(cellData.FormattedValue)
if err != nil {
return t, fmt.Errorf("error while parsing date '%v'", cellData.FormattedValue)
}
return &parsedTime, nil
},
}
// stringConverter handles sheets STRING column types.
var stringConverter = data.FieldConverter{
OutputFieldType: data.FieldTypeNullableString,
Converter: func(i any) (any, error) {
var s *string
cellData, ok := i.(*sheets.CellData)
if !ok {
return s, fmt.Errorf("expected type *sheets.CellData, but got %T", i)
}
return &cellData.FormattedValue, nil
},
}
// numberConverter handles sheets STRING column types.
var numberConverter = data.FieldConverter{
OutputFieldType: data.FieldTypeNullableFloat64,
Converter: func(i any) (any, error) {
cellData, ok := i.(*sheets.CellData)
if !ok {
return nil, fmt.Errorf("expected type *sheets.CellData, but got %T", i)
}
return cellData.EffectiveValue.NumberValue, nil
},
}
// converterMap is a map sheets.ColumnType to fieldConverter and
// is used to create a data.FrameInputConverter for a returned sheet.
var converterMap = map[ColumnType]data.FieldConverter{
"TIME": timeConverter,
"STRING": stringConverter,
"NUMBER": numberConverter,
}
func getUniqueColumnName(formattedName string, columnIndex int, columns map[string]bool) string {
name := formattedName
if name == "" {
name = fmt.Sprintf("Field %d", columnIndex+1)
}
nameExist := true
counter := 1
for nameExist {
if _, exist := columns[name]; exist {
name = fmt.Sprintf("%s%d", formattedName, counter)
counter++
} else {
nameExist = false
}
}
return name
}
func getColumnDefinitions(rows []*sheets.RowData) ([]*ColumnDefinition, int) {
if len(rows) < 1 {
return []*ColumnDefinition{}, 0
}
headerRow := rows[0].Values
columns := make([]*ColumnDefinition, 0, len(headerRow))
columnMap := make(map[string]bool, len(headerRow))
start := 0
if len(rows) > 1 {
start = 1
for columnIndex, headerCell := range headerRow {
name := getUniqueColumnName(strings.TrimSpace(headerCell.FormattedValue), columnIndex, columnMap)
columnMap[name] = true
columns = append(columns, NewColumnDefinition(name, columnIndex))
}
} else {
for columnIndex := range headerRow {
name := getUniqueColumnName("", columnIndex, columnMap)
columnMap[name] = true
columns = append(columns, NewColumnDefinition(name, columnIndex))
}
}
// Check the types for each column
for rowIndex := start; rowIndex < len(rows); rowIndex++ {
for _, column := range columns {
if column.ColumnIndex < len(rows[rowIndex].Values) {
column.CheckCell(rows[rowIndex].Values[column.ColumnIndex])
}
}
}
return columns, start
}