-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
210 lines (173 loc) · 4.33 KB
/
engine.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
package timeplus
import (
"bytes"
"context"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
protonDriver "github.com/timeplus-io/proton-go-driver/v2"
)
const (
bufferSize = 1000
defaultTimeout = 10 * time.Second
)
type Column struct {
Name string
Type string
}
type TimeplusEngine struct {
connection *sql.DB
logger log.Logger
analyzeURL string
pingURL string
client *http.Client
header http.Header
}
func NewEngine(logger log.Logger, host string, tcpPort, httpPort int, username, password string) *TimeplusEngine {
if tcpPort == 0 {
tcpPort = 8463
}
if httpPort == 0 {
httpPort = 3218
}
if len(username) == 0 {
username = "default"
}
connection := protonDriver.OpenDB(&protonDriver.Options{
Addr: []string{fmt.Sprintf("%s:%d", host, tcpPort)},
Auth: protonDriver.Auth{
Username: username,
Password: password,
},
DialTimeout: defaultTimeout,
Debug: false,
})
header := http.Header{}
header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))))
header.Set("Content-Type", "application/json")
return &TimeplusEngine{
connection: connection,
logger: logger,
analyzeURL: fmt.Sprintf("http://%s:%d/proton/v1/sqlanalyzer", host, httpPort),
pingURL: fmt.Sprintf("http://%s:%d/proton/ping", host, httpPort),
header: header,
client: &http.Client{
Timeout: defaultTimeout,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: defaultTimeout,
}).Dial,
TLSHandshakeTimeout: defaultTimeout,
},
},
}
}
func (e *TimeplusEngine) Ping(ctx context.Context) error {
if err := e.pingHttp(ctx); err != nil {
return fmt.Errorf("failed to ping via http: %w", err)
}
if err := e.connection.Ping(); err != nil {
return fmt.Errorf("failed to ping via tcp: %w", err)
}
return nil
}
func (e *TimeplusEngine) RunQuery(ctx context.Context, sql string) ([]*sql.ColumnType, chan []any, error) {
ckCtx := protonDriver.Context(ctx)
rows, err := e.connection.QueryContext(ckCtx, sql)
if err != nil {
return nil, nil, err
}
columnTypes, err := rows.ColumnTypes()
if err != nil {
return nil, nil, err
}
ch := make(chan []any, bufferSize)
go func() {
defer func() {
close(ch)
}()
count := len(columnTypes)
for rows.Next() {
values := make([]any, count) // values is raw data
valuePtrs := make([]any, count)
row := make([]any, count) // row is string data
for i := range columnTypes {
valuePtrs[i] = &values[i]
}
if err = rows.Scan(valuePtrs...); err != nil {
return
}
for i := range columnTypes {
rawValue := values[i]
row[i] = rawValue
}
ch <- row
}
rows.Close()
}()
return columnTypes, ch, nil
}
func (e *TimeplusEngine) Dispose() error {
return e.connection.Close()
}
func (e *TimeplusEngine) IsStreamingQuery(ctx context.Context, query string) (bool, error) {
queryMap := map[string]string{"query": query}
jsonData, err := json.Marshal(queryMap)
if err != nil {
return false, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, e.analyzeURL, bytes.NewBuffer(jsonData))
if err != nil {
return false, err
}
req.Header = e.header
resp, err := e.client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 399 {
var errStr string
body, err := io.ReadAll(resp.Body)
if err != nil {
errStr = err.Error()
} else {
errStr = string(body)
}
return false, fmt.Errorf("failed to analyze code: %d, error: %s", resp.StatusCode, errStr)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
var response map[string]interface{}
if err = json.Unmarshal(body, &response); err != nil {
return false, err
}
isStreaming, ok := response["is_streaming"].(bool)
if !ok {
return false, fmt.Errorf("invalid response %s", response)
}
return isStreaming, nil
}
func (e *TimeplusEngine) pingHttp(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, e.pingURL, nil)
if err != nil {
return err
}
req.Header = e.header
resp, err := e.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to ping, got %d", resp.StatusCode)
}
return nil
}