Skip to content
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

🔧 Change ven api to support query builder sqons input #244

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,36 +205,29 @@ export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerP
app.post('/venn', keycloak.protect(), async (req, res, next) => {
const lengthOk = (l: Sqon[]) => [2, 3].includes(l.length);
try {
const rawSqons = req.body?.sqons;
const qbSqons = req.body?.qbSqons;
const rawEntitySqons = req.body?.entitySqons;

if (!lengthOk(rawSqons) || !lengthOk(rawEntitySqons)) {
if (!lengthOk(qbSqons) || !lengthOk(rawEntitySqons)) {
res.status(StatusCodes.UNPROCESSABLE_ENTITY).send('Bad Inputs');
return;
}

const data: VennOutput[][] = [];
for (const [inputSqons, rawIndex, noOpCounts] of [
[rawSqons, 'participant', true],
[rawEntitySqons, req.body?.index, false],
]) {
// Convert sqon(s) with set_id if exists to intelligible sqon for ES query translation.
const { resolvedSqons: sqons, m: mSetItToIds } = await resolveSetsInAllSqonsWithMapper(
inputSqons,
null,
req.headers.authorization,
);
const { resolvedSqons: sqons, m: mSetItToIds } = await resolveSetsInAllSqonsWithMapper(
rawEntitySqons,
null,
req.headers.authorization,
);

const index = ['participant', 'file', 'biospecimen'].includes(rawIndex) ? rawIndex : 'participant';
const index = ['participant', 'file', 'biospecimen'].includes(req.body?.index)
? req.body?.index
: 'participant';

const datum1 = await venn(sqons, index);
const datum2 = datum1.map(x => ({ ...x, sqon: replaceIdsWithSetId(x.sqon, mSetItToIds) }));

const datum1 = await venn(sqons, index, noOpCounts);
const datum2 = datum1.map(x => ({ ...x, sqon: replaceIdsWithSetId(x.sqon, mSetItToIds) }));
data.push(datum2);
}
const indexOfParticipantEntity = 0;
const indexOfEntity = 1;
res.send({
data: reformatVenn(data[indexOfParticipantEntity], data[indexOfEntity]),
data: reformatVenn(datum2, qbSqons),
});
} catch (e) {
next(e);
Expand Down
47 changes: 13 additions & 34 deletions src/endpoints/venn/venn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@ type VennEntityOutput = {
entitySqon: Sqon;
};

type VennParticipantOutput = {
operation: string;
sqon: Sqon;
type VennOutputReformattedElement = VennEntityOutput & {
setId?: string;
};

type VennOutputReformattedElement = VennEntityOutput &
VennParticipantOutput & {
setId?: string;
};

export type VennOutputReformatted = {
summary: VennOutputReformattedElement[];
summary: (VennOutputReformattedElement & { qbSqon: Sqon })[];
operations: VennOutputReformattedElement[];
};

Expand Down Expand Up @@ -100,18 +94,10 @@ const setFormulasTrio = (s1: Sqon, s2: Sqon, s3: Sqon) => [

const mNestedFields = new Map();

export const venn = async (sqons: Sqon[], index: string, noOpCounts = false): Promise<VennOutput[]> => {
export const venn = async (sqons: Sqon[], index: string): Promise<VennOutput[]> => {
const setFormulas =
sqons.length === 2 ? setFormulasDuo(sqons[0], sqons[1]) : setFormulasTrio(sqons[0], sqons[1], sqons[2]);

// Only Venn formulas in Sqon speak is needed. Counts are not.
if (noOpCounts) {
return setFormulas.map(x => ({
...x,
count: undefined,
}));
}

const client = EsInstance.getInstance();
const indexName = `${index}_centric`;
if (!mNestedFields.has(index)) {
Expand Down Expand Up @@ -153,21 +139,16 @@ export const reformatWhenSpecifiedEntity = (os: VennOutput[]): VennEntityOutput[
entityCount: o.count,
}));

const reformatWhenUnspecifiedSqon = (os: VennOutput[]): VennParticipantOutput[] =>
os.map(o => ({
operation: o.operation,
sqon: o.sqon,
}));

const mergeOutputs = (xs: VennEntityOutput[], ys: VennParticipantOutput[]) =>
// Assumes that lists are of same length as well as "operation" value for same index loop
xs.map((x, i) => ({ ...x, ...ys[i] }));

const reformatToTables = (data: VennOutputReformattedElement[]): VennOutputReformatted => {
const reformatToTables = (data: VennOutputReformattedElement[], qbSqons: Sqon[]): VennOutputReformatted => {
const opToQbSqon = {
['Q₁']: qbSqons[0],
['Q₂']: qbSqons[1],
['Q₃']: qbSqons[2],
};
const tables = data.reduce(
(xs: VennOutputReformatted, x: VennOutputReformattedElement) => {
if (['Q₁', 'Q₂', 'Q₃'].some(y => y === x.operation)) {
return { ...xs, summary: [...xs.summary, x] };
return { ...xs, summary: [...xs.summary, { ...x, qbSqon: opToQbSqon[x.operation] }] };
}
return { ...xs, operations: [...xs.operations, x] };
},
Expand All @@ -183,7 +164,5 @@ const reformatToTables = (data: VennOutputReformattedElement[]): VennOutputRefor
};
};

export const reformatVenn = (participantOutputs: VennOutput[], entityOutputs: VennOutput[]): VennOutputReformatted =>
reformatToTables(
mergeOutputs(reformatWhenSpecifiedEntity(entityOutputs), reformatWhenUnspecifiedSqon(participantOutputs)),
);
export const reformatVenn = (outputs: VennOutput[], qbSqons: Sqon[]): VennOutputReformatted =>
reformatToTables(reformatWhenSpecifiedEntity(outputs), qbSqons);
Loading