-
Notifications
You must be signed in to change notification settings - Fork 70
/
index.html
192 lines (185 loc) · 6.05 KB
/
index.html
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<html>
<head>
<title>InfluxDB JavaScript Client Examples</title>
<script type="module">
// import latest release from npm repository
import {
InfluxDB,
Point,
flux,
} from 'https://unpkg.com/@influxdata/influxdb-client/dist/index.browser.mjs'
import {
PingAPI,
SetupAPI,
} from 'https://unpkg.com/@influxdata/influxdb-client-apis/dist/index.mjs'
// or use the following imports to use local builds
// import {InfluxDB, Point, flux} from '../packages/core/dist/index.browser.mjs'
// import {PingAPI, SetupAPI} from '../packages/apis/dist/index.browser.mjs'
/**
* Import InfluxDB configuration rather than inlining it.
*/
import {
url,
token,
org,
bucket,
username,
password,
} from './env_browser.mjs'
const influxDB = new InfluxDB({url, token})
// log results also to HTML page
const logField = document.getElementById('log')
function log(...args) {
console.log.apply(console, args)
const previousValue = logField.value
logField.value +=
(previousValue ? '\n' : '') +
Array.prototype.slice.call(arguments).join('\n')
// scroll to bottom with latest results
logField.scrollTo(
0,
logField.scrollHeight <= logField.offsetHeight
? 0
: logField.scrollHeight - logField.offsetHeight
)
}
function writeExample(value) {
const writeApi = influxDB.getWriteApi(org, bucket)
// setup default tags for all writes through this API
writeApi.useDefaultTags({location: 'browser'})
log('\n*** WRITE ***')
const point1 = new Point('temperature')
.tag('example', 'index.html')
.floatField('value', value)
writeApi.writePoint(point1)
log(` ${point1.toLineProtocol()}`)
// flush pending writes and close writeApi
writeApi
.close()
.then(() => {
log('WRITE FINISHED')
temperatureInput.value = String(
20 + Math.round(100 * Math.random()) / 10
)
})
.catch((e) => {
log('WRITE FAILED', e)
})
}
function queryExample(fluxQuery) {
log('\n*** QUERY ***')
const queryApi = influxDB.getQueryApi(org)
queryApi.queryRows(fluxQuery, {
next(row, tableMeta) {
const o = tableMeta.toObject(row)
if (o.example) {
// custom output for example query
log(
`${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
)
} else {
// default output
log(JSON.stringify(o, null, 2))
}
},
error(error) {
log('QUERY FAILED', error)
},
complete() {
log('QUERY FINISHED')
},
})
}
function onboardingExample() {
log('\n*** ONBOARDING ***')
const setupApi = new SetupAPI(influxDB)
setupApi
.getSetup()
.then(async ({allowed}) => {
if (allowed) {
await setupApi.postSetup({
body: {
org,
bucket,
username,
password,
token,
},
})
log(`InfluxDB '${url}' is now onboarded.`)
} else {
log(`InfluxDB '${url}' has been already onboarded.`)
}
})
.catch((error) => {
log('Onboarding FAILED', error)
})
}
function pingExample() {
log('\n*** PING ***')
const pingApi = new PingAPI(influxDB)
pingApi
.getPing()
.then(() => {
log('Ping SUCCESS')
})
.catch((error) => {
log('Ping FAILED', error)
})
}
// initialize page controls
const temperatureInput = document.getElementById('temperature')
temperatureInput.value = String(20 + Math.round(100 * Math.random()) / 10)
const writeButton = document.getElementById('writeButton')
writeButton.addEventListener('click', () => {
const number = Number(temperatureInput.value)
if (isNaN(number)) log('ERROR: Not a number ' + temperatureInput.value)
else writeExample(number)
})
const queryInput = document.getElementById('query')
document.getElementById('queryButton').addEventListener('click', () => {
queryExample(queryInput.value)
})
document.getElementById('onboardButton').addEventListener('click', () => {
onboardingExample()
})
document.getElementById('pingButton').addEventListener('click', () => {
pingExample()
})
document.addEventListener('DOMContentLoaded', () => {
const fluxQueryParam = new URLSearchParams(window.location.search).get(
'fluxQuery'
)
if (fluxQueryParam) {
queryInput.value = fluxQueryParam
} else {
queryInput.value =
flux`from(bucket:${bucket}) |> range(start: -1d) |> filter(fn: (r) => r._measurement == "temperature")`.toString()
}
})
</script>
</head>
<h1>InfluxDB JavaScript Client Examples</h1>
<hr />
<div>
<button id="onboardButton">InfluxDB Onboarding</button>
<button id="pingButton">InfluxDB Ping</button>
</div>
<hr />
<div>
<button id="writeButton">Write to InfluxDB</button>
<span>Temperature: </span>
<input type="number" id="temperature" value="20" />
</div>
<hr />
<div style="display: flex; margin-bottom: 10px">
<textarea id="query" style="flex: 1" rows="2"></textarea>
</div>
<button id="queryButton">Query InfluxDB</button>
<hr />
<fieldset>
<legend>Log</legend>
<textarea id="log" style="width: 100%" rows="25"></textarea>
<button onclick="document.getElementById('log').value=''">Clear Log</button>
</fieldset>
</html>