working
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
const { outputUtils } = require('generalFunctions');
|
||||
const Specific = require('./specificClass');
|
||||
|
||||
/**
|
||||
* Node-RED wrapper for dashboard generation requests.
|
||||
* It listens for `registerChild` messages and emits Grafana upsert requests.
|
||||
*/
|
||||
class nodeClass {
|
||||
constructor(uiConfig, RED, nodeInstance, nameOfNode) {
|
||||
this.node = nodeInstance;
|
||||
@@ -44,7 +48,7 @@ class nodeClass {
|
||||
this.node.on('input', async (msg, send, done) => {
|
||||
try {
|
||||
if (msg.topic !== 'registerChild') {
|
||||
done();
|
||||
if (typeof done === 'function') done();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -52,9 +56,12 @@ class nodeClass {
|
||||
const childObj = this.RED.nodes.getNode(childId);
|
||||
const childSource = childObj?.source;
|
||||
if (!childSource?.config) {
|
||||
throw new Error(`Missing child source/config for id=${childId}`);
|
||||
this.node.warn(`registerChild skipped: missing child source/config for id=${childId}`);
|
||||
if (typeof done === 'function') done();
|
||||
return;
|
||||
}
|
||||
|
||||
// Generate one dashboard for the root source and optionally its registered children.
|
||||
const dashboards = this.source.generateDashboardsForGraph(childSource, {
|
||||
includeChildren: Boolean(msg.includeChildren ?? true),
|
||||
});
|
||||
@@ -69,6 +76,7 @@ class nodeClass {
|
||||
}
|
||||
|
||||
for (const dash of dashboards) {
|
||||
// Forward dashboard definitions to an HTTP request node configured for Grafana API.
|
||||
const payload = this.source.buildUpsertRequest({ dashboard: dash.dashboard, folderId: 0, overwrite: true });
|
||||
send({
|
||||
topic: 'grafana.dashboard.upsert',
|
||||
@@ -85,19 +93,20 @@ class nodeClass {
|
||||
});
|
||||
}
|
||||
|
||||
done();
|
||||
if (typeof done === 'function') done();
|
||||
} catch (error) {
|
||||
this.node.status({ fill: 'red', shape: 'ring', text: 'dashboardapi error' });
|
||||
this.node.error(error?.message || error, msg);
|
||||
done(error);
|
||||
if (typeof done === 'function') done();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_attachCloseHandler() {
|
||||
this.node.on('close', (done) => done());
|
||||
this.node.on('close', (done) => {
|
||||
if (typeof done === 'function') done();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = nodeClass;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user