Start using mongo timeseries

mooving all database relevant function to influx.js resp. mongo.js

aktual data now also readable from mongo
This commit is contained in:
rxf
2023-05-25 13:19:03 +02:00
parent 5d10f681ea
commit ca4b68d7ff
5 changed files with 164 additions and 93 deletions
+8 -55
View File
@@ -2,10 +2,12 @@
// rxf 2023-03-05
import {returnOnError} from "../utilities/reporterror.js";
import { getActData, getAvgData, getLongAvg, fetchFromInflux, calcRange} from "../actions/getsensorData.js"
import { getActData, getAvgData, getLongAvg, calcRange} from "../actions/getsensorData.js"
import checkParams from "../utilities/checkparams.js";
import {DateTime} from 'luxon'
import { translate as trans } from '../routes/api.js'
import * as influx from "../databases/influx.js"
import * as mongo from "../databases/mongo.js"
const setoptionfromtable = (opt,tabval) => {
@@ -429,61 +431,8 @@ const getAPIprops = (opt) => {
}
const getNoiseAVGData = async (opts) => {
let ret = {err: null, values: []}
let emptyValues = {n_AVG: -1}
let small = '|> keep(columns: ["_time", "peakcount", "n_AVG"])'
if (opts.long) {
small = ''
emptyValues.n_sum = -1
}
let queryAVG = `
import "math"
threshold = ${opts.peak}
let ret = await influx.fetchNoiseAVGData(opts)
data = from(bucket: "sensor_data")
|> range(${opts.start}, ${opts.stop})
|> filter(fn: (r) => r["sid"] == "${opts.sensorid}")
e10 = data
|> filter(fn: (r) => r._field == "E10tel_eq")
|> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
|> map(fn: (r) => ({r with _value: (10.0 * math.log10(x: r._value))}))
|> keep(columns: ["_time","_field","_value"])
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> rename(columns: {"E10tel_eq" : "n_AVG"})
ecnt = data
|> filter(fn: (r) => r._field == "E10tel_eq")
|> aggregateWindow(every: 1h, fn: count, createEmpty: false)
|> keep(columns: ["_time","_field","_value"])
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> rename(columns: {"E10tel_eq" : "count"})
esum = data
|> filter(fn: (r) => r._field == "E10tel_eq")
|> aggregateWindow(every: 1h, fn: sum, createEmpty: false)
|> keep(columns: ["_time","_field","_value"])
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> rename(columns: {"E10tel_eq" : "n_sum"})
peak = data
|> filter(fn: (r) => r._field == "noise_LA_max")
|> aggregateWindow(
every: 1h,
fn: (column, tables=<-) => tables
|> reduce(
identity: {peakcount: 0.0},
fn: (r, accumulator) => ({
peakcount: if r._value >= threshold then
accumulator.peakcount + 1.0
else
accumulator.peakcount + 0.0,
}),
),
)
|> keep(columns: ["_time","peakcount"])
part1 = join( tables: {e10: e10, ecnt: ecnt}, on: ["_time"])
part2 = join( tables: {esum: esum, peak: peak}, on: ["_time"])
join( tables: {P1: part1, P2: part2}, on: ["_time"])
${small}
`
ret = await fetchFromInflux(ret, queryAVG)
if(ret.err) {
return returnOnError(ret, ret.err, getNoiseAVGData.name)
}
@@ -496,6 +445,10 @@ peak = data
// hour in an element in docs becomes the index into the new array (for every new day this
// index will be incremented by 24). Missing values are marked by: {n_sum=-1, n_AVG=-1}.
// For havg add the missed hours to the arry
let emptyValues = {n_AVG: -1}
if (opts.long) {
emptyValues.n_sum = -1
}
const misshours = DateTime.fromISO(ret.values[0].datetime).get('hour')
let hoursArr = new Array(opts.span * 24 + misshours); // generate new array
hoursArr.fill(emptyValues) // fill array with 'empty' values