Skip to content
Snippets Groups Projects
Commit 02e6d56c authored by Leonardo's avatar Leonardo
Browse files

feat: generate insight email button

parent 41805fb0
No related branches found
No related tags found
No related merge requests found
Pipeline #143610 passed
import { RabbitMqBroker } from "ts-common";
import { rabbitMq, ums, mail, SMTP_USER } from "../variables";
import { rabbitMq, ums, mail, SMTP_USER, DEBUG_EMAIL } from "../variables";
import { log } from "../logger";
import type { InsightModel } from "ts-common";
import { populateTemplate, VariableNode } from "../utils/lexical";
......@@ -45,23 +45,24 @@ export const insightProcessor = async () => {
const insightProcessorConsumer = await new RabbitMqBroker(rabbitMq, 'insight-processor', `insight-processor`, `insight-processor`).connect();
log.info('Connected to RabbitMQ ST!');
await insightProcessorConsumer.startConsuming<InsightModel>("query-service", async (message, headers) => {
if (message == null || message.template == null || message.userId == null || message.saveStateId == null) {
log.error("Invalid Insight received in insightProcessorConsumer:", message);
await insightProcessorConsumer.startConsuming<{ insight: InsightModel, force: boolean }>("query-service", async (message, headers) => {
let insight = message.insight;
if (insight == null || insight.template == null || insight.userId == null || insight.saveStateId == null) {
log.error("Invalid Insight received in insightProcessorConsumer:", insight);
return;
}
if (message.alarmMode === "disabled") {
log.debug("Alarm mode is disabled", message.id);
if (insight.alarmMode === "disabled") {
log.debug("Alarm mode is disabled", insight.id);
return;
}
if (message.recipients == null || message.recipients.length === 0) {
if (insight.recipients == null || insight.recipients.length === 0) {
log.debug("No recipients found in the insight, skipping");
return;
}
log.info("Received insight to be processed", message);
log.info("Received insight to be processed", insight);
const editor = createHeadlessEditor({
nodes: [VariableNode],
......@@ -71,12 +72,12 @@ export const insightProcessor = async () => {
});
editor.update(() => {
const state = editor.parseEditorState(JSON.parse(message.template));
const state = editor.parseEditorState(JSON.parse(insight.template));
editor.setEditorState(state);
});
if (message.userId == null) return;
const ss = await ums.getUserSaveState(message.userId, message.saveStateId);
if (insight.userId == null) return;
const ss = await ums.getUserSaveState(insight.userId, insight.saveStateId);
const visualizations = ss.visualizations.openVisualizationArray;
const visualQuery = ss.queries[0].graph;
......@@ -86,21 +87,23 @@ export const insightProcessor = async () => {
if (query == null) return;
const result = await queryService(ss.dbConnections[0], query);
message.status = false;
insight.status = false;
if (message.alarmMode === "always") {
message.status = true;
} else if (message.alarmMode === "diff") {
message = await diffCheck(message, ss, result);
} else if (message.alarmMode === "conditional" && message.conditionsCheck && message.conditionsCheck.length > 0) {
message = statCheck(message, result);
if (insight.alarmMode === "always") {
insight.status = true;
} else if (insight.alarmMode === "diff") {
insight = await diffCheck(insight, ss, result);
} else if (insight.alarmMode === "conditional" && insight.conditionsCheck && insight.conditionsCheck.length > 0) {
insight = statCheck(insight, result);
}
if (message.userId == null) return; // fixes ts but never is the case
await ums.updateInsight(message.userId, message.id, message);
if (insight.userId == null) return; // fixes ts but never is the case
await ums.updateInsight(insight.userId, insight.id, insight);
if (insight.status || message.force) {
if (insight.status) log.debug('Insight passed the check');
if (message.force) log.debug('Forced insight processing');
if (message.status) {
log.debug('Insight passed the check');
editor.read(async () => {
const cleanUpDom = setUpDom();
let html = $generateHtmlFromNodes(editor);
......@@ -108,17 +111,22 @@ export const insightProcessor = async () => {
html = await populateTemplate(html, result, visualizations);
for (const recipient of message.recipients) {
for (const recipient of insight.recipients) {
if (mail == null) {
log.warn('Mail is not configured. Insight processor will be disabled');
return
}
if (DEBUG_EMAIL) {
log.warn('DEBUG: Would have sent mail to', recipient);
continue;
}
log.debug('Sending mail to', recipient);
await mail.sendMail({
to: recipient,
from: SMTP_USER,
subject: `GraphPolaris report: ${message.name}`,
subject: `GraphPolaris report: ${insight.name}`,
html: html
})
log.info('Mail sent to ', recipient);
......
......@@ -33,6 +33,7 @@ export const SMTP_HOST = Bun.env.SMTP_HOST || "";
export const SMTP_PORT = Bun.env.SMTP_PORT || 587;
export const SMTP_USER = Bun.env.SMTP_USER || "";
export const SMTP_PASSWORD = Bun.env.SMTP_PASSWORD || "";
export const DEBUG_EMAIL = (Bun.env.DEBUG_EMAIL !== "false") || false;
export const mail = SMTP_HOST === '' || SMTP_USER === '' || SMTP_PASSWORD === '' ? undefined : nodemailer.createTransport({
// @ts-ignore
......
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