Skip to content

Commit

Permalink
perf: ⚡ increase the speed to generate TRAPI response
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinxin90 committed Feb 14, 2021
1 parent 8c0e585 commit ea23686
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 39 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"axios": "^0.21.1",
"biomedical_id_resolver": "^2.1.1",
"biomedical_id_resolver": "^2.2.1",
"biothings-explorer-graphql": "2.1.1",
"body-parser": "^1.19.0",
"camelcase": "^6.2.0",
Expand Down
67 changes: 36 additions & 31 deletions src/controllers/QueryGraphHandler/knowledge_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,29 @@ module.exports = class KnowledgeGraph {

_createInputNode(record) {
return {
[helper._getInputID(record)]: {
category: "biolink:" + helper._getInputCategory(record),
name: helper._getInputLabel(record),
attributes: [
{
name: "equivalent_identifiers",
value: helper._getInputEquivalentIds(record),
type: "biolink:id"
}
]
}
category: "biolink:" + helper._getInputCategory(record),
name: helper._getInputLabel(record),
attributes: [
{
name: "equivalent_identifiers",
value: helper._getInputEquivalentIds(record),
type: "biolink:id"
}
]
}
}

_createOutputNode(record) {
return {
[helper._getOutputID(record)]: {
category: "biolink:" + helper._getOutputCategory(record),
name: helper._getOutputLabel(record),
attributes: [
{
name: "equivalent_identifiers",
value: helper._getOutputEquivalentIds(record),
type: "biolink:id"
}
]
}
category: "biolink:" + helper._getOutputCategory(record),
name: helper._getOutputLabel(record),
attributes: [
{
name: "equivalent_identifiers",
value: helper._getOutputEquivalentIds(record),
type: "biolink:id"
}
]
}
}

Expand Down Expand Up @@ -80,20 +76,29 @@ module.exports = class KnowledgeGraph {

_createEdge(record) {
return {
[helper._createUniqueEdgeID(record)]: {
predicate: "biolink:" + ((typeof record.$edge_metadata.trapi_qEdge_obj.getQueryPredicate() === "undefined") ? record.$edge_metadata.predicate : record.$edge_metadata.trapi_qEdge_obj.getQueryPredicate()),
subject: helper._getInputID(record),
object: helper._getOutputID(record),
attributes: this._createAttributes(record)
}
predicate: "biolink:" + ((typeof record.$edge_metadata.trapi_qEdge_obj.getQueryPredicate() === "undefined") ? record.$edge_metadata.predicate : record.$edge_metadata.trapi_qEdge_obj.getQueryPredicate()),
subject: helper._getInputID(record),
object: helper._getOutputID(record),
attributes: this._createAttributes(record)
}
}

update(queryResult) {
queryResult.map(record => {
this.nodes = { ...this.nodes, ...this._createInputNode(record) };
this.nodes = { ...this.nodes, ...this._createOutputNode(record) };
this.edges = { ...this.edges, ...this._createEdge(record) };
debug(`record input: ${JSON.stringify(record.$input)}`);
debug(`record output: ${JSON.stringify(record.$output)}`);
if (!(helper._getInputID(record) in this.nodes)) {
this.nodes[helper._getInputID(record)] = this._createInputNode(record);
}
if (!(helper._getOutputID(record) in this.nodes)) {
this.nodes[helper._getOutputID(record)] = this._createOutputNode(record);
}
if (!(helper._createUniqueEdgeID(record) in this.edges)) {
this.edges[helper._createUniqueEdgeID(record)] = this._createEdge(record);
}
// this.nodes = { ...this.nodes, ...this._createInputNode(record) };
// this.nodes = { ...this.nodes, ...this._createOutputNode(record) };
// this.edges = { ...this.edges, ...this._createEdge(record) };

})
this.kg = {
Expand Down
22 changes: 18 additions & 4 deletions src/controllers/QueryGraphHandler/update_nodes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const id_resolver = require("biomedical_id_resolver");
const GraphHelper = require("./helper");
const _ = require("lodash");
const debug = require("debug")("biothings-explorer-trapi:nodeUpdateHandler");

Expand Down Expand Up @@ -77,10 +76,25 @@ module.exports = class NodesUpdateHandler {
* @param {object} queryResult - query response
*/
update(queryResult) {
const node_dict = {};
const id_dict = {};
// queryResult.map(record => {
// record.$edge_metadata.trapi_qEdge_obj.getOutputNode().updateEquivalentIDs(
// this._createEquivalentIDsObject(record)
// );
// })
queryResult.map(record => {
record.$edge_metadata.trapi_qEdge_obj.getOutputNode().updateEquivalentIDs(
this._createEquivalentIDsObject(record)
);
const nodeID = record.$edge_metadata.trapi_qEdge_obj.getOutputNode().getID();
if (!(nodeID in id_dict)) {
id_dict[nodeID] = {};
node_dict[nodeID] = record.$edge_metadata.trapi_qEdge_obj.getOutputNode();
}
if (!(record.$output.obj.primaryID in id_dict[nodeID])) {
id_dict[nodeID][record.$output.obj.primaryID] = record.$output.obj;
}
})
for (const nodeID in id_dict) {
node_dict[nodeID].updateEquivalentIDs(id_dict[nodeID]);
}
}
}

0 comments on commit ea23686

Please sign in to comment.