From 4e49b10877c803eb739eba4a9a3386fbe9e22815 Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Wed, 15 Jan 2025 18:08:06 +0100
Subject: [PATCH] chore: refactor due to frontend inclusion of tscommons

---
 .prettierrc                     |   3 +-
 src/index.ts                    |   3 +-
 src/readers/insightProcessor.ts |   3 +-
 src/readers/queryService.ts     |   7 +-
 src/utils/queryPublisher.ts     | 265 +++++++++++++++++---------------
 src/variables.ts                |   3 +-
 6 files changed, 154 insertions(+), 130 deletions(-)

diff --git a/.prettierrc b/.prettierrc
index 1ced9be..9a6b47b 100644
--- a/.prettierrc
+++ b/.prettierrc
@@ -1,7 +1,8 @@
 {
-  "printWidth": 120,
+  "printWidth": 140,
   "trailingComma": "all",
   "singleQuote": true,
+  "endOfLine": "lf",
   "tabWidth": 2,
   "semi": true,
   "jsxSingleQuote": false,
diff --git a/src/index.ts b/src/index.ts
index c94c87a..21b5520 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,4 +1,5 @@
-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';
diff --git a/src/readers/insightProcessor.ts b/src/readers/insightProcessor.ts
index e0fef37..29b5bf3 100644
--- a/src/readers/insightProcessor.ts
+++ b/src/readers/insightProcessor.ts
@@ -1,6 +1,6 @@
 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() {
diff --git a/src/readers/queryService.ts b/src/readers/queryService.ts
index 3263a09..35051d5 100644
--- a/src/readers/queryService.ts
+++ b/src/readers/queryService.ts
@@ -1,5 +1,5 @@
-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
diff --git a/src/utils/queryPublisher.ts b/src/utils/queryPublisher.ts
index 8922fa0..5df23b6 100644
--- a/src/utils/queryPublisher.ts
+++ b/src/utils/queryPublisher.ts
@@ -1,141 +1,158 @@
-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',
+};
diff --git a/src/variables.ts b/src/variables.ts
index ad788c3..ee8ba87 100644
--- a/src/variables.ts
+++ b/src/variables.ts
@@ -1,4 +1,5 @@
-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';
-- 
GitLab