Skip to content

Commit

Permalink
fix: 🐛 not existing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Apr 17, 2020
1 parent 02e1ae9 commit 7f9b5e7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
10 changes: 5 additions & 5 deletions packages/analytics/src/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@ export default function statistics(data, feed) {
this.stack = {};
}
if (this.isLast()) {
const values = keys.reduce((obj, key) => {
const values = keys.filter((key) => this.stack[key]).reduce((obj, key) => {
const value = calculating(this.stack[key].stat);
value.mean = value.sum / value.count;
value.range = value.max - value.min;
value.midrange = value.range / 2;
value.variance = value.diff / value.count;
value.deviation = Math.sqrt(value.variance);
value.csize = Object.keys(this.stack[key].hash).length;
value.population = Object.keys(this.stack[key].hash).length;
delete value.diff;
obj[key] = value;
return obj;
}, {});
this.store.empty()
.on('data', ({ value }) => {
const localValues = value.hashValues.reduce((obj, item) => {
const size = this.stack[item.key].hash[item.hashValue];
const frequency = size / values[item.key].csize;
const sample = this.stack[item.key].hash[item.hashValue];
const frequency = sample / values[item.key].population;
obj[item.key] = {
size,
sample,
frequency,
...values[item.key],
};
Expand Down
69 changes: 49 additions & 20 deletions packages/analytics/test/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ const ezs = require('../../core/src');
ezs.use(require('../src'));

describe('statistics', () => {
it('of 1 value', (done) => {
from([{ a: 1 }])
.pipe(ezs('statistics', { path: 'a', target: 'statistics' }))
.pipe(ezs.catch((e) => done(e)))
.on('data', (chunk) => {
expect(chunk.statistics.a.count).toEqual(1);
expect(chunk.statistics.a.sum).toEqual(1);
expect(chunk.statistics.a.population).toEqual(1);
expect(chunk.statistics.a.sample).toEqual(1);
})
.on('end', () => {
done();
});
});
it('of 1 unknown value', (done) => {
from([{ total: 0 }])
.pipe(ezs('statistics', { path: 'a', target: 'statistics' }))
.pipe(ezs.catch((e) => done(e)))
.on('data', (chunk) => {
expect(chunk.statistics.a).toBeUndefined();
})
.on('end', () => {
done();
});
});

it('of 1 number fields', (done) => {
from([
{ a: 1 },
Expand All @@ -13,11 +39,12 @@ describe('statistics', () => {
{ a: 1 },
])
.pipe(ezs('statistics', { path: 'a', target: 'statistics' }))
.pipe(ezs.catch((e) => done(e)))
.on('data', (chunk) => {
expect(chunk.statistics.a.count).toEqual(5);
expect(chunk.statistics.a.sum).toEqual(5);
expect(chunk.statistics.a.csize).toEqual(1);
expect(chunk.statistics.a.size).toEqual(5);
expect(chunk.statistics.a.population).toEqual(1);
expect(chunk.statistics.a.sample).toEqual(5);
})
.on('end', () => {
done();
Expand All @@ -33,6 +60,7 @@ describe('statistics', () => {
{ a: 3 },
])
.pipe(ezs('statistics', { path: 'a', target: 'statistics' }))
.pipe(ezs.catch((e) => done(e)))
.on('data', (chunk) => {
expect(chunk.statistics.a.count).toEqual(5);
expect(chunk.statistics.a.sum).toEqual(10);
Expand All @@ -41,15 +69,13 @@ describe('statistics', () => {
expect(chunk.statistics.a.midrange).toEqual(1);
expect(chunk.statistics.a.variance).toEqual(0.4);
expect(chunk.statistics.a.deviation).toEqual(0.6324555320336759);
expect(chunk.statistics.a.csize).toEqual(3);
expect(chunk.statistics.a.population).toEqual(3);
})
.on('end', () => {
done();
});
});



it('of 2 numbers fields', (done) => {
from([
{ a: 1, b: 2 },
Expand All @@ -59,15 +85,16 @@ describe('statistics', () => {
{ a: 1, b: 2 },
])
.pipe(ezs('statistics', { path: ['a', 'b'], target: 'statistics' }))
.pipe(ezs.catch((e) => done(e)))
.on('data', (chunk) => {
expect(chunk.statistics.a.count).toEqual(5);
expect(chunk.statistics.a.sum).toEqual(5);
expect(chunk.statistics.a.csize).toEqual(1);
expect(chunk.statistics.a.size).toEqual(5);
expect(chunk.statistics.a.population).toEqual(1);
expect(chunk.statistics.a.sample).toEqual(5);
expect(chunk.statistics.b.sum).toEqual(10);
expect(chunk.statistics.b.csize).toEqual(1);
expect(chunk.statistics.b.population).toEqual(1);
expect(chunk.statistics.b.count).toEqual(5);
expect(chunk.statistics.b.size).toEqual(5);
expect(chunk.statistics.b.sample).toEqual(5);
})
.on('end', () => {
done();
Expand All @@ -83,15 +110,16 @@ describe('statistics', () => {
{ a: 1, b: 2 },
])
.pipe(ezs('statistics', { path: ['a', 'b'], target: 'statistics' }))
.pipe(ezs.catch((e) => done(e)))
.on('data', (chunk) => {
expect(chunk.statistics.a.count).toEqual(5);
expect(chunk.statistics.a.sum).toEqual(5);
expect(chunk.statistics.a.csize).toEqual(1);
expect(chunk.statistics.a.size).toEqual(5);
expect(chunk.statistics.a.population).toEqual(1);
expect(chunk.statistics.a.sample).toEqual(5);
if (chunk.b) {
expect(chunk.statistics.b.size).toEqual(4);
expect(chunk.statistics.b.sample).toEqual(4);
expect(chunk.statistics.b.sum).toEqual(8);
expect(chunk.statistics.b.csize).toEqual(1);
expect(chunk.statistics.b.population).toEqual(1);
expect(chunk.statistics.b.count).toEqual(4);
} else {
expect(chunk.statistics.b).toBeUndefined();
Expand All @@ -102,7 +130,6 @@ describe('statistics', () => {
});
});


it('of 1 string fields', (done) => {
from([
{ a: 'z' },
Expand All @@ -112,11 +139,12 @@ describe('statistics', () => {
{ a: 'z' },
])
.pipe(ezs('statistics', { path: 'a', target: 'statistics' }))
.pipe(ezs.catch((e) => done(e)))
.on('data', (chunk) => {
expect(chunk.statistics.a.count).toEqual(5);
expect(chunk.statistics.a.sum).toEqual(0);
expect(chunk.statistics.a.size).toEqual(5);
expect(chunk.statistics.a.csize).toEqual(1);
expect(chunk.statistics.a.sample).toEqual(5);
expect(chunk.statistics.a.population).toEqual(1);
})
.on('end', () => {
done();
Expand All @@ -131,14 +159,15 @@ describe('statistics', () => {
{ a: 'y' },
{ a: 'y' },
])
.pipe(ezs('statistics', { path: 'a', target: 'statistics' }))
.pipe(ezs('statistics', { path: 'a', target: 'statistics' }))
.pipe(ezs.catch((e) => done(e)))
.on('data', (chunk) => {
expect(chunk.statistics.a.sum).toEqual(0);
expect(chunk.statistics.a.csize).toEqual(2);
expect(chunk.statistics.a.population).toEqual(2);
if (chunk.a === 'z') {
expect(chunk.statistics.a.size).toEqual(2);
expect(chunk.statistics.a.sample).toEqual(2);
} else {
expect(chunk.statistics.a.size).toEqual(3);
expect(chunk.statistics.a.sample).toEqual(3);
}
})
.on('end', () => {
Expand Down

0 comments on commit 7f9b5e7

Please sign in to comment.