signalk-server
Version:
An implementation of a [Signal K](http://signalk.org) server for boats.
91 lines (89 loc) • 3.53 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
* Copyright 2017 Teppo Kurki, Scott Bender
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CONNECTION_WRITE_EVENT_NAME = void 0;
exports.startDeltaStatistics = startDeltaStatistics;
exports.incDeltaStatistics = incDeltaStatistics;
const lodash_1 = require("lodash");
const STATS_UPDATE_INTERVAL_SECONDS = 5;
exports.CONNECTION_WRITE_EVENT_NAME = 'connectionwrite';
class ProviderStats {
constructor() {
this.writeRate =
this.writeCount =
this.lastIntervalWriteCount =
this.deltaRate =
this.deltaCount =
this.lastIntervalDeltaCount =
0;
}
}
function startDeltaStatistics(app) {
app.deltaCount = 0;
app.lastIntervalDeltaCount = 0;
app.providerStatistics = {};
app.on(exports.CONNECTION_WRITE_EVENT_NAME, (msg) => {
const stats = app.providerStatistics[msg.providerId] ||
(app.providerStatistics[msg.providerId] = new ProviderStats());
if (msg.count !== undefined) {
stats.writeCount += msg.count;
}
else {
stats.writeCount++;
}
});
return setInterval(() => {
updateProviderPeriodStats(app);
const anyApp = app;
app.emit('serverevent', {
type: 'SERVERSTATISTICS',
from: 'signalk-server',
data: {
deltaRate: (app.deltaCount - app.lastIntervalDeltaCount) /
STATS_UPDATE_INTERVAL_SECONDS,
numberOfAvailablePaths: anyApp.streambundle.getAvailablePaths().length,
wsClients: anyApp.interfaces.ws ? anyApp.interfaces.ws.numClients() : 0,
providerStatistics: app.providerStatistics,
uptime: process.uptime()
}
});
app.lastIntervalDeltaCount = app.deltaCount;
}, STATS_UPDATE_INTERVAL_SECONDS * 1000);
}
function incDeltaStatistics(app, providerId) {
app.deltaCount++;
const stats = app.providerStatistics[providerId] ||
(app.providerStatistics[providerId] = new ProviderStats());
stats.deltaCount++;
}
function updateProviderPeriodStats(app) {
app.providers.forEach((provider) => {
if ((0, lodash_1.isUndefined)(app.providerStatistics[provider.id])) {
app.providerStatistics[provider.id] = new ProviderStats();
}
});
(0, lodash_1.values)(app.providerStatistics).forEach((stats) => {
stats.deltaRate =
(stats.deltaCount - stats.lastIntervalDeltaCount) /
STATS_UPDATE_INTERVAL_SECONDS;
stats.lastIntervalDeltaCount = stats.deltaCount;
stats.writeRate =
(stats.writeCount - stats.lastIntervalWriteCount) /
STATS_UPDATE_INTERVAL_SECONDS;
stats.lastIntervalWriteCount = stats.writeCount;
});
}