|
3 | 3 | """
|
4 | 4 | import datetime
|
5 | 5 |
|
6 |
| -from influxdb_client import InfluxDBClient, InvocableScriptsService, ScriptCreateRequest, ScriptInvocationParams, \ |
7 |
| - ScriptLanguage |
| 6 | +from influxdb_client import InfluxDBClient, ScriptCreateRequest, ScriptLanguage, \ |
| 7 | + ScriptUpdateRequest, Point |
| 8 | +from influxdb_client.client.write_api import SYNCHRONOUS |
8 | 9 |
|
9 | 10 | """
|
10 | 11 | Define credentials
|
|
14 | 15 | bucket_name = '...'
|
15 | 16 | org_name = '...'
|
16 | 17 |
|
17 |
| -with InfluxDBClient(url=influx_cloud_url, token=influx_cloud_token, org=org_name, debug=False, timeout=20_000) as client: |
| 18 | +with InfluxDBClient(url=influx_cloud_url, token=influx_cloud_token, org=org_name, debug=False, |
| 19 | + timeout=20_000) as client: |
18 | 20 | uniqueId = str(datetime.datetime.now())
|
| 21 | + |
| 22 | + """ |
| 23 | + Prepare data |
| 24 | + """ |
| 25 | + _point1 = Point("my_measurement").tag("location", "Prague").field("temperature", 25.3) |
| 26 | + _point2 = Point("my_measurement").tag("location", "New York").field("temperature", 24.3) |
| 27 | + client.write_api(write_options=SYNCHRONOUS).write(bucket=bucket_name, record=[_point1, _point2]) |
| 28 | + |
19 | 29 | """
|
20 | 30 | Find Organization ID by Organization API.
|
21 | 31 | """
|
22 | 32 | org = client.organizations_api().find_organizations(org=org_name)[0]
|
23 | 33 |
|
24 |
| - scripts_service = InvocableScriptsService(api_client=client.api_client) |
| 34 | + scripts_api = client.invocable_scripts_api() |
25 | 35 |
|
26 | 36 | """
|
27 | 37 | Create Invocable Script
|
28 | 38 | """
|
29 | 39 | print(f"------- Create -------\n")
|
30 |
| - create_request = ScriptCreateRequest(name=f"my_scrupt_{uniqueId}", |
| 40 | + create_request = ScriptCreateRequest(name=f"my_script_{uniqueId}", |
31 | 41 | description="my first try",
|
32 | 42 | language=ScriptLanguage.FLUX,
|
33 |
| - org_id=org.id, |
34 | 43 | script=f"from(bucket: params.bucket_name) |> range(start: -30d) |> limit(n:2)")
|
35 | 44 |
|
36 |
| - created_script = scripts_service.post_scripts(script_create_request=create_request) |
| 45 | + created_script = scripts_api.create_script(create_request=create_request) |
| 46 | + print(created_script) |
| 47 | + |
| 48 | + """ |
| 49 | + Update Invocable Script |
| 50 | + """ |
| 51 | + print(f"------- Update -------\n") |
| 52 | + update_request = ScriptUpdateRequest(description="my updated description") |
| 53 | + created_script = scripts_api.update_script(script_id=created_script.id, update_request=update_request) |
37 | 54 | print(created_script)
|
38 | 55 |
|
39 | 56 | """
|
40 | 57 | Invoke a script
|
41 | 58 | """
|
42 |
| - print(f"\n------- Invoke -------\n") |
43 |
| - response = scripts_service.post_scripts_id_invoke(script_id=created_script.id, |
44 |
| - script_invocation_params=ScriptInvocationParams( |
45 |
| - params={"bucket_name": bucket_name})) |
46 |
| - print(response) |
| 59 | + # FluxRecords |
| 60 | + print(f"\n------- Invoke to FluxRecords -------\n") |
| 61 | + tables = scripts_api.invoke_scripts(script_id=created_script.id, params={"bucket_name": bucket_name}) |
| 62 | + for table in tables: |
| 63 | + for record in table.records: |
| 64 | + print(f'FluxRecord {record}') |
| 65 | + # Pandas DataFrame |
| 66 | + print(f"\n------- Invoke to PandasData Frame -------\n") |
| 67 | + data_frame = scripts_api.invoke_scripts_data_frame(script_id=created_script.id, params={"bucket_name": bucket_name}) |
| 68 | + print(data_frame.to_string()) |
| 69 | + # CSV |
| 70 | + print(f"\n------- Invoke to CSV-------\n") |
| 71 | + csv_lines = scripts_api.invoke_scripts_csv(script_id=created_script.id, params={"bucket_name": bucket_name}) |
| 72 | + for csv_line in csv_lines: |
| 73 | + if not len(csv_line) == 0: |
| 74 | + print(f'CSV row: {csv_line}') |
| 75 | + # RAW |
| 76 | + print(f"\n------- Invoke to Raw-------\n") |
| 77 | + raw = scripts_api.invoke_scripts_raw(script_id=created_script.id, params={"bucket_name": bucket_name}) |
| 78 | + print(f'RAW output:\n {raw}') |
47 | 79 |
|
48 | 80 | """
|
49 | 81 | List scripts
|
50 | 82 | """
|
51 | 83 | print(f"\n------- List -------\n")
|
52 |
| - scripts = scripts_service.get_scripts().scripts |
| 84 | + scripts = scripts_api.find_scripts() |
53 | 85 | print("\n".join([f" ---\n ID: {it.id}\n Name: {it.name}\n Description: {it.description}" for it in scripts]))
|
54 | 86 | print("---")
|
55 | 87 |
|
56 | 88 | """
|
57 | 89 | Delete previously created Script
|
58 | 90 | """
|
59 | 91 | print(f"------- Delete -------\n")
|
60 |
| - scripts_service.delete_scripts_id(script_id=created_script.id) |
| 92 | + scripts_api.delete_script(script_id=created_script.id) |
61 | 93 | print(f" Successfully deleted script: '{created_script.name}'")
|
0 commit comments