Fix dashboardapi adapter and Jest coverage
This commit is contained in:
@@ -6,6 +6,8 @@ class nodeClass {
|
|||||||
this.node = nodeInstance;
|
this.node = nodeInstance;
|
||||||
this.RED = RED;
|
this.RED = RED;
|
||||||
this.name = nameOfNode;
|
this.name = nameOfNode;
|
||||||
|
this.source = null;
|
||||||
|
this.config = null;
|
||||||
|
|
||||||
this._loadConfig(uiConfig);
|
this._loadConfig(uiConfig);
|
||||||
this._setupSpecificClass();
|
this._setupSpecificClass();
|
||||||
@@ -21,11 +23,12 @@ class nodeClass {
|
|||||||
role: 'auto ui generator',
|
role: 'auto ui generator',
|
||||||
},
|
},
|
||||||
grafanaConnector: {
|
grafanaConnector: {
|
||||||
host: uiConfig.host || 'localhost',
|
|
||||||
port: Number(uiConfig.port) || 3000,
|
|
||||||
protocol: uiConfig.protocol || 'http',
|
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;
|
return runtimeNode || flowNode || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_resolveChildConfig(payload) {
|
_resolveChildSource(payload) {
|
||||||
if (payload?.source?.config) {
|
if (payload?.source?.config) {
|
||||||
return payload.source.config;
|
return payload.source;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (payload?.config) {
|
if (payload?.config) {
|
||||||
return payload.config;
|
return { config: payload.config };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof payload === 'string') {
|
if (typeof payload === 'string') {
|
||||||
return this._resolveChildNode(payload)?.source?.config || null;
|
return this._resolveChildNode(payload)?.source || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -67,45 +70,63 @@ class nodeClass {
|
|||||||
_attachInputHandler() {
|
_attachInputHandler() {
|
||||||
this.node.on('input', async (msg, send, done) => {
|
this.node.on('input', async (msg, send, done) => {
|
||||||
try {
|
try {
|
||||||
switch (msg.topic) {
|
if (msg.topic !== 'registerChild') {
|
||||||
case 'registerChild': {
|
if (typeof done === 'function') done();
|
||||||
const childConfig = this._resolveChildConfig(msg.payload);
|
return;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
} catch (error) {
|
||||||
this.node.status({ fill: 'red', shape: 'ring', text: 'Bad request data' });
|
this.node.status({ fill: 'red', shape: 'ring', text: 'dashboardapi error' });
|
||||||
this.node.error(`Bad request data: ${error.message}`, msg);
|
this.node.error(error?.message || error, msg);
|
||||||
done(error);
|
if (typeof done === 'function') done(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_attachCloseHandler() {
|
_attachCloseHandler() {
|
||||||
this.node.on('close', (done) => {
|
this.node.on('close', (done) => {
|
||||||
done();
|
if (typeof done === 'function') done();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
const test = require('node:test');
|
describe('dashboardAPI basic structure', () => {
|
||||||
const assert = require('node:assert/strict');
|
it('module load smoke', () => {
|
||||||
|
expect(() => {
|
||||||
test('dashboardAPI module load smoke', () => {
|
require('../../dashboardapi.js');
|
||||||
assert.doesNotThrow(() => {
|
}).not.toThrow();
|
||||||
require('../../dashboardapi.js');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
|
|
||||||
const DashboardApi = require('../src/specificClass');
|
const DashboardApi = require('../src/specificClass');
|
||||||
|
|
||||||
function makeNodeSource({ id, name, softwareType, positionVsParent, children = [] }) {
|
function makeNodeSource({ id, name, softwareType, positionVsParent, children = [] }) {
|
||||||
@@ -24,59 +21,61 @@ function makeNodeSource({ id, name, softwareType, positionVsParent, children = [
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
test('buildDashboard sets id=null, stable uid, title, measurement and bucket vars', () => {
|
describe('DashboardApi specificClass', () => {
|
||||||
const api = new DashboardApi({
|
it('buildDashboard sets id=null, stable uid, title, measurement and bucket vars', () => {
|
||||||
general: { name: 'dashboardapi-test', logging: { enabled: false, logLevel: 'error' } },
|
const api = new DashboardApi({
|
||||||
grafanaConnector: { protocol: 'http', host: 'localhost', port: 3000, bearerToken: '' },
|
general: { name: 'dashboardapi-test', logging: { enabled: false, logLevel: 'error' } },
|
||||||
|
grafanaConnector: { protocol: 'http', host: 'localhost', port: 3000, bearerToken: '' },
|
||||||
|
});
|
||||||
|
|
||||||
|
const nodeSource = makeNodeSource({
|
||||||
|
id: 'm-1',
|
||||||
|
name: 'PT-1',
|
||||||
|
softwareType: 'measurement',
|
||||||
|
positionVsParent: 'downstream',
|
||||||
|
});
|
||||||
|
|
||||||
|
const dash = api.buildDashboard({ nodeConfig: nodeSource.config, positionVsParent: 'downstream' });
|
||||||
|
|
||||||
|
expect(dash.dashboard.id).toBeNull();
|
||||||
|
expect(dash.uid).toHaveLength(12);
|
||||||
|
expect(dash.dashboard.uid).toBe(dash.uid);
|
||||||
|
expect(dash.dashboard.title).toBe('PT-1');
|
||||||
|
|
||||||
|
const templ = dash.dashboard.templating.list;
|
||||||
|
const measurement = templ.find((v) => v.name === 'measurement');
|
||||||
|
const bucket = templ.find((v) => v.name === 'bucket');
|
||||||
|
|
||||||
|
expect(measurement.current.value).toBe('measurement_m-1');
|
||||||
|
expect(bucket.current.value).toBe('lvl3');
|
||||||
});
|
});
|
||||||
|
|
||||||
const nodeSource = makeNodeSource({
|
it('generateDashboardsForGraph returns root + direct child dashboards and adds links', () => {
|
||||||
id: 'm-1',
|
const api = new DashboardApi({
|
||||||
name: 'PT-1',
|
general: { name: 'dashboardapi-test', logging: { enabled: false, logLevel: 'error' } },
|
||||||
softwareType: 'measurement',
|
grafanaConnector: { protocol: 'http', host: 'localhost', port: 3000, bearerToken: '' },
|
||||||
positionVsParent: 'downstream',
|
});
|
||||||
|
|
||||||
|
const child = makeNodeSource({
|
||||||
|
id: 'c-1',
|
||||||
|
name: 'ChildSensor',
|
||||||
|
softwareType: 'measurement',
|
||||||
|
positionVsParent: 'upstream',
|
||||||
|
});
|
||||||
|
|
||||||
|
const root = makeNodeSource({
|
||||||
|
id: 'p-1',
|
||||||
|
name: 'ParentMachine',
|
||||||
|
softwareType: 'machine',
|
||||||
|
positionVsParent: 'atEquipment',
|
||||||
|
children: [child],
|
||||||
|
});
|
||||||
|
|
||||||
|
const results = api.generateDashboardsForGraph(root, { includeChildren: true });
|
||||||
|
expect(results).toHaveLength(2);
|
||||||
|
|
||||||
|
const rootDash = results[0];
|
||||||
|
expect(Array.isArray(rootDash.dashboard.links)).toBe(true);
|
||||||
|
expect(rootDash.dashboard.links.some((l) => l.url && l.url.includes('/d/'))).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
const dash = api.buildDashboard({ nodeConfig: nodeSource.config, positionVsParent: 'downstream' });
|
|
||||||
|
|
||||||
assert.equal(dash.dashboard.id, null);
|
|
||||||
assert.equal(dash.uid.length, 12);
|
|
||||||
assert.equal(dash.dashboard.uid, dash.uid);
|
|
||||||
assert.equal(dash.dashboard.title, 'PT-1');
|
|
||||||
|
|
||||||
const templ = dash.dashboard.templating.list;
|
|
||||||
const measurement = templ.find((v) => v.name === 'measurement');
|
|
||||||
const bucket = templ.find((v) => v.name === 'bucket');
|
|
||||||
|
|
||||||
assert.equal(measurement.current.value, 'measurement_m-1');
|
|
||||||
assert.equal(bucket.current.value, 'lvl3');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('generateDashboardsForGraph returns root + direct child dashboards and adds links', () => {
|
|
||||||
const api = new DashboardApi({
|
|
||||||
general: { name: 'dashboardapi-test', logging: { enabled: false, logLevel: 'error' } },
|
|
||||||
grafanaConnector: { protocol: 'http', host: 'localhost', port: 3000, bearerToken: '' },
|
|
||||||
});
|
|
||||||
|
|
||||||
const child = makeNodeSource({
|
|
||||||
id: 'c-1',
|
|
||||||
name: 'ChildSensor',
|
|
||||||
softwareType: 'measurement',
|
|
||||||
positionVsParent: 'upstream',
|
|
||||||
});
|
|
||||||
|
|
||||||
const root = makeNodeSource({
|
|
||||||
id: 'p-1',
|
|
||||||
name: 'ParentMachine',
|
|
||||||
softwareType: 'machine',
|
|
||||||
positionVsParent: 'atEquipment',
|
|
||||||
children: [child],
|
|
||||||
});
|
|
||||||
|
|
||||||
const results = api.generateDashboardsForGraph(root, { includeChildren: true });
|
|
||||||
assert.equal(results.length, 2);
|
|
||||||
|
|
||||||
const rootDash = results[0];
|
|
||||||
assert.ok(Array.isArray(rootDash.dashboard.links));
|
|
||||||
assert.ok(rootDash.dashboard.links.some((l) => l.url && l.url.includes('/d/')));
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
const fs = require('node:fs');
|
const fs = require('node:fs');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
|
|
||||||
const flow = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../../examples/basic.flow.json'), 'utf8'));
|
const flow = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../../examples/basic.flow.json'), 'utf8'));
|
||||||
|
|
||||||
test('basic example includes node type dashboardapi', () => {
|
describe('dashboardAPI edge example structure', () => {
|
||||||
|
it('basic example includes node type dashboardapi', () => {
|
||||||
const count = flow.filter((n) => n && n.type === 'dashboardapi').length;
|
const count = flow.filter((n) => n && n.type === 'dashboardapi').length;
|
||||||
assert.equal(count >= 1, true);
|
expect(count).toBeGreaterThanOrEqual(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
const fs = require('node:fs');
|
const fs = require('node:fs');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
|
|
||||||
@@ -9,15 +7,17 @@ function loadJson(file) {
|
|||||||
return JSON.parse(fs.readFileSync(path.join(dir, file), 'utf8'));
|
return JSON.parse(fs.readFileSync(path.join(dir, file), 'utf8'));
|
||||||
}
|
}
|
||||||
|
|
||||||
test('examples package exists for dashboardAPI', () => {
|
describe('dashboardAPI integration examples', () => {
|
||||||
for (const file of ['README.md', 'basic.flow.json', 'integration.flow.json', 'edge.flow.json']) {
|
it('examples package exists for dashboardAPI', () => {
|
||||||
assert.equal(fs.existsSync(path.join(dir, file)), true, file + ' missing');
|
for (const file of ['README.md', 'basic.flow.json', 'integration.flow.json', 'edge.flow.json']) {
|
||||||
}
|
expect(fs.existsSync(path.join(dir, file))).toBe(true);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
test('example flows are parseable arrays for dashboardAPI', () => {
|
it('example flows are parseable arrays for dashboardAPI', () => {
|
||||||
for (const file of ['basic.flow.json', 'integration.flow.json', 'edge.flow.json']) {
|
for (const file of ['basic.flow.json', 'integration.flow.json', 'edge.flow.json']) {
|
||||||
const parsed = loadJson(file);
|
const parsed = loadJson(file);
|
||||||
assert.equal(Array.isArray(parsed), true);
|
expect(Array.isArray(parsed)).toBe(true);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,7 +5,19 @@ jest.mock('../src/specificClass', () => {
|
|||||||
logger: {
|
logger: {
|
||||||
warn: jest.fn(),
|
warn: jest.fn(),
|
||||||
},
|
},
|
||||||
generateDashB: jest.fn().mockResolvedValue({ dashboard: { title: 'ok' } }),
|
generateDashboardsForGraph: jest.fn(() => [{
|
||||||
|
dashboard: { title: 'ok' },
|
||||||
|
nodeId: 'child-node-id',
|
||||||
|
softwareType: 'measurement',
|
||||||
|
uid: 'child-uid',
|
||||||
|
title: 'ok',
|
||||||
|
}]),
|
||||||
|
buildUpsertRequest: jest.fn(({ dashboard, folderId, overwrite }) => ({
|
||||||
|
dashboard,
|
||||||
|
folderId,
|
||||||
|
overwrite,
|
||||||
|
})),
|
||||||
|
grafanaUpsertUrl: jest.fn(() => 'http://grafana:3000/api/dashboards/db'),
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -77,7 +89,13 @@ describe('dashboardAPI nodeClass', () => {
|
|||||||
expect(send).toHaveBeenCalledWith(
|
expect(send).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
topic: 'create',
|
topic: 'create',
|
||||||
payload: { dashboard: { title: 'ok' } },
|
url: 'http://grafana:3000/api/dashboards/db',
|
||||||
|
method: 'POST',
|
||||||
|
payload: {
|
||||||
|
dashboard: { title: 'ok' },
|
||||||
|
folderId: 0,
|
||||||
|
overwrite: true,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
expect(done).toHaveBeenCalledWith();
|
expect(done).toHaveBeenCalledWith();
|
||||||
@@ -109,7 +127,11 @@ describe('dashboardAPI nodeClass', () => {
|
|||||||
expect(send).toHaveBeenCalledWith(
|
expect(send).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
topic: 'create',
|
topic: 'create',
|
||||||
payload: { dashboard: { title: 'ok' } },
|
payload: {
|
||||||
|
dashboard: { title: 'ok' },
|
||||||
|
folderId: 0,
|
||||||
|
overwrite: true,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
expect(done).toHaveBeenCalledWith();
|
expect(done).toHaveBeenCalledWith();
|
||||||
@@ -145,7 +167,11 @@ describe('dashboardAPI nodeClass', () => {
|
|||||||
expect(send).toHaveBeenCalledWith(
|
expect(send).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
topic: 'create',
|
topic: 'create',
|
||||||
payload: { dashboard: { title: 'ok' } },
|
payload: {
|
||||||
|
dashboard: { title: 'ok' },
|
||||||
|
folderId: 0,
|
||||||
|
overwrite: true,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
expect(done).toHaveBeenCalledWith();
|
expect(done).toHaveBeenCalledWith();
|
||||||
|
|||||||
Reference in New Issue
Block a user