|
8 | 8 | bucket = '...'
|
9 | 9 | org = '...'
|
10 | 10 |
|
11 |
| -client = InfluxDB2::Client.new(url, token, bucket: bucket, org: org, precision: InfluxDB2::WritePrecision::NANOSECOND) |
| 11 | +InfluxDB2::Client.use(url, |
| 12 | + token, |
| 13 | + bucket: bucket, |
| 14 | + org: org, |
| 15 | + precision: InfluxDB2::WritePrecision::NANOSECOND) do |client| |
12 | 16 |
|
13 |
| -unique_id = Time.now.utc.to_i.to_s |
14 |
| -# |
15 |
| -# Prepare data |
16 |
| -# |
17 |
| -point1 = InfluxDB2::Point.new(name: 'my_measurement') |
18 |
| - .add_tag('location', 'Prague') |
19 |
| - .add_field('temperature', 25.3) |
20 |
| -point2 = InfluxDB2::Point.new(name: 'my_measurement') |
21 |
| - .add_tag('location', 'New York') |
22 |
| - .add_field('temperature', 24.3) |
23 |
| -client.create_write_api.write(data: [point1, point2]) |
| 17 | + unique_id = Time.now.utc.to_i.to_s |
| 18 | + # |
| 19 | + # Prepare data |
| 20 | + # |
| 21 | + point1 = InfluxDB2::Point.new(name: 'my_measurement') |
| 22 | + .add_tag('location', 'Prague') |
| 23 | + .add_field('temperature', 25.3) |
| 24 | + point2 = InfluxDB2::Point.new(name: 'my_measurement') |
| 25 | + .add_tag('location', 'New York') |
| 26 | + .add_field('temperature', 24.3) |
| 27 | + client.create_write_api.write(data: [point1, point2]) |
24 | 28 |
|
25 |
| -scripts_api = client.create_invokable_scripts_api |
| 29 | + scripts_api = client.create_invokable_scripts_api |
26 | 30 |
|
27 |
| -# |
28 |
| -# Create Invokable Script |
29 |
| -# |
30 |
| -puts "------- Create -------\n" |
31 |
| -script_query = 'from(bucket: params.bucket_name) |> range(start: -30d) |> limit(n:2)' |
32 |
| -create_request = InfluxDB2::ScriptCreateRequest.new(name: "my_script_#{unique_id}", |
33 |
| - description: 'my first try', |
34 |
| - language: InfluxDB2::ScriptLanguage::FLUX, |
35 |
| - script: script_query) |
| 31 | + # |
| 32 | + # Create Invokable Script |
| 33 | + # |
| 34 | + puts "------- Create -------\n" |
| 35 | + script_query = 'from(bucket: params.bucket_name) |> range(start: -30d) |> limit(n:2)' |
| 36 | + create_request = InfluxDB2::ScriptCreateRequest.new(name: "my_script_#{unique_id}", |
| 37 | + description: 'my first try', |
| 38 | + language: InfluxDB2::ScriptLanguage::FLUX, |
| 39 | + script: script_query) |
36 | 40 |
|
37 |
| -created_script = scripts_api.create_script(create_request) |
38 |
| -puts created_script.inspect |
| 41 | + created_script = scripts_api.create_script(create_request) |
| 42 | + puts created_script.inspect |
39 | 43 |
|
40 |
| -# |
41 |
| -# Update Invokable Script |
42 |
| -# |
43 |
| -puts "------- Update -------\n" |
44 |
| -update_request = InfluxDB2::ScriptUpdateRequest.new(description: 'my updated description') |
45 |
| -created_script = scripts_api.update_script(created_script.id, update_request) |
46 |
| -puts created_script.inspect |
| 44 | + # |
| 45 | + # Update Invokable Script |
| 46 | + # |
| 47 | + puts "------- Update -------\n" |
| 48 | + update_request = InfluxDB2::ScriptUpdateRequest.new(description: 'my updated description') |
| 49 | + created_script = scripts_api.update_script(created_script.id, update_request) |
| 50 | + puts created_script.inspect |
47 | 51 |
|
48 |
| -# |
49 |
| -# Invoke a script |
50 |
| -# |
| 52 | + # |
| 53 | + # Invoke a script |
| 54 | + # |
51 | 55 |
|
52 |
| -# FluxTables |
53 |
| -puts "\n------- Invoke to FluxTables -------\n" |
54 |
| -tables = scripts_api.invoke_script(created_script.id, params: { 'bucket_name' => bucket }) |
55 |
| -tables.each do |_, table| |
56 |
| - table.records.each do |record| |
| 56 | + # FluxTables |
| 57 | + puts "\n------- Invoke to FluxTables -------\n" |
| 58 | + tables = scripts_api.invoke_script(created_script.id, params: { 'bucket_name' => bucket }) |
| 59 | + tables.each do |_, table| |
| 60 | + table.records.each do |record| |
| 61 | + puts "#{record.time} #{record.values['location']}: #{record.field} #{record.value}" |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + # Stream of FluxRecords |
| 66 | + puts "\n------- Invoke to Stream of FluxRecords -------\n" |
| 67 | + records = scripts_api.invoke_script_stream(created_script.id, params: { 'bucket_name' => bucket }) |
| 68 | + records.each do |record| |
57 | 69 | puts "#{record.time} #{record.values['location']}: #{record.field} #{record.value}"
|
58 | 70 | end
|
59 |
| -end |
60 | 71 |
|
61 |
| -# Stream of FluxRecords |
62 |
| -puts "\n------- Invoke to Stream of FluxRecords -------\n" |
63 |
| -records = scripts_api.invoke_script_stream(created_script.id, params: { 'bucket_name' => bucket }) |
64 |
| -records.each do |record| |
65 |
| - puts "#{record.time} #{record.values['location']}: #{record.field} #{record.value}" |
66 |
| -end |
| 72 | + # RAW |
| 73 | + puts "\n------- Invoke to Raw-------\n" |
| 74 | + raw = scripts_api.invoke_script_raw(created_script.id, params: { 'bucket_name' => bucket }) |
| 75 | + puts "RAW output:\n #{raw}" |
67 | 76 |
|
68 |
| -# RAW |
69 |
| -puts "\n------- Invoke to Raw-------\n" |
70 |
| -raw = scripts_api.invoke_script_raw(created_script.id, params: { 'bucket_name' => bucket }) |
71 |
| -puts "RAW output:\n #{raw}" |
| 77 | + # |
| 78 | + # List scripts |
| 79 | + # |
| 80 | + puts "\n------- List -------\n" |
| 81 | + scripts = scripts_api.find_scripts |
| 82 | + scripts.each do |script| |
| 83 | + puts " ---\n ID: #{script.id}\n Name: #{script.name}\n Description: #{script.description}" |
| 84 | + end |
| 85 | + puts '---' |
72 | 86 |
|
73 |
| -# |
74 |
| -# List scripts |
75 |
| -# |
76 |
| -puts "\n------- List -------\n" |
77 |
| -scripts = scripts_api.find_scripts |
78 |
| -scripts.each do |script| |
79 |
| - puts " ---\n ID: #{script.id}\n Name: #{script.name}\n Description: #{script.description}" |
| 87 | + # |
| 88 | + # Delete previously created Script |
| 89 | + # |
| 90 | + puts "------- Delete -------\n" |
| 91 | + scripts_api.delete_script(created_script.id) |
| 92 | + puts " Successfully deleted script: '#{created_script.name}'" |
80 | 93 | end
|
81 |
| -puts '---' |
82 |
| - |
83 |
| -# |
84 |
| -# Delete previously created Script |
85 |
| -# |
86 |
| -puts "------- Delete -------\n" |
87 |
| -scripts_api.delete_script(created_script.id) |
88 |
| -puts " Successfully deleted script: '#{created_script.name}'" |
89 |
| - |
90 |
| -client.close! |
|
0 commit comments