data

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package data provides Parquet read/write, CSV helpers, download utilities, and shared market-data types used across ETL and DuckDB queries.

See https://pkg.go.dev/github.com/eslider/go-fdp/pkg/data

Index

Constants

View Source
const (
	NoFrame     Frame = 0
	Nanosecond  Frame = 1
	Microsecond       = 1000 * Nanosecond
	Millisecond       = 1000 * Microsecond
	Second            = 1000 * Millisecond
	Minute            = 60 * Second
	ThreeMinute       = 3 * Minute
	FiveMinute        = 5 * Minute
	FifteenMin        = 15 * Minute
	Hour              = 60 * Minute
	TwoHour           = 2 * Hour
	OneDay            = 24 * Hour
	OneWeek           = 7 * OneDay
)

Variables

View Source
var (
	ErrFileExists = errors.New("file exists")
	ErrWriteFile  = errors.New("failed to write file")
)
View Source
var Frames = map[string]Frame{
	"":    NoFrame,
	"1ns": Nanosecond,
	"1us": Microsecond,
	"1s":  Second,
	"1m":  Minute,
	"3m":  ThreeMinute,
	"5m":  FiveMinute,
	"15m": FifteenMin,
	"1h":  Hour,
	"2h":  TwoHour,
	"4h":  4 * Hour,
	"1d":  OneDay,
	"1w":  OneWeek,
}

Frames maps Binance interval strings to Frame values.

Functions

func AnyTimestampToTime

func AnyTimestampToTime(ts int64) *time.Time

AnyTimestampToTime converts variants of timestamp to time.Time:

  • 1: microseconds
  • 2: seconds
  • 3: milliseconds

func DrainParquet

func DrainParquet[T any](ctx context.Context, recordCh <-chan *T, errCh <-chan error) ([]*T, error)

DrainParquet reads all records from parquet channels until both close.

func EncodeGetQuery

func EncodeGetQuery(url string, req any) (string, error)

EncodeGetQuery encodes the request parameters into a query string.

func ExtractZip

func ExtractZip(zipFilePath, destinationFolder string) error

ExtractZip extracts a zip file to a destination folder.

func FillStruct

func FillStruct(t any, record []string, tagNames ...string) (err error)

FillStruct fills struct fields based on csv tag numbers

func IsToday

func IsToday(t time.Time) bool

IsToday checks if the given time is today (at or after midnight UTC today)

func JsonDecode

func JsonDecode[T any](w io.Reader) (*T, error)

func LastMomentOfYesterday

func LastMomentOfYesterday() time.Time

LastMomentOfYesterday return's midnight.

func Params

func Params(req any) (string, error)

func ParquetOpenTimeMSExpr

func ParquetOpenTimeMSExpr(year, month, day int) string

ParquetOpenTimeMSExpr returns a DuckDB expression for open_time column → epoch milliseconds. midnight is UTC midnight of the parquet file's calendar day.

func ParseFloat

func ParseFloat(s string) float64

func PopulateStructFromSlice

func PopulateStructFromSlice(data *[]any, v any) error

PopulateStructFromSlice maps []any to struct using `csv:"index"` tags

func QueryParquets

func QueryParquets(
	db *sql.DB,
	query string,
	dq any,
) (resCh chan map[string]any, errCh chan error)

func ReadHeaderlessCSV

func ReadHeaderlessCSV[T any](reader io.Reader) (ch chan struct {
	Value *T
	Error error
})

ReadHeaderlessCSV reads headerless CSV (like Binance data) and sends records to channel.

func ReadHeaderlessCSVChan

func ReadHeaderlessCSVChan[T any](reader io.Reader) (ch chan *T, errCh chan error)

ReadHeaderlessCSVChan reads headerless CSV (like Binance data) and sends records to channel Maps CSV columns positionally to struct fields based on csv tag numbers

func ReadParquet

func ReadParquet[T any](
	path string,
) (
	rCh chan *T,
	errCh chan error,
)

ReadParquet reads records from a parquet file

func SetStructField

func SetStructField(
	field reflect.Value,
	value string,
) (err error)

SetStructField sets field value based on type

func SortBy

func SortBy[S ~[]E, E any, K cmp.Ordered](s S, key func(E) K)

SortBy sorts s in place by key ascending.

func ToMicroseconds

func ToMicroseconds(ts int64) int64

func WriteParquet

func WriteParquet[T any](
	path string,
) (
	rCh chan *T,
	errCh chan error,
)

WriteParquet writes records to a parquet file

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is a custom type that implements io.WriterAt

func Decompress

func Decompress(compressed []byte) (out *Buffer, err error)

Decompress ZIP bytes and returns the original content

func DownloadFile

func DownloadFile(url string) (buf *Buffer, err error)

DownloadFile downloads a file from the given URL to the specified destination path.

func ReadIntoBuffer

func ReadIntoBuffer(path string) (buf *Buffer, err error)

ReadIntoBuffer reads the content of a file into a Buffer

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes return the underlying byte slice

func (*Buffer) Close

func (b *Buffer) Close() error

Close closes the buffer

func (*Buffer) Decompress

func (b *Buffer) Decompress() (out *Buffer, err error)

Decompress buffers content if it's compressed'

func (*Buffer) Persist

func (b *Buffer) Persist(path string) (err error)

Persist writes the buffer to a file

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err error)

Read reads data form Buffer

func (*Buffer) Seek

func (b *Buffer) Seek(offset int64, whence int) (int64, error)

Seek seeks in the underlying memory buffer.

func (*Buffer) String

func (b *Buffer) String() string

String returns the string representation of the buffer

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

Write writes data from p into BufferFile.

func (*Buffer) WriteAt

func (b *Buffer) WriteAt(p []byte, off int64) (n int, err error)

type Frame

type Frame time.Duration

Frame is a candle aggregation interval (duration-based for arithmetic).

func NewFrame

func NewFrame(s string) Frame

NewFrame parses an interval string; empty string defaults to 1m.

func StringToFrame

func StringToFrame(s string) Frame

func (Frame) String

func (f Frame) String() string

type ResponseDate

type ResponseDate time.Time

func (ResponseDate) GoString

func (r ResponseDate) GoString() string

func (ResponseDate) MarshalJSON

func (r ResponseDate) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (ResponseDate) String

func (r ResponseDate) String() string

String

func (*ResponseDate) UnmarshalJSON

func (r *ResponseDate) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface

type TimestampType

type TimestampType int
const (
	TimestampInSeconds TimestampType = iota + 1
	TimestampInMillis
	TimestampInMicros
)

func TypeOfTimestamp

func TypeOfTimestamp(ts int64) TimestampType

TypeOfTimestamp returns the type of timestamp: seconds, milliseconds or microseconds

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL