-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/resolve stats #883
Merged
Merged
Feat/resolve stats #883
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,51 @@ | ||
import { SupportedAggregation, SUPPORTED_AGGREGATIONS } from '../common'; | ||
import { Aggregations, NetworkAggregation, NumericAggregations } from '../types'; | ||
|
||
// TODO: implement | ||
export const resolveAggregations = (networkResults: void) => null; | ||
|
||
/** | ||
* Resolve aggregation based on aggregation type | ||
* @param type | ||
* @param aggregations | ||
*/ | ||
export const resolveToNetworkAggregation = <T>( | ||
type: SupportedAggregation, | ||
aggregations: Aggregations[], | ||
) => { | ||
if (type === SUPPORTED_AGGREGATIONS.Aggregations) { | ||
resolveAggregation(aggregations); | ||
} else if (type === SUPPORTED_AGGREGATIONS.NumericAggregations) { | ||
resolveNumericAggregation(aggregations); | ||
} else { | ||
// no types match | ||
throw Error('No matching aggregation type'); | ||
} | ||
}; | ||
|
||
/** | ||
* Takes an array of the same aggregation type and computes the singular type | ||
* eg. NumericAggregation => NetworkNumericAggregation | ||
* | ||
* @param aggregations | ||
* @returns | ||
*/ | ||
export const resolveAggregation = (aggregations: Aggregations[]): NetworkAggregation => { | ||
const emptyAggregation: NetworkAggregation = { bucket_count: 0, buckets: [] }; | ||
|
||
const resolvedAggregation = aggregations.reduce((resolvedAggregation, agg) => { | ||
const computedBucketCount = resolvedAggregation.bucket_count + agg.bucket_count; | ||
const computedBuckets = agg.buckets.map(({ key, doc_count }) => { | ||
// potentially expensive "find" if array of buckets is very large | ||
const bucket = resolvedAggregation.buckets.find((bucket) => bucket.key === key); | ||
return { key, doc_count: (bucket?.doc_count || 0) + doc_count }; | ||
}); | ||
return { bucket_count: computedBucketCount, buckets: computedBuckets }; | ||
}, emptyAggregation); | ||
return resolvedAggregation; | ||
}; | ||
|
||
const resolveNumericAggregation = (aggregations: NumericAggregations) => { | ||
// TODO: implement | ||
throw Error('Not implemented'); | ||
}; |
27 changes: 27 additions & 0 deletions
27
modules/server/src/network/aggregations/tests/aggregation.test.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,27 @@ | ||
import { resolveAggregation } from '..'; | ||
import { aggregation } from './fixture'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
|
||
describe('Aggregation', () => { | ||
it('should resolve multiple objects of type Aggregation into a single object of type NetworkAggregation', () => { | ||
const result = resolveAggregation([aggregation.inputA, aggregation.inputB]); | ||
|
||
const resultForMaleCount = | ||
result.buckets.find((bucket) => bucket.key === 'Male')?.doc_count || 0; | ||
const resultForFemaleCount = | ||
result.buckets.find((bucket) => bucket.key === 'Female')?.doc_count || 0; | ||
|
||
const expectedMaleCount = | ||
(aggregation.inputA.buckets.find((bucket) => bucket.key === 'Male')?.doc_count || 0) + | ||
(aggregation.inputB.buckets.find((bucket) => bucket.key === 'Male')?.doc_count || 0); | ||
|
||
const expectedFemaleCount = | ||
(aggregation.inputA.buckets.find((bucket) => bucket.key === 'Female')?.doc_count || 0) + | ||
(aggregation.inputB.buckets.find((bucket) => bucket.key === 'Female')?.doc_count || 0); | ||
|
||
expect(result.bucket_count).toEqual( | ||
aggregation.inputA.bucket_count + aggregation.inputB.bucket_count, | ||
); | ||
expect(resultForMaleCount).toEqual(expectedMaleCount); | ||
expect(resultForFemaleCount).toEqual(expectedFemaleCount); | ||
}); | ||
}); |
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,54 @@ | ||
import { Aggregations } from '@/network/types'; | ||
|
||
// Aggregation | ||
const inputA: Aggregations = { | ||
bucket_count: 82, | ||
buckets: [ | ||
{ | ||
key: 'Male', | ||
doc_count: 70, | ||
}, | ||
{ | ||
key: 'Female', | ||
doc_count: 12, | ||
}, | ||
], | ||
}; | ||
|
||
const inputB: Aggregations = { | ||
bucket_count: 715, | ||
buckets: [ | ||
{ | ||
key: 'Male', | ||
doc_count: 15, | ||
}, | ||
{ | ||
key: 'Female', | ||
doc_count: 700, | ||
}, | ||
], | ||
}; | ||
|
||
const inputC: Aggregations = { | ||
bucket_count: 1565, | ||
buckets: [ | ||
{ | ||
key: 'Male', | ||
doc_count: 765, | ||
}, | ||
{ | ||
key: 'Female', | ||
doc_count: 800, | ||
}, | ||
{ | ||
key: 'Unknown', | ||
doc_count: 0, | ||
}, | ||
], | ||
}; | ||
|
||
export const aggregation = { | ||
inputA, | ||
inputB, | ||
inputC, | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you get around to working on this function I'd update this name. I'd add something descriptive to distinguish it's purpose from
resolveAggregation