1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
"""
Use API invokable scripts to create custom InfluxDB API endpoints that query, process, and shape data.
API invokable scripts let you assign scripts to API endpoints and then execute them as standard REST operations
in InfluxDB Cloud.
"""
from typing import List, Iterator, Generator, Any
from influxdb_client import Script, InvokableScriptsService, ScriptCreateRequest, ScriptUpdateRequest, \
ScriptInvocationParams
from influxdb_client.client._base import _BaseQueryApi
from influxdb_client.client.flux_csv_parser import FluxResponseMetadataMode
from influxdb_client.client.flux_table import FluxRecord, TableList, CSVIterator
class InvokableScriptsApi(_BaseQueryApi):
"""Use API invokable scripts to create custom InfluxDB API endpoints that query, process, and shape data."""
def __init__(self, influxdb_client):
"""Initialize defaults."""
self._influxdb_client = influxdb_client
self._invokable_scripts_service = InvokableScriptsService(influxdb_client.api_client)
def create_script(self, create_request: ScriptCreateRequest) -> Script:
"""Create a script.
:param ScriptCreateRequest create_request: The script to create. (required)
:return: The created script.
"""
return self._invokable_scripts_service.post_scripts(script_create_request=create_request)
def update_script(self, script_id: str, update_request: ScriptUpdateRequest) -> Script:
"""Update a script.
:param str script_id: The ID of the script to update. (required)
:param ScriptUpdateRequest update_request: Script updates to apply (required)
:return: The updated.
"""
return self._invokable_scripts_service.patch_scripts_id(script_id=script_id,
script_update_request=update_request)
def delete_script(self, script_id: str) -> None:
"""Delete a script.
:param str script_id: The ID of the script to delete. (required)
:return: None
"""
self._invokable_scripts_service.delete_scripts_id(script_id=script_id)
def find_scripts(self, **kwargs):
"""List scripts.
:key int limit: The number of scripts to return.
:key int offset: The offset for pagination.
:return: List of scripts.
:rtype: list[Script]
"""
return self._invokable_scripts_service.get_scripts(**kwargs).scripts
def invoke_script(self, script_id: str, params: dict = None) -> TableList:
"""
Invoke synchronously a script and return result as a TableList.
The bind parameters referenced in the script are substitutes with `params` key-values sent in the request body.
:param str script_id: The ID of the script to invoke. (required)
:param params: bind parameters
:return: :class:`~influxdb_client.client.flux_table.FluxTable` list wrapped into
:class:`~influxdb_client.client.flux_table.TableList`
:rtype: TableList
Serialization the query results to flattened list of values via :func:`~influxdb_client.client.flux_table.TableList.to_values`:
.. code-block:: python
from influxdb_client import InfluxDBClient
with InfluxDBClient(url="https://us-west-2-1.aws.cloud2.influxdata.com", token="my-token", org="my-org") as client:
# Query: using Table structure
tables = client.invokable_scripts_api().invoke_script(script_id="script-id")
# Serialize to values
output = tables.to_values(columns=['location', '_time', '_value'])
print(output)
.. 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],
...
]
Serialization the query results to JSON via :func:`~influxdb_client.client.flux_table.TableList.to_json`:
.. code-block:: python
from influxdb_client import InfluxDBClient
with InfluxDBClient(url="https://us-west-2-1.aws.cloud2.influxdata.com", token="my-token", org="my-org") as client:
# Query: using Table structure
tables = client.invokable_scripts_api().invoke_script(script_id="script-id")
# Serialize to JSON
output = tables.to_json(indent=5)
print(output)
.. 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
},
...
]
""" # noqa: E501
response = self._invokable_scripts_service \
.post_scripts_id_invoke(script_id=script_id,
script_invocation_params=ScriptInvocationParams(params=params),
async_req=False,
_preload_content=False,
_return_http_data_only=False)
return self._to_tables(response, query_options=None, response_metadata_mode=FluxResponseMetadataMode.only_names)
def invoke_script_stream(self, script_id: str, params: dict = None) -> Generator['FluxRecord', Any, None]:
"""
Invoke synchronously a script and return result as a Generator['FluxRecord'].
The bind parameters referenced in the script are substitutes with `params` key-values sent in the request body.
:param str script_id: The ID of the script to invoke. (required)
:param params: bind parameters
:return: Stream of FluxRecord.
:rtype: Generator['FluxRecord']
"""
response = self._invokable_scripts_service \
.post_scripts_id_invoke(script_id=script_id,
script_invocation_params=ScriptInvocationParams(params=params),
async_req=False,
_preload_content=False,
_return_http_data_only=False)
return self._to_flux_record_stream(response, query_options=None,
response_metadata_mode=FluxResponseMetadataMode.only_names)
def invoke_script_data_frame(self, script_id: str, params: dict = None, data_frame_index: List[str] = None):
"""
Invoke synchronously a script and return Pandas DataFrame.
The bind parameters referenced in the script are substitutes with `params` key-values sent in the request body.
.. note:: If the ``script`` returns tables with differing schemas than the client generates a :class:`~DataFrame` for each of them.
:param str script_id: The ID of the script to invoke. (required)
:param List[str] data_frame_index: The list of columns that are used as DataFrame index.
:param params: bind parameters
:return: :class:`~DataFrame` or :class:`~List[DataFrame]`
.. warning:: For the optimal processing of the query results use the ``pivot() function`` which align results as a table.
.. code-block:: text
from(bucket:"my-bucket")
|> range(start: -5m, stop: now())
|> filter(fn: (r) => r._measurement == "mem")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
For more info see:
- https://docs.influxdata.com/resources/videos/pivots-in-flux/
- https://docs.influxdata.com/flux/latest/stdlib/universe/pivot/
- https://docs.influxdata.com/flux/latest/stdlib/influxdata/influxdb/schema/fieldsascols/
""" # noqa: E501
_generator = self.invoke_script_data_frame_stream(script_id=script_id,
params=params,
data_frame_index=data_frame_index)
return self._to_data_frames(_generator)
def invoke_script_data_frame_stream(self, script_id: str, params: dict = None, data_frame_index: List[str] = None):
"""
Invoke synchronously a script and return stream of Pandas DataFrame as a Generator['pd.DataFrame'].
The bind parameters referenced in the script are substitutes with `params` key-values sent in the request body.
.. note:: If the ``script`` returns tables with differing schemas than the client generates a :class:`~DataFrame` for each of them.
:param str script_id: The ID of the script to invoke. (required)
:param List[str] data_frame_index: The list of columns that are used as DataFrame index.
:param params: bind parameters
:return: :class:`~Generator[DataFrame]`
.. warning:: For the optimal processing of the query results use the ``pivot() function`` which align results as a table.
.. code-block:: text
from(bucket:"my-bucket")
|> range(start: -5m, stop: now())
|> filter(fn: (r) => r._measurement == "mem")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
For more info see:
- https://docs.influxdata.com/resources/videos/pivots-in-flux/
- https://docs.influxdata.com/flux/latest/stdlib/universe/pivot/
- https://docs.influxdata.com/flux/latest/stdlib/influxdata/influxdb/schema/fieldsascols/
""" # noqa: E501
response = self._invokable_scripts_service \
.post_scripts_id_invoke(script_id=script_id,
script_invocation_params=ScriptInvocationParams(params=params),
async_req=False,
_preload_content=False,
_return_http_data_only=False)
return self._to_data_frame_stream(data_frame_index, response, query_options=None,
response_metadata_mode=FluxResponseMetadataMode.only_names)
def invoke_script_csv(self, script_id: str, params: dict = None) -> CSVIterator:
"""
Invoke synchronously a script and return result as a CSV iterator. Each iteration returns a row of the CSV file.
The bind parameters referenced in the script are substitutes with `params` key-values sent in the request body.
:param str script_id: The ID of the script to invoke. (required)
:param params: bind parameters
:return: :class:`~Iterator[List[str]]` wrapped into :class:`~influxdb_client.client.flux_table.CSVIterator`
:rtype: CSVIterator
Serialization the query results to flattened list of values via :func:`~influxdb_client.client.flux_table.CSVIterator.to_values`:
.. code-block:: python
from influxdb_client import InfluxDBClient
with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") as client:
# Query: using CSV iterator
csv_iterator = client.invokable_scripts_api().invoke_script_csv(script_id="script-id")
# Serialize to values
output = csv_iterator.to_values()
print(output)
.. code-block:: python
[
['', 'result', 'table', '_start', '_stop', '_time', '_value', '_field', '_measurement', 'location']
['', '', '0', '2022-06-16', '2022-06-16', '2022-06-16', '24.3', 'temperature', 'my_measurement', 'New York']
['', '', '1', '2022-06-16', '2022-06-16', '2022-06-16', '25.3', 'temperature', 'my_measurement', 'Prague']
...
]
""" # noqa: E501
response = self._invokable_scripts_service \
.post_scripts_id_invoke(script_id=script_id,
script_invocation_params=ScriptInvocationParams(params=params),
async_req=False,
_preload_content=False)
return self._to_csv(response)
def invoke_script_raw(self, script_id: str, params: dict = None) -> Iterator[List[str]]:
"""
Invoke synchronously a script and return result as raw unprocessed result as a str.
The bind parameters referenced in the script are substitutes with `params` key-values sent in the request body.
:param str script_id: The ID of the script to invoke. (required)
:param params: bind parameters
:return: Result as a str.
"""
response = self._invokable_scripts_service \
.post_scripts_id_invoke(script_id=script_id,
script_invocation_params=ScriptInvocationParams(params=params),
async_req=False,
_preload_content=True)
return response
|