Skip to content

Commit

Permalink
feature: add reportOutputMessages to server plugin API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tkurki committed Sep 26, 2023
1 parent 6a48cd7 commit 4940066
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/server-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,21 @@ export interface ServerAPI extends PluginServerApp {
dest: (PointDestination & { arrivalCircle?: number }) | null
) => Promise<void>
activateRoute: (dest: RouteDestination | null) => Promise<void>

/**
* 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
}
9 changes: 7 additions & 2 deletions src/deltastats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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++
})

Expand Down
12 changes: 11 additions & 1 deletion src/interfaces/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4940066

Please sign in to comment.