Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/main/common/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,30 @@ export function registerClass(name: string, ctor: new () => any) {
classRegistry.set(name, ctor);
}

export function submitLogsXmlString(log: LogEntry) {
const logMessage = log.message;
/**
* Escapes a value for one of the XML documents XMDS takes as a string, such as the logs or
* the stats. That document is itself escaped into the SOAP request, so the value is escaped
* twice: once for the document, and once for the request around it. Without this, a single
* "&" or "<" makes the whole request invalid and the CMS rejects every entry in it. Log
* messages are the exception, see submitLogsXmlString().
*
* @param value The value to include.
* @return {string} The value, escaped twice.
*/
export function embeddedXmlValue(value: unknown): string {
return escapeStringForXml(escapeStringForXml(String(value ?? '')));
}

export function submitLogsXmlString(log: LogEntry) {
// The message is not escaped here: the logger (ExtendedConsole) stores it already wrapped in
// CDATA and HTML-encoded, which is the one level of escaping this request needs. The other
// fields are stored as they are, so they are escaped twice.
let xmlString = '&lt;log date=&quot;' + getLogDate() + '&quot; ' +
'category=&quot;' + log.category + '&quot;&gt;' +
'&lt;message&gt;' + logMessage + '&lt;/message&gt;';
'category=&quot;' + embeddedXmlValue(log.category) + '&quot;&gt;' +
'&lt;message&gt;' + log.message + '&lt;/message&gt;';

if (log.method !== undefined && String(log.method).length > 0) {
xmlString += '&lt;method&gt;' + log.method + '&lt;/method&gt;';
xmlString += '&lt;method&gt;' + embeddedXmlValue(log.method) + '&lt;/method&gt;';
}

if (log.scheduleId && log.scheduleId !== null) {
Expand All @@ -299,11 +314,11 @@ export function submitLogsXmlString(log: LogEntry) {

// Fill in log alert fields
if (log.eventType && log.eventType !== null) {
xmlString += '&lt;eventType&gt;' + log.eventType + '&lt;/eventType&gt;';
xmlString += '&lt;eventType&gt;' + embeddedXmlValue(log.eventType) + '&lt;/eventType&gt;';
}

if (log.alertType && log.alertType !== null) {
xmlString += '&lt;alertType&gt;' + log.alertType + '&lt;/alertType&gt;';
xmlString += '&lt;alertType&gt;' + embeddedXmlValue(log.alertType) + '&lt;/alertType&gt;';
}

if (log.refId && log.refId !== null) {
Expand All @@ -327,7 +342,7 @@ export function submitStatXmlString(statObj: StatEntry, recordGeoLocation = fals
'duration=&quot;' + statObj.duration + '&quot; ';

if (statObj.tag !== null && String(statObj.tag).length > 0) {
statXml += 'tag=&quot;' + statObj.tag + '&quot; ';
statXml += 'tag=&quot;' + embeddedXmlValue(statObj.tag) + '&quot; ';
}

if (statObj.type !== 'event') {
Expand Down Expand Up @@ -360,7 +375,7 @@ export function submitStatXmlString(statObj: StatEntry, recordGeoLocation = fals
statXml += '&gt;' +
'&lt;engagements&gt;' +
engagementTags.map(tag =>
'&lt;engagement tag=&quot;' + tag + '&quot; duration=&quot;0&quot; count=&quot;1&quot;&gt;&lt;/engagement&gt;'
'&lt;engagement tag=&quot;' + embeddedXmlValue(tag) + '&quot; duration=&quot;0&quot; count=&quot;1&quot;&gt;&lt;/engagement&gt;'
).join('') +
'&lt;/engagements&gt;' +
'&lt;/stat&gt;';
Expand Down
69 changes: 39 additions & 30 deletions src/main/xmds/response/requiredFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ export type PurgeItemType = {
/**
* Register Required Files.
*/
// The data update timer running for each data widget, by widget id, and its interval in minutes.
const widgetUpdateTimers = new Map<string, { id: NodeJS.Timeout; minutes: number }>();

export default class RequiredFiles {
private readonly response: string;
generated: string | undefined;
filterFrom: string | undefined;
filterTo: string | undefined;
purge?: PurgeItemType[];
files: RequiredFile[] = [];
widgetUpdateIntervalIds = new Map<string, NodeJS.Timeout>();

constructor(response: string) {
this.response = response;
Expand Down Expand Up @@ -131,43 +133,50 @@ export default class RequiredFiles {
};
}

/**
* Keeps one data update timer running for each data widget in this list.
*
* Every fetch of the file list creates a new RequiredFiles, so the timers live in a map
* shared by all of them. A widget keeps its timer while its update interval is unchanged,
* gets a new one when the interval changes, and loses it when it leaves the list.
*/
updateDataWidgets(updateWidgetDataFn: (file: RequiredFile) => Promise<void>) {
const widgetFiles = this.files.filter(file => file.type === 'widget');
const widgetFiles = this.files.filter(file => file.type === 'widget' && file.updateInterval && file.id);
const listed = new Set(widgetFiles.map(file => file.id));

console.debug(`[RequiredFiles] Updating data widgets. Found ${widgetFiles.length} widget files.`, {
intervalIds: Array.from(this.widgetUpdateIntervalIds.entries()),
running: Array.from(widgetUpdateTimers.keys()),
});

if (widgetFiles.length === 0) {
console.debug('[RequiredFiles] No widget files found in required files. Clearing all widget update intervals.');
this.widgetUpdateIntervalIds.forEach((intervalId, fileId) => {
clearInterval(intervalId);
console.debug(`[RequiredFiles] Cleared update interval for widget ${fileId}.`);
});
this.widgetUpdateIntervalIds.clear();
return;
}

if (this.widgetUpdateIntervalIds.size > 0) {
this.widgetUpdateIntervalIds.forEach((intervalId, fileId) => {
if (!widgetFiles.some(file => file.id === fileId)) {
console.debug(`[RequiredFiles] Clearing update interval for widget ${fileId} as it is no longer in the required files.`);
clearInterval(intervalId);
this.widgetUpdateIntervalIds.delete(fileId);
}
});
}
// Stop the timers of widgets that are no longer listed.
widgetUpdateTimers.forEach((timer, fileId) => {
if (!listed.has(fileId)) {
console.debug(`[RequiredFiles] Clearing update interval for widget ${fileId} as it is no longer in the required files.`);
clearInterval(timer.id);
widgetUpdateTimers.delete(fileId);
}
});

widgetFiles.forEach((file) => {
if (file.updateInterval && file.id) {
const intervalId = setInterval(() => {
console.debug(`[RequiredFiles] Updating widget data file: ${file.id}`);
if (updateWidgetDataFn) {
updateWidgetDataFn(file);
}
}, (file.updateInterval * 60 * 1000)); // Convert minutes to milliseconds
this.widgetUpdateIntervalIds.set(file.id, intervalId);
const minutes = Number(file.updateInterval);
const existing = widgetUpdateTimers.get(file.id);

if (existing && existing.minutes === minutes) {
return;
}

if (existing) {
clearInterval(existing.id);
}

const id = setInterval(() => {
console.debug(`[RequiredFiles] Updating widget data file: ${file.id}`);
if (updateWidgetDataFn) {
updateWidgetDataFn(file);
}
}, (minutes * 60 * 1000)); // Convert minutes to milliseconds

widgetUpdateTimers.set(file.id, { id, minutes });
});
}
}
Loading
Loading