This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fluxcsv.go
201 lines (161 loc) · 4.8 KB
/
fluxcsv.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
package flux
import (
"bytes"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/apache/arrow/go/v10/arrow"
"github.com/apache/arrow/go/v10/arrow/array"
"github.com/apache/arrow/go/v10/arrow/memory"
"github.com/influxdata/flux"
flux_array "github.com/influxdata/flux/array"
flux_csv "github.com/influxdata/flux/csv"
"github.com/spiceai/spiceai/pkg/loggers"
"github.com/spiceai/spiceai/pkg/util"
"go.uber.org/zap"
)
var (
zaplog *zap.Logger = loggers.ZapLogger()
)
const (
FluxCsvProcessorName string = "flux-csv"
)
type FluxCsvProcessor struct {
data []byte
dataMutex sync.RWMutex
dataHash []byte
}
func NewFluxCsvProcessor() *FluxCsvProcessor {
return &FluxCsvProcessor{}
}
func (p *FluxCsvProcessor) Init(params map[string]string, identifiers map[string]string, measurements map[string]string, categories map[string]string, tags []string) error {
return nil
}
func (p *FluxCsvProcessor) OnData(data []byte) ([]byte, error) {
p.dataMutex.Lock()
defer p.dataMutex.Unlock()
newDataHash, err := util.ComputeNewHash(p.data, p.dataHash, data)
if err != nil {
return nil, fmt.Errorf("error computing new data hash in %s processor: %w", FluxCsvProcessorName, err)
}
if newDataHash != nil {
// Only update data if new
p.data = data
p.dataHash = newDataHash
}
return data, nil
}
func (p *FluxCsvProcessor) GetRecord() (arrow.Record, error) {
p.dataMutex.Lock()
defer p.dataMutex.Unlock()
if len(p.data) == 0 {
return nil, nil
}
reader := bytes.NewReader(p.data)
readCloser := io.NopCloser(reader)
decoder := flux_csv.NewMultiResultDecoder(flux_csv.ResultDecoderConfig{ /* Use defaults */ })
results, err := decoder.Decode(readCloser)
if err != nil {
return nil, err
}
defer results.Release()
arrow_fields := []arrow.Field{}
pool := memory.NewGoAllocator()
timeBuilder := array.NewInt64Builder(pool)
defer timeBuilder.Release()
valueBuilder := array.NewFloat64Builder(pool)
defer valueBuilder.Release()
tagListBuilder := array.NewListBuilder(pool, arrow.BinaryTypes.String)
defer tagListBuilder.Release()
tagValueBuilder := tagListBuilder.ValueBuilder().(*array.StringBuilder)
defer tagValueBuilder.Release()
for results.More() {
result := results.Next()
err = result.Tables().Do(func(t flux.Table) error {
return t.Do(func(colReader flux.ColReader) error {
defer colReader.Release()
timeIndex := -1
fieldIndex := -1
valueIndex := -1
var tagIndices []int
for colIndex, colMeta := range colReader.Cols() {
// We currently only support one field and float for now
switch {
case colMeta.Label == "_time":
timeIndex = colIndex
case colMeta.Label == "_field":
fieldIndex = colIndex
case colMeta.Label == "_value":
valueIndex = colIndex
case colMeta.Label == "_measurement" || colMeta.Type.String() != "string":
continue
default:
tagIndices = append(tagIndices, colIndex)
}
}
if timeIndex == -1 {
return errors.New("'_time' not found in table data")
}
if fieldIndex == -1 {
return errors.New("'_field' not found in table data")
}
if valueIndex == -1 {
return errors.New("'_value' not found in table data")
}
times := colReader.Times(timeIndex)
defer times.Release()
fields := colReader.Strings(fieldIndex)
defer fields.Release()
values := colReader.Floats(valueIndex)
defer values.Release()
var tags []*flux_array.String
for _, tagIndex := range tagIndices {
tags = append(tags, colReader.Strings(tagIndex))
}
if len(arrow_fields) == 0 {
arrow_fields = []arrow.Field{
{Name: "time", Type: arrow.PrimitiveTypes.Int64},
{Name: fmt.Sprintf("measure.%s", fields.Value(0)), Type: arrow.PrimitiveTypes.Float64},
{Name: "tags", Type: arrow.ListOf(arrow.BinaryTypes.String)},
}
}
secondScale := int64(time.Second)
for i := 0; i < colReader.Len(); i++ {
tagListBuilder.Append(true)
if times.IsValid(i) && !times.IsNull(i) &&
fields.IsValid(i) && !fields.IsNull(i) &&
values.IsValid(i) && !values.IsNull(i) {
for _, tagValue := range tags {
if tagValue.IsValid(i) && !tagValue.IsNull(i) {
tagValueBuilder.Append(tagValue.Value(i))
}
}
timeBuilder.Append(times.Value(i) / secondScale)
valueBuilder.Append(values.Value(i))
}
}
for _, tagCol := range tags {
tagCol.Release()
}
return nil
})
})
if err != nil {
return nil, err
}
}
err = results.Err()
if err != nil {
zaplog.Sugar().Warnf("error decoding flux csv result: %w", err)
return nil, err
}
p.data = nil
numRows := int64(timeBuilder.Len())
record := array.NewRecord(
arrow.NewSchema(arrow_fields, nil),
[]arrow.Array{timeBuilder.NewArray(), valueBuilder.NewArray(), tagListBuilder.NewArray()},
numRows)
return record, nil
}