domotz-remote-pawn
Version:
Domotz Agent
116 lines (95 loc) • 4.25 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2023 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/>.
*
*
* Sends the counters of DHCP requests to Sonic,
* Created by Stefano Cicero <stefano.cicero@domotz.com> on 19/06/23.
*/
var lastReportTs = null;
var previousReportData = null;
var dhcpRequestsReport = function (resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
var time = resourceLocator.utils.time;
var lodash = resourceLocator.lodash;
var dhcpRequestsCounters = resourceLocator.DHCPRequestCounters;
if (lastReportTs === null) {
lastReportTs = time.getTimestamp();
return null;
}
const ONE_HOUR = 3600;
function getHourlyRate(samplesCount, seconds) {
return samplesCount / (seconds / ONE_HOUR);
}
var dhcpRateCalculationWindowS = resourceLocator.settingsManager.getSetting(
'configuration.settings.dhcp_rate_calculation_window_s',
ONE_HOUR * 4
);
var dhcpRateMinReportPeriodS = resourceLocator.settingsManager.getSetting('configuration.settings.dhcp_rate_min_report_period_s', ONE_HOUR);
var ratioThr = resourceLocator.settingsManager.getSetting('configuration.settings.dhcp_rate_calculation_threshold_ratio', 2);
var diffThr = resourceLocator.settingsManager.getSetting('configuration.settings.dhcp_rate_calculation_threshold_diff', 30);
var currentTs = time.getTimestamp();
var timeSinceLastRun = currentTs - lastReportTs;
function getReportData() {
var fromTs = currentTs - dhcpRateCalculationWindowS;
var samples = dhcpRequestsCounters.getDHCPRequest(fromTs);
var totalRate = getHourlyRate(samples.length, dhcpRateCalculationWindowS);
var samplesByMac = {};
lodash.forEach(samples, function (sample) {
var mac = sample.mac;
if (samplesByMac[mac]) {
samplesByMac[mac] += 1;
} else {
samplesByMac[mac] = 1;
}
});
var reportData = {
global_rate: totalRate,
rate_per_mac: {},
};
lodash.forOwn(samplesByMac, function (eventsCount, key) {
var rateForMac = getHourlyRate(eventsCount, dhcpRateCalculationWindowS);
if (rateForMac > 0) {
reportData.rate_per_mac[key] = rateForMac;
}
});
return reportData;
}
var reportData = getReportData();
myConsole.debug('DHCP rate stats %s', JSON.stringify(reportData));
var thresholdExceeded = false;
function ratioThresholdExceeded() {
return reportData.global_rate > ratioThr * previousReportData.global_rate;
}
function diffThresholdExceeded() {
return reportData.global_rate - previousReportData.global_rate > diffThr;
}
function relevantIncrement() {
return ratioThresholdExceeded() && diffThresholdExceeded();
}
if (previousReportData && currentTs - lastReportTs > dhcpRateMinReportPeriodS) {
thresholdExceeded = previousReportData.global_rate > 0 && relevantIncrement();
myConsole.debug('threshold exceeded %s', thresholdExceeded);
}
if (timeSinceLastRun > dhcpRateCalculationWindowS || thresholdExceeded) {
myConsole.info('Sending DHCP requests counters to the cloud');
lastReportTs = currentTs;
resourceLocator.engineRequest.sendWithPromise('/dhcp-requests', 'POST', reportData);
} else {
myConsole.info('Skip, time from last run %s', timeSinceLastRun);
}
previousReportData = lodash.clone(reportData);
};
module.exports.handler = dhcpRequestsReport;