Browse Source

Add helper method to get a string representation of an AverageStatValue

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
pull/5884/head
Daniel Calviño Sánchez 4 years ago
committed by Joas Schilling
parent
commit
fde0e7601d
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 19
      src/utils/webrtc/analyzers/AverageStatValue.js
  2. 37
      src/utils/webrtc/analyzers/AverageStatValue.spec.js

19
src/utils/webrtc/analyzers/AverageStatValue.js

@ -55,6 +55,9 @@ const STAT_VALUE_TYPE = {
* the raw value that was added or the relative one after the conversion (which,
* for non cumulative values, will be the raw value too).
*
* A string representation of the current relative values can be got by calling
* "toString()".
*
* @param {int} count the number of instances to take into account.
* @param {STAT_VALUE_TYPE} type whether the value is cumulative or relative.
* @param {int} lastValueWeight the value to calculate the weights of all the
@ -127,6 +130,22 @@ AverageStatValue.prototype = {
return weightedValues / weightsSum
},
toString() {
if (!this._relativeValues.length) {
return '[]'
}
let relativeValuesAsString = '[' + this._relativeValues[0]
for (let i = 1; i < this._relativeValues.length; i++) {
relativeValuesAsString += ', ' + this._relativeValues[i]
}
relativeValuesAsString += ']'
return relativeValuesAsString
},
}
export {

37
src/utils/webrtc/analyzers/AverageStatValue.spec.js

@ -35,6 +35,43 @@ describe('AverageStatValue', () => {
})
})
describe('to string', () => {
test('no values', () => {
const stat = new AverageStatValue(3, STAT_VALUE_TYPE.CUMULATIVE, 3)
const stat2 = new AverageStatValue(3, STAT_VALUE_TYPE.RELATIVE, 3)
expect(stat.toString()).toBe('[]')
expect(stat2.toString()).toBe('[]')
})
test('single value', () => {
const stat = new AverageStatValue(3, STAT_VALUE_TYPE.CUMULATIVE, 3)
const stat2 = new AverageStatValue(3, STAT_VALUE_TYPE.RELATIVE, 3)
stat.add(42)
stat2.add(42)
// The first cumulative value is treated as 0 as it is the base from
// which the rest of the values will be calculated.
expect(stat.toString()).toBe('[0]')
expect(stat2.toString()).toBe('[42]')
})
test('several values', () => {
const testValues = [100, 200, 150, 123, 30, 50, 22, 33]
const stat = new AverageStatValue(3, STAT_VALUE_TYPE.CUMULATIVE, 3)
const stat2 = new AverageStatValue(3, STAT_VALUE_TYPE.RELATIVE, 3)
testValues.forEach((val, index) => {
stat.add(val)
stat2.add(val)
})
expect(stat.toString()).toBe('[20, -28, 11]')
expect(stat2.toString()).toBe('[50, 22, 33]')
})
})
describe('cumulative average', () => {
test('returns the last relative value', () => {
const testValues = [

Loading…
Cancel
Save