UNPKG

domotz-remote-pawn

Version:

Domotz Agent

100 lines (91 loc) 3.57 kB
/** * This file is part of Domotz Agent. * Copyright (C) 2016 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 Tommaso Latini <tommaso@domotz.com> on 10/09/15. * * See projects hub for detailed documentation of domotz_speed_test. * For this module it's sufficient to know that it prints the following: * * *) stdout ==> JSON summary * *) stderr ==> LOG and progresses */ const MODULE = 'MLAB'; const SPEED_CONVERSION_FACTOR = 1e3; const LATENCY_CONVERSION_FACTOR = 1e-3; module.exports.factory = function (q, path, procUtils, file, log) { var myConsole = log.decorateLogs(); var command = path.join(process.env.DOMOTZ_BIN_DIR, 'domotz_speed_test'); function parseOutput(data) { var deferred = q.defer(); try { var parsed = JSON.parse(data); deferred.resolve(parsed); } catch (error) { deferred.reject('parsing error'); } myConsole.info('%s: Results ==>\n%s', MODULE, data); return deferred.promise; } function formatOutput(mlabResult) { var testKeys = mlabResult.test_keys; return { raw: testKeys, server: { url: testKeys[testKeys.simple.fastest_test].server_address, sponsor: 'MLAB', url2: testKeys[testKeys.simple.fastest_test].server_address, }, 'speed.upload': testKeys.simple.upload * SPEED_CONVERSION_FACTOR, 'speed.download': testKeys.simple.download * SPEED_CONVERSION_FACTOR, MaxRTT: testKeys.advanced.max_rtt * LATENCY_CONVERSION_FACTOR, MinRTT: testKeys.advanced.min_rtt * LATENCY_CONVERSION_FACTOR, rttsec: testKeys.advanced.avg_rtt * LATENCY_CONVERSION_FACTOR, }; } function isAvailable() { if (!file.getFileStats(command)) { myConsole.warn('%s: cannot stat %s, mlab is not available', MODULE, command); return false; } return true; } return { performSpeedTest: function () { return procUtils .exec(command, { onStderr: stdErrCallback }) .then(parseOutput) .catch(function (err) { return { status: 500, message: err.message }; }); }, formatOutput: formatOutput, isAvailable: isAvailable, _parseOutput: parseOutput, _stdErrCallback: stdErrCallback, }; function stdErrCallback(data, deferred) { const logRow = data.toString(); myConsole.debug('stderr output: %s', logRow); if (logRow.indexOf('Starting upload') !== -1) { deferred.notify('upload'); } else if (logRow.indexOf('Ending upload') !== -1) { deferred.notify('download'); } else if (logRow.indexOf('Send results to test server') !== -1) { deferred.notify('pause'); } } };