1. /**
  2. * Flags anomalies in queues and logs errors.
  3. *
  4. * @param {Array} queue - The queue to analyze.
  5. * @param {object} config - Configuration object with anomaly thresholds.
  6. */
  7. function flagQueueAnomalies(queue, config) {
  8. if (!Array.isArray(queue)) {
  9. console.error("Invalid input: queue must be an array.");
  10. return;
  11. }
  12. if (!config || typeof config !== 'object') {
  13. console.error("Invalid input: config must be an object.");
  14. return;
  15. }
  16. const anomalyThreshold = config.anomalyThreshold || 0.1; // Default threshold
  17. const minQueueLength = config.minQueueLength || 5; // Default min length
  18. const maxQueueLength = config.maxQueueLength || 100; // Default max length
  19. const maxItemSize = config.maxItemSize || 100; // Default max item size
  20. // Check queue length
  21. if (queue.length < minQueueLength) {
  22. console.warn(`Queue length (${queue.length}) below minimum (${minQueueLength}).`);
  23. }
  24. if (queue.length > maxQueueLength) {
  25. console.warn(`Queue length (${queue.length}) above maximum (${maxQueueLength}).`);
  26. }
  27. // Check item size (assuming items are objects)
  28. for (const item of queue) {
  29. if (typeof item !== 'object' || item === null) {
  30. console.warn("Queue contains non-object item. Skipping size check.");
  31. continue;
  32. }
  33. if (Object.keys(item).length > maxItemSize) {
  34. console.warn(`Item size (${Object.keys(item).length}) exceeds maximum (${maxItemSize}).`);
  35. }
  36. }
  37. //Example: Check for duplicate items (simple example)
  38. const itemCounts = {};
  39. for (const item of queue) {
  40. const itemStr = JSON.stringify(item); //Convert object to string for comparison
  41. if (itemCounts[itemStr]) {
  42. itemCounts[itemStr]++;
  43. if (itemCounts[itemStr] > 2) { //Flag if item appears more than twice
  44. console.warn(`Duplicate item found: ${itemStr}`);
  45. }
  46. } else {
  47. itemCounts[itemStr] = 1;
  48. }
  49. }
  50. }

Add your comment