Skip to content
Snippets Groups Projects
Commit 4e49b108 authored by Leonardo's avatar Leonardo
Browse files

chore: refactor due to frontend inclusion of tscommons

parent e5586c97
Branches test/insightsPopulateTemplate
No related tags found
1 merge request!24chore: refactor due to frontend inclusion of tscommons
Pipeline #144171 failed
{
"printWidth": 120,
"printWidth": 140,
"trailingComma": "all",
"singleQuote": true,
"endOfLine": "lf",
"tabWidth": 2,
"semi": true,
"jsxSingleQuote": false,
......
import { RabbitMqBroker, RabbitMqConnection, RedisConnector } from 'ts-common';
import { RedisConnector } from 'ts-common/redis';
import { RabbitMqBroker } from 'ts-common/rabbitMq';
import { REDIS_PASSWORD, REDIS_HOST, REDIS_PORT, RABBIT_USER, RABBIT_PASSWORD, RABBIT_PORT, RABBIT_HOST, rabbitMq } from './variables';
import { log } from './logger';
import { queryServiceReader } from './readers/queryService';
......
import { rabbitMq, ums, mail, SMTP_USER, DEBUG_EMAIL } from '../variables';
import { log } from '../logger';
import { RabbitMqBroker, type InsightModel } from 'ts-common';
import { type InsightModel } from 'ts-common';
import { createHeadlessEditor } from '@lexical/headless';
import { $generateHtmlFromNodes } from '@lexical/html';
import { JSDOM } from 'jsdom';
......@@ -11,6 +11,7 @@ import { statCheck } from './statCheck';
import { diffCheck } from './diffCheck';
import { VariableNode } from '../utils/lexical';
import { populateTemplate } from '../utils/insights';
import { RabbitMqBroker } from 'ts-common/rabbitMq';
const dom = new JSDOM();
function setUpDom() {
......
import { type QueryRequest } from 'ts-common';
import { Neo4jConnection, type DbConnection, RabbitMqBroker, RedisConnector } from 'ts-common';
import { type DbConnection, type QueryRequest } from 'ts-common';
import { rabbitMq, ums, type QueryExecutionTypes } from '../variables';
import { log } from '../logger';
import { QueryPublisher } from '../utils/queryPublisher';
......@@ -9,6 +9,9 @@ import { formatTimeDifference } from 'ts-common/src/logger/logger';
import { graphQueryBackend2graphQuery } from '../frontend/statistics';
import { Query2BackendQuery } from '../utils/reactflow/query2backend';
import type { GraphQueryResultMetaFromBackend } from 'ts-common/src/model/webSocket/graphResult';
import { RabbitMqBroker } from 'ts-common/rabbitMq';
import { RedisConnector } from 'ts-common/redis';
import { Neo4jConnection } from 'ts-common/neo4j';
export const queryService = async (db: DbConnection, query: string): Promise<GraphQueryResultMetaFromBackend> => {
// TODO: only neo4j is supported for now
......
import { RabbitMqBroker, wsReturnKey, type BackendMessageHeader, type MachineLearning, type ToMLMessage } from "ts-common";
import { log } from "../logger";
import type { GraphQueryResultFromBackend } from "ts-common/src/model/webSocket/graphResult";
import { wsReturnKey, type BackendMessageHeader, type MachineLearning, type ToMLMessage } from 'ts-common';
import { log } from '../logger';
import type { GraphQueryResultFromBackend } from 'ts-common/src/model/webSocket/graphResult';
import type { RabbitMqBroker } from 'ts-common/rabbitMq';
export class QueryPublisher {
private frontendPublisher: RabbitMqBroker;
private mlPublisher: RabbitMqBroker;
private routingKey?: string;
private headers?: BackendMessageHeader;
private queryID?: string;
constructor(frontendPublisher: RabbitMqBroker, mlPublisher: RabbitMqBroker) {
this.frontendPublisher = frontendPublisher;
this.mlPublisher = mlPublisher;
private frontendPublisher: RabbitMqBroker;
private mlPublisher: RabbitMqBroker;
private routingKey?: string;
private headers?: BackendMessageHeader;
private queryID?: string;
constructor(frontendPublisher: RabbitMqBroker, mlPublisher: RabbitMqBroker) {
this.frontendPublisher = frontendPublisher;
this.mlPublisher = mlPublisher;
}
withHeaders(headers?: BackendMessageHeader) {
this.headers = headers;
return this;
}
withRoutingKey(routingKey?: string) {
this.routingKey = routingKey;
return this;
}
withQueryID(queryID?: string) {
this.queryID = queryID;
return this;
}
publishStatusToFrontend(status: string) {
if (!this.headers || !this.routingKey || !this.queryID) {
throw new Error('Headers or RoutingKey or queryID not set');
}
withHeaders(headers?: BackendMessageHeader) {
this.headers = headers;
return this;
this.frontendPublisher.publishMessageToFrontend(
{
type: wsReturnKey.queryStatusUpdate,
callID: this.headers.callID,
value: this.queryID,
status: status,
},
this.routingKey,
this.headers,
);
}
publishErrorToFrontend(reason: string) {
if (!this.headers || !this.routingKey || !this.queryID) {
throw new Error('Headers or RoutingKey or queryID not set');
}
withRoutingKey(routingKey?: string) {
this.routingKey = routingKey;
return this;
this.frontendPublisher.publishMessageToFrontend(
{
type: wsReturnKey.queryStatusError,
callID: this.headers.callID,
value: this.queryID,
status: reason,
},
this.routingKey,
this.headers,
);
}
publishTranslationResultToFrontend(query: string) {
if (!this.headers || !this.routingKey || !this.queryID) {
throw new Error('Headers or RoutingKey or queryID not set');
}
withQueryID(queryID?: string) {
this.queryID = queryID;
return this;
this.frontendPublisher.publishMessageToFrontend(
{
type: wsReturnKey.queryStatusTranslationResult,
callID: this.headers.callID,
value: {
result: query,
queryID: this.headers.callID,
},
status: 'success',
},
this.routingKey,
this.headers,
);
}
publishResultToFrontend(result: GraphQueryResultFromBackend) {
if (!this.headers || !this.routingKey || !this.queryID) {
throw new Error('Headers or RoutingKey or queryID not set');
}
publishStatusToFrontend(status: string) {
if (!this.headers || !this.routingKey || !this.queryID) {
throw new Error('Headers or RoutingKey or queryID not set');
}
this.frontendPublisher.publishMessageToFrontend({
type: wsReturnKey.queryStatusUpdate,
callID: this.headers.callID,
value: this.queryID,
status: status
}, this.routingKey, this.headers);
this.frontendPublisher.publishMessageToFrontend(
{
type: wsReturnKey.queryStatusResult,
callID: this.headers.callID,
value: {
result: {
type: 'nodelink',
payload: result,
},
queryID: this.headers.callID,
},
status: 'success',
},
this.routingKey,
this.headers,
);
}
publishMachineLearningRequest(result: GraphQueryResultFromBackend, mlAttributes: MachineLearning, headers: BackendMessageHeader) {
if (!this.headers || !this.routingKey) {
throw new Error('Headers or RoutingKey or queryID not set');
}
publishErrorToFrontend(reason: string) {
if (!this.headers || !this.routingKey || !this.queryID) {
throw new Error('Headers or RoutingKey or queryID not set');
}
this.frontendPublisher.publishMessageToFrontend({
type: wsReturnKey.queryStatusError,
callID: this.headers.callID,
value: this.queryID,
status: reason
}, this.routingKey, this.headers);
// FIXME: Change ML to use the same message format that the frontend uses
const toMlResult = {
nodes: result.nodes.map(node => ({ ...node, id: node._id })),
edges: result.edges.map(edge => ({ ...edge, id: edge._id })),
};
const message: ToMLMessage = {
queryID: headers.callID,
type: 'query_result',
value: {
type: 'nodelink',
payload: toMlResult,
},
mlAttributes: mlAttributes.parameters,
};
const queueName = mlType2Queue[mlAttributes.type];
if (!queueName) {
log.error('Invalid ML type:', mlAttributes, mlAttributes.type);
throw new Error('Invalid ML type');
}
publishTranslationResultToFrontend(query: string) {
if (!this.headers || !this.routingKey || !this.queryID) {
throw new Error('Headers or RoutingKey or queryID not set');
}
this.frontendPublisher.publishMessageToFrontend({
type: wsReturnKey.queryStatusTranslationResult,
callID: this.headers.callID,
value: {
result: query,
queryID: this.headers.callID
},
status: 'success'
}, this.routingKey, this.headers);
}
publishResultToFrontend(result: GraphQueryResultFromBackend) {
if (!this.headers || !this.routingKey || !this.queryID) {
throw new Error('Headers or RoutingKey or queryID not set');
}
this.frontendPublisher.publishMessageToFrontend({
type: wsReturnKey.queryStatusResult,
callID: this.headers.callID,
value: {
result: {
type: 'nodelink',
payload: result
},
queryID: this.headers.callID
},
status: 'success'
}, this.routingKey, this.headers);
}
publishMachineLearningRequest(result: GraphQueryResultFromBackend, mlAttributes: MachineLearning, headers: BackendMessageHeader) {
if (!this.headers || !this.routingKey) {
throw new Error('Headers or RoutingKey or queryID not set');
}
// FIXME: Change ML to use the same message format that the frontend uses
const toMlResult = {
nodes: result.nodes.map(node => ({ ...node, id: node._id })),
edges: result.edges.map(edge => ({ ...edge, id: edge._id }))
}
const message: ToMLMessage = {
queryID: headers.callID,
type: "query_result",
value: {
type: 'nodelink',
payload: toMlResult
},
mlAttributes: mlAttributes.parameters
}
const queueName = mlType2Queue[mlAttributes.type];
if (!queueName) {
log.error('Invalid ML type:', mlAttributes, mlAttributes.type);
throw new Error('Invalid ML type');
}
this.mlPublisher.publishMessage({
message: JSON.stringify(message),
queueName,
routingKey: queueName,
options: {
headers: {
...headers,
message: Buffer.from(JSON.stringify(headers.message)),
}
}
});
log.debug('Published ML request:', queueName, headers.callID);
}
this.mlPublisher.publishMessage({
message: JSON.stringify(message),
queueName,
routingKey: queueName,
options: {
headers: {
...headers,
message: Buffer.from(JSON.stringify(headers.message)),
},
},
});
log.debug('Published ML request:', queueName, headers.callID);
}
}
const mlType2Queue = {
centrality: "ctr_queue",
linkPrediction: "lpr_queue",
communityDetection: "cdt_queue",
shortestPath: "stp_queue",
}
centrality: 'ctr_queue',
linkPrediction: 'lpr_queue',
communityDetection: 'cdt_queue',
shortestPath: 'stp_queue',
};
import { RabbitMqConnection, UMSApi } from 'ts-common';
import { UMSApi } from 'ts-common';
import { RabbitMqConnection } from 'ts-common/rabbitMq';
import nodemailer from 'nodemailer';
export type QueryExecutionTypes = 'neo4j';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment