From e64294a53e29f9d8d375e1099602564c1e2c107b Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Mon, 3 Oct 2022 19:59:41 +0200 Subject: [PATCH 1/3] feat: add record.row with response data stored in List --- example/record_row_example.dart | 52 ++++++++++++++++++++++++++++++++ lib/client/flux_table.dart | 2 ++ lib/client/flux_transformer.dart | 9 ++++++ test/flux_transformer_test.dart | 25 +++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 example/record_row_example.dart diff --git a/example/record_row_example.dart b/example/record_row_example.dart new file mode 100644 index 0000000..64a9442 --- /dev/null +++ b/example/record_row_example.dart @@ -0,0 +1,52 @@ +import 'dart:async'; + +import 'package:influxdb_client/api.dart'; + +void main() async { + var client = InfluxDBClient( + url: 'http://localhost:8086', + token: 'my-token', + org: 'my-org', + bucket: 'my-bucket', + ); + + var writeApi = client.getWriteService(WriteOptions().merge( + precision: WritePrecision.s, + batchSize: 100, + flushInterval: 5000, + gzip: true)); + + var point = Point('point') + .addField('table', 'my-table') + .addField('result', 3.14) + .time(DateTime.now().toUtc()); + + await writeApi.write(point); + + var queryService = client.getQueryService(); + + var fluxQuery = ''' + from(bucket: "my-bucket") + |> range(start: -1d) + |> filter(fn: (r) => r["_measurement"] == "point") + |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value") + '''; + + var recordStream = await queryService.query(fluxQuery); + + var recordValues = []; + var recordRow = []; + + await recordStream.forEach((record) { + recordValues.add(record.values.join(",")); + recordRow.add(record.row.join(", ")); + }); + + print("-------------------------- record.values ---------------------------"); + print(recordValues.join("\n")); + print("---------------------------- record.row ----------------------------"); + print(recordRow.join("\n")); + + await Future.delayed(Duration(seconds: 10)); + client.close(); +} diff --git a/lib/client/flux_table.dart b/lib/client/flux_table.dart index e6d4bf2..e012a9d 100644 --- a/lib/client/flux_table.dart +++ b/lib/client/flux_table.dart @@ -39,6 +39,7 @@ class FluxTableMetaData { } var csvValue = values[index]; record[columnName] = _toValue(csvValue, column); + record.row.add(csvValue); } return record; @@ -63,6 +64,7 @@ class FluxRecord extends MapMixin { /// index of table final int tableIndex; final Map _values = {}; + final List row = []; FluxRecord(this.tableIndex); diff --git a/lib/client/flux_transformer.dart b/lib/client/flux_transformer.dart index 73fc1c7..e76a0ae 100644 --- a/lib/client/flux_transformer.dart +++ b/lib/client/flux_transformer.dart @@ -185,6 +185,15 @@ class FluxTransformer implements StreamTransformer { column.label = csv[i]; i++; } + + var duplicates = table.columns.map((item) => item.label).toList(); + duplicates.toSet().forEach((item) => {duplicates.remove(item)}); + + if (duplicates.isNotEmpty) { + logPrint('The response contains columns with duplicated names:' + ' ${duplicates.join(", ")}\nYou should use the "record.row" ' + 'to access your data instead of "record.values" dictionary.'); + } } void _addGroups(FluxTableMetaData table, List? csv) { diff --git a/test/flux_transformer_test.dart b/test/flux_transformer_test.dart index 3355ec6..4e456ca 100644 --- a/test/flux_transformer_test.dart +++ b/test/flux_transformer_test.dart @@ -21,5 +21,30 @@ void main() { expect(records[0]['value1'], 11); expect(records[0]['region'], 'west'); }); + + test('parseDuplicateColumnNames', () async { + var csv = + '#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:' + 'RFC3339,string,string,double$defaultEol' + '#group,false,false,true,true,false,true,true,false$defaultEol' + '#default,_result,,,,,,,$defaultEol ' + ',result,table,_start,_stop,_time,_measurement,location,result$defaultEol' + ',,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,' + '2022-09-13T06:24:33.746Z,my_measurement,Prague,25.3$defaultEol' + ',,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,' + '2022-09-13T06:24:39.299Z,my_measurement,Prague,25.3$defaultEol' + ',,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,' + '2022-09-13T06:24:40.454Z,my_measurement,Prague,25.3$defaultEol'; + + var records = await Stream.value(csv) + .transform(CsvToListConverter()) + .transform(FluxTransformer(responseMode: FluxResponseMode.onlyNames)) + .toList(); + print(records.join("\n")); + expect(records.length, 3); + expect(records[0].values.length, 7); + expect(records[0].row.length, 8); + expect(records[0].row[7], 25.3); + }); }); } From 92ceda68a13aa9f8f7a76bffb5d2b3f8ec41647f Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Mon, 3 Oct 2022 20:15:55 +0200 Subject: [PATCH 2/3] feat: add record.row with response data stored in List --- CHANGELOG.md | 3 +++ example/README.md | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4635046..36a5d27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 2.7.0 [unreleased] +### Features +1. [#78](https://github.com/influxdata/influxdb-client-dart/pull/78): Added `FluxRecord.row` which stores response data in a list + ## 2.6.0 [2022-07-29] ### Bug Fixes diff --git a/example/README.md b/example/README.md index fa475d4..720818e 100644 --- a/example/README.md +++ b/example/README.md @@ -15,4 +15,5 @@ ## Others - [delete_data_example.dart](delete_data_example.dart) - How to delete data from InfluxDB by client - [main.dart](main.dart) - How to write, query and delete data from InfluxDB -- [invokable_scripts.dart](invokable_scripts.dart) - How to use Invokable scripts Cloud API to create custom endpoints that query data \ No newline at end of file +- [invokable_scripts.dart](invokable_scripts.dart) - How to use Invokable scripts Cloud API to create custom endpoints that query data +- [record_row_example.dart](record_row_example.dart) - How to use `FluxRecord.row`(List) instead of `FluxRecord.values`(Map), in case of duplicity column names \ No newline at end of file From 462619366a297e8393745eb0bfac72e755463f2d Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Tue, 4 Oct 2022 08:36:34 +0200 Subject: [PATCH 3/3] refactor: record row example --- example/record_row_example.dart | 3 --- 1 file changed, 3 deletions(-) diff --git a/example/record_row_example.dart b/example/record_row_example.dart index 64a9442..33f4df4 100644 --- a/example/record_row_example.dart +++ b/example/record_row_example.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:influxdb_client/api.dart'; void main() async { @@ -47,6 +45,5 @@ void main() async { print("---------------------------- record.row ----------------------------"); print(recordRow.join("\n")); - await Future.delayed(Duration(seconds: 10)); client.close(); }