apm-optima 0.0.1

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 (131) hide show
  1. package/dist/adapters/http.adapter.d.ts +12 -0
  2. package/dist/adapters/http.adapter.js +2 -0
  3. package/dist/adapters/websocket.adapter.d.ts +5 -0
  4. package/dist/adapters/websocket.adapter.js +2 -0
  5. package/dist/config/config.manager.d.ts +12 -0
  6. package/dist/config/config.manager.js +111 -0
  7. package/dist/config/config.types.d.ts +19 -0
  8. package/dist/config/config.types.js +2 -0
  9. package/dist/config/index.d.ts +2 -0
  10. package/dist/config/index.js +18 -0
  11. package/dist/core/delivery/index.d.ts +2 -0
  12. package/dist/core/delivery/index.js +18 -0
  13. package/dist/core/delivery/metrics.publisher.d.ts +18 -0
  14. package/dist/core/delivery/metrics.publisher.js +36 -0
  15. package/dist/core/delivery/websocket.events.d.ts +12 -0
  16. package/dist/core/delivery/websocket.events.js +16 -0
  17. package/dist/core/domain/common.types.d.ts +44 -0
  18. package/dist/core/domain/common.types.js +20 -0
  19. package/dist/core/domain/dashboard.types.d.ts +108 -0
  20. package/dist/core/domain/dashboard.types.js +2 -0
  21. package/dist/core/domain/endpoints.types.d.ts +43 -0
  22. package/dist/core/domain/endpoints.types.js +2 -0
  23. package/dist/core/domain/index.d.ts +6 -0
  24. package/dist/core/domain/index.js +22 -0
  25. package/dist/core/domain/metrics.types.d.ts +119 -0
  26. package/dist/core/domain/metrics.types.js +2 -0
  27. package/dist/core/domain/system.types.d.ts +51 -0
  28. package/dist/core/domain/system.types.js +2 -0
  29. package/dist/core/domain/telemetry.types.d.ts +8 -0
  30. package/dist/core/domain/telemetry.types.js +2 -0
  31. package/dist/core/storage/index.d.ts +3 -0
  32. package/dist/core/storage/index.js +19 -0
  33. package/dist/core/storage/local.repository.d.ts +17 -0
  34. package/dist/core/storage/local.repository.js +52 -0
  35. package/dist/core/storage/stores/alert.store.d.ts +13 -0
  36. package/dist/core/storage/stores/alert.store.js +71 -0
  37. package/dist/core/storage/stores/analytics.store.d.ts +15 -0
  38. package/dist/core/storage/stores/analytics.store.js +59 -0
  39. package/dist/core/storage/stores/bucket.store.d.ts +40 -0
  40. package/dist/core/storage/stores/bucket.store.js +75 -0
  41. package/dist/core/storage/stores/dashboard.store.d.ts +15 -0
  42. package/dist/core/storage/stores/dashboard.store.js +78 -0
  43. package/dist/core/storage/stores/endpoint.store.d.ts +16 -0
  44. package/dist/core/storage/stores/endpoint.store.js +62 -0
  45. package/dist/core/storage/stores/health.store.d.ts +20 -0
  46. package/dist/core/storage/stores/health.store.js +133 -0
  47. package/dist/core/storage/stores/index.d.ts +8 -0
  48. package/dist/core/storage/stores/index.js +24 -0
  49. package/dist/core/storage/stores/metrics.store.d.ts +23 -0
  50. package/dist/core/storage/stores/metrics.store.js +49 -0
  51. package/dist/core/storage/stores/system.store.d.ts +8 -0
  52. package/dist/core/storage/stores/system.store.js +19 -0
  53. package/dist/core/storage/utility/histogram.d.ts +37 -0
  54. package/dist/core/storage/utility/histogram.js +91 -0
  55. package/dist/core/storage/utility/index.d.ts +2 -0
  56. package/dist/core/storage/utility/index.js +18 -0
  57. package/dist/core/storage/utility/ring-buffer.d.ts +12 -0
  58. package/dist/core/storage/utility/ring-buffer.js +47 -0
  59. package/dist/core/telemetry/collector.service.d.ts +18 -0
  60. package/dist/core/telemetry/collector.service.js +92 -0
  61. package/dist/core/telemetry/collectors/cpu.collector.d.ts +22 -0
  62. package/dist/core/telemetry/collectors/cpu.collector.js +39 -0
  63. package/dist/core/telemetry/collectors/event-loop.collector.d.ts +22 -0
  64. package/dist/core/telemetry/collectors/event-loop.collector.js +27 -0
  65. package/dist/core/telemetry/collectors/gc.collector.d.ts +61 -0
  66. package/dist/core/telemetry/collectors/gc.collector.js +86 -0
  67. package/dist/core/telemetry/collectors/handles.collector.d.ts +20 -0
  68. package/dist/core/telemetry/collectors/handles.collector.js +26 -0
  69. package/dist/core/telemetry/collectors/index.d.ts +6 -0
  70. package/dist/core/telemetry/collectors/index.js +22 -0
  71. package/dist/core/telemetry/collectors/interface.d.ts +3 -0
  72. package/dist/core/telemetry/collectors/interface.js +2 -0
  73. package/dist/core/telemetry/collectors/memory.collector.d.ts +18 -0
  74. package/dist/core/telemetry/collectors/memory.collector.js +24 -0
  75. package/dist/core/telemetry/collectors/runtime.collector.d.ts +32 -0
  76. package/dist/core/telemetry/collectors/runtime.collector.js +29 -0
  77. package/dist/core/telemetry/correlation.service.d.ts +9 -0
  78. package/dist/core/telemetry/correlation.service.js +72 -0
  79. package/dist/core/telemetry/logger.d.ts +11 -0
  80. package/dist/core/telemetry/logger.js +63 -0
  81. package/dist/core/telemetry/telemetry.service.d.ts +10 -0
  82. package/dist/core/telemetry/telemetry.service.js +36 -0
  83. package/dist/core/utility/base-math.d.ts +15 -0
  84. package/dist/core/utility/base-math.js +26 -0
  85. package/dist/core/utility/conversion.d.ts +31 -0
  86. package/dist/core/utility/conversion.js +74 -0
  87. package/dist/core/utility/index.d.ts +4 -0
  88. package/dist/core/utility/index.js +20 -0
  89. package/dist/core/utility/mocking.d.ts +21 -0
  90. package/dist/core/utility/mocking.js +40 -0
  91. package/dist/core/utility/pagination.d.ts +9 -0
  92. package/dist/core/utility/pagination.js +23 -0
  93. package/dist/core/utility/statistics.d.ts +140 -0
  94. package/dist/core/utility/statistics.js +349 -0
  95. package/dist/express/index.d.ts +6 -0
  96. package/dist/express/index.js +22 -0
  97. package/dist/express/metrics-websocket.adapter.d.ts +14 -0
  98. package/dist/express/metrics-websocket.adapter.js +70 -0
  99. package/dist/express/metrics.adapter.d.ts +12 -0
  100. package/dist/express/metrics.adapter.js +31 -0
  101. package/dist/express/metrics.bootstrap.d.ts +2 -0
  102. package/dist/express/metrics.bootstrap.js +35 -0
  103. package/dist/express/metrics.dashboard.d.ts +2 -0
  104. package/dist/express/metrics.dashboard.js +15 -0
  105. package/dist/express/metrics.middleware.d.ts +2 -0
  106. package/dist/express/metrics.middleware.js +29 -0
  107. package/dist/index.d.ts +3 -0
  108. package/dist/index.js +6 -0
  109. package/dist/nest/index.d.ts +4 -0
  110. package/dist/nest/index.js +11 -0
  111. package/dist/nest/metrics-websocket.adapter.d.ts +15 -0
  112. package/dist/nest/metrics-websocket.adapter.js +88 -0
  113. package/dist/nest/metrics.adapter.d.ts +12 -0
  114. package/dist/nest/metrics.adapter.js +33 -0
  115. package/dist/nest/metrics.bootstrap.service.d.ts +13 -0
  116. package/dist/nest/metrics.bootstrap.service.js +66 -0
  117. package/dist/nest/metrics.interceptor.d.ts +11 -0
  118. package/dist/nest/metrics.interceptor.js +57 -0
  119. package/dist/nest/metrics.module.d.ts +6 -0
  120. package/dist/nest/metrics.module.js +74 -0
  121. package/dist/simulation/config.d.ts +20 -0
  122. package/dist/simulation/config.js +54 -0
  123. package/dist/simulation/journey.d.ts +7 -0
  124. package/dist/simulation/journey.js +49 -0
  125. package/dist/simulation/scenarios.d.ts +17 -0
  126. package/dist/simulation/scenarios.js +96 -0
  127. package/dist/simulation/simulation.d.ts +24 -0
  128. package/dist/simulation/simulation.js +154 -0
  129. package/dist/simulation/utility.d.ts +23 -0
  130. package/dist/simulation/utility.js +50 -0
  131. package/package.json +71 -0
@@ -0,0 +1,16 @@
1
+ import type { EndpointTelemetry } from "../../domain";
2
+ declare class EndpointStore {
3
+ private readonly endpoints;
4
+ private createKey;
5
+ record(input: {
6
+ method: string;
7
+ route: string;
8
+ duration: number;
9
+ statusCode: number;
10
+ }): void;
11
+ get(method: string, route: string): EndpointTelemetry | undefined;
12
+ all(): EndpointTelemetry[];
13
+ private map;
14
+ reset(): void;
15
+ }
16
+ export { EndpointStore };
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EndpointStore = void 0;
4
+ const utility_1 = require("../utility");
5
+ class EndpointStore {
6
+ endpoints = new Map();
7
+ createKey(method, route) {
8
+ return `${method}:${route}`;
9
+ }
10
+ record(input) {
11
+ const key = this.createKey(input.method, input.route);
12
+ let endpoint = this.endpoints.get(key);
13
+ if (!endpoint) {
14
+ endpoint = {
15
+ method: input.method,
16
+ route: input.route,
17
+ requestCount: 0,
18
+ errorCount: 0,
19
+ totalLatency: 0,
20
+ histogram: new utility_1.Histogram(),
21
+ lastRequestAt: Date.now(),
22
+ };
23
+ this.endpoints.set(key, endpoint);
24
+ }
25
+ endpoint.requestCount++;
26
+ endpoint.totalLatency += input.duration;
27
+ endpoint.lastRequestAt = Date.now();
28
+ endpoint.histogram.record(input.duration);
29
+ if (input.statusCode >= 400) {
30
+ endpoint.errorCount++;
31
+ }
32
+ }
33
+ get(method, route) {
34
+ const endpoint = this.endpoints.get(this.createKey(method, route));
35
+ if (!endpoint) {
36
+ return undefined;
37
+ }
38
+ return this.map(endpoint);
39
+ }
40
+ all() {
41
+ return [...this.endpoints.values()].map(endpoint => this.map(endpoint));
42
+ }
43
+ map(endpoint) {
44
+ const histogram = endpoint.histogram.snapshot();
45
+ return {
46
+ method: endpoint.method,
47
+ route: endpoint.route,
48
+ rps: endpoint.requestCount - histogram.count, //????
49
+ // calculated later from buckets
50
+ requestCount: endpoint.requestCount,
51
+ averageLatency: endpoint.requestCount === 0 ? 0 : endpoint.totalLatency / endpoint.requestCount,
52
+ p95: histogram.p95,
53
+ p99: histogram.p99,
54
+ errorRate: endpoint.requestCount === 0 ? 0 : endpoint.errorCount / endpoint.requestCount,
55
+ status: endpoint.errorCount > 0 ? "DEGRADED" : "HEALTHY",
56
+ };
57
+ }
58
+ reset() {
59
+ this.endpoints.clear();
60
+ }
61
+ }
62
+ exports.EndpointStore = EndpointStore;
@@ -0,0 +1,20 @@
1
+ import type { EventLoopMetrics, MemoryMetrics, V8RuntimeInfo, LibuvHandles, HealthData, CpuMetrics, GCMetrics } from "../../domain";
2
+ declare class HealthStore {
3
+ private cpu;
4
+ private memory;
5
+ private eventLoop;
6
+ private handles;
7
+ private garbageCollection;
8
+ private runtime;
9
+ private readonly history;
10
+ constructor(bufferSize?: number, eventLoopLagThreshold?: number);
11
+ updateCPU(cpu: CpuMetrics): void;
12
+ updateMemory(memory: MemoryMetrics): void;
13
+ updateEventLoop(eventLoop: EventLoopMetrics): void;
14
+ updateHandles(handles: LibuvHandles): void;
15
+ updateGC(gc: GCMetrics): void;
16
+ updateRuntime(runtime: V8RuntimeInfo): void;
17
+ get(): HealthData;
18
+ resetHistory(): void;
19
+ }
20
+ export { HealthStore };
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HealthStore = void 0;
4
+ const utility_1 = require("../utility");
5
+ class HealthStore {
6
+ cpu;
7
+ memory;
8
+ eventLoop;
9
+ handles;
10
+ garbageCollection;
11
+ runtime;
12
+ history;
13
+ constructor(bufferSize = 60, eventLoopLagThreshold = 100) {
14
+ this.history = {
15
+ eventLoopLag: new utility_1.RingBuffer(bufferSize),
16
+ usedHeap: new utility_1.RingBuffer(bufferSize),
17
+ totalHeap: new utility_1.RingBuffer(bufferSize),
18
+ rssMemory: new utility_1.RingBuffer(bufferSize),
19
+ };
20
+ this.cpu = {
21
+ usageRate: 0,
22
+ numberOfCores: 0,
23
+ perCoreUsage: [],
24
+ userUsage: 0,
25
+ systemUsage: 0,
26
+ idleUsage: 0,
27
+ };
28
+ this.memory = {
29
+ heapUsage: 0,
30
+ heapSize: 0,
31
+ rssMemory: 0,
32
+ rssMemoryTotal: 0,
33
+ externalMemory: 0,
34
+ };
35
+ this.eventLoop = {
36
+ lag: 0,
37
+ threshold: eventLoopLagThreshold,
38
+ };
39
+ this.handles = {
40
+ activeHandles: 0,
41
+ activeHandlesTimers: 0,
42
+ activeHandlesSockets: 0,
43
+ activeLibuvHandles: 0,
44
+ timers: 0,
45
+ fileDescriptors: 0,
46
+ };
47
+ this.garbageCollection = {
48
+ gcCount: 0,
49
+ gcTime: 0,
50
+ gcPauseAverage: 0,
51
+ minorGC: {
52
+ runCount: 0,
53
+ averageTime: 0,
54
+ },
55
+ majorGC: {
56
+ runCount: 0,
57
+ averageTime: 0,
58
+ },
59
+ incrementalGC: {
60
+ runCount: 0,
61
+ averageTime: 0,
62
+ },
63
+ heapSpaces: [],
64
+ gcTotals: {
65
+ totalPauseTime: 0,
66
+ freedMemory: 0,
67
+ promotions: 0,
68
+ tenuredSize: 0,
69
+ },
70
+ };
71
+ this.runtime = {
72
+ pid: process.pid,
73
+ platform: process.platform,
74
+ nodeVersion: process.version,
75
+ v8Version: process.versions.v8,
76
+ libuvVersion: process.versions.uv,
77
+ openSSLVersion: process.versions.openssl,
78
+ threadPoolSize: 0,
79
+ activeThreads: 0,
80
+ startup: {
81
+ bootstrapTime: 0,
82
+ requiredModules: 0,
83
+ },
84
+ };
85
+ }
86
+ updateCPU(cpu) {
87
+ this.cpu = cpu;
88
+ }
89
+ updateMemory(memory) {
90
+ this.memory = memory;
91
+ this.history.usedHeap.push(memory.heapUsage);
92
+ this.history.totalHeap.push(memory.heapSize);
93
+ this.history.rssMemory.push(memory.rssMemory);
94
+ }
95
+ updateEventLoop(eventLoop) {
96
+ this.eventLoop = eventLoop;
97
+ this.history.eventLoopLag.push(eventLoop.lag);
98
+ }
99
+ updateHandles(handles) {
100
+ this.handles = handles;
101
+ }
102
+ updateGC(gc) {
103
+ this.garbageCollection = gc;
104
+ }
105
+ updateRuntime(runtime) {
106
+ this.runtime = runtime;
107
+ }
108
+ get() {
109
+ return {
110
+ cpu: this.cpu,
111
+ memory: this.memory,
112
+ eventLoop: this.eventLoop,
113
+ handles: this.handles,
114
+ garbageCollection: this.garbageCollection,
115
+ runtime: this.runtime,
116
+ history: {
117
+ eventLoopLag: this.history.eventLoopLag.values(),
118
+ memoryBreakdown: {
119
+ usedHeap: this.history.usedHeap.values(),
120
+ totalHeap: this.history.totalHeap.values(),
121
+ rssMemory: this.history.rssMemory.values(),
122
+ }
123
+ }
124
+ };
125
+ }
126
+ resetHistory() {
127
+ this.history.eventLoopLag.clear();
128
+ this.history.usedHeap.clear();
129
+ this.history.totalHeap.clear();
130
+ this.history.rssMemory.clear();
131
+ }
132
+ }
133
+ exports.HealthStore = HealthStore;
@@ -0,0 +1,8 @@
1
+ export * from './alert.store';
2
+ export * from './analytics.store';
3
+ export * from './dashboard.store';
4
+ export * from './endpoint.store';
5
+ export * from './health.store';
6
+ export * from './metrics.store';
7
+ export * from './system.store';
8
+ export * from './bucket.store';
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./alert.store"), exports);
18
+ __exportStar(require("./analytics.store"), exports);
19
+ __exportStar(require("./dashboard.store"), exports);
20
+ __exportStar(require("./endpoint.store"), exports);
21
+ __exportStar(require("./health.store"), exports);
22
+ __exportStar(require("./metrics.store"), exports);
23
+ __exportStar(require("./system.store"), exports);
24
+ __exportStar(require("./bucket.store"), exports);
@@ -0,0 +1,23 @@
1
+ import { HistogramSnapshot } from '../utility';
2
+ export interface MetricsSnapshot {
3
+ requestCount: number;
4
+ errorClientCount: number;
5
+ errorServerCount: number;
6
+ averageLatency: number;
7
+ errorRate: number;
8
+ latency: HistogramSnapshot;
9
+ }
10
+ export declare class MetricsStore {
11
+ private requestCount;
12
+ private errorClientCount;
13
+ private errorServerCount;
14
+ private totalLatency;
15
+ private readonly histogram;
16
+ constructor();
17
+ record(input: {
18
+ duration: number;
19
+ statusCode: number;
20
+ }): "SUCCESS" | "FAILED" | "ANOMALY";
21
+ snapshot(): MetricsSnapshot;
22
+ reset(): void;
23
+ }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MetricsStore = void 0;
4
+ const utility_1 = require("../utility");
5
+ class MetricsStore {
6
+ requestCount;
7
+ errorClientCount;
8
+ errorServerCount;
9
+ totalLatency;
10
+ histogram;
11
+ constructor() {
12
+ this.requestCount = 0;
13
+ this.errorClientCount = 0;
14
+ this.errorServerCount = 0;
15
+ this.totalLatency = 0;
16
+ this.histogram = new utility_1.Histogram();
17
+ }
18
+ record(input) {
19
+ this.requestCount++;
20
+ this.totalLatency += input.duration;
21
+ const status = this.histogram.record(input.duration);
22
+ if (input.statusCode >= 400 && input.statusCode < 500) {
23
+ this.errorClientCount++;
24
+ }
25
+ else if (input.statusCode >= 500) {
26
+ this.errorServerCount++;
27
+ }
28
+ return status;
29
+ }
30
+ snapshot() {
31
+ const latency = this.histogram.snapshot();
32
+ return {
33
+ requestCount: this.requestCount,
34
+ errorClientCount: this.errorClientCount,
35
+ errorServerCount: this.errorServerCount,
36
+ averageLatency: this.requestCount === 0 ? 0 : this.totalLatency / this.requestCount,
37
+ errorRate: this.requestCount === 0 ? 0 : (this.errorClientCount + this.errorServerCount) / this.requestCount,
38
+ latency,
39
+ };
40
+ }
41
+ reset() {
42
+ this.requestCount = 0;
43
+ this.errorClientCount = 0;
44
+ this.errorServerCount = 0;
45
+ this.totalLatency = 0;
46
+ this.histogram.reset();
47
+ }
48
+ }
49
+ exports.MetricsStore = MetricsStore;
@@ -0,0 +1,8 @@
1
+ import type { SystemStaticInfo } from '../../domain';
2
+ declare class SystemStore {
3
+ private readonly env;
4
+ private readonly startedAt;
5
+ constructor(env?: string);
6
+ get(): SystemStaticInfo;
7
+ }
8
+ export { SystemStore };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SystemStore = void 0;
4
+ class SystemStore {
5
+ env;
6
+ startedAt;
7
+ constructor(env = process.env.NODE_ENV ?? "unknown") {
8
+ this.env = env;
9
+ this.startedAt = Date.now();
10
+ }
11
+ get() {
12
+ return {
13
+ uptime: Date.now() - this.startedAt,
14
+ nodeVersion: process.version,
15
+ env: this.env,
16
+ };
17
+ }
18
+ }
19
+ exports.SystemStore = SystemStore;
@@ -0,0 +1,37 @@
1
+ export interface HistogramSnapshot {
2
+ buckets: number[];
3
+ count: number;
4
+ sum: number;
5
+ average: number;
6
+ sumOfSquares: number;
7
+ p50: number;
8
+ p95: number;
9
+ p99: number;
10
+ anomalyCount: number;
11
+ }
12
+ type RecordStatus = 'SUCCESS' | 'FAILED' | 'ANOMALY';
13
+ export declare class Histogram {
14
+ private readonly anomalySampleSize;
15
+ private readonly anomalyLowerLimit;
16
+ private readonly limits;
17
+ private readonly counters;
18
+ private totalCount;
19
+ private totalSum;
20
+ private totalSumOfSquares;
21
+ private anomalyCount;
22
+ constructor(anomalySampleSize?: number, //! For this use calculation
23
+ anomalyLowerLimit?: number);
24
+ record(durationMs: number): RecordStatus;
25
+ snapshot(): HistogramSnapshot;
26
+ private percentile;
27
+ private checkIfAnomaly;
28
+ reset(): void;
29
+ }
30
+ /**
31
+ * Calculates the tolerance for a given value based on the provided histogram limits.
32
+ * @param value The value for which to calculate the tolerance.
33
+ * @param limits An array of upper limits for each histogram bucket.
34
+ * @returns The tolerance for the given value, which is the difference between the upper and lower limits of the bucket that contains the value.
35
+ */
36
+ export declare function getBucketTolerance(value: number, limits: number[]): number;
37
+ export {};
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Histogram = void 0;
4
+ exports.getBucketTolerance = getBucketTolerance;
5
+ const utility_1 = require("../../utility");
6
+ class Histogram {
7
+ anomalySampleSize;
8
+ anomalyLowerLimit;
9
+ limits = [
10
+ 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100,
11
+ 120, 140, 160, 180, 200, 250, 300, 350, 400, 450, 500,
12
+ 550, 600, 700, 800, 900, 1000, 1500, 2000, 3000, 5000,
13
+ Infinity,
14
+ ];
15
+ counters;
16
+ totalCount = 0;
17
+ totalSum = 0;
18
+ totalSumOfSquares = 0;
19
+ anomalyCount = 0;
20
+ constructor(anomalySampleSize = 60, //! For this use calculation
21
+ anomalyLowerLimit = 180) {
22
+ this.anomalySampleSize = anomalySampleSize;
23
+ this.anomalyLowerLimit = anomalyLowerLimit;
24
+ this.counters = new Uint32Array(this.limits.length);
25
+ }
26
+ record(durationMs) {
27
+ const isAnomaly = this.checkIfAnomaly(durationMs);
28
+ this.totalCount++;
29
+ this.totalSum += durationMs;
30
+ this.totalSumOfSquares += durationMs * durationMs;
31
+ for (let i = 0; i < this.limits.length; i++) {
32
+ if (durationMs <= this.limits[i]) {
33
+ this.counters[i]++;
34
+ break;
35
+ }
36
+ }
37
+ return isAnomaly ? 'ANOMALY' : 'SUCCESS';
38
+ }
39
+ snapshot() {
40
+ return {
41
+ buckets: Array.from(this.counters),
42
+ count: this.totalCount,
43
+ sum: this.totalSum,
44
+ average: this.totalCount === 0 ? 0 : this.totalSum / this.totalCount,
45
+ sumOfSquares: this.totalSumOfSquares,
46
+ p50: this.percentile(0.50),
47
+ p95: this.percentile(0.95),
48
+ p99: this.percentile(0.99),
49
+ anomalyCount: this.anomalyCount,
50
+ };
51
+ }
52
+ percentile(percent) {
53
+ return (0, utility_1.percentileHistogram)(this.counters, this.totalCount, percent, this.limits);
54
+ }
55
+ checkIfAnomaly(durationMs) {
56
+ if (this.totalCount < this.anomalySampleSize ||
57
+ durationMs < this.anomalyLowerLimit)
58
+ return false;
59
+ const isAnomaly = (0, utility_1.isDataAnomalyHistogram)(durationMs, this.totalCount, this.totalSum, this.totalSumOfSquares);
60
+ if (isAnomaly) {
61
+ this.anomalyCount++;
62
+ return true;
63
+ }
64
+ return false;
65
+ }
66
+ reset() {
67
+ this.counters.fill(0);
68
+ this.totalCount = 0;
69
+ this.totalSum = 0;
70
+ this.totalSumOfSquares = 0;
71
+ this.anomalyCount = 0;
72
+ }
73
+ }
74
+ exports.Histogram = Histogram;
75
+ /**
76
+ * Calculates the tolerance for a given value based on the provided histogram limits.
77
+ * @param value The value for which to calculate the tolerance.
78
+ * @param limits An array of upper limits for each histogram bucket.
79
+ * @returns The tolerance for the given value, which is the difference between the upper and lower limits of the bucket that contains the value.
80
+ */
81
+ function getBucketTolerance(value, limits) {
82
+ let lower = 0;
83
+ for (const limit of limits) {
84
+ if (value <= limit) {
85
+ const upper = Number.isFinite(limit) ? limit : lower;
86
+ return (upper - lower) + upper * 0.2; // 20% of the upper limit
87
+ }
88
+ lower = limit;
89
+ }
90
+ return 0;
91
+ }
@@ -0,0 +1,2 @@
1
+ export * from './histogram';
2
+ export * from './ring-buffer';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./histogram"), exports);
18
+ __exportStar(require("./ring-buffer"), exports);
@@ -0,0 +1,12 @@
1
+ export declare class RingBuffer<T> {
2
+ private readonly capacity;
3
+ private readonly buffer;
4
+ private index;
5
+ private count;
6
+ constructor(capacity: number);
7
+ push(value: T): void;
8
+ values(): T[];
9
+ latest(): T | undefined;
10
+ clear(): void;
11
+ size(): number;
12
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RingBuffer = void 0;
4
+ class RingBuffer {
5
+ capacity;
6
+ buffer;
7
+ index = 0;
8
+ count = 0;
9
+ constructor(capacity) {
10
+ this.capacity = capacity;
11
+ this.buffer = new Array(capacity);
12
+ }
13
+ push(value) {
14
+ this.buffer[this.index] = value;
15
+ this.index = (this.index + 1) % this.capacity;
16
+ if (this.count < this.capacity) {
17
+ this.count++;
18
+ }
19
+ }
20
+ values() {
21
+ if (this.count === 0) {
22
+ return [];
23
+ }
24
+ if (this.count < this.capacity) {
25
+ return this.buffer.slice(0, this.count);
26
+ }
27
+ return [
28
+ ...this.buffer.slice(this.index),
29
+ ...this.buffer.slice(0, this.index),
30
+ ];
31
+ }
32
+ latest() {
33
+ if (this.count === 0) {
34
+ return undefined;
35
+ }
36
+ const position = (this.index - 1 + this.capacity) % this.capacity;
37
+ return this.buffer[position];
38
+ }
39
+ clear() {
40
+ this.index = 0;
41
+ this.count = 0;
42
+ }
43
+ size() {
44
+ return this.count;
45
+ }
46
+ }
47
+ exports.RingBuffer = RingBuffer;
@@ -0,0 +1,18 @@
1
+ import type { SystemStaticInfo, AnalyticsData, DashboardData, HealthData } from '../domain';
2
+ import { CPUCollector, MemoryCollector, EventLoopCollector, GCCollector, HandlesCollector, RuntimeCollector } from './collectors';
3
+ declare class CollectorService {
4
+ private readonly cpuCollector;
5
+ private readonly memoryCollector;
6
+ private readonly eventLoopCollector;
7
+ private readonly gcCollector;
8
+ private readonly handlesCollector;
9
+ private readonly runtimeCollector;
10
+ constructor(cpuCollector: CPUCollector, memoryCollector: MemoryCollector, eventLoopCollector: EventLoopCollector, gcCollector: GCCollector, handlesCollector: HandlesCollector, runtimeCollector: RuntimeCollector);
11
+ getSystemStaticInfo(): SystemStaticInfo;
12
+ getDashboardData(): DashboardData;
13
+ getAnalyticsData(): AnalyticsData;
14
+ getHealthData(): HealthData;
15
+ tick(): void;
16
+ }
17
+ export declare const collectorService: CollectorService;
18
+ export {};