domotz-remote-pawn
Version:
Domotz Agent
114 lines (102 loc) • 4 kB
JavaScript
/**
* This file is part of Domotz Agent.
* Copyright (C) 2020 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* Created by NEC Team
*/
function telemetry(resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
var telemetry = {
discovery: {
counter: 0,
checkEmergencyInterfaceEnabled: [],
'bindingDiscoverer.discoverAll': [],
'interfacesBinding.setFreshDiscoveredBinding': [],
'discoveryRetry.retryOnStaleBindings': [],
'engineReportingStrategy.reportDiscovery': [],
'interfacesBinding.purgeStale': [],
'interfacesBinding.saveLastSnapshot': [],
'event.endFastDeviceDiscovery': [],
},
};
/**
* Reports telemetry[module] object (currently only logged) and resets telemetry[module] object
* The report skips the counter value, if present
*
* @param {string} module - the module of telemetry object to be updated
* @param {string} phase - the phase of the module in the telemetry object to be updated
* @param {number} initialTime - reference time to the previous phase event
* @returns {number} - the current time of the phase event
*/
function updateTelemetry(module, phase, initialTime) {
var now = new Date().getTime();
if (telemetry[module].hasOwnProperty(phase)) {
telemetry[module][phase].push(now - initialTime);
} else {
myConsole.error('Phase %s does not exist in telemetry.%s', phase, module);
}
return now;
}
function getTelemetryCounter(module) {
return telemetry[module].counter;
}
function incTelemetryCounter(module) {
return telemetry[module].counter++;
}
/**
* Resets telemetry[module] object to default values, i.e.
* numeric values --> 0
* array --> []
*
* @param {string} module - the module of telemetry object to be reset
* @returns {void} no return, the object is modified in-place.
*/
function _resetTelemetry(module) {
Object.keys(telemetry[module]).forEach(function (key) {
if (Array.isArray(telemetry[module][key])) {
telemetry[module][key] = [];
} else if (typeof telemetry[module][key] === 'number') {
telemetry[module][key] = 0;
}
});
}
/**
* Reports telemetry[module] object (currently only logged) and resets telemetry[module] object
* The report skips the counter value, if present
*
* @param {string} module - the module of telemetry object to be reset
* @returns {void} no return, telemetry[module] is reset after report
*/
function reportTelemetry(module) {
var report = JSON.stringify(telemetry[module], function (key, value) {
if (key === 'counter') return undefined;
return value;
});
myConsole.debug('Telemetry[%s]: %s', module, report);
_resetTelemetry(module);
}
function getTelemetry() {
return telemetry;
}
return {
incTelemetryCounter: incTelemetryCounter,
getTelemetryCounter: getTelemetryCounter,
getTelemetry: getTelemetry,
reportTelemetry: reportTelemetry,
updateTelemetry: updateTelemetry,
};
}
module.exports.telemetry = telemetry;