This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resources.go
182 lines (166 loc) · 4.8 KB
/
resources.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
package flightsql
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"sort"
"time"
"github.com/apache/arrow/go/v12/arrow/array"
"github.com/apache/arrow/go/v12/arrow/flight"
"github.com/apache/arrow/go/v12/arrow/flight/flightsql"
"github.com/apache/arrow/go/v12/arrow/memory"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"google.golang.org/grpc/metadata"
)
func (d *FlightSQLDatasource) getMacros(w http.ResponseWriter, r *http.Request) {
size := len(sqlutil.DefaultMacros) + len(macros)
names := make([]string, 0, size)
for k := range sqlutil.DefaultMacros {
if k == "table" || k == "column" {
// We don't have the information available for these to function
// propperly so omit them from advertisement.
continue
}
names = append(names, k)
}
for k := range macros {
names = append(names, k)
}
sort.Strings(names)
err := json.NewEncoder(w).Encode(struct {
Macros []string `json:"macros"`
}{
Macros: names,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (d *FlightSQLDatasource) getSQLInfo(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
ctx = metadata.NewOutgoingContext(ctx, d.md)
info, err := d.client.GetSqlInfo(ctx, []flightsql.SqlInfo{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reader, err := d.client.DoGet(ctx, info.Endpoint[0].Ticket)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer reader.Release()
if err := writeDataResponse(w, newDataResponse(reader)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (d *FlightSQLDatasource) getTables(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
ctx = metadata.NewOutgoingContext(ctx, d.md)
info, err := d.client.GetTables(ctx, &flightsql.GetTablesOpts{
TableTypes: []string{"BASE TABLE", "table"},
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reader, err := d.client.DoGet(ctx, info.Endpoint[0].Ticket)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer reader.Release()
if err := writeDataResponse(w, newDataResponse(reader)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (d *FlightSQLDatasource) getColumns(w http.ResponseWriter, r *http.Request) {
tableName := r.URL.Query().Get("table")
if tableName == "" {
http.Error(w, `query parameter "table" is required`, http.StatusBadRequest)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
ctx = metadata.NewOutgoingContext(ctx, d.md)
info, err := d.client.GetTables(ctx, &flightsql.GetTablesOpts{
TableNameFilterPattern: &tableName,
IncludeSchema: true,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reader, err := d.client.DoGet(ctx, info.Endpoint[0].Ticket)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer reader.Release()
if !reader.Next() {
http.Error(w, "table not found", http.StatusNotFound)
return
}
rec := reader.Record()
rec.Retain()
defer rec.Release()
reader.Next()
if err := reader.Err(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
indices := rec.Schema().FieldIndices("table_schema")
if len(indices) == 0 {
http.Error(w, "table_schema field not found", http.StatusInternalServerError)
return
}
col := rec.Column(indices[0])
serializedSchema := array.NewStringData(col.Data()).Value(0)
schema, err := flight.DeserializeSchema([]byte(serializedSchema), memory.DefaultAllocator)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var resp backend.DataResponse
resp.Frames = append(resp.Frames, newFrame(schema))
if err := writeDataResponse(w, resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func newDataResponse(reader recordReader) backend.DataResponse {
var resp backend.DataResponse
frame := newFrame(reader.Schema())
READER:
for reader.Next() {
record := reader.Record()
for i, col := range record.Columns() {
if err := copyData(frame.Fields[i], col); err != nil {
resp.Error = err
break READER
}
}
if err := reader.Err(); err != nil && !errors.Is(err, io.EOF) {
resp.Error = err
break
}
}
resp.Frames = append(resp.Frames, frame)
return resp
}
func writeDataResponse(w io.Writer, resp backend.DataResponse) error {
json, err := resp.MarshalJSON()
if err != nil {
return err
}
_, err = w.Write(json)
return err
}