Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Include bytes in total rates
Browse files Browse the repository at this point in the history
This is to give more weight to reports that have more bytes and less weight
to reports that have less bytes.
  • Loading branch information
Braydon Fuller committed Dec 6, 2017
1 parent 11fe0f3 commit c1639f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
14 changes: 10 additions & 4 deletions lib/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,12 @@ UserSchema.methods.exceedsUnknownReportsThreshold = function(threshold) {
*
* @param {Boolean} unknown
* @param {Date} timestamp
* @param {Number} bytes
* @param {Function} callback
*/
UserSchema.methods.updateUnknownReports = function(unknown,
timestamp,
bytes,
callback) {

assert(typeof unknown === 'boolean', 'unknown is expected to be boolean');
Expand All @@ -407,9 +409,9 @@ UserSchema.methods.updateUnknownReports = function(unknown,

const defaultDelta = 5000;

let totalPrevDelta = 0;
let totalPrevRate = 0;
if (this.reports && this.reports.totalRate) {
totalPrevDelta = this.reports.totalRate;
totalPrevRate = this.reports.totalRate;
}
let totalPrevTimestamp = 0;
if (this.reports && this.reports.totalRateTimestamp) {
Expand All @@ -422,12 +424,16 @@ UserSchema.methods.updateUnknownReports = function(unknown,
return callback();
}

if (!totalPrevTimestamp) {
// Prevent totalDelta being negative, 0, null, undefined or NaN
if (!totalPrevTimestamp || timestamp <= totalPrevTimestamp) {
totalPrevTimestamp = timestamp - defaultDelta;
}

// Also keep track of the number of bytes being reported
// to give larger weight to larger reports
const totalDelta = timestamp - totalPrevTimestamp;
const totalRate = ema(totalPrevDelta, totalDelta);
const totalBandwidth = bytes / totalDelta;
const totalRate = ema(totalPrevRate, totalBandwidth);

let update = {
$set: {
Expand Down
10 changes: 5 additions & 5 deletions test/user.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe('Storage/models/User', function() {
});
});

describe('#updateUnknownReports', function() {
describe.only('#updateUnknownReports', function() {
it('it will update report rates without pre-existing data', function(done) {
var user = new User({
_id: 'testreporter1@user.tld',
Expand All @@ -300,7 +300,7 @@ describe('Storage/models/User', function() {

const now = new Date();

user.updateUnknownReports(false, now, (err) => {
user.updateUnknownReports(false, now, 100000000, (err) => {
if (err) {
return done(err);
}
Expand Down Expand Up @@ -338,7 +338,7 @@ describe('Storage/models/User', function() {
return done(err);
}

user.updateUnknownReports(true, now, (err) => {
user.updateUnknownReports(true, now, 100000000, (err) => {
if (err) {
return done(err);
}
Expand Down Expand Up @@ -377,7 +377,7 @@ describe('Storage/models/User', function() {
return done(err);
}

user.updateUnknownReports(true, now, (err) => {
user.updateUnknownReports(true, now, 100000000, (err) => {
if (err) {
return done(err);
}
Expand Down Expand Up @@ -418,7 +418,7 @@ describe('Storage/models/User', function() {
return done(err);
}

user.updateUnknownReports(false, now, (err) => {
user.updateUnknownReports(false, now, 100000000, (err) => {
if (err) {
return done(err);
}
Expand Down

0 comments on commit c1639f5

Please sign in to comment.