8000 feat: add `FluxRecord.row` with response data stored in array (#502) · IPtroll/influxdb-client-python@da73522 · GitHub
[go: up one dir, main page]

Skip to content

Commit da73522

Browse files
authored
feat: add FluxRecord.row with response data stored in array (influxdata#502)
1 parent 0722416 commit da73522

File tree

5 files changed

+200
-14
lines changed

5 files changed

+200
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Features
44
1. [#498](https://github.com/influxdata/influxdb-client-python/pull/498): Add possibility to update user's password by `users_api`
5+
1. [#502](https://github.com/influxdata/influxdb-client-python/pull/502): Add `FluxRecord.row` with response data stored in array
56

67
### Bug Fixes
78
1. [#497](https://github.com/influxdata/influxdb-client-python/pull/497): Parsing InfluxDB response with new line character in CSV column [async/await]

influxdb_client/client/flux_csv_parser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import base64
55
import codecs
66
import csv as csv_parser
7+
import warnings
78
from enum import Enum
89
from typing import List
910

@@ -262,6 +263,7 @@ def parse_record(self, table_index, table, csv):
262263
column_name = fluxColumn.label
263264
str_val = csv[fluxColumn.index + 1]
264265
record.values[column_name] = self._to_value(str_val, fluxColumn)
266+
record.row.append(record.values[column_name])
265267

266268
return record
267269

@@ -321,6 +323,13 @@ def add_default_empty_values(table, default_values):
321323
@staticmethod
322324
def add_column_names_and_tags(table, csv):
323325
"""Add labels to columns."""
326+
if len(csv) != len(set(csv)):
327+
message = f"""The response contains columns with duplicated names: '{csv}'.
328+
329+
You should use the 'record.row' to access your data instead of 'record.values' dictionary.
330+
"""
331+
warnings.warn(message, UserWarning)
332+
print(message)
324333
i = 1
325334
for column in table.columns:
326335
column.label = csv[i]

influxdb_client/client/flux_table.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def __init__(self, table, values=None) -> None:
101101
values = {}
102102
self.table = table
103103
self.values = values
104+
self.row = []
104105

105106
def get_start(self):
106107
"""Get '_start' value."""

tests/query_output.json

Lines changed: 168 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,18 @@
7878
"_time": "2020-02-27T16:20:00+00:00",
7979
"_value": 2.0,
8080
"tag": "test1"
81-
}
81+
},
82+
"row": [
83+
"_result",
84+
0,
85+
"value",
86+
"python_client_test",
87+
"2010-02-27T04:48:32.752600+00:00",
88+
"2020-02-27T16:48:32.752600+00:00",
89+
"2020-02-27T16:20:00+00:00",
90+
2.0,
91+
"test1"
92+
]
8293
},
8394
{
8495
"table": 0,
@@ -92,7 +103,18 @@
92103
"_time": "2020-02-27T16:21:40+00:00",
93104
"_value": 2.0,
94105
"tag": "test1"
95-
}
106+
},
107+
"row": [
108+
"_result",
109+
0,
110+
"value",
111+
"python_client_test",
112+
"2010-02-27T04:48:32.752600+00:00",
113+
"2020-02-27T16:48:32.752600+00:00",
114+
"2020-02-27T16:21:40+00:00",
115+
2.0,
116+
"test1"
117+
]
96118
},
97119
{
98120
"table": 0,
@@ -106,7 +128,18 @@
106128
"_time": "2020-02-27T16:23:20+00:00",
107129
"_value": 2.0,
108130
"tag": "test1"
109-
}
131+
},
132+
"row": [
133+
"_result",
134+
0,
135+
"value",
136+
"python_client_test",
137+
"2010-02-27T04:48:32.752600+00:00",
138+
"2020-02-27T16:48:32.752600+00:00",
139+
"2020-02-27T16:23:20+00:00",
140+
2.0,
141+
"test1"
142+
]
110143
},
111144
{
112145
"table": 0,
@@ -120,7 +153,18 @@
120153
"_time": "2020-02-27T16:25:00+00:00",
121154
"_value": 2.0,
122155
"tag": "test1"
123-
}
156+
},
157+
"row": [
158+
"_result",
159+
0,
160+
"value",
161+
"python_client_test",
162+
"2010-02-27T04:48:32.752600+00:00",
163+
"2020-02-27T16:48:32.752600+00:00",
164+
"2020-02-27T16:25:00+00:00",
165+
2.0,
166+
"test1"
167+
]
124168
},
125169
{
126170
"table": 0,
@@ -134,7 +178,18 @@
134178
"_time": "2020-02-27T16:26:40+00:00",
135179
"_value": 2.0,
136180
"tag": "test1"
137-
}
181+
},
182+
"row": [
183+
"_result",
184+
0,
185+
"value",
186+
"python_client_test",
187+
"2010-02-27T04:48:32.752600+00:00",
188+
"2020-02-27T16:48:32.752600+00:00",
189+
"2020-02-27T16:26:40+00:00",
190+
2.0,
191+
"test1"
192+
]
138193
},
139194
{
140195
"table": 0,
@@ -148,7 +203,18 @@
148203
"_time": "2020-02-27T16:28:20+00:00",
149204
"_value": 2.0,
150205
"tag": "test1"
151-
}
206+
},
207+
"row": [
208+
"_result",
209+
0,
210+
"value",
211+
"python_client_test",
212+
"2010-02-27T04:48:32.752600+00:00",
213+
"2020-02-27T16:48:32.752600+00:00",
214+
"2020-02-27T16:28:20+00:00",
215+
2.0,
216+
"test1"
217+
]
152218
},
153219
{
154220
"table": 0,
@@ -162,7 +228,18 @@
162228
"_time": "2020-02-27T16:30:00+00:00",
163229
"_value": 2.0,
164230
"tag": "test1"
165-
}
231+
},
232+
"row": [
233+
"_result",
234+
0,
235+
"value",
236+
"python_client_test",
237+
"2010-02-27T04:48:32.752600+00:00",
238+
"2020-02-27T16:48:32.752600+00:00",
239+
"2020-02-27T16:30:00+00:00",
240+
2.0,
241+
"test1"
242+
]
166243
}
167244
]
168245
},
@@ -245,7 +322,18 @@
245322
"_time": "2020-02-27T16:20:00+00:00",
246323
"_value": 2.0,
247324
"tag": "test2"
248-
}
325+
},
326+
"row": [
327+
"_result",
328+
1,
329+
"value",
330+
"python_client_test",
331+
"2010-02-27T04:48:32.752600+00:00",
332+
"2020-02-27T16:48:32.752600+00:00",
333+
"2020-02-27T16:20:00+00:00",
334+
2.0,
335+
"test2"
336+
]
249337
},
250338
{
251339
"table": 1,
@@ -259,7 +347,18 @@
259347
"_time": "2020-02-27T16:21:40+00:00",
260348
"_value": 2.0,
261349
"tag": "test2"
262-
}
350+
},
351+
"row": [
352+
"_result",
353+
1,
354+
"value",
355+
"python_client_test",
356+
"2010-02-27T04:48:32.752600+00:00",
357+
"2020-02-27T16:48:32.752600+00:00",
358+
"2020-02-27T16:21:40+00:00",
359+
2.0,
360+
"test2"
361+
]
263362
},
264363
{
265364
"table": 1,
@@ -273,7 +372,18 @@
273372
"_time": "2020-02-27T16:23:20+00:00",
274373
"_value": 2.0,
275374
"tag": "test2"
276-
}
375+
},
376+
"row": [
377+
"_result",
378+
1,
379+
"value",
380+
"python_client_test",
381+
"2010-02-27T04:48:32.752600+00:00",
382+
"2020-02-27T16:48:32.752600+00:00",
383+
"2020-02-27T16:23:20+00:00",
384+
2.0,
385+
"test2"
386+
]
277387
},
278388
{
279389
"table": 1,
@@ -287,7 +397,18 @@
287397
"_time": "2020-02-27T16:25:00+00:00",
288398
"_value": 2.0,
289399
"tag": "test2"
290-
}
400+
},
401+
"row": [
402+
"_result",
403+
1,
404+
"value",
405+
"python_client_test",
406+
"2010-02-27T04:48:32.752600+00:00",
407+
"2020-02-27T16:48:32.752600+00:00",
408+
"2020-02-27T16:25:00+00:00",
409+
2.0,
410+
"test2"
411+
]
291412
},
292413
{
293414
"table": 1,
@@ -301,7 +422,18 @@
301422
"_time": "2020-02-27T16:26:40+00:00",
302423
"_value": 2.0,
303424
"tag": "test2"
304-
}
425+
},
426+
"row": [
427+
"_result",
428+
1,
429+
"value",
430+
"python_client_test",
431+
"2010-02-27T04:48:32.752600+00:00",
432+
"2020-02-27T16:48:32.752600+00:00",
433+
"2020-02-27T16:26:40+00:00",
434+
2.0,
435+
"test2"
436+
]
305437
},
306438
{
307439
"table": 1,
@@ -315,7 +447,18 @@
315447
"_time": "2020-02-27T16:28:20+00:00",
316448
"_value": 2.0,
317449
"tag": "test2"
318-
}
450+
},
451+
"row": [
452+
"_result",
453+
1,
454+
"value",
455+
"python_client_test",
456+
"2010-02-27T04:48:32.752600+00:00",
457+
"2020-02-27T16:48:32.752600+00:00",
458+
"2020-02-27T16:28:20+00:00",
459+
2.0,
460+
"test2"
461+
]
319462
},
320463
{
321464
"table": 1,
@@ -329,7 +472,18 @@
329472
"_time": "2020-02-27T16:30:00+00:00",
330473
"_value": 2.0,
331474
"tag": "test2"
332-
}
475+
},
476+
"row": [
477+
"_result",
478+
1,
479+
"value",
480+
"python_client_test",
481+
"2010-02-27T04:48:32.752600+00:00",
482+
"2020-02-27T16:48:32.752600+00:00",
483+
"2020-02-27T16:30:00+00:00",
484+
2.0,
485+
"test2"
486+
]
333487
}
334488
]
335489
}

tests/test_FluxCSVParser.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from io import BytesIO
55

6+
import pytest
67
from urllib3 import HTTPResponse
78

89
from influxdb_client.client.flux_csv_parser import FluxCsvParser, FluxSerializationMode, FluxQueryException, \
@@ -356,6 +357,26 @@ def test_parse_to_values(self):
356357
self.assertEqual(['south', 'B', None, 18], parsed[2])
357358
self.assertEqual(['south', 'D', None, 22], parsed[3])
358359

360+
def test_parse_duplicate_column_names(self):
361+
data = """#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,string,string,double
362+
#group,false,false,true,true,false,true,true,false
363+
#default,_result,,,,,,,
364+
,result,table,_start,_stop,_time,_measurement,location,result
365+
,,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,2022-09-13T06:24:33.746Z,my_measurement,Prague,25.3
366+
,,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,2022-09-13T06:24:39.299Z,my_measurement,Prague,25.3
367+
,,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,2022-09-13T06:24:40.454Z,my_measurement,Prague,25.3
368+
"""
369+
with pytest.warns(UserWarning) as warnings:
370+
tables = self._parse_to_tables(data=data)
371+
self.assertEqual(1, len(warnings))
372+
self.assertEqual(1, tables.__len__())
373+
self.assertEqual(8, tables[0].columns.__len__())
374+
self.assertEqual(3, tables[0].records.__len__())
375+
self.assertEqual(7, tables[0].records[0].values.__len__())
376+
self.assertEqual(8, tables[0].records[0].row.__len__())
377+
self.assertEqual(25.3, tables[0].records[0].row[7])
378+
379+
359380
@staticmethod
360381
def _parse_to_tables(data: str, serialization_mode=FluxSerializationMode.tables,
361382
response_metadata_mode=FluxResponseMetadataMode.full) -> TableList:

0 commit comments

Comments
 (0)
0