-
Notifications
You must be signed in to change notification settings - Fork 70
/
query.ts
executable file
·111 lines (100 loc) · 3.86 KB
/
query.ts
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
#!./node_modules/.bin/esr
/* eslint-disable @typescript-eslint/no-unused-vars */
//////////////////////////////////////////
// Shows how to use InfluxDB query API. //
//////////////////////////////////////////
import {InfluxDB, FluxTableMetaData} from '@influxdata/influxdb-client'
import {url, token, org} from './env.mjs'
const queryApi = new InfluxDB({url, token}).getQueryApi(org)
const fluxQuery =
'from(bucket:"my-bucket") |> range(start: -1d) |> filter(fn: (r) => r._measurement == "temperature")'
// There are more ways of how to receive results,
// the essential ones are shown in functions below.
// Execution of a particular function follows
// its definition, comment/uncomment it at will.
// See also rxjs-query.ts and queryWithParams.mjs .
// Execute query and receive table metadata and table row values using async iterator.
async function iterateRows() {
console.log('*** IterateRows ***')
for await (const {values, tableMeta} of queryApi.iterateRows(fluxQuery)) {
// the following line creates an object for each row
const o = tableMeta.toObject(values)
// console.log(JSON.stringify(o, null, 2))
console.log(
`${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
)
// alternatively, you can get only a specific column value without
// the need to create an object for every row
// console.log(tableMeta.get(row, '_time'))
}
console.log('\nIterateRows SUCCESS')
}
// iterateRows().catch((error) => console.error('IterateRows ERROR', error))
// Execute query and receive table metadata and rows in a result observer.
function queryRows() {
console.log('*** QueryRows ***')
queryApi.queryRows(fluxQuery, {
next: (row: string[], tableMeta: FluxTableMetaData) => {
// the following line creates an object for each row
const o = tableMeta.toObject(row)
// console.log(JSON.stringify(o, null, 2))
console.log(
`${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
)
// alternatively, you can get only a specific column value without
// the need to create an object for every row
// console.log(tableMeta.get(row, '_time'))
},
error: (error: Error) => {
console.error(error)
console.log('\nQueryRows ERROR')
},
complete: () => {
console.log('\nQueryRows SUCCESS')
},
})
}
queryRows()
// Execute query and collect result rows in a Promise.
// Use with caution, it copies the whole stream of results into memory.
async function collectRows() {
console.log('\n*** CollectRows ***')
const data = await queryApi.collectRows(
fluxQuery //, you can also specify a row mapper as a second argument
)
data.forEach((x) => console.log(JSON.stringify(x)))
console.log('\nCollect ROWS SUCCESS')
}
// collectRows().catch((error) => console.error('CollectRows ERROR', error))
// Execute query and return the whole result as a string.
// Use with caution, it copies the whole stream of results into memory.
async function queryRaw() {
const result = await queryApi.queryRaw(fluxQuery)
console.log(result)
console.log('\nQueryRaw SUCCESS')
}
// queryRaw().catch((error) => console.error('QueryRaw ERROR', error))
// Execute query and receive result CSV lines in an observer
function queryLines() {
queryApi.queryLines(fluxQuery, {
next: (line: string) => {
console.log(line)
},
error: (error: Error) => {
console.error(error)
console.log('\nQueryLines ERROR')
},
complete: () => {
console.log('\nQueryLines SUCCESS')
},
})
}
// queryLines()
// Execute query and receive result csv lines using async iterable
async function iterateLines() {
for await (const line of queryApi.iterateLines(fluxQuery)) {
console.log(line)
}
console.log('\nIterateLines SUCCESS')
}
// iterateLines().catch((error) => console.error('\nIterateLines ERROR', error))