Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // import { EntityType, RelationType } from 'kpb-core-pkg';
- import * as request from 'request-promise-native';
- import { Logger, LoggerCst } from 'kpb-logger-pkg';
- // import * as tagProcesser from './tagFunctions';
- import { EntityInterface, RelationInterface, VertexInterface, EdgeInterface, ResponsedPath } from './graphInterfaces';
- import { formatBasicProperties, gremlinQuery, formatAddRelationProperties, buildAddVertexQuery, buildAddRelationQuery, buildCountVerticesQuery, buildGetVerticesQuery, buildGetRelationsQuery, formatFindPathProperties, buildFindPathQuery } from './gremlinQueryBuilder';
- const log: Logger = new Logger('Janus.ts', LoggerCst.LEVELS_NAME[process.env.LOGGER_LEVEL]);
- export class Janus {
- constructor(
- private _graphName: string,
- private _host: string,
- private _port: number,
- private _username: string,
- private _password: string,
- private _token: string,
- ) { }
- get graphName(): string {
- return this._graphName;
- }
- get host(): string {
- return this._host;
- }
- get port(): number {
- return this._port;
- }
- get username(): string {
- return this._username;
- }
- get password(): string {
- return this._password;
- }
- get token(): string {
- return this._token;
- }
- set token(token) {
- this._token = token;
- }
- static async refreshToken(
- username: string,
- password: string,
- host: string,
- port: number,
- ): Promise<request.RequestPromise> {
- const baseUrl: string = `https://${username}:${password}@${host}:${port}/session`;
- const options = {
- uri: baseUrl,
- json: true,
- resolveWithFullResponse: true,
- };
- return await request.get(options);
- }
- static async authenticate(
- host: string,
- port: number,
- username: string,
- password: string,
- graphName: string,
- ): Promise<Janus> {
- const response = await this.refreshToken(username, password, host, port);
- const token = response.body.token;
- return new Janus(graphName, host, port, username, password, token);
- }
- async createGraph(graphName: string): Promise<string> {
- try {
- const response =
- await gremlinQuery(this, `def g=ConfiguredGraphFactory.create('${graphName}');0;`);
- if (response.body.status.code === 200) {
- return `${graphName} created!`;
- }
- } catch {
- return `${graphName} already exists.`;
- }
- }
- async addVertex(entity: EntityInterface): Promise<VertexInterface> {
- const formatedProperties = formatBasicProperties(entity);
- const query = buildAddVertexQuery(this, formatedProperties);
- try {
- if (!(await this.vertexExists(entity))) {
- const response = await gremlinQuery(this, query);
- if (response.body.status.code === 200) {
- log.info('Vertex inserted.');
- return response.body.result.data;
- }
- }
- log.info('Vertex already exists, skipping insert...');
- } catch {
- log.error('Error occured while adding a Vertex');
- }
- }
- async addRelation(relation: RelationInterface): Promise<EdgeInterface> {
- const v1 = `def v1 = g.V().has('entity_id', '${relation.in_entity_id}').next();`;
- const v2 = `def v2 = g.V().has('entity_id', '${relation.out_entity_id}').next();`;
- const formatedProperties = formatAddRelationProperties(relation);
- const query = buildAddRelationQuery(this, v1, v2, formatedProperties);
- try {
- if (!(await this.relationExists(relation))) {
- const response = await gremlinQuery(this, query);
- if (response.body.status.code === 200) {
- log.info('Relation inserted.');
- return response.body.result.data;
- }
- }
- log.info('Relation already exists, skipping insert...');
- } catch (err) {
- log.error(err);
- }
- }
- async insertKpbDocument(kpbDocument): Promise<any> {
- // const entityTypes: EntityType[] = typeSystem.entity_types;
- // const relationTypes: RelationType[] = typeSystem.relation_types;
- await kpbDocument.doc.annotations.map(async (annotation) => {
- await Promise.all(annotation.entities.map(async (entity) => {
- // const formattedEntity = tagProcesser.processEntityTags(entity, entityTypes);
- await this.addVertex(entity);
- }));
- await Promise.all(annotation.relations.map(async (relation) => {
- // const formattedRelation = tagProcesser.processRelationTags(relation, relationTypes);
- await this.addRelation(relation);
- }));
- });
- return kpbDocument;
- }
- async relationExists(relation: RelationInterface): Promise<boolean> {
- const nbRelations = await this.countRelations(relation);
- if (nbRelations !== 0) {
- return true;
- }
- return false;
- }
- async countRelations(properties): Promise<number> {
- const formatedProperties = formatBasicProperties(properties);
- const query = buildCountVerticesQuery(this, formatedProperties);
- try {
- const response = await gremlinQuery(this, query);
- return response.body.result.data[0];
- } catch {
- return 0;
- }
- }
- async vertexExists(entity: EntityInterface): Promise<boolean> {
- const nbVertices = await this.countVertices(entity);
- if (nbVertices !== 0) {
- return true;
- }
- return false;
- }
- async countVertices(properties): Promise<number> {
- const formatedProperties = formatBasicProperties(properties);
- const query = buildCountVerticesQuery(this, formatedProperties);
- try {
- const response = await gremlinQuery(this, query);
- return response.body.result.data[0];
- } catch {
- return 0;
- }
- }
- async getVertices(properties): Promise<VertexInterface[]> {
- const formatedProperties = formatBasicProperties(properties);
- const query = buildGetVerticesQuery(this, formatedProperties);
- const response = await gremlinQuery(this, query);
- return response.body.result.data;
- }
- async getRelations(properties): Promise<RelationInterface[]> {
- delete properties.start;
- delete properties.end;
- const formatedProperties = formatBasicProperties(properties);
- const query = buildGetRelationsQuery(this, formatedProperties);
- const response = await gremlinQuery(this, query);
- return response.body.result.data;
- }
- async findPath(entities, labels: string[], nbHops: number): Promise<ResponsedPath> {
- const formattedProps = formatFindPathProperties(entities, labels);
- const query = buildFindPathQuery(this, formattedProps, nbHops);
- const response = await gremlinQuery(this, query);
- return response.body.result.data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement