-
Notifications
You must be signed in to change notification settings - Fork 25
Feature/threshold calc #909
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
Changes from all commits
7061634
dab77af
8f7f129
7af8cff
5c00113
a3ebbe4
7bbcb4b
426c46e
ce4f9f6
78bc623
c0a0413
8790d77
edec98f
b310cc0
d8fcb20
46080c1
75cb5ab
13c7b5a
4581e2b
104813c
b792460
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ import mappingToObjectTypes from './mappingToObjectTypes'; | |
| import mappingToScalarFields from './mappingToScalarFields'; | ||
|
|
||
| const mappingToFields = ({ enableDocumentHits, type, parent }) => { | ||
| const fieldsToExclude = enableDocumentHits ? [] : ['hits']; | ||
| const showRecords = enableDocumentHits; | ||
| return [ | ||
| mappingToObjectTypes(type.name, type.mapping, parent, type.extendedFields), | ||
| Object.entries(type.mapping) | ||
|
|
@@ -29,7 +29,7 @@ const mappingToFields = ({ enableDocumentHits, type, parent }) => { | |
| type.customFields, | ||
| ], | ||
| createStateTypeDefs: 'createState' in type ? type.createState : true, | ||
| fieldsToExclude, | ||
| showRecords, | ||
|
Member
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. given the meaningful ternaries within ...or better yet, make this |
||
| }), | ||
| ].join(); | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { Aggregation } from './types'; | ||
|
|
||
| /** | ||
| * This returns a total count that is less than or equal to the actual total hits in the query. | ||
| * It is calculated by adding +1 for values under threshold and bucket.doc_count | ||
| * for values greater than or equal to | ||
| * | ||
| * @param aggregation an aggregation with the most buckets which has data masking applied | ||
| * @returns hits total value | ||
| */ | ||
| const calculateHitsFromAggregation = ({ | ||
| aggregation, | ||
| }: { | ||
| aggregation: Aggregation | undefined; | ||
| }) => { | ||
| if (!aggregation) { | ||
| console.error('No aggregation found for calculating hits.'); | ||
| return 0; | ||
| } | ||
| return aggregation.buckets.reduce( | ||
| (totalAcc, bucket) => (bucket.belowThreshold ? totalAcc + 1 : totalAcc + bucket.doc_count), | ||
| 0, | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * | ||
| * 1) Iterate through aggs applying data masking to buckets if applicable | ||
| * 2) Find the agg with the most bucket count and data masking applied to be used in calculating hits.total | ||
| * | ||
| * @param aggregations - aggregations from query | ||
| * @param thresholdMin - threshold value | ||
| * @returns aggregations with data masking applied and hits total | ||
| */ | ||
| export const applyAggregationMasking = ({ | ||
| aggregations, | ||
| thresholdMin, | ||
| }: { | ||
| aggregations: Record< | ||
| string, | ||
| { | ||
| bucket_count: number; | ||
| buckets: Array<{ | ||
| doc_count: number; | ||
| key: string; | ||
| }>; | ||
| } | ||
| >; | ||
| thresholdMin: number; | ||
| }) => { | ||
|
Comment on lines
+38
to
+50
Contributor
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. Very helpful to see the return type declaration. This one is quite large though and may benefit from using some type aliases. No immediate suggestions from me, but the content of aggregations is probably used in multiple places and a good candidate. Somethign like (naming only very quickly considered): type AggregationData = {
bucket_count: number;
buckets: Array<{
doc_count: number;
key: string;
}>;
}
const applyAggregationMasking(...): { aggregations: Record<string, AggregationData>, thresholdMin: number } => {...} |
||
| // set data masked properties to one less than the configured threshold value (under threshold) | ||
| const THRESHOLD_REPLACEMENT_VALUE = thresholdMin - 1; | ||
ciaranschutte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const { aggsTotal: dataMaskedAggregations, totalHitsAgg } = Object.entries(aggregations).reduce<{ | ||
| aggsTotal: Record<string, Aggregation>; | ||
| totalHitsAgg: { key: string; bucketCount: number }; | ||
| }>( | ||
| ({ aggsTotal, totalHitsAgg }, [type, aggregation]) => { | ||
| // mask buckets if under threshold | ||
| const dataMaskedBuckets = aggregation.buckets.map((bucket) => | ||
| bucket.doc_count < thresholdMin | ||
| ? { ...bucket, doc_count: THRESHOLD_REPLACEMENT_VALUE, belowThreshold: true } | ||
| : { ...bucket, belowThreshold: false }, | ||
| ); | ||
|
|
||
| // update total hits selected agg if needed | ||
| const bucketIsMasked = dataMaskedBuckets.some((bucket) => bucket.belowThreshold); | ||
| const hitsAgg = | ||
| totalHitsAgg.bucketCount < aggregation.bucket_count && bucketIsMasked | ||
| ? { key: type, bucketCount: aggregation.bucket_count } | ||
| : totalHitsAgg; | ||
|
|
||
| return { | ||
| aggsTotal: { | ||
| ...aggsTotal, | ||
| [type]: { | ||
| ...aggregation, | ||
| buckets: dataMaskedBuckets, | ||
| }, | ||
| }, | ||
| totalHitsAgg: hitsAgg, | ||
| }; | ||
| }, | ||
| { | ||
| aggsTotal: {}, | ||
| totalHitsAgg: { key: '', bucketCount: 0 }, | ||
| }, | ||
| ); | ||
|
|
||
| const hitsTotal = calculateHitsFromAggregation({ | ||
| aggregation: dataMaskedAggregations[totalHitsAgg.key], | ||
| }); | ||
|
|
||
| return { hitsTotal, dataMaskedAggregations }; | ||
| }; | ||
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.
Does this mean that if thresholding is enabled but no limit is set we default to everything being masked since we are using max int?