Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate redis client to utils; use redis for registered apis #179

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions __test__/unittest/redisClient.test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/batch_edge_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const debug = Debug('bte:biothings-explorer-trapi:batch_edge_query');
import CacheHandler from './cache_handler';
import { threadId } from 'worker_threads';
import MetaKG from '@biothings-explorer/smartapi-kg';
import { StampedLog } from '@biothings-explorer/utils';
import { QueryHandlerOptions, redisClient } from '.';
import { StampedLog, redisClient } from '@biothings-explorer/utils';
import { QueryHandlerOptions } from '.';
import QEdge from './query_edge';
import { UnavailableAPITracker } from './types';
import { Record } from '@biothings-explorer/api-response-transform';
Expand Down
2 changes: 1 addition & 1 deletion src/cache_handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { redisClient } from './redis-client';
import { redisClient } from '@biothings-explorer/utils';
import Debug from 'debug';
const debug = Debug('bte:biothings-explorer-trapi:cache_handler');
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
Expand Down
36 changes: 28 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ import {
} from './types';
import BTEGraph from './graph/graph';
import QEdge from './query_edge';
import { Telemetry } from '@biothings-explorer/utils';
import { Telemetry, redisClient } from '@biothings-explorer/utils';

// Exports for external availability
export * from './types';
export { redisClient, getNewRedisClient } from './redis-client';
export { getTemplates, supportedLookups } from './inferred_mode/template_lookup';
export { default as QEdge } from './query_edge';
export { default as QNode } from './query_node';
Expand Down Expand Up @@ -87,14 +86,27 @@ 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:smartapi`).catch(console.log)
debug(redisData);
if (redisData) {
smartapiRegistry = (JSON.parse(redisData))?.smartapi;
}
}

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);
});
Expand All @@ -110,7 +122,7 @@ export default class TRAPIQueryHandler {
return unregisteredAPIs;
}

_loadMetaKG(): MetaKG {
async _loadMetaKG(): Promise<MetaKG> {
const metaKG = new MetaKG(this.path, this.predicatePath);
debug(
`Query options are: ${JSON.stringify({
Expand All @@ -119,7 +131,7 @@ export default class TRAPIQueryHandler {
})}`,
);
debug(`SmartAPI Specs read from path: ${this.path}`);
metaKG.constructMetaKGSync(this.includeReasoner, this.options);
await metaKG.constructMetaKGSync(this.includeReasoner, this.options);
return metaKG;
}

Expand Down Expand Up @@ -313,6 +325,7 @@ export default class TRAPIQueryHandler {
}

async addQueryNodes(): Promise<void> {
debug("c1");
const qNodeIDsByOriginalID: Map<string, TrapiQNode> = new Map();
const curiesToResolve = [
...Object.values(this.queryGraph.nodes).reduce((set: Set<string>, qNode) => {
Expand All @@ -323,7 +336,9 @@ export default class TRAPIQueryHandler {
return set;
}, new Set()),
] as string[];
const resolvedCuries = await resolveSRI({ unknown: curiesToResolve });
debug("d1");
const resolvedCuries = {} as {[n: string]: any} /*await resolveSRI({ unknown: curiesToResolve })*/;
debug("e1");
Object.entries(resolvedCuries).forEach(([originalCurie, resolvedEntity]) => {
if (!this.bteGraph.nodes[resolvedEntity.primaryID]) {
const category = resolvedEntity.primaryTypes?.[0]
Expand All @@ -340,6 +355,7 @@ export default class TRAPIQueryHandler {
});
}
});
debug("f1");
}

getResponse(): TrapiResponse {
Expand Down Expand Up @@ -620,13 +636,17 @@ export default class TRAPIQueryHandler {
};

async query(): Promise<void> {
debug("a");
this._initializeResponse();
debug("b");
await this.addQueryNodes();
debug("c");

const span1 = Telemetry.startSpan({ description: 'loadMetaKG' });

debug('Start to load metakg.');
const metaKG = this._loadMetaKG();
let metaKG;
try { metaKG = await this._loadMetaKG(); } catch (err) { console.log("an error");console.log(err);console.log(err.stack);}
if (!metaKG.ops.length) {
let error: string;
if (this.options.smartAPIID) {
Expand Down
Loading
Loading