Skip to content

Commit

Permalink
fix: FluxRecord.Table() returns value of the table column (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlastahajek committed Jan 19, 2022
1 parent a00324e commit 7137cc0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## unreleased
### Features
- [#297](https://github.com/influxdata/influxdb-client-go/pull/297),[#298](https://github.com/influxdata/influxdb-client-go/pull/298) Optimized `WriteRecord` of [WriteAPIBlocking](https://pkg.go.dev/github.com/influxdata/influxdb-client-go/v2/api#WriteAPIBlocking).
- [#297](https://github.com/influxdata/influxdb-client-go/pull/297),[#298](https://github.com/influxdata/influxdb-client-go/pull/298) Optimized `WriteRecord` of [WriteAPIBlocking](https://pkg.go.dev/github.com/influxdata/influxdb-client-go/v2/api#WriteAPIBlocking). Custom batch can be written by single argument.

### Bug fixes
- [#294](https://github.com/influxdata/influxdb-client-go/pull/294) `WritePoint` and `WriteRecord` of [WriteAPIBlocking](https://pkg.go.dev/github.com/influxdata/influxdb-client-go/v2/api#WriteAPIBlocking) returns always full error information.

- [300](https://github.com/influxdata/influxdb-client-go/pull/300) Closing the response body after write batch.
- [302](https://github.com/influxdata/influxdb-client-go/pull/302) FluxRecord.Table() returns value of the table column.

## 2.6.0[2021-11-26]
### Features
Expand Down
39 changes: 30 additions & 9 deletions api/query/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package query

import (
"fmt"
"sort"
"strings"
"time"
)
Expand Down Expand Up @@ -146,9 +147,10 @@ func NewFluxRecord(table int, values map[string]interface{}) *FluxRecord {
return &FluxRecord{table: table, values: values}
}

// Table returns index of the table record belongs to
// Table returns value of the table column
// It returns zero if the table column is not found
func (r *FluxRecord) Table() int {
return r.table
return int(intValue(r.values, "table"))
}

// Start returns the inclusive lower time bound of all records in the current table.
Expand Down Expand Up @@ -204,15 +206,23 @@ func (r *FluxRecord) ValueByKey(key string) interface{} {

// String returns FluxRecord string dump
func (r *FluxRecord) String() string {
var buffer strings.Builder
if len(r.values) == 0 {
return ""
}

i := 0
for k, v := range r.values {
if i > 0 {
buffer.WriteString(",")
}
buffer.WriteString(fmt.Sprintf("%s:%v", k, v))
keys := make([]string, len(r.values))
for k := range r.values {
keys[i] = k
i++
}
sort.Strings(keys)
var buffer strings.Builder
buffer.WriteString(fmt.Sprintf("%s:%v", keys[0], r.values[keys[0]]))
for _, k := range keys[1:] {
buffer.WriteString(",")
buffer.WriteString(fmt.Sprintf("%s:%v", k, r.values[k]))
}
return buffer.String()
}

Expand All @@ -227,7 +237,7 @@ func timeValue(values map[string]interface{}, key string) time.Time {
return time.Time{}
}

// timeValue returns string value from values map according to the key
// stringValue returns string value from values map according to the key
// Empty string is returned if key is not found
func stringValue(values map[string]interface{}, key string) string {
if val, ok := values[key]; ok {
Expand All @@ -237,3 +247,14 @@ func stringValue(values map[string]interface{}, key string) string {
}
return ""
}

// intValue returns int64 value from values map according to the key
// Zero value is returned if key is not found
func intValue(values map[string]interface{}, key string) int64 {
if val, ok := values[key]; ok {
if i, ok := val.(int64); ok {
return i
}
}
return 0
}
2 changes: 1 addition & 1 deletion api/query/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestRecord(t *testing.T) {
record := &FluxRecord{table: 2,
values: map[string]interface{}{
"result": "_result",
"_table": int64(0),
"table": int64(2),
"_start": mustParseTime("2020-02-17T22:19:49.747562847Z"),
"_stop": mustParseTime("2020-02-18T22:19:49.747562847Z"),
"_time": mustParseTime("2020-02-18T10:34:08.135814545Z"),
Expand Down
11 changes: 7 additions & 4 deletions api/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestQueryCVSResultSingleTable(t *testing.T) {
#group,false,false,true,true,false,false,true,true,true,true
#default,_result,,,,,,,,,
,result,table,_start,_stop,_time,_value,_field,_measurement,a,b
,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T10:34:08.135814545Z,1.4,f,test,1,adsfasdf
,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T22:08:44.850214724Z,6.6,f,test,1,adsfasdf
,,1,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T10:34:08.135814545Z,1.4,f,test,1,adsfasdf
,,1,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T22:08:44.850214724Z,6.6,f,test,1,adsfasdf
`
expectedTable := query.NewFluxTableMetadataFull(0,
Expand All @@ -56,7 +56,7 @@ func TestQueryCVSResultSingleTable(t *testing.T) {
expectedRecord1 := query.NewFluxRecord(0,
map[string]interface{}{
"result": "_result",
"table": int64(0),
"table": int64(1),
"_start": mustParseTime("2020-02-17T22:19:49.747562847Z"),
"_stop": mustParseTime("2020-02-18T22:19:49.747562847Z"),
"_time": mustParseTime("2020-02-18T10:34:08.135814545Z"),
Expand All @@ -71,7 +71,7 @@ func TestQueryCVSResultSingleTable(t *testing.T) {
expectedRecord2 := query.NewFluxRecord(0,
map[string]interface{}{
"result": "_result",
"table": int64(0),
"table": int64(1),
"_start": mustParseTime("2020-02-17T22:19:49.747562847Z"),
"_stop": mustParseTime("2020-02-18T22:19:49.747562847Z"),
"_time": mustParseTime("2020-02-18T22:08:44.850214724Z"),
Expand Down Expand Up @@ -100,6 +100,8 @@ func TestQueryCVSResultSingleTable(t *testing.T) {
assert.False(t, queryResult.tableChanged)
require.NotNil(t, queryResult.Record())
require.Equal(t, queryResult.Record(), expectedRecord2)
assert.Equal(t, "_result", queryResult.Record().Result())
assert.Equal(t, 1, queryResult.Record().Table())

require.False(t, queryResult.Next())
require.Nil(t, queryResult.Err())
Expand Down Expand Up @@ -383,6 +385,7 @@ func TestQueryCVSResultMultiTables(t *testing.T) {
require.NotNil(t, queryResult.Record())
require.Equal(t, queryResult.Record(), expectedRecord42)
assert.Equal(t, "_result4", queryResult.Record().Result())
assert.Equal(t, 3, queryResult.Record().Table())

require.False(t, queryResult.Next())
require.Nil(t, queryResult.Err())
Expand Down

0 comments on commit 7137cc0

Please sign in to comment.