-
Notifications
You must be signed in to change notification settings - Fork 1
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
🐛 Fix family sqon (family-clinical-data) #114
Merged
evans-g-crsj
merged 1 commit into
master
from
downloading-participants-and-families-has-incorrect-numbers
Jan 9, 2024
Merged
Changes from all commits
Commits
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 was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/reports/family-clinical-data/generatePtSqonWithRelativesIfExist.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,50 @@ | ||
import { | ||
extractFieldAggregationIds, | ||
mergeParticipantsWithoutDuplicates, | ||
xIsSubsetOfY, | ||
} from './generatePtSqonWithRelativesIfExist'; | ||
|
||
describe('Sqon generator for selected participants and their relatives', () => { | ||
test('checks if X is a subset of Y', () => { | ||
// | ||
const x = ['p1', 'p2']; | ||
const y = ['p1', 'p2', 'p4']; | ||
expect(xIsSubsetOfY(x, y)).toBeTruthy(); | ||
}); | ||
test('merges participants adequately', () => { | ||
const x = ['p1', 'p2', 'p3']; | ||
const y = ['p2', 'p3', 'p4']; | ||
expect(mergeParticipantsWithoutDuplicates(x, y).every(p => ['p1', 'p2', 'p3', 'p4'].includes(p))).toBeTruthy(); | ||
}); | ||
test('extracts correctly all participants from initial ES response', async () => { | ||
const query = { | ||
bool: { | ||
must: [ | ||
{ | ||
terms: { | ||
down_syndrome_status: ['T21'], | ||
boost: 0, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
const searchExecutor = async (_: object) => | ||
Promise.resolve({ | ||
query: _, | ||
body: { | ||
aggregations: { | ||
ids: { | ||
buckets: [ | ||
{ key: 'p1', count: 1 }, | ||
{ key: 'p2', count: 1 }, | ||
{ key: 'p2', count: 1 }, | ||
], | ||
}, | ||
}, | ||
}, | ||
}); | ||
const ps = await extractFieldAggregationIds(query, 'participant_id', searchExecutor); | ||
expect(ps).toEqual(['p1', 'p2']); | ||
}); | ||
}); |
130 changes: 130 additions & 0 deletions
130
src/reports/family-clinical-data/generatePtSqonWithRelativesIfExist.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,130 @@ | ||
import { buildQuery } from '@arranger/middleware'; | ||
|
||
import { getExtendedConfigs, getNestedFields } from '../../utils/arrangerUtils'; | ||
import { executeSearch } from '../../utils/esUtils'; | ||
import { Client } from '@elastic/elasticsearch'; | ||
import { resolveSetsInSqon } from '../../utils/sqonUtils'; | ||
|
||
import { Sqon } from '../../utils/setsTypes'; | ||
import { ES_QUERY_MAX_SIZE } from '../../env'; | ||
|
||
type Bucket = { key: string; count: number }; | ||
type AggregationIdsRequest = { | ||
[index: string]: any; | ||
query: object; | ||
body: { | ||
aggregations: { | ||
ids: { | ||
buckets: Bucket[]; | ||
}; | ||
}; | ||
}; | ||
}; | ||
export const extractFieldAggregationIds = async ( | ||
query: object, | ||
field: string, | ||
searchExecutor: (q: object) => Promise<AggregationIdsRequest>, | ||
): Promise<string[]> => { | ||
const r = await searchExecutor({ | ||
query, | ||
aggs: { | ||
ids: { | ||
terms: { field: field, size: ES_QUERY_MAX_SIZE }, | ||
}, | ||
}, | ||
}); | ||
const rawIds: string[] = (r.body?.aggregations?.ids?.buckets || []).map((bucket: Bucket) => bucket.key); | ||
return [...new Set(rawIds)]; | ||
}; | ||
|
||
export const mergeParticipantsWithoutDuplicates = (x: string[], y: string[]) => [...new Set([...x, ...y])]; | ||
|
||
// extract in a more general file when and if needed. | ||
export const xIsSubsetOfY = (x: string[], y: string[]) => x.every((e: string) => y.includes(e)); | ||
/** | ||
* Generate a sqon from the family_id of all the participants in the given `sqon`. | ||
* @param {object} es - an `elasticsearch.Client` instance. | ||
* @param {string} projectId - the id of the arranger project. | ||
* @param {object} sqon - the sqon used to filter the results. | ||
* @param {object} normalizedConfigs - the normalized report configuration. | ||
* @param {string} userId - the user id. | ||
* @param {string} accessToken - the user access token. | ||
* @returns {object} - A sqon of all the `family_id`. | ||
*/ | ||
const generatePtSqonWithRelativesIfExist = async ( | ||
es: Client, | ||
projectId: string, | ||
sqon: Sqon, | ||
normalizedConfigs: { indexName: string; alias: string; [index: string]: any }, | ||
userId: string, | ||
accessToken: string, | ||
): Promise<Sqon> => { | ||
const extendedConfig = await getExtendedConfigs(es, projectId, normalizedConfigs.indexName); | ||
const nestedFields = getNestedFields(extendedConfig); | ||
const newSqon = await resolveSetsInSqon(sqon, userId, accessToken); | ||
|
||
const query = buildQuery({ nestedFields, filters: newSqon }); | ||
const searchExecutor = async (q: object) => await executeSearch(es, normalizedConfigs.alias, q); | ||
|
||
const allSelectedParticipantsIds: string[] = await extractFieldAggregationIds( | ||
query, | ||
'participant_id', | ||
searchExecutor, | ||
); | ||
const allFamiliesIdsOfSelectedParticipants: string[] = await extractFieldAggregationIds( | ||
{ | ||
bool: { | ||
must: [ | ||
{ | ||
terms: { | ||
participant_id: allSelectedParticipantsIds, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
'families_id', | ||
searchExecutor, | ||
); | ||
const allRelativesIds: string[] = await extractFieldAggregationIds( | ||
{ | ||
bool: { | ||
must: [ | ||
{ | ||
terms: { | ||
families_id: allFamiliesIdsOfSelectedParticipants, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
'participant_id', | ||
searchExecutor, | ||
); | ||
const selectedParticipantsIdsPlusRelatives = mergeParticipantsWithoutDuplicates( | ||
allSelectedParticipantsIds, | ||
allRelativesIds, | ||
); | ||
|
||
console.assert( | ||
selectedParticipantsIdsPlusRelatives.length >= allSelectedParticipantsIds.length && | ||
xIsSubsetOfY(allSelectedParticipantsIds, selectedParticipantsIdsPlusRelatives), | ||
`Family Report (sqon enhancer): The participants ids computed must be equal or greater than the selected participants. | ||
Moreover, selected participants must a subset of the computed ids.`, | ||
); | ||
|
||
return { | ||
op: 'and', | ||
content: [ | ||
{ | ||
op: 'in', | ||
content: { | ||
field: 'participant_id', | ||
value: selectedParticipantsIdsPlusRelatives, | ||
}, | ||
}, | ||
], | ||
}; | ||
}; | ||
|
||
export default generatePtSqonWithRelativesIfExist; |
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.
This is file
src/reports/family-clinical-data/generateFamilySqon.ts
renamed!