Skip to content

Commit

Permalink
feat: use types package
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Mar 22, 2024
1 parent 97ddbdc commit 996370a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 123 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@
"@biothings-explorer/node-expansion": "workspace:../node-expansion",
"@biothings-explorer/smartapi-kg": "workspace:../smartapi-kg",
"@biothings-explorer/utils": "workspace:../utils",
"@sentry/node": "^7.74.1",
"async": "^3.2.4",
"@biothings-explorer/types": "workspace:../types",
"biolink-model": "workspace:../biolink-model",
"biomedical_id_resolver": "workspace:../biomedical_id_resolver",
"@sentry/node": "^7.74.1",
"async": "^3.2.4",
"chi-square-p-value": "^1.0.5",
"debug": "^4.3.4",
"ioredis": "^5.3.2",
Expand Down
37 changes: 4 additions & 33 deletions src/batch_edge_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ import CacheHandler from './cache_handler';
import { threadId } from 'worker_threads';
import MetaKG from '@biothings-explorer/smartapi-kg';
import { StampedLog } from '@biothings-explorer/utils';
import { QueryHandlerOptions } from '.';
import { QueryHandlerOptions } from '@biothings-explorer/types';
import QEdge from './query_edge';
import { UnavailableAPITracker } from './types';
import { Record } from '@biothings-explorer/api-response-transform';

export interface BatchEdgeQueryOptions extends QueryHandlerOptions {
recordHashEdgeAttributes: string[];
caching: boolean;
}

export default class BatchEdgeQueryHandler {
metaKG: MetaKG;
subscribers: any[];
logs: StampedLog[];
caching: boolean;
options: QueryHandlerOptions;
resolveOutputIDs: boolean;
qEdges: QEdge | QEdge[];
constructor(metaKG: MetaKG, resolveOutputIDs = true, options?: BatchEdgeQueryOptions) {
this.metaKG = metaKG;
this.subscribers = [];
this.logs = [];
this.caching = options && options.caching;
this.options = options;
Expand Down Expand Up @@ -101,7 +100,7 @@ export default class BatchEdgeQueryHandler {
const equivalentAlreadyIncluded = qEdge
.getInputNode()
.getEquivalentIDs()
[curie].equivalentIDs.some((equivalentCurie) => reducedCuries.includes(equivalentCurie));
[curie].equivalentIDs.some((equivalentCurie) => reducedCuries.includes(equivalentCurie));
if (!equivalentAlreadyIncluded) {
reducedCuries.push(curie);
} else {
Expand All @@ -113,8 +112,7 @@ export default class BatchEdgeQueryHandler {
strippedCuries.push(...nodeStrippedCuries);
if (nodeStrippedCuries.length > 0) {
debug(
`stripped (${nodeStrippedCuries.length}) duplicate equivalent curies from ${
node.id
`stripped (${nodeStrippedCuries.length}) duplicate equivalent curies from ${node.id
}: ${nodeStrippedCuries.join(',')}`,
);
}
Expand Down Expand Up @@ -174,31 +172,4 @@ export default class BatchEdgeQueryHandler {
debug('Update nodes completed!');
return queryRecords;
}

/**
* Register subscribers
* @param {object} subscriber
*/
subscribe(subscriber): void {
this.subscribers.push(subscriber);
}

/**
* Unsubscribe a listener
* @param {object} subscriber
*/
unsubscribe(subscriber): void {
this.subscribers = this.subscribers.filter((fn) => {
if (fn != subscriber) return fn;
});
}

/**
* Nofity all listeners
*/
notify(res): void {
this.subscribers.map((subscriber) => {
subscriber.update(res);
});
}
}
6 changes: 3 additions & 3 deletions src/cache_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { Readable, Transform } from 'stream';
import { Record, RecordPackage } from '@biothings-explorer/api-response-transform';
import { threadId } from 'worker_threads';
import MetaKG from '../../smartapi-kg/built';
import { QueryHandlerOptions } from '.';
import QEdge from './query_edge';
import { QueryHandlerOptions } from '@biothings-explorer/types';

export interface RecordPacksByQedgeMetaKGHash {
[QEdgeHash: string]: RecordPackage;
Expand Down Expand Up @@ -113,7 +113,7 @@ export default class CacheHandler {
}

async categorizeEdges(qEdges: QEdge[]): Promise<{ cachedRecords: Record[]; nonCachedQEdges: QEdge[] }> {
if (this.cacheEnabled === false || process.env.INTERNAL_DISABLE_REDIS) {
if (this.cacheEnabled === false || process.env.INTERNAL_DISABLE_REDIS === "true") {
return {
cachedRecords: [],
nonCachedQEdges: qEdges,
Expand Down Expand Up @@ -210,7 +210,7 @@ export default class CacheHandler {
}

async cacheEdges(queryRecords: Record[]): Promise<void> {
if (this.cacheEnabled === false || process.env.INTERNAL_DISABLE_REDIS) {
if (this.cacheEnabled === false || process.env.INTERNAL_DISABLE_REDIS === "true") {
if (global.parentPort) {
global.parentPort.postMessage({ threadId, cacheDone: true });
}
Expand Down
20 changes: 9 additions & 11 deletions src/edge_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import BatchEdgeQueryHandler, { BatchEdgeQueryOptions } from './batch_edge_query
import { Telemetry } from '@biothings-explorer/utils';
import QEdge from './query_edge';
import MetaKG from '@biothings-explorer/smartapi-kg';
import { QueryHandlerOptions } from '.';
import { QueryHandlerOptions } from '@biothings-explorer/types';
import { Record } from '@biothings-explorer/api-response-transform';
import { UnavailableAPITracker } from './types';
import { RecordsByQEdgeID } from './results_assembly/query_results';
Expand Down Expand Up @@ -108,7 +108,7 @@ export default class QueryEdgeManager {
}
debug(
`(5) Sending next edge '${nextQEdge.getID()}' ` +
`WITH entity count...(${nextQEdge.subject.entity_count || nextQEdge.object.entity_count})`,
`WITH entity count...(${nextQEdge.subject.entity_count || nextQEdge.object.entity_count})`,
);
return this.preSendOffCheck(nextQEdge);
}
Expand All @@ -117,19 +117,18 @@ export default class QueryEdgeManager {
this._qEdges.forEach((qEdge) => {
debug(
`'${qEdge.getID()}'` +
` : (${qEdge.subject.entity_count || 0}) ` +
`${qEdge.reverse ? '<--' : '-->'}` +
` (${qEdge.object.entity_count || 0})`,
` : (${qEdge.subject.entity_count || 0}) ` +
`${qEdge.reverse ? '<--' : '-->'}` +
` (${qEdge.object.entity_count || 0})`,
);
});
}

_logSkippedQueries(unavailableAPIs: UnavailableAPITracker): void {
Object.entries(unavailableAPIs).forEach(([api, { skippedQueries }]) => {
if (skippedQueries > 0) {
const skipMessage = `${skippedQueries} additional quer${skippedQueries > 1 ? 'ies' : 'y'} to ${api} ${
skippedQueries > 1 ? 'were' : 'was'
} skipped as the API was unavailable.`;
const skipMessage = `${skippedQueries} additional quer${skippedQueries > 1 ? 'ies' : 'y'} to ${api} ${skippedQueries > 1 ? 'were' : 'was'
} skipped as the API was unavailable.`;
debug(skipMessage);
this.logs.push(new LogEntry('WARNING', null, skipMessage).getLog());
}
Expand Down Expand Up @@ -195,7 +194,7 @@ export default class QueryEdgeManager {
const objectCuries = qEdge.object.curie;
debug(
`'${qEdge.getID()}' Reversed[${qEdge.reverse}] (${JSON.stringify(subjectCuries.length || 0)})` +
`--(${JSON.stringify(objectCuries.length || 0)}) entities / (${records.length}) records.`,
`--(${JSON.stringify(objectCuries.length || 0)}) entities / (${records.length}) records.`,
);
// debug(`IDS SUB ${JSON.stringify(sub_count)}`)
// debug(`IDS OBJ ${JSON.stringify(obj_count)}`)
Expand Down Expand Up @@ -395,8 +394,7 @@ export default class QueryEdgeManager {
new LogEntry(
'INFO',
null,
`Executing ${currentQEdge.getID()}${currentQEdge.isReversed() ? ' (reversed)' : ''}: ${
currentQEdge.subject.id
`Executing ${currentQEdge.getID()}${currentQEdge.isReversed() ? ' (reversed)' : ''}: ${currentQEdge.subject.id
} ${currentQEdge.isReversed() ? '<--' : '-->'} ${currentQEdge.object.id}`,
).getLog(),
);
Expand Down
3 changes: 1 addition & 2 deletions src/graph/knowledge_graph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { toArray } from '../utils';
import Debug from 'debug';
import {
APIList,
TrapiAttribute,
TrapiKnowledgeGraph,
TrapiKGEdge,
Expand All @@ -10,11 +9,11 @@ import {
TrapiKGNodes,
TrapiQualifier,
TrapiSource,
APIDefinition,
} from '../types';
import KGNode from './kg_node';
import KGEdge from './kg_edge';
import { BTEGraphUpdate } from './graph';
import { APIDefinition } from '@biothings-explorer/types';

const debug = Debug('bte:biothings-explorer-trapi:KnowledgeGraph');

Expand Down
34 changes: 8 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import InferredQueryHandler from './inferred_mode/inferred_mode';
import KGNode from './graph/kg_node';
import KGEdge from './graph/kg_edge';
import {
APIList,
TrapiAuxGraphCollection,
TrapiAuxiliaryGraph,
TrapiQNode,
TrapiQueryGraph,
TrapiResponse,
TrapiResult,
} from './types';
import { QueryHandlerOptions } from '@biothings-explorer/types';
import BTEGraph from './graph/graph';
import QEdge from './query_edge';
import { Telemetry } from '@biothings-explorer/utils';
Expand All @@ -38,20 +38,6 @@ export { default as QNode } from './query_node';
export { default as InvalidQueryGraphError } from './exceptions/invalid_query_graph_error';
export * from './qedge2apiedge';

export interface QueryHandlerOptions {
provenanceUsesServiceProvider?: boolean;
smartAPIID?: string;
teamName?: string;
enableIDResolution?: boolean;
// TODO: type instances of `any`
apiList?: APIList;
schema?: any; // might be hard to type -- it's the entire TRAPI schema IIRC
dryrun?: boolean;
resolveOutputIDs?: boolean;
submitter?: string;
caching?: boolean; // from request url query values
EDGE_ATTRIBUTES_USED_IN_RECORD_HASH?: string[];
}
export default class TRAPIQueryHandler {
logs: StampedLog[];
options: QueryHandlerOptions;
Expand Down Expand Up @@ -447,13 +433,11 @@ export default class TRAPIQueryHandler {

let log_msg: string;
if (currentQEdge.reverse) {
log_msg = `qEdge ${currentQEdge.id} (reversed): ${currentQEdge.object.categories} > ${
currentQEdge.predicate ? `${currentQEdge.predicate} > ` : ''
}${currentQEdge.subject.categories}`;
log_msg = `qEdge ${currentQEdge.id} (reversed): ${currentQEdge.object.categories} > ${currentQEdge.predicate ? `${currentQEdge.predicate} > ` : ''
}${currentQEdge.subject.categories}`;
} else {
log_msg = `qEdge ${currentQEdge.id}: ${currentQEdge.subject.categories} > ${
currentQEdge.predicate ? `${currentQEdge.predicate} > ` : ''
}${currentQEdge.object.categories}`;
log_msg = `qEdge ${currentQEdge.id}: ${currentQEdge.subject.categories} > ${currentQEdge.predicate ? `${currentQEdge.predicate} > ` : ''
}${currentQEdge.object.categories}`;
}
this.logs.push(new LogEntry('INFO', null, log_msg).getLog());

Expand Down Expand Up @@ -494,9 +478,8 @@ export default class TRAPIQueryHandler {
});
const qEdgesLogStr = qEdgesToLog.length > 1 ? `[${qEdgesToLog.join(', ')}]` : `${qEdgesToLog.join(', ')}`;
if (len > 0) {
const terminateLog = `Query Edge${len !== 1 ? 's' : ''} ${qEdgesLogStr} ${
len !== 1 ? 'have' : 'has'
} no MetaKG edges. Your query terminates.`;
const terminateLog = `Query Edge${len !== 1 ? 's' : ''} ${qEdgesLogStr} ${len !== 1 ? 'have' : 'has'
} no MetaKG edges. Your query terminates.`;
debug(terminateLog);
this.logs.push(new LogEntry('WARNING', null, terminateLog).getLog());
return false;
Expand Down Expand Up @@ -610,8 +593,7 @@ export default class TRAPIQueryHandler {
new LogEntry(
'INFO',
null,
`Execution Summary: (${KGNodes}) nodes / (${kgEdges}) edges / (${results}) results; (${resultQueries}/${queries}) queries${
cached ? ` (${cached} cached qEdges)` : ''
`Execution Summary: (${KGNodes}) nodes / (${kgEdges}) edges / (${results}) results; (${resultQueries}/${queries}) queries${cached ? ` (${cached} cached qEdges)` : ''
} returned results from(${sources.length}) unique API${sources.length === 1 ? 's' : ''}`,
).getLog(),
new LogEntry('INFO', null, `APIs: ${sources.join(', ')} `).getLog(),
Expand Down
20 changes: 9 additions & 11 deletions src/inferred_mode/inferred_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import async from 'async';
import biolink from '../biolink';
import { getTemplates, MatchedTemplate, TemplateLookup } from './template_lookup';
import { scaled_sigmoid, inverse_scaled_sigmoid } from '../results_assembly/score';
import TRAPIQueryHandler, { QueryHandlerOptions } from '../index';
import TRAPIQueryHandler from '../index';
import { QueryHandlerOptions } from '@biothings-explorer/types';
import {
CompactQualifiers,
TrapiAuxGraphCollection,
Expand Down Expand Up @@ -384,9 +385,9 @@ export default class InferredQueryHandler {
if (typeof combinedResponse.message.results[resultID].analyses[0].score !== 'undefined') {
combinedResponse.message.results[resultID].analyses[0].score = resScore
? scaled_sigmoid(
inverse_scaled_sigmoid(combinedResponse.message.results[resultID].analyses[0].score) +
inverse_scaled_sigmoid(resScore),
)
inverse_scaled_sigmoid(combinedResponse.message.results[resultID].analyses[0].score) +
inverse_scaled_sigmoid(resScore),
)
: combinedResponse.message.results[resultID].analyses[0].score;
} else {
combinedResponse.message.results[resultID].analyses[0].score = resScore;
Expand Down Expand Up @@ -554,11 +555,9 @@ export default class InferredQueryHandler {
const message = [
`Addition of ${creativeLimitHit} results from Template ${i + 1}`,
Object.keys(combinedResponse.message.results).length === this.CREATIVE_LIMIT ? ' meets ' : ' exceeds ',
`creative result maximum of ${this.CREATIVE_LIMIT} (reaching ${
Object.keys(combinedResponse.message.results).length
`creative result maximum of ${this.CREATIVE_LIMIT} (reaching ${Object.keys(combinedResponse.message.results).length
} merged). `,
`Response will be truncated to top-scoring ${this.CREATIVE_LIMIT} results. Skipping remaining ${
subQueries.length - (i + 1)
`Response will be truncated to top-scoring ${this.CREATIVE_LIMIT} results. Skipping remaining ${subQueries.length - (i + 1)
} `,
subQueries.length - (i + 1) === 1 ? `template.` : `templates.`,
].join('');
Expand All @@ -583,9 +582,8 @@ export default class InferredQueryHandler {
const total =
Object.values(mergedResultsCount).reduce((sum, count) => sum + count, 0) +
Object.keys(mergedResultsCount).length;
const message = `Merging Summary: (${total}) inferred-template results were merged into (${
Object.keys(mergedResultsCount).length
}) final results, reducing result count by (${total - Object.keys(mergedResultsCount).length})`;
const message = `Merging Summary: (${total}) inferred-template results were merged into (${Object.keys(mergedResultsCount).length
}) final results, reducing result count by (${total - Object.keys(mergedResultsCount).length})`;
debug(message);
combinedResponse.logs.push(new LogEntry('INFO', null, message).getLog());
}
Expand Down
34 changes: 0 additions & 34 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
// TODO change how types are exported on smartapi-kg so this is a normal package import
import { SmartAPISpec } from '@biothings-explorer/smartapi-kg';
import { TrapiLog } from '@biothings-explorer/utils';

declare global {
var missingAPIs: SmartAPISpec[];
var BIOLINK_VERSION: string;
var SCHEMA_VERSION: string;
var parentPort: MessagePort;
var cachingTasks: Promise<void>[];
var queryInformation: {
queryGraph: TrapiQueryGraph;
isCreativeMode: boolean;
creativeTemplate?: string;
totalRecords: number;
jobID?: string;
callback_url?: string;
};
var job: {
log: (logString: string) => void;
}; // TODO type as Piscina job
}

export interface TrapiQNode {
ids?: string[];
categories?: string[];
Expand Down Expand Up @@ -168,19 +147,6 @@ export interface TrapiResponse {
logs: TrapiLog[];
}

export type APIDefinition = {
// Must have one of id or infores
id?: string; // SmartAPI ID, takes priority over infores
name: string; // Must match name on SmartAPI registry
infores?: string; // infores of API
primarySource?: boolean;
} & ({ id: string } | { infores: string });

export interface APIList {
include: APIDefinition[];
// takes priority over include, taking into account id/infores prioritization
exclude: APIDefinition[];
}

export interface UnavailableAPITracker {
[server: string]: { skip: boolean; skippedQueries: number };
Expand Down
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"biolink-model": ["../biolink-model"],
"biomedical_id_resolver": ["../biomedical_id_resolver"],
"@biothings-explorer/smartapi-kg": ["../smartapi-kg"],
"@biothings-explorer/utils": ["../utils"]
"@biothings-explorer/utils": ["../utils"],
"@biothings-explorer/types": ["../types"]
}
},
"include": ["./src/**/*", "./src/biolink.json", "./src/smartapi_specs.json", "./src/predicates.json"],
Expand All @@ -29,6 +30,9 @@
},
{
"path": "../utils"
},
{
"path": "../types"
}
]
}

0 comments on commit 996370a

Please sign in to comment.