Skip to content

Commit

Permalink
Merge pull request #183 from biothings/kg-refactor
Browse files Browse the repository at this point in the history
Use MetaKG from main thread, specs from redis (if avaliable)
  • Loading branch information
tokebe authored May 21, 2024
2 parents b0fc94d + 505922a commit da2271f
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MetaKG from '@biothings-explorer/smartapi-kg';
import MetaKG, { SmartAPIQueryResult } from '@biothings-explorer/smartapi-kg';
import path from 'path';
import QueryGraph from './query_graph';
import KnowledgeGraph from './graph/knowledge_graph';
Expand Down Expand Up @@ -72,14 +72,21 @@ export default class TRAPIQueryHandler {

async findUnregisteredAPIs() {
const configListAPIs = this.options.apiList['include'];
const smartapiRegistry = await fs.readFile(this.path, { encoding: 'utf8' });

let smartapiRegistry: SmartAPIQueryResult;
if (this.options.smartapi) {
smartapiRegistry = this.options.smartapi;
} else {
const file = await fs.readFile(this.path, "utf-8");
smartapiRegistry = JSON.parse(file);
}

const smartapiIds: string[] = [];
const inforesIds: string[] = [];
const unregisteredAPIs: string[] = [];

// TODO typing for smartapiRegistration
JSON.parse(smartapiRegistry).hits.forEach((smartapiRegistration) => {
smartapiRegistry.hits.forEach((smartapiRegistration) => {
smartapiIds.push(smartapiRegistration._id);
inforesIds.push(smartapiRegistration.info?.['x-translator']?.infores);
});
Expand All @@ -95,14 +102,23 @@ export default class TRAPIQueryHandler {
return unregisteredAPIs;
}

_loadMetaKG(): MetaKG {
const metaKG = new MetaKG(this.path, this.predicatePath);
async _loadMetaKG(): Promise<MetaKG> {
debug(
`Query options are: ${JSON.stringify({
...this.options,
schema: this.options.schema ? this.options.schema.info.version : 'not included',
metakg: "",
smartapi: ""
})}`,
);

if (this.options.metakg) {
const metaKG = new MetaKG(undefined, undefined, (this.options as any).metakg);
metaKG.filterKG(this.options);
return metaKG;
}

const metaKG = new MetaKG(this.path, this.predicatePath);
debug(`SmartAPI Specs read from path: ${this.path}`);
metaKG.constructMetaKGSync(this.includeReasoner, this.options);
return metaKG;
Expand Down Expand Up @@ -611,7 +627,7 @@ export default class TRAPIQueryHandler {
const span1 = Telemetry.startSpan({ description: 'loadMetaKG' });

debug('Start to load metakg.');
const metaKG = this._loadMetaKG();
const metaKG = await this._loadMetaKG();
if (!metaKG.ops.length) {
let error: string;
if (this.options.smartAPIID) {
Expand Down

0 comments on commit da2271f

Please sign in to comment.