Fix dashboardapi adapter and Jest coverage
This commit is contained in:
@@ -6,6 +6,8 @@ class nodeClass {
|
||||
this.node = nodeInstance;
|
||||
this.RED = RED;
|
||||
this.name = nameOfNode;
|
||||
this.source = null;
|
||||
this.config = null;
|
||||
|
||||
this._loadConfig(uiConfig);
|
||||
this._setupSpecificClass();
|
||||
@@ -21,11 +23,12 @@ class nodeClass {
|
||||
role: 'auto ui generator',
|
||||
},
|
||||
grafanaConnector: {
|
||||
host: uiConfig.host || 'localhost',
|
||||
port: Number(uiConfig.port) || 3000,
|
||||
protocol: uiConfig.protocol || 'http',
|
||||
bearerToken: uiConfig.bearerToken || null,
|
||||
host: uiConfig.host || 'localhost',
|
||||
port: Number(uiConfig.port || 3000),
|
||||
bearerToken: uiConfig.bearerToken || '',
|
||||
},
|
||||
defaultBucket: uiConfig.defaultBucket || process.env.INFLUXDB_BUCKET || '',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -48,17 +51,17 @@ class nodeClass {
|
||||
return runtimeNode || flowNode || null;
|
||||
}
|
||||
|
||||
_resolveChildConfig(payload) {
|
||||
_resolveChildSource(payload) {
|
||||
if (payload?.source?.config) {
|
||||
return payload.source.config;
|
||||
return payload.source;
|
||||
}
|
||||
|
||||
if (payload?.config) {
|
||||
return payload.config;
|
||||
return { config: payload.config };
|
||||
}
|
||||
|
||||
if (typeof payload === 'string') {
|
||||
return this._resolveChildNode(payload)?.source?.config || null;
|
||||
return this._resolveChildNode(payload)?.source || null;
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -67,45 +70,63 @@ class nodeClass {
|
||||
_attachInputHandler() {
|
||||
this.node.on('input', async (msg, send, done) => {
|
||||
try {
|
||||
switch (msg.topic) {
|
||||
case 'registerChild': {
|
||||
const childConfig = this._resolveChildConfig(msg.payload);
|
||||
if (!childConfig) {
|
||||
throw new Error('Missing or invalid child node');
|
||||
}
|
||||
|
||||
const payload = await this.source.generateDashB(childConfig);
|
||||
const authToken = process.env.GRAFANA_TOKEN || this.config.grafanaConnector.bearerToken || '';
|
||||
|
||||
send({
|
||||
...msg,
|
||||
topic: 'create',
|
||||
payload,
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
},
|
||||
});
|
||||
break;
|
||||
}
|
||||
default:
|
||||
this.source.logger.warn(`Unknown topic: ${msg.topic}`);
|
||||
break;
|
||||
if (msg.topic !== 'registerChild') {
|
||||
if (typeof done === 'function') done();
|
||||
return;
|
||||
}
|
||||
|
||||
done();
|
||||
const childSource = this._resolveChildSource(msg.payload);
|
||||
if (!childSource?.config) {
|
||||
throw new Error('Missing or invalid child node');
|
||||
}
|
||||
|
||||
const dashboards = this.source.generateDashboardsForGraph(childSource, {
|
||||
includeChildren: Boolean(msg.includeChildren ?? true),
|
||||
});
|
||||
|
||||
const url = this.source.grafanaUpsertUrl();
|
||||
const headers = {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
if (this.config.grafanaConnector.bearerToken) {
|
||||
headers.Authorization = `Bearer ${this.config.grafanaConnector.bearerToken}`;
|
||||
}
|
||||
|
||||
for (const dash of dashboards) {
|
||||
send({
|
||||
...msg,
|
||||
topic: 'create',
|
||||
url,
|
||||
method: 'POST',
|
||||
headers,
|
||||
payload: this.source.buildUpsertRequest({
|
||||
dashboard: dash.dashboard,
|
||||
folderId: 0,
|
||||
overwrite: true,
|
||||
}),
|
||||
meta: {
|
||||
nodeId: dash.nodeId,
|
||||
softwareType: dash.softwareType,
|
||||
uid: dash.uid,
|
||||
title: dash.title,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof done === 'function') done();
|
||||
} catch (error) {
|
||||
this.node.status({ fill: 'red', shape: 'ring', text: 'Bad request data' });
|
||||
this.node.error(`Bad request data: ${error.message}`, msg);
|
||||
done(error);
|
||||
this.node.status({ fill: 'red', shape: 'ring', text: 'dashboardapi error' });
|
||||
this.node.error(error?.message || error, msg);
|
||||
if (typeof done === 'function') done(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_attachCloseHandler() {
|
||||
this.node.on('close', (done) => {
|
||||
done();
|
||||
if (typeof done === 'function') done();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user