Skip to content

Commit

Permalink
Fix family external id participant grid filter (#982)
Browse files Browse the repository at this point in the history
* roll back recent changes to ValueFilter as they introduced bugs

* add support for querying participant grid by family external id
  • Loading branch information
dancoates authored Oct 30, 2024
1 parent 0d9e0bd commit 5c83cd8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
18 changes: 16 additions & 2 deletions db/python/tables/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async def _construct_participant_query(
) -> tuple[str, dict[str, Any]]:
"""Construct a participant query"""
needs_family = False
needs_family_eid = False
needs_participant_eid = True # always join, query optimiser can figure it out
needs_sample = False
needs_sample_eid = False
Expand All @@ -68,21 +69,30 @@ async def _construct_participant_query(
},
exclude=['family', 'sample', 'sequencing_group', 'assay'],
)

wheres = [_wheres]

if filter_.family:
needs_family = True
fwheres, fvalues = filter_.family.to_sql(
{
'id': 'f.id',
'external_id': 'f.external_id',
'meta': 'f.meta',
}
},
exclude=['external_id'],
)
values.update(fvalues)
if fwheres:
wheres.append(fwheres)

if filter_.family.external_id:
needs_family_eid = True
feid_wheres, feid_values = filter_.family.to_sql(
{'external_id': 'feid.external_id'}, only=['external_id']
)
wheres.append(feid_wheres)
values.update(feid_values)

if filter_.sample:
needs_sample = True
swheres, svalues = filter_.sample.to_sql(
Expand Down Expand Up @@ -169,6 +179,10 @@ async def _construct_participant_query(
'INNER JOIN family_participant fp ON fp.participant_id = pp.id\n'
'INNER JOIN family f ON f.id = fp.family_id'
)
if needs_family_eid:
query_lines.append(
'INNER JOIN family_external_id feid ON feid.family_id = f.id'
)

if wheres:
query_lines.append('WHERE \n' + ' AND '.join(wheres))
Expand Down
25 changes: 15 additions & 10 deletions web/src/pages/project/ValueFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,19 @@ export const ValueFilter: React.FC<IValueFilter> = ({
// So check if the filterKey starts with 'meta.' to determine if it is a meta key, and
// then check the [category].meta object for the value

if (!field.filter_key) return <></>

/* eslint-disable react-hooks/rules-of-hooks*/
const [_defaultFilterType, setDefaultFilterType] = React.useState<
ProjectParticipantGridFilterType | undefined
>()

let optionsToCheck = props?.filterValues?.[category] || {}
const name = (field.filter_key ?? '').replace(/^meta\./, '')

// @ts-ignore
const _value = optionsToCheck?.[name]?.[operator]
const [_tempValue, setTempValue] = React.useState<string | undefined>(_value ?? '')
const tempValue = _tempValue ?? _value

if (!field.filter_key) return <></>
/* eslint-enable react-hooks/rules-of-hooks*/

const isMeta = field.filter_key?.startsWith('meta.')
// set name to the filterKey without the .meta prefix
const name = field.filter_key.replace(/^meta\./, '')

let optionsToCheck = props?.filterValues?.[category] || {}

if (isMeta) {
// get the meta bit from the filterValues
Expand Down Expand Up @@ -146,6 +143,14 @@ export const ValueFilter: React.FC<IValueFilter> = ({
operator = getOperatorFromFilterType(queryType)
}

// @ts-ignore
const _value = optionsToCheck?.[name]?.[operator]

/* eslint-disable react-hooks/rules-of-hooks*/
const [_tempValue, setTempValue] = React.useState<string | undefined>(_value ?? '')
/* eslint-enable react-hooks/rules-of-hooks*/
const tempValue = _tempValue ?? _value

const updateQueryType = (newFilterType: ProjectParticipantGridFilterType) => {
setDefaultFilterType(newFilterType)
const newOperator = getOperatorFromFilterType(newFilterType)
Expand Down

0 comments on commit 5c83cd8

Please sign in to comment.