From 49400669116c22622bc721552c89f58ab9e356a8 Mon Sep 17 00:00:00 2001 From: Teppo Kurki Date: Tue, 26 Sep 2023 21:41:44 +0300 Subject: [PATCH] feature: add reportOutputMessages to server plugin API Add a way for plugins to report that they are handling outgoing (out = from the server to the outside world) traffic to update the output rate & icon on the dashboard. --- packages/server-api/src/index.ts | 17 +++++++++++++++++ src/deltastats.ts | 9 +++++++-- src/interfaces/plugins.ts | 12 +++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packages/server-api/src/index.ts b/packages/server-api/src/index.ts index 208ce5569..a73c08c04 100644 --- a/packages/server-api/src/index.ts +++ b/packages/server-api/src/index.ts @@ -178,4 +178,21 @@ export interface ServerAPI extends PluginServerApp { dest: (PointDestination & { arrivalCircle?: number }) | null ) => Promise activateRoute: (dest: RouteDestination | null) => Promise + + /** + * A plugin can report that it has handled output messages. This will + * update the output message rate and icon in the Dashboard. + * + * This is for traffic that the plugin is sending outside the server, + * for example network packets, http calls or messages sent to + * a broker. This should NOT be used for deltas that the plugin + * sends with handleMessage, they are reported as input from the + * server's perspective. + * + * @param count optional count of handled messages between the last + * call and this one. If omitted the call will count as one output + * message. + */ + + reportOutputMessages: (count?: number) => void } diff --git a/src/deltastats.ts b/src/deltastats.ts index 1933380f4..fc7366d9f 100644 --- a/src/deltastats.ts +++ b/src/deltastats.ts @@ -19,9 +19,11 @@ import { isUndefined, values } from 'lodash' import { EventEmitter } from 'node:events' const STATS_UPDATE_INTERVAL_SECONDS = 5 +export const CONNECTION_WRITE_EVENT_NAME = 'connectionwrite' -interface ConnectionWriteEvent { +export interface ConnectionWriteEvent { providerId: string + count?: number } class ProviderStats { @@ -57,10 +59,13 @@ export function startDeltaStatistics( app.lastIntervalDeltaCount = 0 app.providerStatistics = {} - app.on('connectionwrite', (msg: ConnectionWriteEvent) => { + app.on(CONNECTION_WRITE_EVENT_NAME, (msg: ConnectionWriteEvent) => { const stats = app.providerStatistics[msg.providerId] || (app.providerStatistics[msg.providerId] = new ProviderStats()) + if (msg.count !== undefined) { + stats.writeCount += msg.count + } stats.writeCount++ }) diff --git a/src/interfaces/plugins.ts b/src/interfaces/plugins.ts index 7c09f2739..ec245f58b 100644 --- a/src/interfaces/plugins.ts +++ b/src/interfaces/plugins.ts @@ -40,6 +40,10 @@ const debug = createDebug('signalk-server:interfaces:plugins') import { modulesWithKeyword } from '../modules' import { OpenApiDescription, OpenApiRecord } from '../api/swagger' +import { + CONNECTION_WRITE_EVENT_NAME, + ConnectionWriteEvent +} from '../deltastats' const put = require('../put') const _putPath = put.putPath @@ -502,7 +506,13 @@ module.exports = (theApp: any) => { }, getSerialPorts, supportsMetaDeltas: true, - getMetadata + getMetadata, + reportOutputMessages: (count?: number) => { + app.emit(CONNECTION_WRITE_EVENT_NAME, { + providerId: plugin.id, + count + } as ConnectionWriteEvent) + } }) appCopy.putPath = putPath