Skip to content

Commit

Permalink
fix: update team and api metakg endpoints to use globally loaded grap…
Browse files Browse the repository at this point in the history
…h in async fashion
  • Loading branch information
NeuralFlux committed Oct 31, 2024
1 parent 1e3f83d commit 6518716
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
27 changes: 23 additions & 4 deletions src/routes/v1/meta_knowledge_graph_v1_by_api.ts
Original file line number Diff line number Diff line change
@@ -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();
25 changes: 22 additions & 3 deletions src/routes/v1/meta_knowledge_graph_v1_by_team.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 6518716

Please sign in to comment.