Werte für die Karte auch aus der Influx holen

This commit is contained in:
2025-11-22 10:14:32 +00:00
parent 105cdc74c2
commit 4b86536e7e
2 changed files with 100 additions and 10 deletions

View File

@@ -2,6 +2,7 @@
import {DateTime} from "luxon" import {DateTime} from "luxon"
import * as mongo from "../databases/mongo.js" import * as mongo from "../databases/mongo.js"
import { returnOnError } from "../utilities/reporterror.js" import { returnOnError } from "../utilities/reporterror.js"
import { fetchLatestLAmaxForChips } from '../databases/influx_sql.js'
// Default distance for center search ( in km) // Default distance for center search ( in km)
@@ -127,6 +128,8 @@ export var getData4map = async (params) => {
} }
} }
} }
// Nur Einträge verwenden, die eine ESP-Chipid haben
query.chip = { $exists: true }
try { try {
// fetch mapdata from mongodb // fetch mapdata from mongodb
let { properties, err } = await mongo.getallProperties(mongo.properties_collection, query) let { properties, err } = await mongo.getallProperties(mongo.properties_collection, query)
@@ -134,19 +137,26 @@ export var getData4map = async (params) => {
return returnOnError(ret, 'NOPROPSFOUND', getData4map.name) return returnOnError(ret, 'NOPROPSFOUND', getData4map.name)
} }
let v4map = getValue4Map(typ) let v4map = getValue4Map(typ)
const chipIds = properties.map(prop => prop.chip?.id).filter(id => id)
const result = await fetchLatestLAmaxForChips({ chipids: chipIds})
for (let sensor of properties) { for (let sensor of properties) {
const resIndex = result.values.findIndex((id) => id.chipid == sensor.chip.id)
const chpvalues = result.values[resIndex]
let oneAktData = {} let oneAktData = {}
if (sensor.values !== undefined) { if (!((sensor.values === undefined) || (chpvalues === undefined))) {
oneAktData = { oneAktData = {
location: sensor.location[0].loc.coordinates, location: sensor.location[0].loc.coordinates,
id: sensor._id, id: sensor._id,
name: sensor.name[0].name, name: sensor.chip.name,
indoor: sensor.location[0].indoor, indoor: sensor.location[0].indoor,
lastseen: sensor.values.timestamp lastseen: chpvalues.timestamp
} }
let now = new Date().getTime() let now = new Date().getTime()
if(oneAktData.lastseen !== '') { if(oneAktData.lastseen !== '') {
let diff = now - oneAktData.lastseen.getTime() const dt = new Date(oneAktData.lastseen).getTime()
let diff = now - dt
if (diff >= 365 * 24 * 3600 * 1000) { if (diff >= 365 * 24 * 3600 * 1000) {
oneAktData.value = -4 oneAktData.value = -4
} else if (diff >= 30 * 24 * 3600 * 1000) { } else if (diff >= 30 * 24 * 3600 * 1000) {
@@ -156,15 +166,15 @@ export var getData4map = async (params) => {
} else if (diff >= 2 * 3600 * 1000) { } else if (diff >= 2 * 3600 * 1000) {
oneAktData.value = -1 oneAktData.value = -1
} else { } else {
if (sensor.values !== undefined) { if (chpvalues !== undefined) {
oneAktData.value = Math.round(sensor.values[v4map] * 100) / 100 oneAktData.value = Math.round(chpvalues[v4map] * 100) / 100
} }
} }
let weeks = Math.round(diff / (7 * 24 * 3600 * 1000)) let weeks = Math.round(diff / (7 * 24 * 3600 * 1000))
oneAktData.weeks = weeks oneAktData.weeks = weeks
} }
if (sensor.values.timestamp > lastDate) { if (new Date(oneAktData.lastseen).getTime() > lastDate) {
lastDate = sensor.values.timestamp lastDate = new Date(oneAktData.lastseen).getTime()
} }
} else { } else {
oneAktData.value = -5 oneAktData.value = -5
@@ -176,7 +186,7 @@ export var getData4map = async (params) => {
ret = { ret = {
err: null, err: null,
options: { options: {
lastdate: lastDate, lastdate: new Date(lastDate).toISOString(),
count: aktData.length, count: aktData.length,
data: 'map' data: 'map'
}, },

View File

@@ -144,9 +144,11 @@ const transformInfluxResult = (series) => {
if (col === 'time') { if (col === 'time') {
// Convert timestamp to ISO string for compatibility // Convert timestamp to ISO string for compatibility
record.datetime = new Date(row[index]).toISOString() record.datetime = new Date(row[index]).toISOString()
} else { } else if (col.startsWith('DNMS')) {
col = col.slice(11) col = col.slice(11)
record[col] = row[index] record[col] = row[index]
} else {
record[col] = row[index]
} }
}) })
result.push(record) result.push(record)
@@ -423,5 +425,83 @@ export const fetchNoiseAVGData = async (opts) => {
return ret return ret
} }
/**
* Fetch latest LA_max values for multiple chip IDs
* @param {Object} opts - Options object
* @param {Array<string>} opts.chipids - Array of chip IDs
* @returns {Object} - {err: null, values: [{chipid, LA_max, timestamp}]}
*/
export const fetchLatestLAmaxForChips = async (opts) => {
let ret = { err: null, values: [] }
if (!opts.chipids || !Array.isArray(opts.chipids) || opts.chipids.length === 0) {
ret.err = 'No chip IDs provided'
logit(`ERROR ${fetchLatestLAmaxForChips.name}: ${ret.err}`)
return ret
}
try {
// Build WHERE clause with multiple chip IDs using OR (InfluxQL doesn't support IN)
const chipIdConditions = opts.chipids.map(id => `"node" = '${id}'`).join(' OR ')
// Query to get latest LA_max for each chip
const query = `SELECT "DNMS_noise_LA_max", "node" FROM "DNMS" WHERE (${chipIdConditions}) AND time >= now() - 24h ORDER BY time DESC`
let { values: lamaxValues, err: lamaxErr } = await influxRead(query)
if (lamaxErr) {
ret.err = lamaxErr.toString()
logit(`ERROR ${fetchLatestLAmaxForChips.name}: ${ret.err}`)
return ret
}
if (!lamaxValues || !lamaxValues.length || !lamaxValues[0].series) {
ret.err = 'NODATA'
logit(`ERROR ${fetchLatestLAmaxForChips.name}: No data returned from query`)
return ret
}
// Transform results
const allData = transformInfluxResult(lamaxValues[0].series)
// Get latest value for each chip (data is already sorted by time DESC)
const latestByChip = {}
allData.forEach(record => {
const chipid = record.node
const lamax = record.LA_max
// Only keep the first (latest) value for each chip
if (!latestByChip[chipid] && lamax !== null && lamax !== undefined) {
latestByChip[chipid] = {
chipid: chipid,
LA_max: lamax,
timestamp: record.datetime
}
}
})
// Convert to array
ret.values = Object.values(latestByChip)
// Add null entries for chips without data
opts.chipids.forEach(chipid => {
if (!latestByChip[chipid]) {
ret.values.push({
chipid: chipid,
LA_max: null,
timestamp: null
})
}
})
} catch (e) {
ret.err = e.toString()
logit(`ERROR ${fetchLatestLAmaxForChips.name}: ${ret.err}`)
return ret
}
return ret
}
// Export write function for compatibility // Export write function for compatibility
export { influxWrite } export { influxWrite }