From ccc0681aa688473aa36ba84af29b14ce2e082b3f Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 09:29:05 +0200 Subject: [PATCH 01/11] feat: add FluxRecord.row with response data stored in Array --- examples/README.md | 2 ++ examples/record_row_example.rb | 38 +++++++++++++++++++++++++ lib/influxdb2/client/flux_csv_parser.rb | 13 +++++++-- lib/influxdb2/client/flux_table.rb | 9 ++++-- test/influxdb/flux_csv_parser_test.rb | 20 +++++++++++++ 5 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 examples/record_row_example.rb diff --git a/examples/README.md b/examples/README.md index 3878ae4a..cd0cc5ee 100644 --- a/examples/README.md +++ b/examples/README.md @@ -12,3 +12,5 @@ ## Others - [invokable_scripts.rb](invokable_scripts.rb) - How to use Invokable scripts Cloud API to create custom endpoints that query data +- [record_row_example.rb](record_row_example.rb) - How to use FluxRecord.row (Array) instead of FluxRecord.values (Hash), + in case of duplicity column names \ No newline at end of file diff --git a/examples/record_row_example.rb b/examples/record_row_example.rb new file mode 100644 index 00000000..b8961c82 --- /dev/null +++ b/examples/record_row_example.rb @@ -0,0 +1,38 @@ +require 'influxdb-client' + +url = 'http://localhost:8086' +token = 'my-token' +bucket = 'my-bucket' +org = 'my-org' + +client = InfluxDB2::Client.new(url, + token, + bucket: bucket, + org: org, + precision: InfluxDB2::WritePrecision::NANOSECOND, + use_ssl: false) + +# Prepare Data +write_api = client.create_write_api +(1..5).each { |i| + write_api.write(data: "point,table=my-table result=#{i}", bucket: bucket, org: org) +} + +# Query data with pivot +query_api = client.create_query_api +query = "from(bucket: \"#{bucket}\") |> range(start: -1m) |> filter(fn: (r) => (r[\"_measurement\"] == \"point\")) +|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")" +result = query_api.query(query: query) + +# Write data to output +puts "----------------------------------------------- FluxRecord.values ----------------------------------------------" +result[0].records.each do |record| + puts record.values +end + +puts "------------------------------------------------- FluxRecord.row -----------------------------------------------" +result[0].records.each do |record| + puts record.row.join(",") +end + +client.close! \ No newline at end of file diff --git a/lib/influxdb2/client/flux_csv_parser.rb b/lib/influxdb2/client/flux_csv_parser.rb index d7757ca7..79e5b9cb 100644 --- a/lib/influxdb2/client/flux_csv_parser.rb +++ b/lib/influxdb2/client/flux_csv_parser.rb @@ -126,7 +126,7 @@ def _parse_line(csv) # start new table if ((ANNOTATIONS.include? token) && !@start_new_table) || - (@response_mode == InfluxDB2::FluxResponseMode::ONLY_NAMES && @table.nil?) + (@response_mode == InfluxDB2::FluxResponseMode::ONLY_NAMES && @table.nil?) # Return already parsed DataFrame @start_new_table = true @@ -187,6 +187,13 @@ def _add_column_names_and_tags(table, csv) column.label = csv[i] i += 1 end + + duplicates = table.columns.group_by { |e| e.label }.select { |k, v| v.size > 1 } + + if duplicates.length > 0 + puts "The response contains columns with duplicated names: #{duplicates.keys.join(", ")}\n" + puts "You should use the 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." + end end def _parse_values(csv) @@ -234,7 +241,9 @@ def _parse_record(table_index, table, csv) table.columns.each do |flux_column| column_name = flux_column.label str_val = csv[flux_column.index + 1] - record.values[column_name] = _to_value(str_val, flux_column) + value = _to_value(str_val, flux_column) + record.values[column_name] = value + record.row.push(value) end record diff --git a/lib/influxdb2/client/flux_table.rb b/lib/influxdb2/client/flux_table.rb index 1b4cdf4e..124dbdba 100644 --- a/lib/influxdb2/client/flux_table.rb +++ b/lib/influxdb2/client/flux_table.rb @@ -26,6 +26,7 @@ def initialize @columns = [] @records = [] end + attr_reader :columns, :records # A table's group key is subset of the entire columns dataset that assigned to the table. @@ -46,11 +47,14 @@ def group_key class FluxRecord # @param [Integer] table the index of table which contains the record # @param [Hash] values tuple of values - def initialize(table, values: nil) + # @param [Array] row record columns + def initialize(table, values: nil, row: nil) @table = table @values = values || {} + @row = row || [] end - attr_reader :table, :values + + attr_reader :table, :values, :row attr_writer :table # @return [Time] the inclusive lower time bound of all records @@ -93,6 +97,7 @@ def initialize(index: nil, label: nil, data_type: nil, group: nil, default_value @group = group @default_value = default_value end + attr_reader :index, :label, :data_type, :group, :default_value attr_writer :index, :label, :data_type, :group, :default_value end diff --git a/test/influxdb/flux_csv_parser_test.rb b/test/influxdb/flux_csv_parser_test.rb index 8c23cd5c..ce692641 100644 --- a/test/influxdb/flux_csv_parser_test.rb +++ b/test/influxdb/flux_csv_parser_test.rb @@ -506,4 +506,24 @@ def test_parse_without_datatype assert_equal '11', tables[0].records[0].values['value1'] assert_equal 'west', tables[0].records[0].values['region'] end + + def test_parse_duplicate_column_names + data = '#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,string,string,double +#group,false,false,true,true,false,true,true,false +#default,_result,,,,,,, + ,result,table,_start,_stop,_time,_measurement,location,result +,,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,2022-09-13T06:24:33.746Z,my_measurement,Prague,25.3 +,,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,2022-09-13T06:24:39.299Z,my_measurement,Prague,25.3 +,,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,2022-09-13T06:24:40.454Z,my_measurement,Prague,25.3' + + tables = InfluxDB2::FluxCsvParser.new(data, stream: false, response_mode: InfluxDB2::FluxResponseMode::ONLY_NAMES) + .parse + .tables + assert_equal 1, tables.size + assert_equal 8, tables[0].columns.size + assert_equal 3, tables[0].records.size + assert_equal 7, tables[0].records[0].values.size + assert_equal 8, tables[0].records[0].row.size + assert_equal 25.3, tables[0].records[0].row[7] + end end From f35343896835db9b9421d7eb4b86a7f144acfb52 Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 10:03:32 +0200 Subject: [PATCH 02/11] docs: updated CHANGELOG.md --- CHANGELOG.md | 3 +++ examples/record_row_example.rb | 12 ++++++------ lib/influxdb2/client/flux_csv_parser.rb | 8 +++----- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89f61f17..24433709 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 2.8.0 [unreleased] +### Features +1. [#118](https://github.com/influxdata/influxdb-client-ruby/pull/118): Added `FluxRecord.row` which stores response data in a array + ## 2.7.0 [2022-07-29] ### Features diff --git a/examples/record_row_example.rb b/examples/record_row_example.rb index b8961c82..6675c523 100644 --- a/examples/record_row_example.rb +++ b/examples/record_row_example.rb @@ -14,9 +14,9 @@ # Prepare Data write_api = client.create_write_api -(1..5).each { |i| +(1..5).each do |i| write_api.write(data: "point,table=my-table result=#{i}", bucket: bucket, org: org) -} +end # Query data with pivot query_api = client.create_query_api @@ -25,14 +25,14 @@ result = query_api.query(query: query) # Write data to output -puts "----------------------------------------------- FluxRecord.values ----------------------------------------------" +puts '----------------------------------------------- FluxRecord.values ----------------------------------------------' result[0].records.each do |record| puts record.values end -puts "------------------------------------------------- FluxRecord.row -----------------------------------------------" +puts '------------------------------------------------- FluxRecord.row -----------------------------------------------' result[0].records.each do |record| - puts record.row.join(",") + puts record.row.join(',') end -client.close! \ No newline at end of file +client.close! diff --git a/lib/influxdb2/client/flux_csv_parser.rb b/lib/influxdb2/client/flux_csv_parser.rb index 79e5b9cb..f3b895e5 100644 --- a/lib/influxdb2/client/flux_csv_parser.rb +++ b/lib/influxdb2/client/flux_csv_parser.rb @@ -188,12 +188,10 @@ def _add_column_names_and_tags(table, csv) i += 1 end - duplicates = table.columns.group_by { |e| e.label }.select { |k, v| v.size > 1 } + duplicates = table.columns.group_by { :label }.select { |_k, v| v.size > 1 } + + puts "The response contains columns with duplicated names: #{duplicates.keys.join(', ')}\nYou should use the 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." if !duplicates.empty? - if duplicates.length > 0 - puts "The response contains columns with duplicated names: #{duplicates.keys.join(", ")}\n" - puts "You should use the 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." - end end def _parse_values(csv) From 546ef1a458816beb9d74b61c09a4b9688b4026e2 Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 10:28:06 +0200 Subject: [PATCH 03/11] style: edit code style --- lib/influxdb2/client/flux_csv_parser.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/influxdb2/client/flux_csv_parser.rb b/lib/influxdb2/client/flux_csv_parser.rb index f3b895e5..72ee397c 100644 --- a/lib/influxdb2/client/flux_csv_parser.rb +++ b/lib/influxdb2/client/flux_csv_parser.rb @@ -126,7 +126,7 @@ def _parse_line(csv) # start new table if ((ANNOTATIONS.include? token) && !@start_new_table) || - (@response_mode == InfluxDB2::FluxResponseMode::ONLY_NAMES && @table.nil?) + (@response_mode == InfluxDB2::FluxResponseMode::ONLY_NAMES && @table.nil?) # Return already parsed DataFrame @start_new_table = true @@ -190,8 +190,8 @@ def _add_column_names_and_tags(table, csv) duplicates = table.columns.group_by { :label }.select { |_k, v| v.size > 1 } - puts "The response contains columns with duplicated names: #{duplicates.keys.join(', ')}\nYou should use the 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." if !duplicates.empty? - + puts "The response contains columns with duplicated names: #{duplicates.keys.join(', ')} +You should use the 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." unless duplicates.empty? end def _parse_values(csv) From 3721ffca994047f9e55c312c9bb0209a57b8a093 Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 11:24:50 +0200 Subject: [PATCH 04/11] style: edit code style --- lib/influxdb2/client/flux_csv_parser.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/influxdb2/client/flux_csv_parser.rb b/lib/influxdb2/client/flux_csv_parser.rb index 72ee397c..ccbea3f3 100644 --- a/lib/influxdb2/client/flux_csv_parser.rb +++ b/lib/influxdb2/client/flux_csv_parser.rb @@ -126,7 +126,7 @@ def _parse_line(csv) # start new table if ((ANNOTATIONS.include? token) && !@start_new_table) || - (@response_mode == InfluxDB2::FluxResponseMode::ONLY_NAMES && @table.nil?) + (@response_mode == InfluxDB2::FluxResponseMode::ONLY_NAMES && @table.nil?) # Return already parsed DataFrame @start_new_table = true @@ -190,8 +190,9 @@ def _add_column_names_and_tags(table, csv) duplicates = table.columns.group_by { :label }.select { |_k, v| v.size > 1 } - puts "The response contains columns with duplicated names: #{duplicates.keys.join(', ')} -You should use the 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." unless duplicates.empty? + warning = "The response contains columns with duplicated names: #{duplicates.keys.join(', ')}\nYou should use the + 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." + put warning unless duplicates.empty? end def _parse_values(csv) From cf4c0ce47e9343232da8d2d39ef3caf050e41584 Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 11:28:24 +0200 Subject: [PATCH 05/11] style: edit code style --- lib/influxdb2/client/flux_csv_parser.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/influxdb2/client/flux_csv_parser.rb b/lib/influxdb2/client/flux_csv_parser.rb index ccbea3f3..d80c6eb3 100644 --- a/lib/influxdb2/client/flux_csv_parser.rb +++ b/lib/influxdb2/client/flux_csv_parser.rb @@ -125,8 +125,8 @@ def _parse_line(csv) token = csv[0] # start new table - if ((ANNOTATIONS.include? token) && !@start_new_table) || - (@response_mode == InfluxDB2::FluxResponseMode::ONLY_NAMES && @table.nil?) + if ((ANNOTATIONS.include? token) && !@start_new_table) || (@response_mode == + InfluxDB2::FluxResponseMode::ONLY_NAMES && @table.nil?) # Return already parsed DataFrame @start_new_table = true From cb0e216135940bc8d0c564f26a121da44b4cf36d Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 11:37:56 +0200 Subject: [PATCH 06/11] style: edit code style --- influxdb-client.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/influxdb-client.gemspec b/influxdb-client.gemspec index 2422abfc..cfe24ac1 100644 --- a/influxdb-client.gemspec +++ b/influxdb-client.gemspec @@ -51,4 +51,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rubocop', '~> 0.66.0' spec.add_development_dependency 'simplecov-cobertura', '~> 1.4.2' spec.add_development_dependency 'webmock', '~> 3.7' + spec.add_development_dependency 'typhoeus', '~> 1.0', '>= 1.0.1' end From 22e2aec4a7a8114309e6376415cf039b5bc3a630 Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 11:39:52 +0200 Subject: [PATCH 07/11] style: edit code style --- influxdb-client.gemspec | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/influxdb-client.gemspec b/influxdb-client.gemspec index cfe24ac1..9c1a3def 100644 --- a/influxdb-client.gemspec +++ b/influxdb-client.gemspec @@ -24,15 +24,15 @@ require 'influxdb2/client/version' # noinspection DuplicatedCode Gem::Specification.new do |spec| - spec.name = 'influxdb-client' - spec.version = ENV['CIRCLE_BUILD_NUM'] ? "#{InfluxDB2::VERSION}-#{ENV['CIRCLE_BUILD_NUM']}" : InfluxDB2::VERSION - spec.authors = ['Jakub Bednar'] - spec.email = ['jakub.bednar@gmail.com'] + spec.name = 'influxdb-client' + spec.version = ENV['CIRCLE_BUILD_NUM'] ? "#{InfluxDB2::VERSION}-#{ENV['CIRCLE_BUILD_NUM']}" : InfluxDB2::VERSION + spec.authors = ['Jakub Bednar'] + spec.email = ['jakub.bednar@gmail.com'] - spec.summary = 'Ruby library for InfluxDB 2.' - spec.description = 'This is the official Ruby library for InfluxDB 2.' - spec.homepage = 'https://github.com/influxdata/influxdb-client-ruby' - spec.license = 'MIT' + spec.summary = 'Ruby library for InfluxDB 2.' + spec.description = 'This is the official Ruby library for InfluxDB 2.' + spec.homepage = 'https://github.com/influxdata/influxdb-client-ruby' + spec.license = 'MIT' spec.metadata['homepage_uri'] = spec.homepage spec.metadata['source_code_uri'] = 'https://github.com/influxdata/influxdb-client-ruby' @@ -50,6 +50,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rake', '~> 13.0' spec.add_development_dependency 'rubocop', '~> 0.66.0' spec.add_development_dependency 'simplecov-cobertura', '~> 1.4.2' - spec.add_development_dependency 'webmock', '~> 3.7' spec.add_development_dependency 'typhoeus', '~> 1.0', '>= 1.0.1' + spec.add_development_dependency 'webmock', '~> 3.7' end From 75319474e770386033a8dd938ce5d30ad10f9b58 Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 11:43:34 +0200 Subject: [PATCH 08/11] style: edit code style --- lib/influxdb2/client/flux_csv_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/influxdb2/client/flux_csv_parser.rb b/lib/influxdb2/client/flux_csv_parser.rb index d80c6eb3..b9108c91 100644 --- a/lib/influxdb2/client/flux_csv_parser.rb +++ b/lib/influxdb2/client/flux_csv_parser.rb @@ -192,7 +192,7 @@ def _add_column_names_and_tags(table, csv) warning = "The response contains columns with duplicated names: #{duplicates.keys.join(', ')}\nYou should use the 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." - put warning unless duplicates.empty? + puts warning unless duplicates.empty? end def _parse_values(csv) From 12b520cc6a822913a163cb30051840e82a509a5b Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 12:03:30 +0200 Subject: [PATCH 09/11] docs: updated example/README.md --- examples/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index cd0cc5ee..a6ffb632 100644 --- a/examples/README.md +++ b/examples/README.md @@ -12,5 +12,5 @@ ## Others - [invokable_scripts.rb](invokable_scripts.rb) - How to use Invokable scripts Cloud API to create custom endpoints that query data -- [record_row_example.rb](record_row_example.rb) - How to use FluxRecord.row (Array) instead of FluxRecord.values (Hash), +- [record_row_example.rb](record_row_example.rb) - How to use `FluxRecord.row` (Array) instead of `FluxRecord.values` (Hash), in case of duplicity column names \ No newline at end of file From e0394ec62ee27069423ae180fdb60b5afa04e2d8 Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Thu, 6 Oct 2022 14:07:27 +0200 Subject: [PATCH 10/11] revert: influxdb-client.gemspec --- influxdb-client.gemspec | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/influxdb-client.gemspec b/influxdb-client.gemspec index 9c1a3def..2422abfc 100644 --- a/influxdb-client.gemspec +++ b/influxdb-client.gemspec @@ -24,15 +24,15 @@ require 'influxdb2/client/version' # noinspection DuplicatedCode Gem::Specification.new do |spec| - spec.name = 'influxdb-client' - spec.version = ENV['CIRCLE_BUILD_NUM'] ? "#{InfluxDB2::VERSION}-#{ENV['CIRCLE_BUILD_NUM']}" : InfluxDB2::VERSION - spec.authors = ['Jakub Bednar'] - spec.email = ['jakub.bednar@gmail.com'] + spec.name = 'influxdb-client' + spec.version = ENV['CIRCLE_BUILD_NUM'] ? "#{InfluxDB2::VERSION}-#{ENV['CIRCLE_BUILD_NUM']}" : InfluxDB2::VERSION + spec.authors = ['Jakub Bednar'] + spec.email = ['jakub.bednar@gmail.com'] - spec.summary = 'Ruby library for InfluxDB 2.' - spec.description = 'This is the official Ruby library for InfluxDB 2.' - spec.homepage = 'https://github.com/influxdata/influxdb-client-ruby' - spec.license = 'MIT' + spec.summary = 'Ruby library for InfluxDB 2.' + spec.description = 'This is the official Ruby library for InfluxDB 2.' + spec.homepage = 'https://github.com/influxdata/influxdb-client-ruby' + spec.license = 'MIT' spec.metadata['homepage_uri'] = spec.homepage spec.metadata['source_code_uri'] = 'https://github.com/influxdata/influxdb-client-ruby' @@ -50,6 +50,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rake', '~> 13.0' spec.add_development_dependency 'rubocop', '~> 0.66.0' spec.add_development_dependency 'simplecov-cobertura', '~> 1.4.2' - spec.add_development_dependency 'typhoeus', '~> 1.0', '>= 1.0.1' spec.add_development_dependency 'webmock', '~> 3.7' end From 188aa1e5b686b06fc35edfbafa278d50026f5071 Mon Sep 17 00:00:00 2001 From: michaelahojna Date: Fri, 7 Oct 2022 13:07:59 +0200 Subject: [PATCH 11/11] style: edit code style --- lib/influxdb2/client/flux_csv_parser.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/influxdb2/client/flux_csv_parser.rb b/lib/influxdb2/client/flux_csv_parser.rb index b9108c91..317b691f 100644 --- a/lib/influxdb2/client/flux_csv_parser.rb +++ b/lib/influxdb2/client/flux_csv_parser.rb @@ -190,8 +190,8 @@ def _add_column_names_and_tags(table, csv) duplicates = table.columns.group_by { :label }.select { |_k, v| v.size > 1 } - warning = "The response contains columns with duplicated names: #{duplicates.keys.join(', ')}\nYou should use the - 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." + warning = "The response contains columns with duplicated names: #{duplicates.keys.join(', ')} +You should use the 'FluxRecord.row to access your data instead of 'FluxRecord.values' hash." puts warning unless duplicates.empty? end