Skip to content

Commit c9e6870

Browse files
authored
🔧 Adapt venn route for new front end requirements (#243)
* 🔧 Adapt venn route for new front end requirements * 🐛 Fix return in venn route
1 parent 8630590 commit c9e6870

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

src/app.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import { CreateSetBody, Set, SetSqon, UpdateSetContentBody, UpdateSetTagBody } f
2020
import { getStatistics, getStudiesStatistics } from './endpoints/statistics';
2121
import transcriptomicsRouter from './endpoints/transcriptomics/route';
2222
import { computeUpset } from './endpoints/upset';
23-
import { reformatVenn, venn, VennOutputReformatted } from './endpoints/venn/venn';
23+
import { reformatVenn, venn, VennOutput } from './endpoints/venn/venn';
2424
import { esHost, keycloakURL, userApiURL } from './env';
2525
import { globalErrorHandler, globalErrorLogger } from './errors';
2626
import { flushAllCache, STATISTICS_CACHE_ID, STATISTICS_PUBLIC_CACHE_ID, twineWithCache } from './middleware/cache';
2727
import { injectBodyHttpHeaders } from './middleware/injectBodyHttpHeaders';
2828
import { resolveSetIdMiddleware } from './middleware/resolveSetIdInSqon';
2929
import { replaceIdsWithSetId, resolveSetsInAllSqonsWithMapper } from './sqon/resolveSetInSqon';
30+
import { Sqon } from './sqon/types';
3031

3132
export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerProject): Express => {
3233
const app = express();
@@ -202,20 +203,24 @@ export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerP
202203
});
203204

204205
app.post('/venn', keycloak.protect(), async (req, res, next) => {
206+
const lengthOk = (l: Sqon[]) => [2, 3].includes(l.length);
205207
try {
206-
if (![2, 3].includes(req.body?.sqons?.length) || ![2, 3].includes(req.body?.entityCentricSqons?.length)) {
208+
const rawSqons = req.body?.sqons;
209+
const rawEntitySqons = req.body?.entitySqons;
210+
211+
if (!lengthOk(rawSqons) || !lengthOk(rawEntitySqons)) {
207212
res.status(StatusCodes.UNPROCESSABLE_ENTITY).send('Bad Inputs');
208213
return;
209214
}
210215

211-
const data: VennOutputReformatted[] = [];
212-
for (const [rawSqons, rawIndex, noOpCounts] of [
213-
[req.body.sqons, 'participant', true],
214-
[req.body.entityCentricSqons, req.body?.index, false],
216+
const data: VennOutput[][] = [];
217+
for (const [inputSqons, rawIndex, noOpCounts] of [
218+
[rawSqons, 'participant', true],
219+
[rawEntitySqons, req.body?.index, false],
215220
]) {
216221
// Convert sqon(s) with set_id if exists to intelligible sqon for ES query translation.
217222
const { resolvedSqons: sqons, m: mSetItToIds } = await resolveSetsInAllSqonsWithMapper(
218-
rawSqons,
223+
inputSqons,
219224
null,
220225
req.headers.authorization,
221226
);
@@ -224,14 +229,12 @@ export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerP
224229

225230
const datum1 = await venn(sqons, index, noOpCounts);
226231
const datum2 = datum1.map(x => ({ ...x, sqon: replaceIdsWithSetId(x.sqon, mSetItToIds) }));
227-
data.push(reformatVenn(datum2));
232+
data.push(datum2);
228233
}
229-
234+
const indexOfParticipantEntity = 0;
235+
const indexOfEntity = 1;
230236
res.send({
231-
data: {
232-
participantCentric: data[0],
233-
entityCentric: data[1],
234-
},
237+
data: reformatVenn(data[indexOfParticipantEntity], data[indexOfEntity]),
235238
});
236239
} catch (e) {
237240
next(e);

src/endpoints/venn/venn.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,28 @@ import { getNestedFieldsForIndex } from '../../sqon/getNestedFieldsForIndex';
55
import { and, not } from '../../sqon/manipulateSqon';
66
import { Sqon } from '../../sqon/types';
77

8-
type VennOutput = {
8+
export type VennOutput = {
99
operation: string;
1010
count: number;
1111
sqon: Sqon;
1212
};
1313

14-
type VennOutputReformattedElement = VennOutput & {
15-
setId?: string;
14+
type VennEntityOutput = {
15+
operation: string;
16+
entityCount: number;
17+
entitySqon: Sqon;
18+
};
19+
20+
type VennParticipantOutput = {
21+
operation: string;
22+
sqon: Sqon;
1623
};
1724

25+
type VennOutputReformattedElement = VennEntityOutput &
26+
VennParticipantOutput & {
27+
setId?: string;
28+
};
29+
1830
export type VennOutputReformatted = {
1931
summary: VennOutputReformattedElement[];
2032
operations: VennOutputReformattedElement[];
@@ -134,7 +146,24 @@ export const venn = async (sqons: Sqon[], index: string, noOpCounts = false): Pr
134146
}));
135147
};
136148

137-
export const reformatVenn = (data: VennOutput[]): VennOutputReformatted => {
149+
export const reformatWhenSpecifiedEntity = (os: VennOutput[]): VennEntityOutput[] =>
150+
os.map(o => ({
151+
operation: o.operation,
152+
entitySqon: o.sqon,
153+
entityCount: o.count,
154+
}));
155+
156+
const reformatWhenUnspecifiedSqon = (os: VennOutput[]): VennParticipantOutput[] =>
157+
os.map(o => ({
158+
operation: o.operation,
159+
sqon: o.sqon,
160+
}));
161+
162+
const mergeOutputs = (xs: VennEntityOutput[], ys: VennParticipantOutput[]) =>
163+
// Assumes that lists are of same length as well as "operation" value for same index loop
164+
xs.map((x, i) => ({ ...x, ...ys[i] }));
165+
166+
const reformatToTables = (data: VennOutputReformattedElement[]): VennOutputReformatted => {
138167
const tables = data.reduce(
139168
(xs: VennOutputReformatted, x: VennOutputReformattedElement) => {
140169
if (['Q₁', 'Q₂', 'Q₃'].some(y => y === x.operation)) {
@@ -147,6 +176,14 @@ export const reformatVenn = (data: VennOutput[]): VennOutputReformatted => {
147176

148177
return {
149178
summary: tables.summary,
150-
operations: tables.operations.map((x: VennOutput, i: number) => ({ ...x, setId: `set-${i}` })),
179+
operations: tables.operations.map((x: VennOutputReformattedElement, i: number) => ({
180+
...x,
181+
setId: `set-${i}`,
182+
})),
151183
};
152184
};
185+
186+
export const reformatVenn = (participantOutputs: VennOutput[], entityOutputs: VennOutput[]): VennOutputReformatted =>
187+
reformatToTables(
188+
mergeOutputs(reformatWhenSpecifiedEntity(entityOutputs), reformatWhenUnspecifiedSqon(participantOutputs)),
189+
);

0 commit comments

Comments
 (0)