-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
697 lines (599 loc) · 22.5 KB
/
main.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright (c) 2024 Neomantra Corp
//
// NOTE: this may incur billing, handle with care!
//
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/NimbleMarkets/dbn-go"
dbn_hist "github.com/NimbleMarkets/dbn-go/hist"
dbn_tui "github.com/NimbleMarkets/dbn-go/internal/tui"
"github.com/charmbracelet/huh"
"github.com/dustin/go-humanize"
"github.com/neomantra/ymdflag"
"github.com/segmentio/encoding/json"
"github.com/spf13/cobra"
)
///////////////////////////////////////////////////////////////////////////////
const defaultMaxActiveDownloads = 4
var (
databentoApiKey string
dataset string
schemaStr string
allSymbols bool
symbolsFile string
stypeIn dbn.SType = dbn.SType_RawSymbol
stypeOut dbn.SType = dbn.SType_InstrumentId
outputFile string
emitJSON bool // emit json from responses
encoding dbn.Encoding = dbn.Encoding_Dbn
compression dbn.Compression = dbn.Compress_ZStd
maxActiveDownloads int = defaultMaxActiveDownloads
jobID string
stateFilter string
startYMD ymdflag.YMDFlag
endYMD ymdflag.YMDFlag
useForce bool
)
func getDateRangeArg() dbn_hist.DateRange {
dateRange := dbn_hist.DateRange{}
if !startYMD.IsZero() {
dateRange.Start = startYMD.AsTime()
}
if !endYMD.IsZero() {
dateRange.End = endYMD.AsTime()
}
return dateRange
}
// getMetadataQueryParams returns a MetadataQueryParams struct based on CLI globals and the given symbols.
func getMetadataQueryParams(symbols []string) dbn_hist.MetadataQueryParams {
return dbn_hist.MetadataQueryParams{
Dataset: dataset,
Symbols: symbols,
Schema: schemaStr,
DateRange: getDateRangeArg(),
Mode: dbn_hist.FeedMode_Historical,
StypeIn: stypeIn,
Limit: -1,
}
}
// SubmitJobParams returns a MetadataQueryParams struct based on CLI globals and the given symbols.
func getSubmitJobParams(symbols []string) dbn_hist.SubmitJobParams {
symbolsStr := strings.Join(symbols, ",")
schema, _ := dbn.SchemaFromString(schemaStr)
return dbn_hist.SubmitJobParams{
Dataset: dataset,
Symbols: symbolsStr,
Schema: schema,
DateRange: getDateRangeArg(),
Encoding: encoding,
Compression: compression,
Delivery: dbn_hist.Delivery_Download,
StypeIn: stypeIn,
StypeOut: stypeOut,
}
}
// Returns a list of symbols from the command line arguments, or otherwise exits with an error.
// If --all is set, returns ["ALL_SYMBOLS"]. Also handles loading from a symbol file.
func requireSymbolArgs(args []string) []string {
if allSymbols {
return []string{"ALL_SYMBOLS"}
}
result := args
if symbolsFile != "" {
symbols, err := loadSymbolFile(symbolsFile)
requireNoError(err)
result = append(result, symbols...)
}
if len(result) == 0 {
fmt.Fprint(os.Stderr, "must pass symbols as arguments or use --file or --all\n")
os.Exit(1)
}
return result
}
// loadSymbolFile loads a newline delimited file of symbols from `filename` and returns them as a slice.
// Returns an error if any.
// Empty lines and rows starting with `#“ are ignored.
func loadSymbolFile(filename string) ([]string, error) {
if filename == "" {
return nil, fmt.Errorf("filename was empty")
}
fileBytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var symbols []string
scanner := bufio.NewScanner(bytes.NewReader(fileBytes))
for scanner.Scan() {
text := scanner.Text()
if len(text) > 0 && text[0] != '#' { // skip empty lines and comments
symbols = append(symbols, strings.TrimSpace(text))
}
}
if err := scanner.Err(); err != nil {
return symbols, err
}
return symbols, nil
}
///////////////////////////////////////////////////////////////////////////////
func requireDatabentoApiKey() string {
if databentoApiKey == "" {
databentoApiKey = os.Getenv("DATABENTO_API_KEY")
if databentoApiKey == "" {
fmt.Fprint(os.Stderr, "DATABENTO_API_KEY not set. Use --key or DATABENTO_API_KEY envvar.\n")
os.Exit(1)
}
}
return databentoApiKey
}
func requireNoError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
os.Exit(1)
}
}
func requireNoErrorMsg(err error, msg string) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s\n", msg, err.Error())
os.Exit(1)
}
}
func requireHumanConfirmation(promptTitle string, verbName string) {
doVerb := false
form := huh.NewForm(
huh.NewGroup(
huh.NewConfirm().
Affirmative(fmt.Sprintf("Yes, %s", verbName)).
Negative("No, Cancel").
Title(promptTitle).
Value(&doVerb),
))
err := form.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "confirmation error: %s\n", err.Error())
os.Exit(1)
}
if !doVerb {
os.Exit(0)
}
}
///////////////////////////////////////////////////////////////////////////////
func main() {
cobra.OnInitialize()
rootCmd.PersistentFlags().StringVarP(&databentoApiKey, "key", "k", "", "Databento API key (or use DATABENT_API_KEY envvar)")
rootCmd.AddCommand(listDatasetsCmd)
listDatasetsCmd.Flags().VarP(&startYMD, "start", "t", "Start date as YYYYMMDD")
listDatasetsCmd.Flags().VarP(&endYMD, "end", "e", "End date as YYYYMMDD")
listDatasetsCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
rootCmd.AddCommand(listPublishersCmd)
listPublishersCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
rootCmd.AddCommand(listSchemasCmd)
listSchemasCmd.Flags().StringVarP(&dataset, "dataset", "d", "", "Dataset to list schema for")
listSchemasCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
listSchemasCmd.MarkFlagRequired("dataset")
rootCmd.AddCommand(listFieldsCmd)
listFieldsCmd.Flags().StringVarP(&schemaStr, "schema", "s", "", "Schema to list fields for")
listFieldsCmd.Flags().VarP(&encoding, "encoding", "", "Encoding to use ('dbn', 'csv', 'json')")
listFieldsCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
listFieldsCmd.MarkFlagRequired("schema")
rootCmd.AddCommand(listUnitPricesCmd)
listUnitPricesCmd.Flags().StringVarP(&dataset, "dataset", "d", "", "Dataset to list schema for")
listUnitPricesCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
listUnitPricesCmd.MarkFlagRequired("dataset")
rootCmd.AddCommand(getDatasetConditionCmd)
getDatasetConditionCmd.Flags().StringVarP(&dataset, "dataset", "d", "", "Dataset to get condition for")
getDatasetConditionCmd.Flags().VarP(&startYMD, "start", "t", "Start date as YYYYMMDD")
getDatasetConditionCmd.Flags().VarP(&endYMD, "end", "e", "End date as YYYYMMDD")
getDatasetConditionCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
getDatasetConditionCmd.MarkFlagRequired("dataset")
rootCmd.AddCommand(getDatasetRangeCmd)
getDatasetRangeCmd.Flags().StringVarP(&dataset, "dataset", "d", "", "Dataset to get date range for")
getDatasetRangeCmd.MarkFlagRequired("dataset")
rootCmd.AddCommand(getCostCmd)
getCostCmd.Flags().StringVarP(&dataset, "dataset", "d", "", "Dataset to get cost for")
getCostCmd.Flags().StringVarP(&schemaStr, "schema", "s", "", "Schema to get cost for")
getCostCmd.Flags().StringVarP(&symbolsFile, "file", "f", "", "Newline delimited file to read symbols from (# is comment)")
getCostCmd.Flags().BoolVarP(&allSymbols, "all", "", false, "Get record count for all symbols")
getCostCmd.Flags().VarP(&startYMD, "start", "t", "Start date as YYYYMMDD")
getCostCmd.Flags().VarP(&endYMD, "end", "e", "End date as YYYYMMDD")
getCostCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
getCostCmd.Flags().VarP(&stypeIn, "sin", "", "Set stype_in: one of instrument_id, id, instr, raw_symbol, raw, smart, continuous, parent, nasdaq, cms (default: 'raw')")
getCostCmd.Flags().VarP(&stypeOut, "sout", "", "Set stype_out: one of instrument_id, id, instr, raw_symbol, raw, smart, continuous, parent, nasdaq, cms (default: 'id')")
getCostCmd.MarkFlagRequired("dataset")
getCostCmd.MarkFlagRequired("schema")
rootCmd.AddCommand(listJobsCmd)
listJobsCmd.Flags().StringVarP(&stateFilter, "state", "", "", "Comma-separated Filter for job states. Can include 'received', 'queued', 'processing', 'done', and 'expired'.")
listJobsCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
listJobsCmd.Flags().VarP(&startYMD, "start", "t", "Start date as YYYYMMDD (optional)")
rootCmd.AddCommand(listFilesCmd)
listFilesCmd.Flags().StringVarP(&jobID, "job", "", "", "Job ID to list files for")
listFilesCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
listFilesCmd.MarkFlagRequired("job")
rootCmd.AddCommand(submitJobCmd)
submitJobCmd.Flags().StringVarP(&dataset, "dataset", "d", "", "Dataset to request")
submitJobCmd.Flags().StringVarP(&schemaStr, "schema", "s", "", "Schema to request")
submitJobCmd.Flags().VarP(&encoding, "encoding", "", "Encoding to use ('dbn', 'csv', 'json')")
submitJobCmd.Flags().VarP(&compression, "compression", "", "Compression to use ('none', 'zstd')")
submitJobCmd.Flags().StringVarP(&symbolsFile, "file", "f", "", "Newline delimited file to read symbols from (# is comment)")
submitJobCmd.Flags().BoolVarP(&allSymbols, "all", "", false, "Request data for all symbols")
submitJobCmd.Flags().BoolVarP(&useForce, "force", "", false, "Do not warn about all symbols or cost")
submitJobCmd.Flags().VarP(&startYMD, "start", "t", "Start date as YYYYMMDD.")
submitJobCmd.Flags().VarP(&endYMD, "end", "e", "End date as YYYYMMDD")
submitJobCmd.Flags().VarP(&stypeIn, "sin", "", "Set stype_in: one of instrument_id, id, instr, raw_symbol, raw, smart, continuous, parent, nasdaq, cms (default: 'raw')")
submitJobCmd.Flags().VarP(&stypeOut, "sout", "", "Set stype_out: one of instrument_id, id, instr, raw_symbol, raw, smart, continuous, parent, nasdaq, cms (default: 'id')")
submitJobCmd.MarkFlagRequired("dataset")
submitJobCmd.MarkFlagRequired("schema")
submitJobCmd.MarkFlagRequired("start")
rootCmd.AddCommand(getRangeCmd)
getRangeCmd.Flags().StringVarP(&dataset, "dataset", "d", "", "Dataset to request")
getRangeCmd.Flags().StringVarP(&schemaStr, "schema", "s", "", "Schema to request")
getRangeCmd.Flags().VarP(&encoding, "encoding", "", "Encoding to use ('dbn', 'csv', 'json')")
getRangeCmd.Flags().VarP(&compression, "compression", "", "Compression to use ('none', 'zstd')")
getRangeCmd.Flags().StringVarP(&symbolsFile, "file", "f", "", "Newline delimited file to read symbols from (# is comment)")
getRangeCmd.Flags().BoolVarP(&allSymbols, "all", "", false, "Request data for all symbols")
getRangeCmd.Flags().BoolVarP(&useForce, "force", "", false, "Do not warn about all symbols or cost")
getRangeCmd.Flags().VarP(&startYMD, "start", "t", "Start date as YYYYMMDD.")
getRangeCmd.Flags().VarP(&endYMD, "end", "e", "End date as YYYYMMDD")
getRangeCmd.Flags().VarP(&stypeIn, "sin", "", "Set stype_in: one of instrument_id, id, instr, raw_symbol, raw, smart, continuous, parent, nasdaq, cms (default: 'raw')")
getRangeCmd.Flags().VarP(&stypeOut, "sout", "", "Set stype_out: one of instrument_id, id, instr, raw_symbol, raw, smart, continuous, parent, nasdaq, cms (default: 'id')")
getRangeCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file for data ('-' is stdout)")
getRangeCmd.MarkFlagRequired("dataset")
getRangeCmd.MarkFlagRequired("schema")
getRangeCmd.MarkFlagRequired("start")
getRangeCmd.MarkFlagRequired("output")
rootCmd.AddCommand(resolveCmd)
resolveCmd.Flags().StringVarP(&dataset, "dataset", "d", "", "Dataset to resolve")
resolveCmd.Flags().StringVarP(&schemaStr, "schema", "s", "", "Schema to resolve")
resolveCmd.Flags().BoolVarP(&allSymbols, "all", "", false, "Resolve all symbols")
resolveCmd.Flags().VarP(&startYMD, "start", "t", "Start date as YYYYMMDD")
resolveCmd.Flags().VarP(&endYMD, "end", "e", "End date as YYYYMMDD")
resolveCmd.Flags().VarP(&stypeIn, "sin", "", "Set stype_in: one of instrument_id, id, instr, raw_symbol, raw, smart, continuous, parent, nasdaq, cms (default: 'raw')")
resolveCmd.Flags().VarP(&stypeOut, "sout", "", "Set stype_out: one of instrument_id, id, instr, raw_symbol, raw, smart, continuous, parent, nasdaq, cms (default: 'id')")
resolveCmd.Flags().BoolVarP(&emitJSON, "json", "j", false, "Emit JSON instead of simple summary")
resolveCmd.MarkFlagRequired("dataset")
resolveCmd.MarkFlagRequired("start")
rootCmd.AddCommand(tuiCmd)
tuiCmd.Flags().IntVarP(&maxActiveDownloads, "limit", "l", defaultMaxActiveDownloads, "Limit maximum concurrent downloads")
err := rootCmd.Execute()
requireNoError(err)
}
///////////////////////////////////////////////////////////////////////////////
var rootCmd = &cobra.Command{
Use: "dbn-go-hist",
Short: "dbn-go-hist queries the Databento Historical API.",
Long: "dbn-go-hist queries the Databento Historical API.",
}
var listDatasetsCmd = &cobra.Command{
Use: "datasets",
Aliases: []string{"d"},
Short: "Queries Databento Hist for datasets and prints them",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
dateRange := getDateRangeArg()
datasets, err := dbn_hist.ListDatasets(apiKey, dateRange)
requireNoError(err)
if emitJSON {
printJSON(datasets)
return
}
for _, dataset := range datasets {
fmt.Fprintf(os.Stdout, "%s\n", dataset)
}
},
}
var listPublishersCmd = &cobra.Command{
Use: "publishers",
Aliases: []string{"p"},
Short: "Queries Databento Hist for publishers and prints them",
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
publishers, err := dbn_hist.ListPublishers(apiKey)
requireNoError(err)
if emitJSON {
printJSON(publishers)
return
}
for _, publisher := range publishers {
fmt.Fprintf(os.Stdout, "%d %s %s %s\n",
publisher.PublisherID,
publisher.Venue,
publisher.Dataset,
publisher.Description,
)
}
},
}
var listSchemasCmd = &cobra.Command{
Use: "schemas",
Aliases: []string{"s"},
Short: "Queries Databento Hist for publishers and prints them",
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
schemas, err := dbn_hist.ListSchemas(apiKey, dataset)
requireNoError(err)
if emitJSON {
printJSON(schemas)
return
}
for _, schema := range schemas {
fmt.Fprintf(os.Stdout, " %s\n", schema)
}
},
}
var listFieldsCmd = &cobra.Command{
Use: "fields",
Aliases: []string{"f"},
Short: "Queries Databento Hist for fields of a schema/encoding and prints them",
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
schema, err := dbn.SchemaFromString(schemaStr)
requireNoError(err)
fields, err := dbn_hist.ListFields(apiKey, encoding, schema)
requireNoError(err)
if emitJSON {
printJSON(fields)
return
}
for _, field := range fields {
fmt.Fprintf(os.Stdout, "%s %s\n", field.Name, field.TypeName)
}
},
}
var listUnitPricesCmd = &cobra.Command{
Use: "unit-prices",
Aliases: []string{"u", "up"},
Short: "Queries Databento Hist for unit prices of a dataset",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
unitPrices, err := dbn_hist.ListUnitPrices(apiKey, dataset)
requireNoError(err)
if emitJSON {
printJSON(unitPrices)
return
}
for _, unitPrice := range unitPrices {
fmt.Fprintf(os.Stdout, "%s %s\n", dataset, unitPrice.Mode)
for schema, price := range unitPrice.UnitPrices {
fmt.Fprintf(os.Stdout, " %s %0.4f\n", schema, price)
}
}
},
}
var getDatasetConditionCmd = &cobra.Command{
Use: "dataset-condition",
Aliases: []string{"dc"},
Short: "Queries Databento Hist for condition of a dataset",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
conditions, err := dbn_hist.GetDatasetCondition(apiKey, dataset, getDateRangeArg())
requireNoError(err)
if emitJSON {
printJSON(conditions)
return
}
for _, c := range conditions {
fmt.Fprintf(os.Stdout, "%s %s %s %s\n", dataset, c.Condition, c.Date, c.LastModified)
}
},
}
var getDatasetRangeCmd = &cobra.Command{
Use: "dataset-range",
Aliases: []string{"dr"},
Short: "Queries Databento Hist for date range of a dataset",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
datasetRange, err := dbn_hist.GetDatasetRange(apiKey, dataset)
requireNoError(err)
fmt.Fprintf(os.Stdout, "%s start:%s end: %s\n",
dataset,
datasetRange.Start.Format("2006-01-02"),
datasetRange.End.Format("2006-01-02"),
)
},
}
var getCostCmd = &cobra.Command{
Use: "cost",
Aliases: []string{"c"},
Short: "Queries Databento Hist for the cost and size of a GetRange query",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
symbols := requireSymbolArgs(args)
metaParams := getMetadataQueryParams(symbols)
cost, err := dbn_hist.GetCost(apiKey, metaParams)
requireNoError(err)
dataSize, err := dbn_hist.GetBillableSize(apiKey, metaParams)
requireNoError(err)
recordCount, err := dbn_hist.GetRecordCount(apiKey, metaParams)
requireNoError(err)
if emitJSON {
printJSON(map[string]interface{}{
"query": metaParams,
"cost": cost,
"data_size": dataSize,
"record_count": recordCount,
})
return
}
fmt.Fprintf(os.Stdout, "%s %s $ %0.06f %d bytes %d records\n",
metaParams.Dataset, metaParams.Schema, cost, dataSize, recordCount)
},
}
var listJobsCmd = &cobra.Command{
Use: "jobs",
Aliases: []string{"lj", "j"},
Short: "Lists Databento Hist jobs",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
jobs, err := dbn_hist.ListJobs(apiKey, stateFilter, getDateRangeArg().Start)
requireNoError(err)
if emitJSON {
printJSON(jobs)
return
}
for _, job := range jobs {
jstr, err := json.Marshal(job)
requireNoError(err)
fmt.Fprintf(os.Stdout, "%s\n", jstr)
}
},
}
var listFilesCmd = &cobra.Command{
Use: "files",
Aliases: []string{"lf", "f"},
Short: "Lists files for the given Databento Hist JobID",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
files, err := dbn_hist.ListFiles(apiKey, jobID)
requireNoError(err)
if emitJSON {
printJSON(files)
return
}
for _, file := range files {
jstr, err := json.Marshal(file)
requireNoError(err)
fmt.Fprintf(os.Stdout, "%s\n", jstr)
}
},
}
var submitJobCmd = &cobra.Command{
Use: "submit-job",
Aliases: []string{"submit"},
Short: "Submit a data request job to the Hist API",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
symbols := requireSymbolArgs(args)
jobParams := getSubmitJobParams(symbols)
requireBudgetApproval(apiKey, symbols, &jobParams)
batchJob, err := dbn_hist.SubmitJob(apiKey, jobParams)
requireNoErrorMsg(err, "error submitting job")
jstr, err := json.Marshal(batchJob)
requireNoErrorMsg(err, "error unmarshalling batchJob")
fmt.Fprintf(os.Stdout, "%s\n", jstr)
},
}
var getRangeCmd = &cobra.Command{
Use: "get-range",
Aliases: []string{"range"},
Short: "Download a range of data from the Hist API",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
// Check for file creation first
var writer io.Writer
var closer io.Closer
fileCloser := func() {
if closer != nil {
closer.Close()
}
}
if outputFile != "-" {
file, err := os.Create(outputFile)
requireNoErrorMsg(err, "error creating output file")
writer, closer = file, file
} else {
writer, closer = os.Stdout, nil
}
defer fileCloser()
// Now proceed with request
apiKey := requireDatabentoApiKey()
symbols := requireSymbolArgs(args)
jobParams := getSubmitJobParams(symbols)
requireBudgetApproval(apiKey, symbols, &jobParams)
dbnData, err := dbn_hist.GetRange(apiKey, jobParams)
requireNoErrorMsg(err, "error getting range")
// Write the output
_, err = writer.Write(dbnData)
requireNoErrorMsg(err, "error writing output")
},
}
var resolveCmd = &cobra.Command{
Use: "resolve",
Aliases: []string{"symbols"},
Short: "Resolve symbols via the Databento Symbology API",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
apiKey := requireDatabentoApiKey()
symbols := requireSymbolArgs(args)
dateRange := getDateRangeArg()
resolveParams := dbn_hist.ResolveParams{
Dataset: dataset,
Symbols: symbols,
StypeIn: stypeIn,
StypeOut: stypeOut,
DateRange: dateRange,
}
resolution, err := dbn_hist.SymbologyResolve(apiKey, resolveParams)
requireNoError(err)
if emitJSON {
printJSON(resolution)
return
}
// human mode just print the symbols
for symbol := range resolution.Mappings {
fmt.Fprintf(os.Stdout, "%s\n", symbol)
}
},
}
var tuiCmd = &cobra.Command{
Use: "tui",
Aliases: []string{"symbols"},
Short: "dbn-go-hist TUI",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
config := dbn_tui.Config{
DatabentoApiKey: requireDatabentoApiKey(),
MaxActiveDownloads: maxActiveDownloads,
}
if config.MaxActiveDownloads < 0 {
fmt.Fprintf(os.Stderr, "--limit cannot be negative\n")
return
}
err := dbn_tui.Run(config)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
}
},
}
//////////////////////////////////////////////////////////////////////////////
func requireBudgetApproval(apiKey string, symbols []string, params *dbn_hist.SubmitJobParams) {
if useForce {
return
}
if params.Symbols == "" || strings.Contains(params.Symbols, "ALL_SYMBOLS") {
fmt.Fprint(os.Stderr, "Submitting for ALL_SYMBOLS...\n")
requireHumanConfirmation(
"Are you sure you want to submit for ALL_SYMBOLS?",
"Submit for ALL")
}
// Request cost of this job
fmt.Fprintf(os.Stderr, "Getting cost estimates for job...\n")
metaParams := getMetadataQueryParams(symbols)
cost, err := dbn_hist.GetCost(apiKey, metaParams)
requireNoError(err)
dataSize, err := dbn_hist.GetBillableSize(apiKey, metaParams)
requireNoError(err)
recordCount, err := dbn_hist.GetRecordCount(apiKey, metaParams)
requireNoError(err)
fmt.Fprintf(os.Stderr, "Estimated cost of $%.2f for %s records and %s of data.\n",
cost, humanize.Comma(int64(recordCount)), humanize.Bytes(uint64(dataSize)))
requireHumanConfirmation("Are you sure you want to submit?\n", "Submit Job")
}
// printJSON is a generic helper to print a value as JSON to stdout.
func printJSON[T any](val T) {
jstr, err := json.Marshal(val)
requireNoError(err)
fmt.Fprintf(os.Stdout, "%s\n", jstr)
}