forked from watsonwanda/oracle-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tick_generic_jq.go
284 lines (263 loc) · 8.06 KB
/
tick_generic_jq.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
// Copyright (C) 2021-2023 Chronicle Labs, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package origin
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/itchyny/gojq"
"github.com/orcfax/oracle-suite/pkg/datapoint"
"github.com/orcfax/oracle-suite/pkg/datapoint/value"
"github.com/orcfax/oracle-suite/pkg/log"
"github.com/orcfax/oracle-suite/pkg/log/null"
"github.com/orcfax/oracle-suite/pkg/util/bn"
)
const TickGenericJQLoggerTag = "TICK_GENERIC_JQ_ORIGIN"
type TickGenericJQConfig struct {
// URL is an TickGenericHTTP endpoint that returns JSON data. It may contain
// the following variables:
// - ${lcbase} - lower case base asset
// - ${ucbase} - upper case base asset
// - ${lcquote} - lower case quote asset
// - ${ucquote} - upper case quote asset
// - ${lcbases} - lower case base assets joined by commas
// - ${ucbases} - upper case base assets joined by commas
// - ${lcquotes} - lower case quote assets joined by commas
// - ${ucquotes} - upper case quote assets joined by commas
URL string
// Query is a JQ query that is used to parse JSON data. It must
// return a single value that will be used as a price or an object with the
// following fields:
// - price - a price
// - time - a timestamp (optional)
// - volume - a 24h volume (optional)
//
// The JQ query may contain the following variables:
// - $lcbase - lower case base asset
// - $ucbase - upper case base asset
// - $lcquote - lower case quote asset
// - $ucquote - upper case quote asset
//
// Price and volume must be a number or a string that can be parsed as a number.
// Time must be a number or a string that can be parsed as a number or a string
// that can be parsed as a time.
Query string
// Headers is a set of TickGenericHTTP headers that are sent with each request.
Headers http.Header
// Client is an TickGenericHTTP client that is used to fetch data from the
// TickGenericHTTP endpoint. If nil, http.DefaultClient is used.
Client *http.Client
// Logger is an TickGenericHTTP logger that is used to log errors. If nil,
// null logger is used.
Logger log.Logger
}
// TickGenericJQ is a generic origin implementation that uses JQ to parse JSON data
// from an TickGenericHTTP endpoint.
type TickGenericJQ struct {
http *TickGenericHTTP
rawQuery string
query *gojq.Code
logger log.Logger
}
// NewTickGenericJQ creates a new TickGenericJQ instance.
//
// The client argument is an TickGenericHTTP client that is used to fetch data from the
// TickGenericHTTP endpoint.
//
// The url argument is an TickGenericHTTP endpoint that returns JSON data. It may contain
// the following variables:
// - ${lcbase} - lower case base asset
// - ${ucbase} - upper case base asset
// - ${lcquote} - lower case quote asset
// - ${ucquote} - upper case quote asset
// - ${lcbases} - lower case base assets joined by commas
// - ${ucbases} - upper case base assets joined by commas
// - ${lcquotes} - lower case quote assets joined by commas
// - ${ucquotes} - upper case quote assets joined by commas
//
// The jq argument is a JQ query that is used to parse JSON data. It must
// return a single value that will be used as a price or an object with the
// following fields:
// - price - a price
// - time - a timestamp (optional)
// - volume - a 24h volume (optional)
//
// The JQ query may contain the following variables:
// - $lcbase - lower case base asset
// - $ucbase - upper case base asset
// - $lcquote - lower case quote asset
// - $ucquote - upper case quote asset
//
// Price and volume must be a string that can be parsed as a number or a number.
//
// Time must be a string that can be parsed as time or a number that represents
// a UNIX timestamp.
//
// If JQ query returns multiple values, the dataPoint will be invalid.
func NewTickGenericJQ(config TickGenericJQConfig) (*TickGenericJQ, error) {
if config.URL == "" {
return nil, fmt.Errorf("url cannot be empty")
}
if config.Query == "" {
return nil, fmt.Errorf("query must be specified")
}
if config.Client == nil {
config.Client = &http.Client{}
}
if config.Logger == nil {
config.Logger = null.New()
}
parsed, err := gojq.Parse(config.Query)
if err != nil {
return nil, err
}
compiled, err := gojq.Compile(parsed, gojq.WithVariables([]string{
"$lcbase",
"$ucbase",
"$lcquote",
"$ucquote",
}))
if err != nil {
return nil, err
}
jq := &TickGenericJQ{}
gh, err := NewTickGenericHTTP(TickGenericHTTPConfig{
URL: config.URL,
Headers: config.Headers,
Callback: jq.handle,
Client: config.Client,
Logger: config.Logger,
})
if err != nil {
return nil, err
}
jq.http = gh
jq.rawQuery = config.Query
jq.query = compiled
jq.logger = config.Logger.WithField("tag", TickGenericJQLoggerTag)
return jq, nil
}
// FetchDataPoints implements the Origin interface.
func (g *TickGenericJQ) FetchDataPoints(ctx context.Context, query []any) (map[any]datapoint.Point, error) {
return g.http.FetchDataPoints(ctx, query)
}
//nolint:funlen
func (g *TickGenericJQ) handle(
ctx context.Context,
pairs []value.Pair,
body io.Reader,
) (map[any]datapoint.Point, error) {
points := make(map[any]datapoint.Point)
// Parse JSON data.
var decoded any
if err := json.NewDecoder(body).Decode(&decoded); err != nil {
return fillDataPointsWithError(points, pairs, err), err
}
// Run JQ query for each pair and parse the result.
for _, pair := range pairs {
g.logger.
WithFields(log.Fields{
"url": g.http.url,
"query": g.rawQuery,
"pairs": pairs,
}).
Debug("JQ request")
point := datapoint.Point{Time: time.Now()}
tick := value.Tick{Pair: pair}
iter := g.query.RunWithContext(
ctx,
decoded,
strings.ToLower(pair.Base), // $lcbase
strings.ToUpper(pair.Base), // $ucbase
strings.ToLower(pair.Quote), // $lcquote
strings.ToUpper(pair.Quote), // $ucquote
)
v, ok := iter.Next()
if !ok {
point.Value = tick
point.Error = fmt.Errorf("no result from JQ query")
points[pair] = point
continue
}
if err, ok := v.(error); ok {
point.Value = tick
point.Error = err
points[pair] = point
continue
}
if _, ok := iter.Next(); ok {
point.Value = tick
point.Error = fmt.Errorf("multiple results from JQ query")
points[pair] = point
continue
}
switch v := v.(type) {
case map[string]any:
for k, v := range v {
switch k {
case "price":
tick.Price = bn.DecFloatPoint(v)
case "volume":
tick.Volume24h = bn.DecFloatPoint(v)
case "time":
if tm, ok := anyToTime(v); ok {
point.Time = tm
}
default:
point.Error = fmt.Errorf("unknown key in JQ result: %s", k)
}
}
case int, int32, int64, uint, uint32, uint64, float32, float64:
tick.Price = bn.DecFloatPoint(v)
}
point.Value = tick
points[pair] = point
}
return points, nil
}
// anyToTime converts an arbitrary value to a time.Time.
func anyToTime(v any) (time.Time, bool) {
switch v := v.(type) {
case string:
for _, layout := range []string{
time.RFC3339,
time.RFC3339Nano,
time.RFC1123,
time.RFC1123Z,
time.RFC822,
time.RFC822Z,
time.RFC850,
time.ANSIC,
time.UnixDate,
time.RubyDate,
} {
t, err := time.Parse(layout, v)
if err == nil {
return t, true
}
}
case int, int32, int64:
return time.Unix(v.(int64), 0), true
case uint, uint32, uint64:
return time.Unix(int64(v.(uint64)), 0), true
case float32, float64:
return time.Unix(int64(v.(float64)), 0), true
}
return time.Time{}, false
}