U vi5@sxdZddlmZmZmZmZddlmZmZm Z m Z m Z ddl m Z ddlmZddlmZmZmZGddde Zd S) z 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. )ListIterator GeneratorAny)ScriptInvokableScriptsServiceScriptCreateRequestScriptUpdateRequestScriptInvocationParams) _BaseQueryApi)FluxResponseMetadataMode) FluxRecord TableList CSVIteratorc@seZdZdZddZeedddZee eddd Z ed d d d Z ddZ dee edddZd ee eded fdddZd!ee eedddZd"ee eedddZd#ee edddZd$ee eeedddZd S)%InvokableScriptsApizfUse API invokable scripts to create custom InfluxDB API endpoints that query, process, and shape data.cCs||_t|j|_dS)zInitialize defaults.N)Z_influxdb_clientrZ api_client_invokable_scripts_service)selfinfluxdb_clientr]/opt/alt/python38/lib/python3.8/site-packages/influxdb_client/client/invokable_scripts_api.py__init__szInvokableScriptsApi.__init__)create_requestreturncCs|jj|dS)zCreate a script. :param ScriptCreateRequest create_request: The script to create. (required) :return: The created script. )Zscript_create_request)rZ post_scripts)rrrrr create_scriptsz!InvokableScriptsApi.create_script) script_idupdate_requestrcCs|jj||dS)zUpdate 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. )rZscript_update_request)rZpatch_scripts_id)rrrrrr update_script!sz!InvokableScriptsApi.update_scriptN)rrcCs|jj|ddS)zyDelete a script. :param str script_id: The ID of the script to delete. (required) :return: None )rN)rZdelete_scripts_id)rrrrr delete_script+sz!InvokableScriptsApi.delete_scriptcKs|jjf|jS)zList scripts. :key int limit: The number of scripts to return. :key int offset: The offset for pagination. :return: List of scripts. :rtype: list[Script] )rZ get_scriptsscripts)rkwargsrrr find_scripts3sz InvokableScriptsApi.find_scripts)rparamsrcCs.|jj|t|ddddd}|j|dtjdS)aA 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 }, ... ] r!Frscript_invocation_params async_req_preload_contentZ_return_http_data_onlyNZ query_optionsZresponse_metadata_mode)rpost_scripts_id_invoker Z _to_tablesr only_namesrrr!responserrr invoke_script=sJz!InvokableScriptsApi.invoke_scriptr cCs.|jj|t|ddddd}|j|dtjdS)a 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'] r"Fr#Nr')rr(r Z_to_flux_record_streamr r)r*rrrinvoke_script_streams z(InvokableScriptsApi.invoke_script_streamrr!data_frame_indexcCs|j|||d}||S)a4 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/ r.)invoke_script_data_frame_streamZ_to_data_frames)rrr!r/Z _generatorrrrinvoke_script_data_frames z,InvokableScriptsApi.invoke_script_data_framecCs0|jj|t|ddddd}|j||dtjdS)aK 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/ r"Fr#Nr')rr(r Z_to_data_frame_streamr r))rrr!r/r+rrrr0s z3InvokableScriptsApi.invoke_script_data_frame_streamcCs$|jj|t|dddd}||S)a 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'] ... ] r"Frr$r%r&)rr(r Z_to_csvr*rrrinvoke_script_csvs$z%InvokableScriptsApi.invoke_script_csvcCs|jj|t|dddd}|S)aq 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. r"FTr2)rr(r r*rrrinvoke_script_raws z%InvokableScriptsApi.invoke_script_raw)N)N)NN)NN)N)N)__name__ __module__ __qualname____doc__rrrrstrr rrr dictrr,rrr-rr1r0rr3rr4rrrrrs  R %,rN)r8typingrrrrrrrrr r Zinfluxdb_client.client._baser Z&influxdb_client.client.flux_csv_parserr Z!influxdb_client.client.flux_tabler rrrrrrrs