Skip to content

feat: add FluxRecord.row with response data stored in array #502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions influxdb_client/client/flux_csv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import base64
import codecs
import csv as csv_parser
import warnings
from enum import Enum
from typing import List

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions influxdb_client/client/flux_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
182 changes: 168 additions & 14 deletions tests/query_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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"
]
}
]
},
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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"
]
}
]
}
Expand Down
21 changes: 21 additions & 0 deletions tests/test_FluxCSVParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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:
Expand Down