navi-hey 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/README.md +38 -4
  2. package/bin/navi.js +3 -3
  3. package/lib/{factories → background}/JobFactory.js +2 -2
  4. package/lib/{registry → background}/JobRegistry.js +17 -0
  5. package/lib/{registry → background}/JobRegistryInstance.js +42 -1
  6. package/lib/{models → background}/Worker.js +5 -3
  7. package/lib/{factories → background}/WorkerFactory.js +2 -2
  8. package/lib/{registry → background}/WorkersRegistryInstance.js +1 -1
  9. package/lib/enqueuers/ActionEnqueuer.js +53 -0
  10. package/lib/{models → enqueuers}/ActionsEnqueuer.js +6 -3
  11. package/lib/{models → enqueuers}/AssetRequestEnqueuer.js +6 -2
  12. package/lib/enqueuers/PaginatedActionEnqueuer.js +53 -0
  13. package/lib/enqueuers/PaginatedActionsEnqueuer.js +47 -0
  14. package/lib/exceptions/{ConfigurationFileNotFound.js → config/ConfigurationFileNotFound.js} +1 -1
  15. package/lib/exceptions/{ConfigurationFileNotProvided.js → config/ConfigurationFileNotProvided.js} +1 -1
  16. package/lib/exceptions/{MissingTopLevelConfgKey.js → config/MissingTopLevelConfgKey.js} +1 -1
  17. package/lib/exceptions/http/ConflictError.js +14 -0
  18. package/lib/exceptions/{ForbiddenError.js → http/ForbiddenError.js} +1 -1
  19. package/lib/exceptions/{NotFoundError.js → http/NotFoundError.js} +1 -1
  20. package/lib/exceptions/{ItemNotFound.js → registry/ItemNotFound.js} +1 -1
  21. package/lib/exceptions/{MissingActionResource.js → registry/MissingActionResource.js} +1 -1
  22. package/lib/exceptions/{MissingMappingVariable.js → registry/MissingMappingVariable.js} +1 -1
  23. package/lib/exceptions/{InvalidHtmlResponseBody.js → request/InvalidHtmlResponseBody.js} +1 -1
  24. package/lib/exceptions/{InvalidResponseBody.js → request/InvalidResponseBody.js} +1 -1
  25. package/lib/exceptions/{NullResponse.js → request/NullResponse.js} +1 -1
  26. package/lib/exceptions/{RequestFailed.js → request/RequestFailed.js} +1 -1
  27. package/lib/{models → jobs}/ActionProcessingJob.js +21 -7
  28. package/lib/{models → jobs}/AssetDownloadJob.js +6 -6
  29. package/lib/{models → jobs}/HtmlParseJob.js +23 -10
  30. package/lib/jobs/PaginatedActionProcessingJob.js +77 -0
  31. package/lib/{models → jobs}/ResourceRequestJob.js +18 -15
  32. package/lib/models/{Config.js → configs/Config.js} +3 -3
  33. package/lib/models/configs/PaginationConfig.js +63 -0
  34. package/lib/models/configs/WebConfig.js +19 -0
  35. package/lib/models/{ResourceRequest.js → request/ResourceRequest.js} +32 -7
  36. package/lib/models/{ResourceRequestAction.js → request/ResourceRequestAction.js} +9 -6
  37. package/lib/models/request/ResourceRequestPaginatedAction.js +81 -0
  38. package/lib/models/{PathSegmentTraverser.js → response/PathSegmentTraverser.js} +1 -1
  39. package/lib/models/{ResponseParser.js → response/ResponseParser.js} +1 -1
  40. package/lib/registry/ClientRegistry.js +1 -1
  41. package/lib/registry/LogRegistry.js +146 -0
  42. package/lib/registry/LogRegistryInstance.js +156 -0
  43. package/lib/registry/NamedRegistry.js +1 -1
  44. package/lib/registry/ResourceRegistry.js +1 -1
  45. package/lib/serializers/JobIndexSerializer.js +13 -3
  46. package/lib/serializers/LogSerializer.js +26 -0
  47. package/lib/server/PathValidator.js +1 -1
  48. package/lib/server/RouteRegister.js +42 -9
  49. package/lib/server/Router.js +49 -31
  50. package/lib/server/WebServer.js +28 -8
  51. package/lib/server/{AssetsRequestHandler.js → handlers/AssetsRequestHandler.js} +3 -3
  52. package/lib/server/{BaseUrlsRequestHandler.js → handlers/BaseUrlsRequestHandler.js} +3 -3
  53. package/lib/server/{IndexRequestHandler.js → handlers/IndexRequestHandler.js} +2 -2
  54. package/lib/server/handlers/JobsFilter.js +55 -0
  55. package/lib/server/handlers/LogsRequestHandler.js +55 -0
  56. package/lib/server/handlers/SettingsRequestHandler.js +35 -0
  57. package/lib/server/{StatsRequestHandler.js → handlers/StatsRequestHandler.js} +3 -3
  58. package/lib/server/handlers/engine/EngineContinueRequestHandler.js +34 -0
  59. package/lib/server/handlers/engine/EnginePauseRequestHandler.js +34 -0
  60. package/lib/server/handlers/engine/EngineRestartRequestHandler.js +34 -0
  61. package/lib/server/handlers/engine/EngineShutdownRequestHandler.js +29 -0
  62. package/lib/server/handlers/engine/EngineStartRequestHandler.js +34 -0
  63. package/lib/server/handlers/engine/EngineStatusRequestHandler.js +27 -0
  64. package/lib/server/handlers/engine/EngineStopRequestHandler.js +34 -0
  65. package/lib/server/handlers/jobs/JobLogsRequestHandler.js +39 -0
  66. package/lib/server/{JobRequestHandler.js → handlers/jobs/JobRequestHandler.js} +4 -4
  67. package/lib/server/handlers/jobs/JobRetryRequestHandler.js +40 -0
  68. package/lib/server/{JobsRequestHandler.js → handlers/jobs/JobsRequestHandler.js} +7 -4
  69. package/lib/services/Application.js +138 -84
  70. package/lib/services/ApplicationInstance.js +274 -0
  71. package/lib/services/Client.js +23 -19
  72. package/lib/services/ConfigLoader.js +1 -1
  73. package/lib/services/ConfigParser.js +7 -7
  74. package/lib/services/Engine.js +51 -6
  75. package/lib/services/EngineEvents.js +42 -0
  76. package/lib/services/FailureChecker.js +4 -4
  77. package/lib/services/WorkersAllocator.js +2 -2
  78. package/lib/utils/HtmlElementParser.js +5 -4
  79. package/lib/utils/HtmlParser.js +5 -5
  80. package/lib/utils/PromiseAggregator.js +11 -5
  81. package/lib/utils/logging/BufferedLogger.js +8 -0
  82. package/lib/utils/logging/LogBuffer.js +25 -5
  83. package/lib/utils/logging/LogBufferCollection.js +56 -0
  84. package/lib/utils/logging/LogContext.js +51 -0
  85. package/lib/utils/logging/LogFilter.js +34 -0
  86. package/package.json +1 -1
  87. package/static/assets/{index-DBkz-poX.css → index-CuWjW0sT.css} +1 -1
  88. package/static/assets/index-D5tOOLpl.js +11 -0
  89. package/static/index.html +2 -2
  90. package/lib/exceptions/LockedByOtherWorker.js +0 -14
  91. package/lib/models/ActionEnqueuer.js +0 -34
  92. package/lib/models/WebConfig.js +0 -15
  93. /package/lib/{models → background}/Job.js +0 -0
  94. /package/lib/{registry → background}/WorkersRegistry.js +0 -0
  95. /package/lib/exceptions/{MissingClientsConfig.js → config/MissingClientsConfig.js} +0 -0
  96. /package/lib/exceptions/{MissingResourceConfig.js → config/MissingResourceConfig.js} +0 -0
  97. /package/lib/exceptions/{ClientNotFound.js → registry/ClientNotFound.js} +0 -0
  98. /package/lib/exceptions/{ResourceNotFound.js → registry/ResourceNotFound.js} +0 -0
  99. /package/lib/{factories → factory}/Factory.js +0 -0
  100. /package/lib/models/{FailureConfig.js → configs/FailureConfig.js} +0 -0
  101. /package/lib/models/{LogConfig.js → configs/LogConfig.js} +0 -0
  102. /package/lib/models/{WorkersConfig.js → configs/WorkersConfig.js} +0 -0
  103. /package/lib/models/{AssetRequest.js → request/AssetRequest.js} +0 -0
  104. /package/lib/models/{Resource.js → request/Resource.js} +0 -0
  105. /package/lib/models/{ParametersMapper.js → response/ParametersMapper.js} +0 -0
  106. /package/lib/models/{PathResolver.js → response/PathResolver.js} +0 -0
  107. /package/lib/models/{ResponseWrapper.js → response/ResponseWrapper.js} +0 -0
@@ -1,11 +1,11 @@
1
1
  import { Client } from './Client.js';
2
- import { MissingClientsConfig } from '../exceptions/MissingClientsConfig.js';
3
- import { MissingResourceConfig } from '../exceptions/MissingResourceConfig.js';
4
- import { FailureConfig } from '../models/FailureConfig.js';
5
- import { LogConfig } from '../models/LogConfig.js';
6
- import { Resource } from '../models/Resource.js';
7
- import { WebConfig } from '../models/WebConfig.js';
8
- import { WorkersConfig } from '../models/WorkersConfig.js';
2
+ import { MissingClientsConfig } from '../exceptions/config/MissingClientsConfig.js';
3
+ import { MissingResourceConfig } from '../exceptions/config/MissingResourceConfig.js';
4
+ import { FailureConfig } from '../models/configs/FailureConfig.js';
5
+ import { LogConfig } from '../models/configs/LogConfig.js';
6
+ import { WebConfig } from '../models/configs/WebConfig.js';
7
+ import { WorkersConfig } from '../models/configs/WorkersConfig.js';
8
+ import { Resource } from '../models/request/Resource.js';
9
9
 
10
10
  /**
11
11
  * ConfigParser receives a parsed YAML configuration object and maps it to
@@ -1,6 +1,6 @@
1
1
  import { WorkersAllocator } from './WorkersAllocator.js';
2
- import { JobRegistry } from '../registry/JobRegistry.js';
3
- import { WorkersRegistry } from '../registry/WorkersRegistry.js';
2
+ import { JobRegistry } from '../background/JobRegistry.js';
3
+ import { WorkersRegistry } from '../background/WorkersRegistry.js';
4
4
  import { Logger } from '../utils/logging/Logger.js';
5
5
 
6
6
  /**
@@ -8,35 +8,70 @@ import { Logger } from '../utils/logging/Logger.js';
8
8
  *
9
9
  * It continuously checks for available jobs and idle workers, and assigns
10
10
  * jobs to workers until there are no more jobs and no more busy workers.
11
+ * In keepAlive mode (web UI present), the loop continues indefinitely until
12
+ * `stop()` is explicitly called.
11
13
  */
12
14
  class Engine {
13
15
  #sleepMs;
16
+ #stopped = false;
17
+ #paused = false;
18
+ #keepAlive;
14
19
 
15
20
  /**
16
21
  * Creates an instance of Engine.
17
22
  * @param {object} param0 - The parameters for creating an Engine instance.
18
23
  * @param {WorkersAllocator} param0.allocator - The workers allocator to manage job allocation.
19
24
  * @param {number} [param0.sleepMs=500] - Milliseconds to wait when all jobs are in cooldown. Use a negative value to disable sleeping (e.g. in tests).
25
+ * @param {boolean} [param0.keepAlive=false] - When true, the loop runs indefinitely until `stop()` is called (web UI mode).
20
26
  */
21
- constructor({ allocator, sleepMs = 500 } = {}) {
27
+ constructor({ allocator, sleepMs = 500, keepAlive = false } = {}) {
22
28
  this.#sleepMs = sleepMs;
29
+ this.#keepAlive = keepAlive;
23
30
 
24
31
  this.allocator = allocator || new WorkersAllocator();
25
32
  }
26
33
 
34
+ /**
35
+ * Stops the engine by setting the stop flag.
36
+ * The current iteration will complete before the loop exits.
37
+ * @returns {void}
38
+ */
39
+ stop() {
40
+ this.#stopped = true;
41
+ }
42
+
43
+ /**
44
+ * Pauses allocation without exiting the loop.
45
+ * While paused, the engine keeps iterating but skips job allocation.
46
+ * @returns {void}
47
+ */
48
+ pause() {
49
+ this.#paused = true;
50
+ }
51
+
52
+ /**
53
+ * Resumes allocation after a pause.
54
+ * @returns {void}
55
+ */
56
+ resume() {
57
+ this.#paused = false;
58
+ }
59
+
27
60
  /**
28
61
  * Starts the engine by processing jobs.
29
62
  *
30
63
  * This method continuously checks for available jobs and idle workers, and assigns
31
- * jobs to workers until there are no more jobs and no more busy workers.
64
+ * jobs to workers until there are no more jobs and no more busy workers,
65
+ * or until `stop()` has been called. In keepAlive mode, the loop continues even
66
+ * when the queue is empty, waiting for new work.
32
67
  * @returns {Promise<void>}
33
68
  */
34
69
  async start() {
35
- while (this.#continueAllocating()) {
70
+ while (!this.#stopped && this.#shouldContinue()) {
36
71
  Logger.debug('Promoting ready jobs and allocating to idle workers if available...');
37
72
  JobRegistry.promoteReadyJobs();
38
73
 
39
- if (JobRegistry.hasReadyJob()) {
74
+ if (!this.#paused && JobRegistry.hasReadyJob()) {
40
75
  this.allocator.allocate();
41
76
  }
42
77
 
@@ -45,6 +80,16 @@ class Engine {
45
80
  }
46
81
  }
47
82
 
83
+ /**
84
+ * Determines whether the engine loop should continue.
85
+ * In keepAlive mode, always returns true. Otherwise, returns true only
86
+ * while there are jobs or busy workers.
87
+ * @returns {boolean} True if the loop should continue.
88
+ */
89
+ #shouldContinue() {
90
+ return this.#keepAlive || this.#continueAllocating();
91
+ }
92
+
48
93
  /**
49
94
  * Checks if the engine should continue allocating jobs to workers.
50
95
  * @returns {boolean} True if there are jobs to process or busy workers, false otherwise.
@@ -0,0 +1,42 @@
1
+ import { EventEmitter } from 'events';
2
+
3
+ /**
4
+ * EngineEvents is a singleton event bus for engine lifecycle transitions.
5
+ * Components subscribe via `EngineEvents.on(name, handler)` and the engine
6
+ * fires events via `EngineEvents.emit(name)`.
7
+ *
8
+ * Supported event names: 'stop', 'start', 'restart', 'reset'.
9
+ * @author darthjee
10
+ */
11
+ class EngineEvents {
12
+ static #emitter = new EventEmitter();
13
+
14
+ /**
15
+ * Fires the named event, invoking all registered listeners synchronously.
16
+ * @param {string} eventName - The event to fire.
17
+ * @returns {void}
18
+ */
19
+ static emit(eventName) {
20
+ EngineEvents.#emitter.emit(eventName);
21
+ }
22
+
23
+ /**
24
+ * Registers a listener for the named event.
25
+ * @param {string} eventName - The event to listen for.
26
+ * @param {Function} handler - The callback to invoke when the event fires.
27
+ * @returns {void}
28
+ */
29
+ static on(eventName, handler) {
30
+ EngineEvents.#emitter.on(eventName, handler);
31
+ }
32
+
33
+ /**
34
+ * Removes all registered listeners. Intended for test teardown.
35
+ * @returns {void}
36
+ */
37
+ static reset() {
38
+ EngineEvents.#emitter.removeAllListeners();
39
+ }
40
+ }
41
+
42
+ export { EngineEvents };
@@ -1,5 +1,5 @@
1
- import { JobRegistry } from '../registry/JobRegistry.js';
2
- import { Logger } from '../utils/logging/Logger.js';
1
+ import { JobRegistry } from '../background/JobRegistry.js';
2
+ import { LogRegistry } from '../registry/LogRegistry.js';
3
3
 
4
4
  /**
5
5
  * FailureChecker evaluates the dead-job ratio after a run and exits with a non-zero
@@ -59,7 +59,7 @@ class FailureChecker {
59
59
 
60
60
  /**
61
61
  * Returns true when the dead-job ratio exceeds the configured threshold.
62
- * @returns {boolean}
62
+ * @returns {boolean} True if the ratio is above the configured threshold.
63
63
  */
64
64
  #isOverThreshold() {
65
65
  return this.#ratio() > this.#failureConfig.threshold;
@@ -70,7 +70,7 @@ class FailureChecker {
70
70
  * @returns {void}
71
71
  */
72
72
  #fail() {
73
- Logger.error(`Failure threshold exceeded: ${this.#ratio().toFixed(2)}% of jobs are dead (threshold: ${this.#failureConfig.threshold}%)`);
73
+ LogRegistry.error(`Failure threshold exceeded: ${this.#ratio().toFixed(2)}% of jobs are dead (threshold: ${this.#failureConfig.threshold}%)`);
74
74
  process.exit(1);
75
75
  }
76
76
  }
@@ -1,5 +1,5 @@
1
- import { JobRegistry } from '../registry/JobRegistry.js';
2
- import { WorkersRegistry } from '../registry/WorkersRegistry.js';
1
+ import { JobRegistry } from '../background/JobRegistry.js';
2
+ import { WorkersRegistry } from '../background/WorkersRegistry.js';
3
3
 
4
4
  /**
5
5
  * WorkersAllocator is responsible for allocating jobs to idle workers.
@@ -1,5 +1,3 @@
1
- import { Logger } from './logging/Logger.js';
2
-
3
1
  /**
4
2
  * HtmlElementParser wraps a single parsed HTML element and extracts attribute values,
5
3
  * logging a warning when the requested attribute is absent.
@@ -8,14 +6,17 @@ import { Logger } from './logging/Logger.js';
8
6
  class HtmlElementParser {
9
7
  #element;
10
8
  #selector;
9
+ #logContext;
11
10
 
12
11
  /**
13
12
  * @param {object} element A parsed HTML element with a `getAttribute` method.
14
13
  * @param {string} selector The CSS selector that matched this element (used in warning messages).
14
+ * @param {LogContext} logContext Context carrying workerId/jobId for log entries.
15
15
  */
16
- constructor(element, selector) {
16
+ constructor(element, selector, logContext) {
17
17
  this.#element = element;
18
18
  this.#selector = selector;
19
+ this.#logContext = logContext;
19
20
  }
20
21
 
21
22
  /**
@@ -28,7 +29,7 @@ class HtmlElementParser {
28
29
  const value = this.#element.getAttribute(attribute);
29
30
 
30
31
  if (value === undefined || value === null) {
31
- Logger.warn(`HtmlParser: element matched by "${this.#selector}" is missing attribute "${attribute}"`);
32
+ this.#logContext.warn(`HtmlParser: element matched by "${this.#selector}" is missing attribute "${attribute}"`);
32
33
  return null;
33
34
  }
34
35
 
@@ -1,7 +1,6 @@
1
1
  import { parse } from 'node-html-parser';
2
2
  import { HtmlElementParser } from './HtmlElementParser.js';
3
- import { InvalidHtmlResponseBody } from '../exceptions/InvalidHtmlResponseBody.js';
4
- import { Logger } from './logging/Logger.js';
3
+ import { InvalidHtmlResponseBody } from '../exceptions/request/InvalidHtmlResponseBody.js';
5
4
 
6
5
  /**
7
6
  * HtmlParser parses a raw HTML string and extracts attribute values using CSS selectors.
@@ -17,10 +16,11 @@ class HtmlParser {
17
16
  * @param {string} rawHtml The raw HTML string to parse.
18
17
  * @param {string} selector CSS selector to match elements.
19
18
  * @param {string} attribute Attribute name whose value is extracted from each matched element.
19
+ * @param {LogContext} logContext Context carrying workerId/jobId for log entries.
20
20
  * @returns {Array<string>} Array of attribute values from matched elements.
21
21
  * @throws {InvalidHtmlResponseBody} If the HTML cannot be parsed.
22
22
  */
23
- static parse(rawHtml, selector, attribute) {
23
+ static parse(rawHtml, selector, attribute, logContext) {
24
24
  let root;
25
25
 
26
26
  try {
@@ -32,13 +32,13 @@ class HtmlParser {
32
32
  const elements = root.querySelectorAll(selector);
33
33
 
34
34
  if (elements.length === 0) {
35
- Logger.warn(`HtmlParser: selector "${selector}" matched zero elements`);
35
+ logContext.warn(`HtmlParser: selector "${selector}" matched zero elements`);
36
36
  return [];
37
37
  }
38
38
 
39
39
  const values = [];
40
40
  for (const element of elements) {
41
- const value = new HtmlElementParser(element, selector).getAttribute(attribute);
41
+ const value = new HtmlElementParser(element, selector, logContext).getAttribute(attribute);
42
42
  if (value !== null) values.push(value);
43
43
  }
44
44
 
@@ -19,17 +19,23 @@ class PromiseAggregator {
19
19
  }
20
20
 
21
21
  /**
22
- * Waits for all registered promises to settle.
22
+ * Waits for all registered promises to settle, including any added during the wait.
23
+ * Loops until the internal list is empty, draining settled promises each iteration.
23
24
  * If one or more promises reject, it still waits for the remaining
24
25
  * ones and then re-throws the first rejection error.
25
26
  * @returns {Promise<void>}
26
27
  */
27
28
  async wait() {
28
- const results = await Promise.allSettled(this.#promises);
29
- const firstRejection = results.find((r) => r.status === 'rejected');
29
+ while (this.#promises.length > 0) {
30
+ const current = [...this.#promises];
31
+ const results = await Promise.allSettled(current);
30
32
 
31
- if (firstRejection) {
32
- throw firstRejection.reason;
33
+ this.#promises = this.#promises.filter((p) => !current.includes(p));
34
+
35
+ const firstRejection = results.find((r) => r.status === 'rejected');
36
+ if (firstRejection) {
37
+ throw firstRejection.reason;
38
+ }
33
39
  }
34
40
  }
35
41
  }
@@ -30,6 +30,14 @@ class BufferedLogger extends BaseLogger {
30
30
  this.#buffer.add(level, message, attributes);
31
31
  }
32
32
 
33
+ /**
34
+ * Returns the most recently added log, or undefined if the buffer is empty.
35
+ * @returns {import('./Log.js').Log|undefined} The most recently added log entry, or undefined.
36
+ */
37
+ get latestLog() {
38
+ return this.#buffer.latestLog;
39
+ }
40
+
33
41
  /**
34
42
  * Gets all logs from the buffer.
35
43
  * @returns {Array<import('./Log.js').Log>} Array of log entries.
@@ -31,10 +31,10 @@ class LogBuffer {
31
31
  add(level, message, attributes = {}) {
32
32
  const log = this.#factory.build(level, message, attributes);
33
33
 
34
- this.#logs.push(log);
34
+ this.#logs.unshift(log);
35
35
 
36
36
  if (this.#logs.length > this.#retention) {
37
- this.#logs.shift();
37
+ this.#logs.pop();
38
38
  }
39
39
 
40
40
  return log;
@@ -45,7 +45,7 @@ class LogBuffer {
45
45
  * @returns {Array<Log>} Array of log entries.
46
46
  */
47
47
  getLogs() {
48
- return [...this.#logs];
48
+ return [...this.#logs].reverse();
49
49
  }
50
50
 
51
51
  /**
@@ -63,7 +63,27 @@ class LogBuffer {
63
63
  * @returns {Array<Log>} Array of log entries matching the level.
64
64
  */
65
65
  getLogsByLevel(level) {
66
- return this.#logs.filter(log => log.level === level);
66
+ return this.#logs.filter(log => log.level === level).reverse();
67
+ }
68
+
69
+ /**
70
+ * Appends an existing Log instance to the buffer, respecting the retention limit.
71
+ * @param {import('./Log.js').Log} log - The Log instance to append.
72
+ * @returns {void}
73
+ */
74
+ push(log) {
75
+ this.#logs.unshift(log);
76
+ if (this.#logs.length > this.#retention) {
77
+ this.#logs.pop();
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Returns the most recently added log, or undefined if the buffer is empty.
83
+ * @returns {import('./Log.js').Log|undefined} The most recently added log entry, or undefined.
84
+ */
85
+ get latestLog() {
86
+ return this.#logs[0];
67
87
  }
68
88
 
69
89
  /**
@@ -95,7 +115,7 @@ class LogBuffer {
95
115
  * @returns {Array<object>} Array of log objects.
96
116
  */
97
117
  toJSON() {
98
- return this.#logs.map(log => log.toJSON());
118
+ return [...this.#logs].reverse().map(log => log.toJSON());
99
119
  }
100
120
  }
101
121
 
@@ -0,0 +1,56 @@
1
+ import { LogBuffer } from './LogBuffer.js';
2
+ import { EngineEvents } from '../../services/EngineEvents.js';
3
+
4
+ /**
5
+ * LogBufferCollection manages a keyed map of LogBuffer instances.
6
+ * Buffers are created on first use and cleared automatically when the engine stops.
7
+ * @author darthjee
8
+ */
9
+ class LogBufferCollection {
10
+ #buffers;
11
+ #retention;
12
+
13
+ /**
14
+ * @param {number} [retention=100] - Maximum number of logs to retain per buffer.
15
+ */
16
+ constructor(retention = 100) {
17
+ this.#buffers = new Map();
18
+ this.#retention = retention;
19
+ EngineEvents.on('stop', () => this.clear());
20
+ }
21
+
22
+ /**
23
+ * Appends an existing Log instance to the buffer for the given key.
24
+ * Lazily creates the buffer on first use.
25
+ * @param {string|number} key - The key identifying the buffer (e.g. jobId or workerId).
26
+ * @param {import('./Log.js').Log} log - The Log instance to append.
27
+ * @returns {void}
28
+ */
29
+ push(key, log) {
30
+ if (!this.#buffers.has(key)) {
31
+ this.#buffers.set(key, new LogBuffer(this.#retention));
32
+ }
33
+ this.#buffers.get(key).push(log);
34
+ }
35
+
36
+ /**
37
+ * Returns all logs for the given key in chronological order (oldest first).
38
+ * Returns an empty array if no buffer exists for that key.
39
+ * @param {string|number} key - The key to look up.
40
+ * @returns {Array<import('./Log.js').Log>} Array of log entries for the given key, oldest first.
41
+ */
42
+ getLogs(key) {
43
+ if (!this.#buffers.has(key)) return [];
44
+ return this.#buffers.get(key).getLogs();
45
+ }
46
+
47
+ /**
48
+ * Removes all per-key buffers.
49
+ * @returns {void}
50
+ */
51
+ clear() {
52
+ this.#buffers = new Map();
53
+ }
54
+ }
55
+
56
+ export { LogBufferCollection };
@@ -0,0 +1,51 @@
1
+ import { LogRegistry } from '../../registry/LogRegistry.js';
2
+
3
+ /**
4
+ * LogContext wraps a fixed set of attributes and forwards log calls to LogRegistry
5
+ * with those attributes merged into every entry.
6
+ * @author darthjee
7
+ */
8
+ class LogContext {
9
+ #attributes;
10
+
11
+ /**
12
+ * @param {object} attributes Fixed attributes to include on every log entry (e.g. workerId, jobId).
13
+ */
14
+ constructor(attributes) {
15
+ this.#attributes = attributes;
16
+ }
17
+
18
+ /**
19
+ * @param {string} message The log message.
20
+ * @param {object} [attrs={}] Additional per-call attributes merged with context attributes.
21
+ */
22
+ debug(message, attrs = {}) {
23
+ LogRegistry.debug(message, { ...this.#attributes, ...attrs });
24
+ }
25
+
26
+ /**
27
+ * @param {string} message The log message.
28
+ * @param {object} [attrs={}] Additional per-call attributes merged with context attributes.
29
+ */
30
+ info(message, attrs = {}) {
31
+ LogRegistry.info(message, { ...this.#attributes, ...attrs });
32
+ }
33
+
34
+ /**
35
+ * @param {string} message The log message.
36
+ * @param {object} [attrs={}] Additional per-call attributes merged with context attributes.
37
+ */
38
+ warn(message, attrs = {}) {
39
+ LogRegistry.warn(message, { ...this.#attributes, ...attrs });
40
+ }
41
+
42
+ /**
43
+ * @param {string} message The log message.
44
+ * @param {object} [attrs={}] Additional per-call attributes merged with context attributes.
45
+ */
46
+ error(message, attrs = {}) {
47
+ LogRegistry.error(message, { ...this.#attributes, ...attrs });
48
+ }
49
+ }
50
+
51
+ export { LogContext };
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Filters a list of log entries based on query options.
3
+ * @author darthjee
4
+ */
5
+ class LogFilter {
6
+ #logs;
7
+
8
+ /**
9
+ * @param {Array<import('./Log.js').Log>} logs - The full list of logs to filter (oldest first).
10
+ */
11
+ constructor(logs) {
12
+ this.#logs = logs;
13
+ }
14
+
15
+ /**
16
+ * Returns the filtered log list.
17
+ * @param {object} [options={}] - Filter options.
18
+ * @param {number|string} [options.lastId] - When provided, returns only logs newer than this ID.
19
+ * Returns an empty array if the ID is not found.
20
+ * @returns {Array<import('./Log.js').Log>} Filtered array of log entries.
21
+ */
22
+ filter({ lastId } = {}) {
23
+ if (lastId === undefined) return this.#logs;
24
+
25
+ const id = parseInt(lastId, 10);
26
+ const index = this.#logs.findIndex(log => log.id === id);
27
+
28
+ if (index === -1) return [];
29
+
30
+ return this.#logs.slice(index + 1);
31
+ }
32
+ }
33
+
34
+ export { LogFilter };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "navi-hey",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.2.0",
5
5
  "description": "Navi cache warmer CLI",
6
6
  "readme": "README.md",
7
7
  "main": "index.js",