forked from influxdata/influxdb-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvokableScripts.mjs
executable file
·105 lines (97 loc) · 3.07 KB
/
invokableScripts.mjs
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
#!/usr/bin/env node
/*
This example shows how to use API-invokable scripts. See also
https://docs.influxdata.com/influxdb/cloud/api-guide/api-invokable-scripts/ .
*/
import {InfluxDB, HttpError} from '@influxdata/influxdb-client'
import {
ScriptsAPI,
FluxScriptInvocationAPI,
} from '@influxdata/influxdb-client-apis'
import {url, token} from './env.mjs'
import {argv} from 'node:process'
const influxDB = new InfluxDB({url, token})
const scriptsAPI = new ScriptsAPI(influxDB)
const scriptName = 'example_generate_values'
async function listScripts() {
console.log('*** List scripts ***')
// current scripts
const scripts = await scriptsAPI.getScripts()
console.log(scripts.scripts)
return scripts
}
async function createScript() {
console.log('*** Create example script ***')
// current scripts
const script = await scriptsAPI.postScripts({
body: {
script: `import "generate"
generate.from(
count: int(v: params.count),
fn: (n) => (n + 1) * (n + 2),
start: 2021-01-01T00:00:00Z,
stop: 2021-01-02T00:00:00Z,
)`,
description:
'This is an example script generated by a javascript client.',
name: scriptName,
language: 'flux',
},
})
console.log(script)
return script
}
async function deleteScript(id) {
console.log('*** Delete example script ***')
if (!id) {
const scripts = (await scriptsAPI.getScripts()).scripts
id = scripts.find((x) => x.name === scriptName)?.id
}
if (id) {
await scriptsAPI.deleteScriptsID({scriptID: id})
console.log(`Script ${scriptName} was deleted!`)
}
}
async function invokeScript(scriptID) {
console.log('*** Invoke example script ***')
// parse count as a first script argument or use 10
const count = Number.parseInt(argv[2] || '10')
// execute script with count parameter
const params = {count: count}
console.log('Script parameters: ', params)
// Use FluxScriptInvocationAPI to execute a particular
// script with specified parameters and process parsed results
const invocationAPI = new FluxScriptInvocationAPI(influxDB)
const results = invocationAPI.invoke(scriptID, params)
let cnt = 0
for await (const {values, tableMeta} of results.iterateRows()) {
cnt++
// console.log(tableMetaData.toObject(row))
console.log(cnt, '*', cnt + 1, '=', tableMeta.get(values, '_value'))
}
// // You can also receive the whole response body. Use with caution,
// // a possibly huge stream of results is copied to memory.
// const response = await scriptsAPI.postScriptsIDInvoke({
// scriptID,
// body: {params},
// })
// console.log('Response:')
// console.log(response)
}
try {
await listScripts()
await deleteScript()
const {id} = await createScript()
await invokeScript(id)
console.log('\nFinished SUCCESS')
} catch (e) {
if (e instanceof HttpError && e.statusCode === 404) {
console.error(
`API invokable scripts are not supported by InfluxDB at ${url} .`
)
console.error('Modify env.mjs with InfluxDB Cloud URL and token.')
} else {
console.error(e)
}
console.log('\nFinished ERROR')
}