U vi$@sdZddlZddlZddlmZddlmZddlmZm Z ddl m Z GdddZ Gd d d eZ Gd d d e ZGd dde ZGddde ZGdddeeZGddde eeZdS)zt Flux employs a basic data model built from basic data types. The data model consists of tables, records, columns. N) HTTPResponse) JSONEncoder)ListIterator)_UTF_8_encodingc@seZdZdZdS) FluxStructurez4The data model consists of tables, records, columns.N)__name__ __module__ __qualname____doc__r r R/opt/alt/python38/lib/python3.8/site-packages/influxdb_client/client/flux_table.pyrsrcs eZdZdZfddZZS)FluxStructureEncoderz:The FluxStructure encoder to encode query results to JSON.cs>ddl}t|tr|jSt||j|jfr2|St|S)z,Return serializable objects for JSONEncoder.rN)datetime isinstancer__dict__date isoformatsuperdefault)selfobjr __class__r r rs  zFluxStructureEncoder.default)rr r r r __classcell__r r rr rsrc@s>eZdZdZddddZddZdd Zd d Zd d ZdS) FluxTableaA A table is set of records with a common set of columns and a group key. The table can be serialized into JSON by:: import json from influxdb_client.client.flux_table import FluxStructureEncoder output = json.dumps(tables, cls=FluxStructureEncoder, indent=2) print(output) NreturncCsg|_g|_dSzInitialize defaults.N)columnsrecordsrr r r __init__/szFluxTable.__init__cCsttdd|jS)u Group key is a list of columns. A table’s group key denotes which subset of the entire dataset is assigned to the table. cSs |jdkS)NT)group)columnr r r :z)FluxTable.get_group_key..)listfilterrr!r r r get_group_key4szFluxTable.get_group_keycCs2t|j}|dtt|jdtt|jS)Return formatted output.z () columns: z , records: )typerstrlenrr rcls_namer r r __str__<s zFluxTable.__str__cCs*dt|jdt|jdt|jdS)Format for inspection.)r+rr-rr r!r r r __repr__AszFluxTable.__repr__cCs t|jS)zIterate over records.)iterr r!r r r __iter__EszFluxTable.__iter__) rr r r r"r)r0r3r5r r r r r!s  rc@s(eZdZdZdddddZddZdS) FluxColumnz%A column has a label and a data type.NrcCs"||_||_||_||_||_dSr) default_valuer# data_typelabelindex)rr:r9r8r#r7r r r r"Ms zFluxColumn.__init__cs:tjgfdddD}tjdd|dS)r1cs.g|]&}t|dk r|dt|qS)N=)getattr).0namer!r r Wsz'FluxColumn.__repr__..)r9r8r#r7(, ))reprr:r+rjoin)rfieldsr r!r r3UszFluxColumn.__repr__)NNNNN)rr r r r"r3r r r r r6Jsr6c@speZdZdZdddddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZdS) FluxRecordzLA record is a tuple of named values and is represented using an object type.NrcCs"|dkr i}||_||_g|_dSr)tablevaluesrow)rrGrHr r r r"bs zFluxRecord.__init__cCs|dS)zGet '_start' value._startr r!r r r get_startjszFluxRecord.get_startcCs|dS)zGet '_stop' value._stopr r!r r r get_stopnszFluxRecord.get_stopcCs|dS)zGet timestamp._timer r!r r r get_timerszFluxRecord.get_timecCs|dS)zGet field value._valuer r!r r r get_valuevszFluxRecord.get_valuecCs|dS)zGet field name._fieldr r!r r r get_fieldzszFluxRecord.get_fieldcCs|dS)zGet measurement name.Z _measurementr r!r r r get_measurement~szFluxRecord.get_measurementcCs |j|S)zGet value by key.)rH __getitem__)rkeyr r r rUszFluxRecord.__getitem__cCs|j||S)zSet value with key and value.)rH __setitem__)rrVvaluer r r rWszFluxRecord.__setitem__cCs*t|j}|dt|jdt|jS)r*z () table: rA)r+rr,rGrHr.r r r r0s zFluxRecord.__str__cCs.dt|jd|jdd|jddS)r1r2z: field=rRz, value=rP>)r+rrHgetr!r r r r3szFluxRecord.__repr__)N)rr r r r"rKrMrOrQrSrTrUrWr0r3r r r r rF_srFc@sLeZdZdZd edeeedddZd ededddZd d Z dS) TableListzy:class:`~influxdb_client.client.flux_table.FluxTable` list with additionally functional to better handle of query result.Nr,)rrcsfdd}||S)a Serialize query results to a flattened list of values. :param columns: if not ``None`` then only specified columns are presented in results :return: :class:`~list` of values Output example: .. code-block:: python [ ['New York', datetime.datetime(2022, 6, 7, 11, 3, 22, 917593, tzinfo=tzutc()), 24.3], ['Prague', datetime.datetime(2022, 6, 7, 11, 3, 22, 917593, tzinfo=tzutc()), 25.3], ... ] Configure required columns: .. code-block:: python from influxdb_client import InfluxDBClient with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") as client: # Query: using Table structure tables = client.query_api().query('from(bucket:"my-bucket") |> range(start: -10m)') # Serialize to values output = tables.to_values(columns=['location', '_time', '_value']) print(output) cs$dk rfddDSjS)Ncsg|]}j|qSr )rHrZ)r=krecordr r r?sz>TableList.to_values..filter_values..)rHr]rr]r filter_valuessz*TableList.to_values..filter_values) _to_values)rrr`r r_r to_valuess! zTableList.to_valuesc s@d|krd|d<fdd}ddl}|j||fdti|S)a Serialize query results to a JSON formatted :class:`~str`. :param columns: if not ``None`` then only specified columns are presented in results :return: :class:`~str` The query results is flattened to array: .. code-block:: javascript [ { "_measurement": "mem", "_start": "2021-06-23T06:50:11.897825+00:00", "_stop": "2021-06-25T06:50:11.897825+00:00", "_time": "2020-02-27T16:20:00.897825+00:00", "region": "north", "_field": "usage", "_value": 15 }, { "_measurement": "mem", "_start": "2021-06-23T06:50:11.897825+00:00", "_stop": "2021-06-25T06:50:11.897825+00:00", "_time": "2020-02-27T16:20:01.897825+00:00", "region": "west", "_field": "usage", "_value": 10 }, ... ] The JSON format could be configured via ``**kwargs`` arguments: .. code-block:: python from influxdb_client import InfluxDBClient with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") as client: # Query: using Table structure tables = client.query_api().query('from(bucket:"my-bucket") |> range(start: -10m)') # Serialize to JSON output = tables.to_json(indent=5) print(output) For all available options see - `json.dump `_. indentcs&dk r fdd|jDS|jS)Ncsi|]\}}|kr||qSr r )r=r\vr_r r sz.filter_values..)rHitemsr]r_r r r`sz(TableList.to_json..filter_valuesrNcls)jsondumpsrar)rrkwargsr`rir r_r to_jsons 2 zTableList.to_jsoncsfdd|DS)Ncs g|]}|jD] }|qqSr )r )r=rGr^mappingr r r?sz(TableList._to_values..r )rrnr rmr raszTableList._to_values)N)N) rr r r robjectrbr,rlrar r r r r[s(=r[c@sFeZdZdZeddddZddZdd Zeee d d d Z dS) CSVIteratorz[:class:`Iterator[List[str]]` with additionally functional to better handle of query result.N)responsercCstt|t|_dS)zInitialize ``csv.reader``.N)csvreadercodecs iterdecoderdelegate)rrqr r r r"szCSVIterator.__init__cCs|S)zReturn an iterator object.r r!r r r r5szCSVIterator.__iter__cCs|j}|s|j}q |S)z)Retrieve the next item from the iterator.)rv__next__)rrIr r r rw s  zCSVIterator.__next__rcCs t|S)ak Serialize query results to a flattened list of values. :return: :class:`~list` of values Output example: .. code-block:: python [ ['New York', '2022-06-14T08:00:51.749072045Z', '24.3'], ['Prague', '2022-06-14T08:00:51.749072045Z', '25.3'], ... ] )r'r5r!r r r rbszCSVIterator.to_values) rr r r rr"r5rwrr,rbr r r r rps rp)r rtrr http.clientrrirtypingrrZinfluxdb_client.restrrrrr6rFr[r,rpr r r r s    )5l