From e72edd0ce721fd029be8891de24ba45ce2660e1a Mon Sep 17 00:00:00 2001 From: Rohan Juneja Date: Thu, 7 Mar 2024 18:23:38 -0800 Subject: [PATCH 1/4] receive metakg from main thread; receieve specs from redis --- src/index.ts | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index e0a029ad..9d5aebe8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import MetaKG from '@biothings-explorer/smartapi-kg'; +import MetaKG, { SmartAPIKGOperationObject } from '@biothings-explorer/smartapi-kg'; import path from 'path'; import QueryGraph from './query_graph'; import KnowledgeGraph from './graph/knowledge_graph'; @@ -28,6 +28,7 @@ import { } from './types'; import BTEGraph from './graph/graph'; import QEdge from './query_edge'; +import { redisClient } from './redis-client'; import { Telemetry } from '@biothings-explorer/utils'; // Exports for external availability @@ -51,6 +52,7 @@ export interface QueryHandlerOptions { resolveOutputIDs?: boolean; submitter?: string; caching?: boolean; // from request url query values + metakg?: SmartAPIKGOperationObject[]; // list of meta kg ops EDGE_ATTRIBUTES_USED_IN_RECORD_HASH?: string[]; } export default class TRAPIQueryHandler { @@ -87,14 +89,26 @@ export default class TRAPIQueryHandler { async findUnregisteredAPIs() { const configListAPIs = this.options.apiList['include']; - const smartapiRegistry = await fs.readFile(this.path, { encoding: 'utf8' }); + + let smartapiRegistry; + if (redisClient.clientEnabled) { + const redisData = await redisClient.client.getTimeout(`bte:smartapi:specs`) + if (redisData) { + smartapiRegistry = JSON.parse(redisData); + } + } + + if (!smartapiRegistry) { + 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); }); @@ -110,14 +124,20 @@ export default class TRAPIQueryHandler { return unregisteredAPIs; } - _loadMetaKG(): MetaKG { - const metaKG = new MetaKG(this.path, this.predicatePath); + async _loadMetaKG(): Promise { debug( `Query options are: ${JSON.stringify({ ...this.options, schema: this.options.schema ? this.options.schema.info.version : 'not included', + metakg: "" })}`, ); + + if (this.options.metakg) { + return new MetaKG(undefined, undefined, (this.options as any).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; @@ -626,7 +646,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) { From fa889e421b9dcb91dceb373ed5a90b251b749455 Mon Sep 17 00:00:00 2001 From: Rohan Juneja Date: Mon, 11 Mar 2024 17:46:36 -0700 Subject: [PATCH 2/4] filter meta kg when it is sent via options --- src/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 9d5aebe8..346a4c79 100644 --- a/src/index.ts +++ b/src/index.ts @@ -134,7 +134,9 @@ export default class TRAPIQueryHandler { ); if (this.options.metakg) { - return new MetaKG(undefined, undefined, (this.options as any).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); From 91e398a4eac957cc77c907f1c6f3fa5d16f31b0d Mon Sep 17 00:00:00 2001 From: Rohan Juneja Date: Wed, 15 May 2024 17:58:05 -0700 Subject: [PATCH 3/4] pass specs through thread instead of redis --- src/index.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index 84747d1f..a692179f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,6 @@ import { import { QueryHandlerOptions } from '@biothings-explorer/types'; import BTEGraph from './graph/graph'; import QEdge from './query_edge'; -import { redisClient } from '@biothings-explorer/utils'; import { Telemetry } from '@biothings-explorer/utils'; // Exports for external availability @@ -75,16 +74,11 @@ export default class TRAPIQueryHandler { const configListAPIs = this.options.apiList['include']; let smartapiRegistry; - if (redisClient.clientEnabled) { - const redisData = await redisClient.client.getTimeout(`bte:smartapi:specs`) - if (redisData) { - smartapiRegistry = JSON.parse(redisData); - } - } - - if (!smartapiRegistry) { - const file = await fs.readFile(this.path, "utf-8"); - smartapiRegistry = JSON.parse(file); + 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[] = []; @@ -113,7 +107,8 @@ export default class TRAPIQueryHandler { `Query options are: ${JSON.stringify({ ...this.options, schema: this.options.schema ? this.options.schema.info.version : 'not included', - metakg: "" + metakg: "", + smartapi: "" })}`, ); From 505922a880bddef46f3cbba503713ad622a1ba19 Mon Sep 17 00:00:00 2001 From: tokebe <43009413+tokebe@users.noreply.github.com> Date: Thu, 16 May 2024 11:42:00 -0400 Subject: [PATCH 4/4] chore: fix types after merge from main --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index a692179f..59401b36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -73,7 +73,7 @@ export default class TRAPIQueryHandler { async findUnregisteredAPIs() { const configListAPIs = this.options.apiList['include']; - let smartapiRegistry; + let smartapiRegistry: SmartAPIQueryResult; if (this.options.smartapi) { smartapiRegistry = this.options.smartapi; } else {