Werte für die Karte auch aus der Influx holen
This commit is contained in:
@@ -144,9 +144,11 @@ const transformInfluxResult = (series) => {
|
||||
if (col === 'time') {
|
||||
// Convert timestamp to ISO string for compatibility
|
||||
record.datetime = new Date(row[index]).toISOString()
|
||||
} else {
|
||||
} else if (col.startsWith('DNMS')) {
|
||||
col = col.slice(11)
|
||||
record[col] = row[index]
|
||||
} else {
|
||||
record[col] = row[index]
|
||||
}
|
||||
})
|
||||
result.push(record)
|
||||
@@ -423,5 +425,83 @@ export const fetchNoiseAVGData = async (opts) => {
|
||||
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 { influxWrite }
|
||||
|
||||
Reference in New Issue
Block a user