1. class ExecutionTracker {
  2. constructor() {
  3. this.lists = {}; // Store lists with execution status
  4. }
  5. /**
  6. * Adds a new list to the tracker.
  7. * @param {string} listName The name of the list.
  8. */
  9. addList(listName) {
  10. if (!this.lists[listName]) {
  11. this.lists[listName] = {
  12. items: [],
  13. executed: false,
  14. errors: [],
  15. lastExecuted: null
  16. };
  17. }
  18. }
  19. /**
  20. * Adds an item to a list.
  21. * @param {string} listName The name of the list.
  22. * @param {any} item The item to add.
  23. */
  24. addItem(listName, item) {
  25. if (!this.lists[listName]) {
  26. this.addList(listName);
  27. }
  28. this.lists[listName].items.push(item);
  29. }
  30. /**
  31. * Marks a list as executed.
  32. * @param {string} listName The name of the list.
  33. */
  34. executeList(listName) {
  35. if (!this.lists[listName]) {
  36. console.warn(`List "${listName}" not found.`);
  37. return;
  38. }
  39. this.lists[listName].executed = true;
  40. this.lists[listName].lastExecuted = new Date();
  41. this.lists[listName].errors = []; // Reset errors
  42. this.processList(listName);
  43. }
  44. /**
  45. * Processes a list, simulating quick fixes.
  46. * @param {string} listName The name of the list.
  47. */
  48. processList(listName) {
  49. const list = this.lists[listName];
  50. for (const item of list.items) {
  51. try {
  52. // Simulate a quick fix/check
  53. if (typeof item === 'string' && item.length > 10) {
  54. // Example fix: Truncate long strings
  55. const fixedItem = item.substring(0, 10) + "...";
  56. console.log(`Fixed: ${item} -> ${fixedItem}`);
  57. list.items[list.items.indexOf(item)] = fixedItem; // Update the item
  58. } else if (typeof item === 'number' && item < 0) {
  59. //Example fix: Make numbers positive
  60. const fixedItem = Math.abs(item);
  61. console.log(`Fixed: ${item} -> ${fixedItem}`);
  62. list.items[list.items.indexOf(item)] = fixedItem;
  63. }
  64. } catch (error) {
  65. console.error(`Error processing item ${item} in list ${listName}:`, error);
  66. list.errors.push({ item, error });
  67. }
  68. }
  69. }
  70. /**
  71. * Gets the execution status of a list.
  72. * @param {string} listName The name of the list.
  73. * @returns {object} An object containing the list's status.
  74. */
  75. getListStatus(listName) {
  76. if (!this.lists[listName]) {
  77. console.warn(`List "${listName}" not found.`);
  78. return null;
  79. }
  80. return this.lists[listName];
  81. }
  82. /**
  83. * Logs all errors across all lists.
  84. */
  85. logAllErrors() {
  86. for (const listName in this.lists) {
  87. if (this.lists.hasOwnProperty(listName)) {
  88. const list = this.lists[listName];
  89. if (list.errors.length > 0) {
  90. console.warn(`Errors in list "${listName}":`);
  91. list.errors.forEach(error => {
  92. console.error(` Item: ${error.item}, Error: ${error.error}`);
  93. });
  94. }
  95. }
  96. }
  97. }
  98. }
  99. // Example Usage:
  100. const tracker = new ExecutionTracker();
  101. tracker.addList("config_values");
  102. tracker.addItem("config_values", "This is a long string");
  103. tracker.addItem("config_values", 123);
  104. tracker.addItem("config_values", -5);
  105. tracker.executeList("config_values");
  106. tracker.executeList("config_values");
  107. console.log(tracker.getListStatus("config_values")); // Access the status
  108. tracker.logAll

Add your comment