12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /* global APP, require */
- /* jshint -W101 */
- import EventEmitter from "events";
-
- import CQEvents from "../../service/connectionquality/CQEvents";
-
- const eventEmitter = new EventEmitter();
-
- /**
- * local stats
- * @type {{}}
- */
- var stats = {};
-
- /**
- * remote stats
- * @type {{}}
- */
- var remoteStats = {};
-
- /**
- * Quality percent( 100% - good, 0% - bad.) for the local user.
- */
- var localConnectionQuality = 100;
-
- /**
- * Quality percent( 100% - good, 0% - bad.) stored per id.
- */
- var remoteConnectionQuality = {};
-
- /**
- * Calculates the quality percent based on passed new and old value.
- * @param newVal the new value
- * @param oldVal the old value
- */
- function calculateQuality(newVal, oldVal) {
- return (newVal <= oldVal) ? newVal : (9*oldVal + newVal) / 10;
- }
-
- export default {
- /**
- * Updates the local statistics
- * @param data new statistics
- */
- updateLocalStats: function (data) {
- stats = data;
- var newVal = 100 - stats.packetLoss.total;
- localConnectionQuality =
- calculateQuality(newVal, localConnectionQuality);
- eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, localConnectionQuality,
- stats);
- },
-
- /**
- * Updates remote statistics
- * @param id the id associated with the statistics
- * @param data the statistics
- */
- updateRemoteStats: function (id, data) {
- if (!data || !("packetLoss" in data) || !("total" in data.packetLoss)) {
- eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null);
- return;
- }
- remoteStats[id] = data;
-
- var newVal = 100 - data.packetLoss.total;
- var oldVal = remoteConnectionQuality[id];
- remoteConnectionQuality[id] = calculateQuality(newVal, oldVal || 100);
-
- eventEmitter.emit(
- CQEvents.REMOTESTATS_UPDATED, id, remoteConnectionQuality[id],
- remoteStats[id]);
- },
-
- /**
- * Returns the local statistics.
- */
- getStats: function () {
- return stats;
- },
-
- addListener: function (type, listener) {
- eventEmitter.on(type, listener);
- }
- };
|