From 6518716b49ecf144c80bbe0f1cc7caa2e7ceac49 Mon Sep 17 00:00:00 2001 From: NeuralFlux <40491005+NeuralFlux@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:19:23 -0400 Subject: [PATCH] fix: update team and api metakg endpoints to use globally loaded graph in async fashion --- src/routes/index.ts | 4 +-- .../v1/meta_knowledge_graph_v1_by_api.ts | 27 ++++++++++++++++--- .../v1/meta_knowledge_graph_v1_by_team.ts | 25 ++++++++++++++--- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/routes/index.ts b/src/routes/index.ts index ffa0673..80c9e6e 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -52,9 +52,9 @@ export const tasks: TaskByRoute = { asyncquery_v1_by_team: (taskInfo: TaskInfo) => V1AsyncQueryByTeam.task(taskInfo), // load MetaKG from global meta_knowledge_graph_v1: (taskInfo: TaskInfo) => V1MetaKG.task(taskInfo), + meta_knowledge_graph_v1_by_team: (taskInfo: TaskInfo) => V1MetaKGByTeam.task(taskInfo), + meta_knowledge_graph_v1_by_api: (taskInfo: TaskInfo) => V1MetaKGByAPI.task(taskInfo), // Not threaded due to being lightweight/speed being higher priority // performance: (taskInfo: TaskInfo) => Performance.task(taskInfo), // metakg: (taskInfo: TaskInfo) => MetaKG.task(taskInfo), - // meta_knowledge_graph_v1_by_api: (taskInfo: TaskInfo) => V1MetaKGByAPI.task(taskInfo), - // meta_knowledge_graph_v1_by_team: (taskInfo: TaskInfo) => V1MetaKGByTeam.task(taskInfo), }; diff --git a/src/routes/v1/meta_knowledge_graph_v1_by_api.ts b/src/routes/v1/meta_knowledge_graph_v1_by_api.ts index ea6e165..0e25c13 100644 --- a/src/routes/v1/meta_knowledge_graph_v1_by_api.ts +++ b/src/routes/v1/meta_knowledge_graph_v1_by_api.ts @@ -1,23 +1,42 @@ import handler from "../../controllers/meta_knowledge_graph"; import * as utils from "../../utils/common"; +import path from "path"; +import { TaskInfo } from "@biothings-explorer/types"; +import { runTask, taskResponse, taskError } from "../../controllers/threading/threadHandler"; import { Express, NextFunction, Request, Response, RequestHandler } from "express"; +import MetaKnowledgeGraph from "@biothings-explorer/smartapi-kg"; + class MetaKGByAPI { setRoutes(app: Express) { app - .route("/v1/smartapi/:smartapiID/meta_knowledge_graph") + .route("/v1/smartapi/:smartAPIID/meta_knowledge_graph") .get((async (req: Request, res: Response, next: NextFunction) => { try { - const metaKGHandler = new handler(req.params.smartapiID); - const kg = await metaKGHandler.getKG(); + const response = await runTask(req, res, path.parse(__filename).name); res.setHeader("Content-Type", "application/json"); - res.end(JSON.stringify(kg)); + res.end(JSON.stringify(response)); } catch (error) { next(error); } })as RequestHandler) .all(utils.methodNotAllowed); } + + async task(taskInfo: TaskInfo) { + try { + const metaKGHandler = new handler(taskInfo.data.smartAPIID, undefined); + let metakg = undefined; + // initialize MetaKG only if ops are provided because handler logic is built upon that + if (taskInfo.data.options.metakg_ops !== undefined) + metakg = new MetaKnowledgeGraph(undefined, undefined, taskInfo.data.options.metakg_ops); + const kg = await metaKGHandler.getKG(metakg); + // response.logs = utils.filterForLogLevel(response.logs, options.logLevel); + return taskResponse(kg); + } catch (error) { + taskError(error as Error); + } + } } export default new MetaKGByAPI(); diff --git a/src/routes/v1/meta_knowledge_graph_v1_by_team.ts b/src/routes/v1/meta_knowledge_graph_v1_by_team.ts index 03e9061..3d3f90e 100644 --- a/src/routes/v1/meta_knowledge_graph_v1_by_team.ts +++ b/src/routes/v1/meta_knowledge_graph_v1_by_team.ts @@ -1,23 +1,42 @@ import handler from "../../controllers/meta_knowledge_graph"; +import path from "path"; +import { TaskInfo } from "@biothings-explorer/types"; import * as utils from "../../utils/common"; +import { runTask, taskResponse, taskError } from "../../controllers/threading/threadHandler"; import { Express, NextFunction, Request, Response, RequestHandler } from "express"; +import MetaKnowledgeGraph from "@biothings-explorer/smartapi-kg"; + class MetaKGByTeam { setRoutes(app: Express) { app .route("/v1/team/:teamName/meta_knowledge_graph") .get((async (req: Request, res: Response, next: NextFunction) => { try { - const metaKGHandler = new handler(undefined, req.params.teamName); - const kg = await metaKGHandler.getKG(); + const response = await runTask(req, res, path.parse(__filename).name); res.setHeader("Content-Type", "application/json"); - res.end(JSON.stringify(kg)); + res.end(JSON.stringify(response)); } catch (error) { next(error); } }) as RequestHandler) .all(utils.methodNotAllowed); } + + async task(taskInfo: TaskInfo) { + try { + const metaKGHandler = new handler(undefined, taskInfo.data.teamName); + let metakg = undefined; + // initialize MetaKG only if ops are provided because handler logic is built upon that + if (taskInfo.data.options.metakg_ops !== undefined) + metakg = new MetaKnowledgeGraph(undefined, undefined, taskInfo.data.options.metakg_ops); + const kg = await metaKGHandler.getKG(metakg); + // response.logs = utils.filterForLogLevel(response.logs, options.logLevel); + return taskResponse(kg); + } catch (error) { + taskError(error as Error); + } + } } export default new MetaKGByTeam();