/* Zugriff auf VictoriaMetrics per HTTP (InfluxDB-Line-Protocol-kompatibler Write-Endpunkt) Gemeinsam genutzt von readin (Live-API) und readarchive (CSV-Archiv). */ import axios from 'axios' import { logit, logerror } from './logit.js' import { statistics } from './statistics.js' import { DateTime } from 'luxon' let VICTORIAHOST = process.env.VICTORIAHOST || "localhost" let VICTORIAPORT = process.env.VICTORIAPORT || 8428 const VICTORIAURL_WRITE = `http://${VICTORIAHOST}:${VICTORIAPORT}/write?precision=ms` // `${e}` liefert bei einem AggregateError nur "AggregateError" - die einzelnen // Verbindungsfehler (je einer pro aufgeloester IP) stecken in e.errors bzw. e.cause.errors function describeError(e) { const sub = e.errors || e.cause?.errors if (sub?.length) { return `${e.message || e.name}: ` + sub.map(s => `${s.code} ${s.address}:${s.port}`).join(', ') } if (e.response) { return `HTTP ${e.response.status} ${JSON.stringify(e.response.data)}` } return `${e.code ? e.code + ' ' : ''}${e.message || e}` } // liefert true, wenn VictoriaMetrics die Daten uebernommen hat, sonst false export const victoriaWrite = async (data) => { let start = DateTime.now() let ok = false try { const ret = await axios({ method: 'post', url: VICTORIAURL_WRITE, data: data, headers: { Accept: 'application/json', 'Content-Type': 'text/plain; charset=utf-8' }, timeout: 10000, }) if (ret.status != 204) { logerror(`doWrite2API Status: ${ret.status}`) } else { ok = true } } catch (e) { logerror(`doWrite2API ${VICTORIAURL_WRITE} ${describeError(e)}`) } statistics['writeVictoriaData[sensor_data]Time'] = DateTime.now().diff(start, ['seconds']).toObject().seconds logit(`Victoria-Write-Time: ${start.diffNow('seconds').toObject().seconds * -1} sec`) return ok }