From 5b9ca9198c9060f13ac83e39e4a208698822a612 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Wed, 14 Sep 2022 11:00:32 +0200 Subject: [PATCH 1/3] feat: add `FluxRecord.row` with response data stored in array --- influxdb_client/client/flux_csv_parser.py | 9 +++++++++ influxdb_client/client/flux_table.py | 1 + tests/test_FluxCSVParser.py | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/influxdb_client/client/flux_csv_parser.py b/influxdb_client/client/flux_csv_parser.py index 4756d32a..db4c4c7e 100644 --- a/influxdb_client/client/flux_csv_parser.py +++ b/influxdb_client/client/flux_csv_parser.py @@ -4,6 +4,7 @@ import base64 import codecs import csv as csv_parser +import warnings from enum import Enum from typing import List @@ -262,6 +263,7 @@ def parse_record(self, table_index, table, csv): column_name = fluxColumn.label str_val = csv[fluxColumn.index + 1] record.values[column_name] = self._to_value(str_val, fluxColumn) + record.row.append(record.values[column_name]) return record @@ -321,6 +323,13 @@ def add_default_empty_values(table, default_values): @staticmethod def add_column_names_and_tags(table, csv): """Add labels to columns.""" + if len(csv) != len(set(csv)): + message = f"""The response contains columns with duplicated names: '{csv}'. + +You should use the 'record.row' to access your data instead of 'record.values' dictionary. +""" + warnings.warn(message, UserWarning) + print(message) i = 1 for column in table.columns: column.label = csv[i] diff --git a/influxdb_client/client/flux_table.py b/influxdb_client/client/flux_table.py index 5ae54907..42324766 100644 --- a/influxdb_client/client/flux_table.py +++ b/influxdb_client/client/flux_table.py @@ -101,6 +101,7 @@ def __init__(self, table, values=None) -> None: values = {} self.table = table self.values = values + self.row = [] def get_start(self): """Get '_start' value.""" diff --git a/tests/test_FluxCSVParser.py b/tests/test_FluxCSVParser.py index b7461f26..3091ff95 100644 --- a/tests/test_FluxCSVParser.py +++ b/tests/test_FluxCSVParser.py @@ -3,6 +3,7 @@ import unittest from io import BytesIO +import pytest from urllib3 import HTTPResponse from influxdb_client.client.flux_csv_parser import FluxCsvParser, FluxSerializationMode, FluxQueryException, \ @@ -356,6 +357,26 @@ def test_parse_to_values(self): self.assertEqual(['south', 'B', None, 18], parsed[2]) self.assertEqual(['south', 'D', None, 22], parsed[3]) + def test_parse_duplicate_column_names(self): + 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 +""" + with pytest.warns(UserWarning) as warnings: + tables = self._parse_to_tables(data=data) + self.assertEqual(1, len(warnings)) + self.assertEqual(1, tables.__len__()) + self.assertEqual(8, tables[0].columns.__len__()) + self.assertEqual(3, tables[0].records.__len__()) + self.assertEqual(7, tables[0].records[0].values.__len__()) + self.assertEqual(8, tables[0].records[0].row.__len__()) + self.assertEqual(25.3, tables[0].records[0].row[7]) + + @staticmethod def _parse_to_tables(data: str, serialization_mode=FluxSerializationMode.tables, response_metadata_mode=FluxResponseMetadataMode.full) -> TableList: From 5f021dab13a34e5309627c94aeb6f3a1cbe48404 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Wed, 14 Sep 2022 11:09:48 +0200 Subject: [PATCH 2/3] fix: test --- tests/query_output.json | 182 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 168 insertions(+), 14 deletions(-) diff --git a/tests/query_output.json b/tests/query_output.json index 4cbe36d0..b60aac06 100644 --- a/tests/query_output.json +++ b/tests/query_output.json @@ -78,7 +78,18 @@ "_time": "2020-02-27T16:20:00+00:00", "_value": 2.0, "tag": "test1" - } + }, + "row": [ + "_result", + 0, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:20:00+00:00", + 2.0, + "test1" + ] }, { "table": 0, @@ -92,7 +103,18 @@ "_time": "2020-02-27T16:21:40+00:00", "_value": 2.0, "tag": "test1" - } + }, + "row": [ + "_result", + 0, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:21:40+00:00", + 2.0, + "test1" + ] }, { "table": 0, @@ -106,7 +128,18 @@ "_time": "2020-02-27T16:23:20+00:00", "_value": 2.0, "tag": "test1" - } + }, + "row": [ + "_result", + 0, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:23:20+00:00", + 2.0, + "test1" + ] }, { "table": 0, @@ -120,7 +153,18 @@ "_time": "2020-02-27T16:25:00+00:00", "_value": 2.0, "tag": "test1" - } + }, + "row": [ + "_result", + 0, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:25:00+00:00", + 2.0, + "test1" + ] }, { "table": 0, @@ -134,7 +178,18 @@ "_time": "2020-02-27T16:26:40+00:00", "_value": 2.0, "tag": "test1" - } + }, + "row": [ + "_result", + 0, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:26:40+00:00", + 2.0, + "test1" + ] }, { "table": 0, @@ -148,7 +203,18 @@ "_time": "2020-02-27T16:28:20+00:00", "_value": 2.0, "tag": "test1" - } + }, + "row": [ + "_result", + 0, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:28:20+00:00", + 2.0, + "test1" + ] }, { "table": 0, @@ -162,7 +228,18 @@ "_time": "2020-02-27T16:30:00+00:00", "_value": 2.0, "tag": "test1" - } + }, + "row": [ + "_result", + 0, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:30:00+00:00", + 2.0, + "test1" + ] } ] }, @@ -245,7 +322,18 @@ "_time": "2020-02-27T16:20:00+00:00", "_value": 2.0, "tag": "test2" - } + }, + "row": [ + "_result", + 1, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:20:00+00:00", + 2.0, + "test2" + ] }, { "table": 1, @@ -259,7 +347,18 @@ "_time": "2020-02-27T16:21:40+00:00", "_value": 2.0, "tag": "test2" - } + }, + "row": [ + "_result", + 1, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:21:40+00:00", + 2.0, + "test2" + ] }, { "table": 1, @@ -273,7 +372,18 @@ "_time": "2020-02-27T16:23:20+00:00", "_value": 2.0, "tag": "test2" - } + }, + "row": [ + "_result", + 1, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:23:20+00:00", + 2.0, + "test2" + ] }, { "table": 1, @@ -287,7 +397,18 @@ "_time": "2020-02-27T16:25:00+00:00", "_value": 2.0, "tag": "test2" - } + }, + "row": [ + "_result", + 1, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:25:00+00:00", + 2.0, + "test2" + ] }, { "table": 1, @@ -301,7 +422,18 @@ "_time": "2020-02-27T16:26:40+00:00", "_value": 2.0, "tag": "test2" - } + }, + "row": [ + "_result", + 1, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:26:40+00:00", + 2.0, + "test2" + ] }, { "table": 1, @@ -315,7 +447,18 @@ "_time": "2020-02-27T16:28:20+00:00", "_value": 2.0, "tag": "test2" - } + }, + "row": [ + "_result", + 1, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:28:20+00:00", + 2.0, + "test2" + ] }, { "table": 1, @@ -329,7 +472,18 @@ "_time": "2020-02-27T16:30:00+00:00", "_value": 2.0, "tag": "test2" - } + }, + "row": [ + "_result", + 1, + "value", + "python_client_test", + "2010-02-27T04:48:32.752600+00:00", + "2020-02-27T16:48:32.752600+00:00", + "2020-02-27T16:30:00+00:00", + 2.0, + "test2" + ] } ] } From c9636234ccfbfb3e52fdb4a0ca205ee21d006a75 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Wed, 14 Sep 2022 11:20:36 +0200 Subject: [PATCH 3/3] docs: update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13c59ff9..d84e990f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features 1. [#498](https://github.com/influxdata/influxdb-client-python/pull/498): Add possibility to update user's password by `users_api` +1. [#502](https://github.com/influxdata/influxdb-client-python/pull/502): Add `FluxRecord.row` with response data stored in array ### Bug Fixes 1. [#497](https://github.com/influxdata/influxdb-client-python/pull/497): Parsing InfluxDB response with new line character in CSV column [async/await]