@unboundcx/sdk 2.8.6 → 2.8.7

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.
@@ -0,0 +1,111 @@
1
+ export class MetricsService {
2
+ constructor(sdk) {
3
+ this.sdk = sdk;
4
+ }
5
+
6
+ /**
7
+ * Get current task router metrics
8
+ * Retrieves real-time metrics for task router queues, tasks, and workers.
9
+ * This provides insights into queue performance, wait times, task counts, and worker activity.
10
+ *
11
+ * @param {Object} params - Metric parameters
12
+ * @param {string} [params.period] - Time period for metrics calculation. Options: '5min', '15min', '30min', '1hour', '24hour'
13
+ * @param {string} [params.queueId] - Specific queue ID to filter metrics. If not provided, returns metrics for all queues
14
+ * @param {string} [params.metricType] - Type of metrics to retrieve: 'queue', 'task', 'worker', or 'all' (default: 'all')
15
+ * @param {number} [params.limit=100] - Maximum number of metric records to return (default: 100)
16
+ * @returns {Promise<Object>} Object containing the requested metrics
17
+ * @returns {Object} result.metrics - The metrics data organized by type
18
+ * @returns {Object} [result.metrics.queue] - Queue-level metrics (if metricType is 'queue' or 'all')
19
+ * @returns {number} result.metrics.queue.tasksWaiting - Number of tasks currently waiting in queue
20
+ * @returns {number} result.metrics.queue.tasksAssigned - Number of tasks currently assigned to workers
21
+ * @returns {number} result.metrics.queue.tasksConnected - Number of tasks currently connected/active
22
+ * @returns {number} result.metrics.queue.avgWaitTime - Average wait time in seconds for tasks in this period
23
+ * @returns {number} result.metrics.queue.longestWaitTime - Longest current wait time in seconds
24
+ * @returns {Object} [result.metrics.task] - Task-level metrics (if metricType is 'task' or 'all')
25
+ * @returns {number} result.metrics.task.created - Number of tasks created in this period
26
+ * @returns {number} result.metrics.task.completed - Number of tasks completed in this period
27
+ * @returns {number} result.metrics.task.abandoned - Number of tasks abandoned in this period
28
+ * @returns {Object} [result.metrics.worker] - Worker-level metrics (if metricType is 'worker' or 'all')
29
+ * @returns {number} result.metrics.worker.available - Number of workers currently available
30
+ * @returns {number} result.metrics.worker.busy - Number of workers currently busy with tasks
31
+ * @returns {number} result.metrics.worker.offline - Number of workers currently offline
32
+ * @returns {string} result.period - The time period used for calculations
33
+ * @returns {string} [result.queueId] - The queue ID if filtered to a specific queue
34
+ *
35
+ * @example
36
+ * // Get all current metrics for all queues
37
+ * const metrics = await sdk.taskRouter.metrics.getCurrent({
38
+ * period: '15min',
39
+ * metricType: 'all'
40
+ * });
41
+ * console.log(metrics.metrics.queue.tasksWaiting); // 5
42
+ * console.log(metrics.metrics.worker.available); // 12
43
+ *
44
+ * @example
45
+ * // Get queue-specific metrics for last 5 minutes
46
+ * const queueMetrics = await sdk.taskRouter.metrics.getCurrent({
47
+ * period: '5min',
48
+ * queueId: 'queue456',
49
+ * metricType: 'queue',
50
+ * limit: 50
51
+ * });
52
+ * console.log(queueMetrics.metrics.queue.avgWaitTime); // 45.3
53
+ *
54
+ * @example
55
+ * // Get worker metrics for last hour
56
+ * const workerMetrics = await sdk.taskRouter.metrics.getCurrent({
57
+ * period: '1hour',
58
+ * metricType: 'worker'
59
+ * });
60
+ * console.log(workerMetrics.metrics.worker.available); // 8
61
+ * console.log(workerMetrics.metrics.worker.busy); // 4
62
+ *
63
+ * @example
64
+ * // Get task completion metrics for last 24 hours
65
+ * const taskMetrics = await sdk.taskRouter.metrics.getCurrent({
66
+ * period: '24hour',
67
+ * metricType: 'task',
68
+ * limit: 200
69
+ * });
70
+ * console.log(taskMetrics.metrics.task.created); // 150
71
+ * console.log(taskMetrics.metrics.task.completed); // 142
72
+ */
73
+ async getCurrent(accountId, params = {}) {
74
+ const { period, queueId, metricType, limit = 100 } = params;
75
+
76
+ this.sdk.validateParams(
77
+ { period, queueId, metricType, limit },
78
+ {
79
+ period: { type: 'string', required: false },
80
+ queueId: { type: 'string', required: false },
81
+ metricType: { type: 'string', required: false },
82
+ limit: { type: 'number', required: false },
83
+ },
84
+ );
85
+
86
+ const requestParams = {
87
+ body: {
88
+ limit,
89
+ },
90
+ };
91
+
92
+ if (period !== undefined) {
93
+ requestParams.body.period = period;
94
+ }
95
+
96
+ if (queueId !== undefined) {
97
+ requestParams.body.queueId = queueId;
98
+ }
99
+
100
+ if (metricType !== undefined) {
101
+ requestParams.body.metricType = metricType;
102
+ }
103
+
104
+ const result = await this.sdk._fetch(
105
+ '/taskRouter/metrics/current',
106
+ 'GET',
107
+ requestParams,
108
+ );
109
+ return result;
110
+ }
111
+ }
@@ -0,0 +1,12 @@
1
+ import { WorkerService } from './WorkerService.js';
2
+ import { TaskService } from './TaskService.js';
3
+ import { MetricsService } from './MetricsService.js';
4
+
5
+ export class TaskRouterService {
6
+ constructor(sdk) {
7
+ this.sdk = sdk;
8
+ this.worker = new WorkerService(sdk);
9
+ this.task = new TaskService(sdk);
10
+ this.metrics = new MetricsService(sdk);
11
+ }
12
+ }