Skip to content

Commit

Permalink
move concept tally functions to utils; clean up some logs
Browse files Browse the repository at this point in the history
  • Loading branch information
katyasoup committed Dec 2, 2024
1 parent 983eeb5 commit e762bca
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 45 deletions.
3 changes: 0 additions & 3 deletions query-connector/src/app/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ const dbConfig: PoolConfig = {
};
const dbClient = new Pool(dbConfig);




/**
* Executes a search for a ValueSets and Concepts against the Postgres
* Database, using the ID of the condition associated with any such data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CategoryNameToConditionOptionMap,
mapFetchedDataToFrontendStructure,
ConditionIdToValueSetArray,
setIncludeAll,
} from "../utils";
import ConditionColumnDisplay from "../buildFromTemplates/ConditionColumnDisplay";
import SearchField from "@/app/query/designSystem/searchField/SearchField";
Expand Down Expand Up @@ -115,12 +116,17 @@ export const ConditionSelection: React.FC<ConditionSelectionProps> = ({
const ConditionValueSets: ConditionIdToValueSetArray = {};

for (const id of conditionIds) {
const result: ValueSet[] = await getValueSetsAndConceptsByConditionID(id);

const results = await mapQueryRowsToValueSets(result);
// when fetching directly from table (as opposed to saved query), default to including all value sets
results.forEach((result) => (result.includeValueSet = true));
ConditionValueSets[id] = results;
const results: ValueSet[] =
await getValueSetsAndConceptsByConditionID(id);

const formattedResults = await mapQueryRowsToValueSets(results);
// when fetching directly from conditions table (as opposed to a saved query),
// default to including all value sets
formattedResults.forEach((result) => {
setIncludeAll(result);
return (result.includeValueSet = true);
});
ConditionValueSets[id] = formattedResults;
}

return ConditionValueSets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ function renderValueSetAccordions(
`placeholder: deselect all child values (${valueSetType}) for condition ID: ${conditionId}`
);
};

const types = Object.keys(valueSets) as Array<DibbsValueSetType>;

const ValueSetAccordionItem = Object.values(types).map(
function (valueSetType) {
const valueSetsForType = Object.values(valueSets[valueSetType]);
console.log(valueSetsForType);
return (
<div
className={styles.valueSetTemplate__accordionContainer}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Checkbox } from "@trussworks/react-uswds";
import styles from "../buildFromTemplates/buildfromTemplate.module.scss";
import { GroupedValueSet } from "@/app/query/components/customizeQuery/customizeQueryUtils";
import { formatDiseaseDisplay } from "../utils";
import { tallyConceptsForSingleValueSet } from "../utils";

type SelectionViewAccordionBodyProps = {
id?: string;
Expand All @@ -25,26 +26,31 @@ const SelectionViewAccordionBody: React.FC<SelectionViewAccordionBodyProps> = ({
return (
<div>
{valueSets.map((vs) => {
const selectedCount = tallyConceptsForSingleValueSet(vs, true);
const totalCount = tallyConceptsForSingleValueSet(vs, false);
return (
<>
<div className={styles.accordionBodyExpanded} key={valueSetType}>
<div className={styles.accordionExpandedInner}>
<Checkbox
name={`checkbox-${vs.valueSetName}`}
className={styles.valueSetTemplate__checkbox}
label={checkboxLabel(vs.valueSetName, vs.author, vs.system)}
onChange={(e) => {
e.stopPropagation();
// handleCheckboxToggle(valueSetType, conditionId);
}}
id={`${vs.valueSetName}-${valueSetType}`}
checked={true}
// disabled={selectedCount == 0}
/>{" "}
</div>
<div>1/1</div>
<div
className={styles.accordionBodyExpanded}
key={`${valueSetType}-${vs.valueSetName}`}
>
<div className={styles.accordionExpandedInner}>
<Checkbox
name={`checkbox-${vs.valueSetName}`}
className={styles.valueSetTemplate__checkbox}
label={checkboxLabel(vs.valueSetName, vs.author, vs.system)}
onChange={(e) => {
e.stopPropagation();
// handleCheckboxToggle(valueSetType, conditionId);
}}
id={`${vs.valueSetName}-${valueSetType}`}
checked={true}
// disabled={selectedCount == 0}
/>{" "}
</div>
</>
<div>
{selectedCount}/{totalCount}
</div>
</div>
);
})}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styles from "../buildFromTemplates/buildfromTemplate.module.scss";
import { Checkbox, Icon } from "@trussworks/react-uswds";
import { GroupedValueSet } from "@/app/query/components/customizeQuery/customizeQueryUtils";
import { tallyConcpetsForValueSetGroup } from "../utils";

type SelectionViewAccordionBodyProps = {
valueSetType: string;
Expand All @@ -22,21 +23,10 @@ type SelectionViewAccordionBodyProps = {
const SelectionViewAccordionHeader: React.FC<
SelectionViewAccordionBodyProps
> = ({ valueSetType, conditionId, valueSets, handleCheckboxToggle }) => {
const selectedTotal =
valueSets &&
valueSets.reduce((sum, vs) => {
sum += vs.items.length;
return sum;
}, 0);

const selectedCount =
valueSets &&
valueSets.reduce((sum, vs) => {
vs.items.forEach((item) => {
if (item.includeValueSet) sum += 1;
});
return sum;
}, 0);
valueSets && tallyConcpetsForValueSetGroup(valueSets, true);
const totalCount =
valueSets && tallyConcpetsForValueSetGroup(valueSets, false);

return (
<>
Expand All @@ -58,15 +48,15 @@ const SelectionViewAccordionHeader: React.FC<
id={`${conditionId}-${valueSetType}`}
checked={
!!selectedCount &&
selectedCount == selectedTotal &&
selectedCount == totalCount &&
selectedCount > 0
}
disabled={selectedCount == 0}
/>
</div>
<div
className={styles.accordionHeaderCount}
>{`${selectedCount} / ${selectedTotal}`}</div>
>{`${selectedCount} / ${totalCount}`}</div>
</div>
</>
);
Expand Down
59 changes: 59 additions & 0 deletions query-connector/src/app/queryBuilding/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,62 @@ export function filterSearchByCategoryAndCondition(
export function formatDiseaseDisplay(diseaseName: string) {
return diseaseName.replace("(disorder)", "");
}

/**
* Utility method that returns the number of concepts associated
* with a given value set, with the option to return only
* concepts marked as included
* @param valueSet - the GroupedValueSet to run the tally on
* @param filterInclude - boolean to indicate whether to only count
* included concepts (defaults to false)
* @returns A number indicating the tally of relevant concpets
*/
export function tallyConceptsForSingleValueSet (
valueSet: GroupedValueSet,
filterInclude?: boolean
) {
const selectedTotal = valueSet.items.reduce((sum, vs) => {
const includedConcepts = !!filterInclude
? vs.concepts.filter((c) => c.include)
: vs.concepts;
sum += includedConcepts.length;
return sum;
}, 0);

return selectedTotal;
};

/**
* Utility method that returns the total number of concepts associated
* with a selection of ValueSets, with the option to return only
* concepts marked as included
* @param valueSets - the array of GroupedValueSets to run the tally on
* @param filterInclude - boolean to indicate whether to only count
* included concepts
* @returns A number indicating the tally of relevant concpets
*/
export function tallyConcpetsForValueSetGroup (
valueSets: GroupedValueSet[],
filterInclude?: boolean
) {
const selectedTotal = valueSets.reduce((sum, valueSet) => {
const childTotal = tallyConceptsForSingleValueSet(valueSet, filterInclude)
sum += childTotal
return sum;
}, 0);
return selectedTotal;
}

/**
* Utility method that marks all concepts as included for
* a given ValueSet
* @param input - the ValueSet to update
* @returns the updated ValueSet
*/
export const setIncludeAll = (input: ValueSet) => {
input.concepts.forEach((concept) => {
concept.include = true;
});

return input
};

0 comments on commit e762bca

Please sign in to comment.