Skip to content

Commit

Permalink
feat(alerts): Add new script log size template variables.
Browse files Browse the repository at this point in the history
Implements #1276
  • Loading branch information
Göran Sander committed Oct 20, 2024
1 parent 154b3f3 commit 44ca5a5
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 139 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/src/butler.js",
"runtimeVersion": "20",
// "runtimeVersion": "20",
"runtimeVersion": "18",
"cwd": "${workspaceFolder}/src",
"env": {
"NODE_CONFIG_DIR": "${workspaceFolder}/src/config",
Expand Down
3 changes: 2 additions & 1 deletion src/config/email_templates/aborted-reload-qseow.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@

<tr>
<td colspan="2">
The script log contains {{scriptLogSize}} rows in total. Here are the first ones:
The script log contains {{scriptLogSizeRows}} rows in total, with {{scriptLogSizeCharacters}} characters in them.<br>
Here are the first {{scriptLogHeadCount}} rows:
</td>
</tr>
<tr>
Expand Down
3 changes: 2 additions & 1 deletion src/config/email_templates/failed-reload-qscloud.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@

<tr>
<td colspan="2">
The script log contains {{scriptLogSize}} rows in total. Here are the first ones:
The script log contains {{scriptLogSizeRows}} rows in total, with {{scriptLogSizeCharacters}} characters in them.<br>
Here are the first {{scriptLogHeadCount}} rows:
</td>
</tr>
<tr>
Expand Down
3 changes: 2 additions & 1 deletion src/config/email_templates/failed-reload-qseow.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@

<tr>
<td colspan="2">
The script log contains {{scriptLogSize}} rows in total. Here are the first ones:
The script log contains {{scriptLogSizeRows}} rows in total, with {{scriptLogSizeCharacters}} characters in them.<br>
Here are the first {{scriptLogHeadCount}} rows:
</td>
</tr>
<tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
"type": "section",
"text": {
"type": "mrkdwn",
"text": "The script log contains {{scriptLogSize}} rows in total. Here are the first ones:"
"text": "The script log contains {{scriptLogSizeRows}} rows in total, with {{scriptLogSizeCharacters}} characters in them. Here are the first {{scriptLogHeadCount}} rows:"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions src/config/slack_templates/failed-reload-qseow.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"type": "section",
"text": {
"type": "mrkdwn",
"text": "The script log contains {{scriptLogSize}} characters in total. Here are the first lines:"
"text": "The script log contains {{scriptLogSizeRows}} rows in total, with {{scriptLogSizeCharacters}} characters in them. Here are the first {{scriptLogHeadCount}} rows:"
}
},
{
Expand All @@ -163,7 +163,7 @@
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Here are the last few lines:"
"text": "Here are the last {{scriptLogTailCount}} rows:"
}
},
{
Expand Down
27 changes: 26 additions & 1 deletion src/config/teams_templates/failed-reload-qscloud.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,31 @@
}
]
},
{
"type": "Container",
"spacing": "extraLarge",
"style": "emphasis",
"items": [
{
"type": "TextBlock",
"size": "large",
"weight": "bolder",
"text": "Beginning of script log",
"style": "heading"
},
{
"type": "TextBlock",
"size": "small",
"weight": "normal",
"text": "The script log contains {{scriptLogSizeRows}} rows in total, with {{scriptLogSizeCharacters}} characters in them. Here are the first {{scriptLogHeadCount}} rows:",
"style": "heading"
},
{
"type": "CodeBlock",
"codeSnippet": "{{scriptLogHead}}"
}
]
},
{
"type": "Container",
"spacing": "extraLarge",
Expand All @@ -210,7 +235,7 @@
"type": "TextBlock",
"size": "small",
"weight": "normal",
"text": "Last {{scriptLogTailCount}} rows shown. The script log contains {{scriptLogSize}} rows in total.",
"text": "Here are the last {{scriptLogTailCount}} rows:",
"style": "heading"
},
{
Expand Down
27 changes: 26 additions & 1 deletion src/config/teams_templates/failed-reload-qseow.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,31 @@
}
]
},
{
"type": "Container",
"spacing": "extraLarge",
"style": "emphasis",
"items": [
{
"type": "TextBlock",
"size": "large",
"weight": "bolder",
"text": "Beginning of script log",
"style": "heading"
},
{
"type": "TextBlock",
"size": "small",
"weight": "normal",
"text": "The script log contains {{scriptLogSizeRows}} rows in total, with {{scriptLogSizeCharacters}} characters in them. Here are the first {{scriptLogHeadCount}} rows:",
"style": "heading"
},
{
"type": "CodeBlock",
"codeSnippet": "{{scriptLogHead}}"
}
]
},
{
"type": "Container",
"spacing": "extraLarge",
Expand All @@ -206,7 +231,7 @@
"type": "TextBlock",
"size": "small",
"weight": "normal",
"text": "Last {{scriptLogTailCount}} rows shown. The script log contains {{scriptLogSize}} rows in total.",
"text": "Here are the last {{scriptLogTailCount}} rows:",
"style": "heading"
},
{
Expand Down
13 changes: 8 additions & 5 deletions src/lib/qscloud/email_notification_qscloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getQlikSenseCloudUserInfo } from './api/user.js';
import { getQlikSenseCloudAppInfo } from './api/app.js';
import { getQlikSenseCloudUrls } from './util.js';
import { sendEmail, isSmtpConfigOk } from '../qseow/smtp.js';
import { getQlikSenseCloudAppReloadScriptLogHead, getQlikSenseCloudAppReloadScriptLogTail } from './api/appreloadinfo.js';

let rateLimiterMemoryFailedReloads;
let emailConfig;
Expand Down Expand Up @@ -232,16 +233,17 @@ export async function sendQlikSenseCloudAppReloadFailureNotificationEmail(reload
// Get length of script log (row count)
scriptLogData.scriptLogSizeRows = reloadParams.scriptLog.scriptLogFull.length;

// Get the first and last n lines of the script log
scriptLogData.scriptLogHead = reloadParams.scriptLog.scriptLogFull.slice(0, scriptLogData.scriptLogHeadCount).join('\r\n');
// Get length of entire script log (character count)
scriptLogData.scriptLogSizeCharacters = reloadParams.scriptLog.scriptLogFull.join('').length;

scriptLogData.scriptLogTail = reloadParams.scriptLog.scriptLogFull
.slice(Math.max(reloadParams.scriptLog.scriptLogFull.length - scriptLogData.scriptLogTailCount, 0))
.join('\r\n');
// Get the first and last rows of the script log
scriptLogData.scriptLogHead = getQlikSenseCloudAppReloadScriptLogHead(reloadParams.scriptLog.scriptLogFull, scriptLogData.scriptLogHeadCount);
scriptLogData.scriptLogTail = getQlikSenseCloudAppReloadScriptLogTail(reloadParams.scriptLog.scriptLogFull, scriptLogData.scriptLogTailCount);
} else {
scriptLogData.scriptLogHead = '';
scriptLogData.scriptLogTail = '';
scriptLogData.scriptLogSizeRows = 0;
scriptLogData.scriptLogSizeCharacters = 0;
}

globals.logger.debug(`EMAIL ALERT - QS CLOUD APP RELOAD FAILED: Script log data:\n${JSON.stringify(scriptLogData, null, 2)}`);
Expand Down Expand Up @@ -309,6 +311,7 @@ export async function sendQlikSenseCloudAppReloadFailureNotificationEmail(reload
executionStatusText: reloadParams.reloadInfo.status,
scriptLogSize: scriptLogData.scriptLogSizeRows.toLocaleString(),
scriptLogSizeRows: scriptLogData.scriptLogSizeRows.toLocaleString(),
scriptLogSizeCharacters: scriptLogData.scriptLogSizeCharacters.toLocaleString(),
scriptLogHead: scriptLogData.scriptLogHead,
scriptLogTail: scriptLogData.scriptLogTail,
scriptLogTailCount: scriptLogData.scriptLogTailCount,
Expand Down
45 changes: 2 additions & 43 deletions src/lib/qscloud/mqtt_event_app_reload_finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import globals from '../../globals.js';
import {
getQlikSenseCloudAppReloadScriptLog,
getQlikSenseCloudAppReloadInfo,
getQlikSenseCloudAppReloadScriptLogHead,
getQlikSenseCloudAppReloadScriptLogTail,
} from './api/appreloadinfo.js';
import { getQlikSenseCloudAppInfo, getQlikSenseCloudAppMetadata, getQlikSenseCloudAppItems } from './api/app.js';
import { sendQlikSenseCloudAppReloadFailureNotificationTeams } from './msteams_notification_qscloud.js';
Expand Down Expand Up @@ -222,21 +220,8 @@ export async function handleQlikSenseCloudAppReloadFinished(message) {
if (
globals.config.get(
'Butler.qlikSenseCloud.event.mqtt.tenant.alert.teamsNotification.reloadAppFailure.basicContentOnly',
) === false
) === true
) {
const headLineCount = globals.config.get(
'Butler.qlikSenseCloud.event.mqtt.tenant.alert.teamsNotification.reloadAppFailure.headScriptLogLines',
);

const tailLineCount = globals.config.get(
'Butler.qlikSenseCloud.event.mqtt.tenant.alert.teamsNotification.reloadAppFailure.tailScriptLogLines',
);

scriptLog.scriptLogHeadCount = headLineCount;
scriptLog.scriptLogTailCount = tailLineCount;
scriptLog.scriptLogHead = getQlikSenseCloudAppReloadScriptLogHead(scriptLog.scriptLogFull, headLineCount);
scriptLog.scriptLogTail = getQlikSenseCloudAppReloadScriptLogTail(scriptLog.scriptLogFull, tailLineCount);
} else {
// Use the basic info provided in the event/MQTT message
scriptLog = {};
reloadInfo.appId = appId;
Expand Down Expand Up @@ -294,21 +279,8 @@ export async function handleQlikSenseCloudAppReloadFinished(message) {
if (
globals.config.get(
'Butler.qlikSenseCloud.event.mqtt.tenant.alert.slackNotification.reloadAppFailure.basicContentOnly',
) === false
) === true
) {
const headLineCount = globals.config.get(
'Butler.qlikSenseCloud.event.mqtt.tenant.alert.slackNotification.reloadAppFailure.headScriptLogLines',
);

const tailLineCount = globals.config.get(
'Butler.qlikSenseCloud.event.mqtt.tenant.alert.slackNotification.reloadAppFailure.tailScriptLogLines',
);

scriptLog.scriptLogHeadCount = headLineCount;
scriptLog.scriptLogTailCount = tailLineCount;
scriptLog.scriptLogHead = getQlikSenseCloudAppReloadScriptLogHead(scriptLog.scriptLogFull, headLineCount);
scriptLog.scriptLogTail = getQlikSenseCloudAppReloadScriptLogTail(scriptLog.scriptLogFull, tailLineCount);
} else {
// Use the basic info provided in the event/MQTT message
scriptLog = {};
reloadInfo.appId = appId;
Expand Down Expand Up @@ -369,19 +341,6 @@ export async function handleQlikSenseCloudAppReloadFinished(message) {
// - App metadata
// - App items

const headLineCount = globals.config.get(
'Butler.qlikSenseCloud.event.mqtt.tenant.alert.emailNotification.reloadAppFailure.headScriptLogLines',
);

const tailLineCount = globals.config.get(
'Butler.qlikSenseCloud.event.mqtt.tenant.alert.emailNotification.reloadAppFailure.tailScriptLogLines',
);

scriptLog.scriptLogHeadCount = headLineCount;
scriptLog.scriptLogTailCount = tailLineCount;
scriptLog.scriptLogHead = getQlikSenseCloudAppReloadScriptLogHead(scriptLog.scriptLogFull, headLineCount);
scriptLog.scriptLogTail = getQlikSenseCloudAppReloadScriptLogTail(scriptLog.scriptLogFull, tailLineCount);

// Send email notification
sendQlikSenseCloudAppReloadFailureNotificationEmail({
tenantId,
Expand Down
23 changes: 13 additions & 10 deletions src/lib/qscloud/msteams_notification_qscloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import globals from '../../globals.js';
import { getQlikSenseCloudUserInfo } from './api/user.js';
import { getQlikSenseCloudAppInfo } from './api/app.js';
import { getQlikSenseCloudUrls } from './util.js';
import { getQlikSenseCloudAppReloadScriptLogHead, getQlikSenseCloudAppReloadScriptLogTail } from './api/appreloadinfo.js';

let rateLimiterMemoryFailedReloads;

Expand Down Expand Up @@ -212,7 +213,7 @@ export function sendQlikSenseCloudAppReloadFailureNotificationTeams(reloadParams
scriptLogTailCount: 0,
};
} else {
// Reduce script log lines to only the ones we want to send to Teams
// Reduce full script log to head and tail adapted for Teams
scriptLogData.scriptLogHeadCount = globals.config.get(
'Butler.qlikSenseCloud.event.mqtt.tenant.alert.teamsNotification.reloadAppFailure.headScriptLogLines',
);
Expand All @@ -221,21 +222,22 @@ export function sendQlikSenseCloudAppReloadFailureNotificationTeams(reloadParams
);

if (reloadParams.scriptLog?.scriptLogFull?.length > 0) {
scriptLogData.scriptLogHead = reloadParams.scriptLog.scriptLogFull
.slice(0, reloadParams.scriptLog.scriptLogHeadCount)
.join('\r\n');
// Get length of entire script log (character count)
scriptLogData.scriptLogSizeCharacters = reloadParams.scriptLog.scriptLogFull.join('').length;

scriptLogData.scriptLogTail = reloadParams.scriptLog.scriptLogFull
.slice(Math.max(reloadParams.scriptLog.scriptLogFull.length - reloadParams.scriptLog.scriptLogTailCount, 0))
.join('\r\n');
// Get length of script log (row count)
scriptLogData.scriptLogSizeRows = reloadParams.scriptLog.scriptLogFull.length;

// Get the first and last rows of the script log
scriptLogData.scriptLogHead = getQlikSenseCloudAppReloadScriptLogHead(reloadParams.scriptLog.scriptLogFull, scriptLogData.scriptLogHeadCount);
scriptLogData.scriptLogTail = getQlikSenseCloudAppReloadScriptLogTail(reloadParams.scriptLog.scriptLogFull, scriptLogData.scriptLogTailCount);
} else {
scriptLogData.scriptLogHead = '';
scriptLogData.scriptLogTail = '';
scriptLogData.scriptLogSizeRows = 0;
scriptLogData.scriptLogSizeCharacters = 0;
}

// Get length of script log (row count)
scriptLogData.scriptLogSizeRows = reloadParams.scriptLog.scriptLogFull.length;

globals.logger.debug(
`TEAMS ALERT - QS CLOUD APP RELOAD FAILED: Script log data:\n${JSON.stringify(scriptLogData, null, 2)}`,
);
Expand Down Expand Up @@ -300,6 +302,7 @@ export function sendQlikSenseCloudAppReloadFailureNotificationTeams(reloadParams
executionStatusText: reloadParams.reloadInfo.status,
scriptLogSize: scriptLogData.scriptLogSizeRows.toLocaleString(),
scriptLogSizeRows: scriptLogData.scriptLogSizeRows.toLocaleString(),
scriptLogSizeCharacters: scriptLogData.scriptLogSizeCharacters.toLocaleString(),
scriptLogHead: scriptLogData.scriptLogHead
.replace(/([\r])/gm, '')
.replace(/([\n])/gm, '\\n')
Expand Down
Loading

0 comments on commit 44ca5a5

Please sign in to comment.