/**
* Flags anomalies in queues and logs errors.
*
* @param {Array} queue - The queue to analyze.
* @param {object} config - Configuration object with anomaly thresholds.
*/
function flagQueueAnomalies(queue, config) {
if (!Array.isArray(queue)) {
console.error("Invalid input: queue must be an array.");
return;
}
if (!config || typeof config !== 'object') {
console.error("Invalid input: config must be an object.");
return;
}
const anomalyThreshold = config.anomalyThreshold || 0.1; // Default threshold
const minQueueLength = config.minQueueLength || 5; // Default min length
const maxQueueLength = config.maxQueueLength || 100; // Default max length
const maxItemSize = config.maxItemSize || 100; // Default max item size
// Check queue length
if (queue.length < minQueueLength) {
console.warn(`Queue length (${queue.length}) below minimum (${minQueueLength}).`);
}
if (queue.length > maxQueueLength) {
console.warn(`Queue length (${queue.length}) above maximum (${maxQueueLength}).`);
}
// Check item size (assuming items are objects)
for (const item of queue) {
if (typeof item !== 'object' || item === null) {
console.warn("Queue contains non-object item. Skipping size check.");
continue;
}
if (Object.keys(item).length > maxItemSize) {
console.warn(`Item size (${Object.keys(item).length}) exceeds maximum (${maxItemSize}).`);
}
}
//Example: Check for duplicate items (simple example)
const itemCounts = {};
for (const item of queue) {
const itemStr = JSON.stringify(item); //Convert object to string for comparison
if (itemCounts[itemStr]) {
itemCounts[itemStr]++;
if (itemCounts[itemStr] > 2) { //Flag if item appears more than twice
console.warn(`Duplicate item found: ${itemStr}`);
}
} else {
itemCounts[itemStr] = 1;
}
}
}
Add your comment