Published
Edited
May 25, 2020
1 star
Insert cell
Insert cell
_ = await require("lodash@4")
Insert cell
_.VERSION
Insert cell
moment = await require("moment")
Insert cell
influx = await require("@influxdata/influxdb-client")
Insert cell
InfluxDB = influx.InfluxDB
Insert cell
FluxTableMetaData = influx.FluxTableMetaData
Insert cell
Insert cell
queryApi = new InfluxDB({url: 'https://if.tam.ma', token:'yjAJvitAjLWuDYnWTcldhjnghbaR07puLL1_bwe78_Ye9YC0J8gPzzMy_BqVUiQ7n1RjgsLb6ALH6XG4NLpYyA=='}).getQueryApi('tamera')
Insert cell
fluxQuery =
`import "math"
import "experimental"
import "date"

gradient_clumping = 0.2 // 10.0 would mean it's clumped to 0, 0.1, 0.2, 0.3, ...
float_glitch_correction = 10.0 // no need to change this, I guess - https://stackoverflow.com/a/18908122/1633985

from(bucket: "weather_5m")
|> range(start: 2019-01-01, stop: 2020-01-01)

|> filter(fn: (r) => r._measurement == "Temp_Avg" and r._field == "value") //r._measurement == "RadSol_Avg" or
|> pivot(
rowKey:["_time"],
columnKey: ["_field"],
valueColumn: "_value"
)
|> rename(columns: {value : "_value"})

|> window(every: 1d)
|> reduce(fn: (r, accumulator) => ({
r with
min: math.mMin(x: accumulator.min, y: r._value),
max: math.mMax(x: accumulator.max, y: r._value),
count: accumulator.count + 1,
total: accumulator.total + r._value,
avg: (accumulator.total + r._value) / float(v: accumulator.count)
}),
identity: {min: 100000.0, max:-100000.0, count: 1, total: 0.0, avg: 0.0}
)`
Insert cell
rows=[] // define rows as a constant to be filled below
Insert cell
{
rows.length=0 // empty rows before doing the query
queryApi.queryRows(fluxQuery, {
next(row, tableMeta) {
const o =tableMeta.toObject(row)
rows.push(o)
// console.log(JSON.stringify(o, null, 2))
console.log(o, `${o._time} ${o._measurement} ${o._key}=${o._value}`)
},
error(error) {
console.error(error)
console.log('\nFinished ERROR')
},
complete() {
console.log('\nFinished SUCCESS')
},
})}
Insert cell
import { vl } from "@vega/vega-lite-api"
Insert cell
{
const chartWidth = 810
const brush = vl.selectInterval().encodings('x');
const opacity = vl.opacity().if(brush, vl.value(0.9)).value(0.1);
const vCandles= vl
.layer(
vl.markErrorbar({"ticks": true})
.data(rows)
.encode(
vl.x().fieldT('_time').axis({title: ''}),
vl.y().field('min').type('quantitative').axis({title: 'temp'}),
vl.y2().field('max').type('quantitative'),
vl.tooltip().field('avg'),
),
vl.markPoint({"filled": true, "color": "black"})
.data(rows)
.encode(
vl.x().fieldT('_time'),
vl.y().field('avg').type('quantitative')
),
vl.markLine({ "stroke": "coral"
// {
// "condition": {"test": "datum.tick === 'u'", "value": "purple"},
// "color": "red"
// }
})
.data(rows)
.encode(
vl.x().fieldT('_time'),
vl.y().field('avg').type('quantitative').scale({zero:false}),
)
.select(vl.selectInterval().encodings('x')) // add interval brush selection to the chart
)
.width(chartWidth) // use the full default chart width
return vCandles.render()
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more