-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grouping): add more Grouping & Aggregators code
- Loading branch information
1 parent
9308d4b
commit 8c20808
Showing
32 changed files
with
314 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/common/src/aggregators/__tests__/cloneAggregator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { CloneAggregator } from '../cloneAggregator'; | ||
|
||
describe('CloneAggregator', () => { | ||
let aggregator: CloneAggregator; | ||
let dataset = []; | ||
|
||
beforeEach(() => { | ||
dataset = [ | ||
{ id: 0, title: 'Product 0', price: 58.5, productGroup: 'Sub-Cat1' }, | ||
{ id: 1, title: 'Product 1', price: 14, productGroup: 'Sub-Cat1' }, | ||
{ id: 2, title: 'Product 2', price: 2, productGroup: 'Sub-Cat2' }, | ||
{ id: 3, title: 'Product 3', price: 87, productGroup: 'Sub-Cat1' }, | ||
{ id: 4, title: 'Product 4', price: null, productGroup: 'Sub-Cat2' }, | ||
]; | ||
}); | ||
|
||
it('should return empty string when the field provided does not exist', () => { | ||
// arrange | ||
const fieldName = 'invalid'; | ||
const groupTotals = {}; | ||
aggregator = new CloneAggregator(fieldName); | ||
aggregator.init(); | ||
|
||
// act | ||
dataset.forEach((row) => aggregator.accumulate(row)); | ||
aggregator.storeResult(groupTotals); | ||
|
||
// assert | ||
expect(groupTotals['clone'][fieldName]).toBe(''); | ||
}); | ||
|
||
it('should return last text analyzed by the aggregator when the chosen field is the product group', () => { | ||
const fieldName = 'productGroup'; | ||
const lastGroupName = 'Sub-Cat2'; | ||
const groupTotals = { clone: {} }; | ||
aggregator = new CloneAggregator(fieldName); | ||
aggregator.init(); | ||
|
||
dataset.forEach((row) => aggregator.accumulate(row)); | ||
aggregator.storeResult(groupTotals); | ||
|
||
expect(groupTotals.clone[fieldName]).toBe(lastGroupName); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
packages/common/src/aggregators/__tests__/distinctAggregator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { DistinctAggregator } from '../distinctAggregator'; | ||
|
||
describe('disctinctAggregator', () => { | ||
let aggregator: DistinctAggregator; | ||
let dataset = []; | ||
|
||
beforeEach(() => { | ||
dataset = [ | ||
{ id: 0, title: 'Task 0', duration: '58', percentComplete: 55 }, | ||
{ id: 1, title: 'Task 1', duration: '14', percentComplete: 87 }, | ||
{ id: 2, title: 'Task 2', duration: '', percentComplete: 60 }, | ||
{ id: 3, title: 'Task 3', duration: '58', percentComplete: 87 }, | ||
{ id: 4, title: 'Task 4', duration: null, percentComplete: 55 }, | ||
{ id: 4, title: 'Task 5', duration: 32, percentComplete: 52 }, | ||
{ id: 4, title: 'Task 6', duration: 58, percentComplete: 52 }, | ||
]; | ||
}); | ||
|
||
it('should return empty array when the field provided does not exist', () => { | ||
// arrange | ||
const fieldName = 'invalid'; | ||
const groupTotals = {}; | ||
aggregator = new DistinctAggregator(fieldName); | ||
aggregator.init(); | ||
|
||
// act | ||
dataset.forEach((row) => aggregator.accumulate(row)); | ||
aggregator.storeResult(groupTotals); | ||
|
||
// assert | ||
expect(groupTotals['distinct'][fieldName]).toEqual([]); | ||
}); | ||
|
||
it('should return the distinct number values when provided field property values are all numbers', () => { | ||
const fieldName = 'percentComplete'; | ||
const groupTotals = { distinct: {} }; | ||
aggregator = new DistinctAggregator(fieldName); | ||
aggregator.init(); | ||
|
||
dataset.forEach((row) => aggregator.accumulate(row)); | ||
aggregator.storeResult(groupTotals); | ||
|
||
expect(groupTotals.distinct[fieldName]).toEqual([55, 87, 60, 52]); | ||
}); | ||
|
||
it('should return the distinct mixed values when provided field property values are all mixed types', () => { | ||
const fieldName = 'duration'; | ||
const groupTotals = { distinct: {} }; | ||
aggregator = new DistinctAggregator(fieldName); | ||
aggregator.init(); | ||
|
||
dataset.forEach((row) => aggregator.accumulate(row)); | ||
aggregator.storeResult(groupTotals); | ||
|
||
expect(groupTotals.distinct[fieldName]).toEqual(['58', '14', '', null, 32, 58]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Aggregator } from './../interfaces/aggregator.interface'; | ||
|
||
export class CloneAggregator implements Aggregator { | ||
private _field: number | string; | ||
private _data: any; | ||
|
||
constructor(field: number | string) { | ||
this._field = field; | ||
} | ||
|
||
init(): void { | ||
this._data = ''; | ||
} | ||
|
||
accumulate(item: any) { | ||
const val = (item && item.hasOwnProperty(this._field)) ? item[this._field] : null; | ||
if (val !== null && val !== '') { | ||
this._data = val; | ||
} | ||
} | ||
|
||
storeResult(groupTotals: any) { | ||
if (!groupTotals || groupTotals.clone === undefined) { | ||
groupTotals.clone = {}; | ||
} | ||
groupTotals.clone[this._field] = this._data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Aggregator } from './../interfaces/aggregator.interface'; | ||
|
||
export class DistinctAggregator implements Aggregator { | ||
private _field: number | string; | ||
private _distinctValues: any[]; | ||
|
||
constructor(field: number | string) { | ||
this._field = field; | ||
} | ||
|
||
init(): void { | ||
this._distinctValues = []; | ||
} | ||
|
||
accumulate(item: any) { | ||
const val = (item && item.hasOwnProperty(this._field)) ? item[this._field] : undefined; | ||
if (this._distinctValues.indexOf(val) === -1 && val !== undefined) { | ||
this._distinctValues.push(val); | ||
} | ||
} | ||
|
||
storeResult(groupTotals: any) { | ||
if (!groupTotals || groupTotals.avg === undefined) { | ||
groupTotals.distinct = {}; | ||
} | ||
groupTotals.distinct[this._field] = this._distinctValues; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,27 @@ | ||
import { AvgAggregator } from './avgAggregator'; | ||
import { CloneAggregator } from './cloneAggregator'; | ||
import { DistinctAggregator } from './distinctAggregator'; | ||
import { MinAggregator } from './minAggregator'; | ||
import { MaxAggregator } from './maxAggregator'; | ||
import { SumAggregator } from './sumAggregator'; | ||
|
||
/** Provides a list of different Aggregators for the Group Formatter */ | ||
export const Aggregators = { | ||
/** Average Aggregator which calculate the average of a given group */ | ||
Avg: AvgAggregator, | ||
|
||
/** Clone Aggregator will simply clone (copy) over the last defined value of a given group */ | ||
Clone: CloneAggregator, | ||
|
||
/** Distinct Aggregator will return an array of distinct values found inside the given group */ | ||
Distinct: DistinctAggregator, | ||
|
||
/** Minimum Aggregator which will find the minimum value inside the given group */ | ||
Min: MinAggregator, | ||
|
||
/** Maximum Aggregator which will find the maximum value inside the given group */ | ||
Max: MaxAggregator, | ||
|
||
/** Sum Aggregator which calculate the sum of a given group */ | ||
Sum: SumAggregator | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.