forked from influxdata/influxdb-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
195 lines (162 loc) · 4.94 KB
/
table.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
// Copyright 2020 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package influxdb2
import (
"fmt"
"strings"
"time"
)
// FluxTableMetadata holds flux query result table information represented by collection of columns.
// Each new table is introduced by annotations
type FluxTableMetadata struct {
position int
columns []*FluxColumn
}
// FluxColumn holds flux query table column properties
type FluxColumn struct {
index int
name string
dataType string
group bool
defaultValue string
}
// FluxRecord represents row in the flux query result table
type FluxRecord struct {
table int
values map[string]interface{}
}
// newFluxTableMetadata creates FluxTableMetadata for the table on position
func newFluxTableMetadata(position int) *FluxTableMetadata {
return &FluxTableMetadata{position: position, columns: make([]*FluxColumn, 0, 10)}
}
// Position returns position of the table in the flux query result
func (f *FluxTableMetadata) Position() int {
return f.position
}
// Columns returns slice of flux query result table
func (f *FluxTableMetadata) Columns() []*FluxColumn {
return f.columns
}
// AddColumn adds column definition to table metadata
func (f *FluxTableMetadata) AddColumn(column *FluxColumn) *FluxTableMetadata {
f.columns = append(f.columns, column)
return f
}
// Column returns flux table column by index
// Returns nil if index is out of the bounds
func (f *FluxTableMetadata) Column(index int) *FluxColumn {
if len(f.columns) == 0 || index < 0 || index >= len(f.columns) {
return nil
}
return f.columns[index]
}
// String returns FluxTableMetadata string dump
func (f *FluxTableMetadata) String() string {
var buffer strings.Builder
for i, c := range f.columns {
if i > 0 {
buffer.WriteString(",")
}
buffer.WriteString("col")
buffer.WriteString(c.String())
}
return buffer.String()
}
// newFluxColumn creates FluxColumn for position and data type
func newFluxColumn(index int, dataType string) *FluxColumn {
return &FluxColumn{index: index, dataType: dataType}
}
// SetDefaultValue sets default value for the column
func (f *FluxColumn) SetDefaultValue(defaultValue string) {
f.defaultValue = defaultValue
}
// SetGroup set group flag for the column
func (f *FluxColumn) SetGroup(group bool) {
f.group = group
}
// SetDataType sets data type for the column
func (f *FluxColumn) SetDataType(dataType string) {
f.dataType = dataType
}
// SetName sets name of the column
func (f *FluxColumn) SetName(name string) {
f.name = name
}
// DefaultValue returns default value of the column
func (f *FluxColumn) DefaultValue() string {
return f.defaultValue
}
// IsGroup return true if the column is grouping column
func (f *FluxColumn) IsGroup() bool {
return f.group
}
// DataType returns data type of the column
func (f *FluxColumn) DataType() string {
return f.dataType
}
// Name returns name of the column
func (f *FluxColumn) Name() string {
return f.name
}
// Index returns index of the column
func (f *FluxColumn) Index() int {
return f.index
}
// String returns FluxColumn string dump
func (f *FluxColumn) String() string {
return fmt.Sprintf("{%d: name: %s, datatype: %s, defaultValue: %s, group: %v}", f.index, f.name, f.dataType, f.defaultValue, f.group)
}
// newFluxRecord returns new record for the table with values
func newFluxRecord(table int, values map[string]interface{}) *FluxRecord {
return &FluxRecord{table: table, values: values}
}
// Table returns index of the table record belongs to
func (r *FluxRecord) Table() int {
return r.table
}
// Start returns the inclusive lower time bound of all records in the current table
func (r *FluxRecord) Start() time.Time {
return r.ValueByKey("_start").(time.Time)
}
// Stop returns the exclusive upper time bound of all records in the current table
func (r *FluxRecord) Stop() time.Time {
return r.ValueByKey("_stop").(time.Time)
}
// Start returns the time of the record
func (r *FluxRecord) Time() time.Time {
return r.ValueByKey("_time").(time.Time)
}
// Value returns the actual field value
func (r *FluxRecord) Value() interface{} {
return r.ValueByKey("_value")
}
// Field returns the field name
func (r *FluxRecord) Field() string {
return r.ValueByKey("_field").(string)
}
// Measurement returns the measurement name of the record
func (r *FluxRecord) Measurement() string {
return r.ValueByKey("_measurement").(string)
}
// Values returns map of the values where key is the column name
func (r *FluxRecord) Values() map[string]interface{} {
return r.values
}
// ValueByKey returns value for given column key for the record
func (r *FluxRecord) ValueByKey(key string) interface{} {
return r.values[key]
}
// String returns FluxRecord string dump
func (r *FluxRecord) String() string {
var buffer strings.Builder
i := 0
for k, v := range r.values {
if i > 0 {
buffer.WriteString(",")
}
buffer.WriteString(fmt.Sprintf("%s:%v", k, v))
i++
}
return buffer.String()
}