Skip to content

Commit

Permalink
Merge pull request #114 from biothings/parallel-creative
Browse files Browse the repository at this point in the history
allow sri queries to be aborted
  • Loading branch information
tokebe authored Oct 11, 2024
2 parents 8c1f094 + 8aaaf09 commit 3761d62
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@commitlint/cli": "^17.8.1",
"@commitlint/config-conventional": "^11.0.0",
"axios-retry": "^3.8.0",
"axios": "^0.21.4",
"axios": "^1.7.2",
"biolink-model": "workspace:../biolink-model",
"debug": "^4.3.4",
"husky": "^8.0.3",
Expand Down
17 changes: 10 additions & 7 deletions src/attrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function buildOneQuery(
prefix: string,
inputs: string[],
semanticType: string,
abortSignal?: AbortSignal
): Promise<IndividualResolverOutput> {
const idReturnFields = getReturnFields(metadata.mapping);
let attrReturnFields = '';
Expand Down Expand Up @@ -135,8 +136,9 @@ function buildOneQuery(
'content-type': 'application/json',
'User-Agent': userAgent,
},
signal: abortSignal,
})
.then((response) => getDBIDs(prefix, semanticType, response.data))
.then((response) => getDBIDs(prefix, semanticType, response.data as any))
.catch(() => undefined);
}

Expand All @@ -145,24 +147,25 @@ function buildQueries(
prefix: string,
inputs: string[],
semanticType: string,
abortSignal?: AbortSignal
): Promise<IndividualResolverOutput>[] {
if (inputs.length > MAX_BIOTHINGS_INPUT_SIZE) {
return _.chunk(inputs, MAX_BIOTHINGS_INPUT_SIZE).map((batch) =>
buildOneQuery(metadata, prefix, batch, semanticType),
buildOneQuery(metadata, prefix, batch, semanticType, abortSignal),
);
} else {
return [buildOneQuery(metadata, prefix, inputs, semanticType)];
return [buildOneQuery(metadata, prefix, inputs, semanticType, abortSignal)];
}
}

function getAPIMetaData(semanticType: string) {
return APIMETA[semanticType];
}

function build(semanticType: string, curies: string[]): Promise<IndividualResolverOutput>[] {
function build(semanticType: string, curies: string[], abortSignal?: AbortSignal): Promise<IndividualResolverOutput>[] {
const grped = groupCuriesByPrefix(curies);
return Object.keys(grped).reduce((prev: Promise<IndividualResolverOutput>[], current) => {
prev = [...prev, ...buildQueries(getAPIMetaData(semanticType), current, grped[current], semanticType)];
prev = [...prev, ...buildQueries(getAPIMetaData(semanticType), current, grped[current], semanticType, abortSignal)];
return prev;
}, []);
}
Expand All @@ -181,7 +184,7 @@ function getSupportedType(category: string): string {
return '';
}

export async function _getAttributes(idsByType: object): Promise<any> {
export async function _getAttributes(idsByType: object, abortSignal?: AbortSignal): Promise<any> {
debug(`Adding attributes of ${JSON.stringify(idsByType)}`);
let promises: Promise<IndividualResolverOutput>[] = [];
for (const type in idsByType) {
Expand All @@ -190,7 +193,7 @@ export async function _getAttributes(idsByType: object): Promise<any> {
if (ids) {
if (supportedType) {
debug(`Processing attributes of: ${JSON.stringify(type)}`);
promises = [...promises, ...build(supportedType, ids)];
promises = [...promises, ...build(supportedType, ids, abortSignal)];
} else {
debug(`Cannot get attributes of type: ${JSON.stringify(type)}`);
}
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export class Resolver implements IResolver {
}
}

export async function resolveSRI(userInput: ResolverInput): Promise<SRIResolverOutput> {
return await _resolveSRI(userInput);
export async function resolveSRI(userInput: ResolverInput, abortSignal?: AbortSignal): Promise<SRIResolverOutput> {
return await _resolveSRI(userInput, abortSignal);
}

export async function getAttributes(idsByType: object): Promise<any> {
return await _getAttributes(idsByType);
export async function getAttributes(idsByType: object, abortSignal?: AbortSignal): Promise<any> {
return await _getAttributes(idsByType, abortSignal);
}

export const METADATA = APIMETA;
Expand Down
11 changes: 6 additions & 5 deletions src/sri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function combineInputs(userInput: ResolverInput): string[] {
* input: array of curies
* handles querying and batching of inputs
*/
async function query(api_input: string[]) {
async function query(api_input: string[], abortSignal?: AbortSignal) {
const url = {
dev: 'https://nodenormalization-sri.renci.org/get_normalized_nodes',
ci: 'https://nodenorm.ci.transltr.io/get_normalized_nodes',
Expand All @@ -47,7 +47,7 @@ async function query(api_input: string[]) {
return axios.post(
url,
{ curies: input, conflate: true, drug_chemical_conflate: true },
{ headers: { 'User-Agent': userAgent } },
{ headers: { 'User-Agent': userAgent }, signal: abortSignal },
);
});
//convert res array into single object with all curies
Expand All @@ -57,7 +57,8 @@ async function query(api_input: string[]) {
});
return Object.assign({}, ...res);
} catch (err) {
throw new SRINodeNormFailure(`SRI resolver failed: ${err.message}`);
// throw new SRINodeNormFailure(`SRI resolver failed: ${err.message}`);
return Object.fromEntries(api_input.map((curie) => [curie, null]));
}
}

Expand Down Expand Up @@ -135,10 +136,10 @@ function mapInputSemanticTypes(originalInput: ResolverInput, result: SRIResolver
return result;
}

export async function _resolveSRI(userInput: ResolverInput): Promise<SRIResolverOutput> {
export async function _resolveSRI(userInput: ResolverInput, abortSignal?: AbortSignal): Promise<SRIResolverOutput> {
const uniqueInputIDs = combineInputs(userInput);

let queryResults = await query(uniqueInputIDs);
let queryResults = await query(uniqueInputIDs, abortSignal);

queryResults = transformResults(queryResults);

Expand Down

0 comments on commit 3761d62

Please sign in to comment.