@zintrust/workers 0.4.67 → 0.4.70

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.
@@ -24,15 +24,17 @@ const _lastWorkers = [];
24
24
  const detailsCache = new Map();
25
25
  const MAX_CACHE_SIZE = 50;
26
26
 
27
+ function toMetricNumber(value, fallback = 0) {
28
+ return Number.isFinite(value) ? value : fallback;
29
+ }
30
+
27
31
  // Theme management
28
32
  function getPreferredTheme() {
29
33
  const stored = localStorage.getItem(THEME_KEY);
30
34
  if (stored === 'light' || stored === 'dark') {
31
35
  return stored;
32
36
  }
33
- const prefersLight =
34
- globalThis.window.matchMedia &&
35
- globalThis.window.matchMedia('(prefers-color-scheme: light)').matches;
37
+ const prefersLight = globalThis.window.matchMedia?.('(prefers-color-scheme: light)').matches;
36
38
  return prefersLight ? 'light' : 'dark';
37
39
  }
38
40
 
@@ -84,7 +86,7 @@ function handleFetchError(elements, error) {
84
86
 
85
87
  // Helper function to validate worker data
86
88
  function validateWorkerData(data) {
87
- if (!data || !data.workers || !Array.isArray(data.workers)) {
89
+ if (!Array.isArray(data?.workers)) {
88
90
  console.error('Invalid worker data structure:', data);
89
91
  return false;
90
92
  }
@@ -312,7 +314,7 @@ function updateDetailViews(detailRow, details) {
312
314
 
313
315
  // Update all data-key elements
314
316
  detailRow.querySelectorAll('[data-key]').forEach((element) => {
315
- updateDetailElement(element);
317
+ updateDetailElement(element, details);
316
318
  });
317
319
 
318
320
  // Delegate to specialized functions
@@ -673,13 +675,16 @@ function createVersionCell(worker) {
673
675
 
674
676
  // Helper function to safely extract performance metrics
675
677
  function getPerformanceMetrics(worker) {
676
- if (!worker) return { processed: 0, avgTime: 0, memory: 0 };
678
+ if (!worker) return { processed: 0, failed: 0, avgTime: 0, memory: 0, cpu: 0, uptime: 0 };
677
679
 
678
- const metrics = worker.metrics || {};
680
+ const metrics = worker.metrics || worker.details?.metrics || {};
679
681
  return {
680
- processed: metrics.processed || worker.processed || 0,
681
- avgTime: metrics.avgTime || worker.avgTime || 0,
682
- memory: metrics.memory || worker.memory || 0,
682
+ processed: toMetricNumber(metrics.processed, toMetricNumber(worker.processed, 0)),
683
+ failed: toMetricNumber(metrics.failed, toMetricNumber(worker.failed, 0)),
684
+ avgTime: toMetricNumber(metrics.avgTime, toMetricNumber(worker.avgTime, 0)),
685
+ memory: toMetricNumber(metrics.memory, toMetricNumber(worker.memory, 0)),
686
+ cpu: toMetricNumber(metrics.cpu, toMetricNumber(worker.cpu, 0)),
687
+ uptime: toMetricNumber(metrics.uptime, toMetricNumber(worker.uptime, 0)),
683
688
  };
684
689
  }
685
690
 
@@ -974,32 +979,34 @@ function createConfigurationSection(worker) {
974
979
 
975
980
  // Helper function to create Performance Metrics section HTML
976
981
  function createPerformanceMetricsSection(worker) {
982
+ const metrics = getPerformanceMetrics(worker);
983
+
977
984
  return `
978
985
  <div class="detail-section">
979
986
  <h4>Performance Metrics</h4>
980
987
  <div class="detail-item">
981
988
  <span>Processed Jobs</span>
982
- <span data-key="metrics.processed">${worker.processed.toLocaleString()}</span>
989
+ <span data-key="metrics.processed">${metrics.processed.toLocaleString()}</span>
983
990
  </div>
984
991
  <div class="detail-item">
985
992
  <span>Failed Jobs</span>
986
- <span data-key="metrics.failed">${worker.failed || 0}</span>
993
+ <span data-key="metrics.failed">${metrics.failed}</span>
987
994
  </div>
988
995
  <div class="detail-item">
989
996
  <span>Average Time</span>
990
- <span data-key="metrics.avgTime">${worker.avgTime}ms</span>
997
+ <span data-key="metrics.avgTime">${metrics.avgTime}ms</span>
991
998
  </div>
992
999
  <div class="detail-item">
993
1000
  <span>Memory Usage</span>
994
- <span data-key="metrics.memory">${worker.memory}MB</span>
1001
+ <span data-key="metrics.memory">${metrics.memory}MB</span>
995
1002
  </div>
996
1003
  <div class="detail-item">
997
1004
  <span>CPU Usage</span>
998
- <span data-key="metrics.cpu">${worker.cpu || 'N/A'}</span>
1005
+ <span data-key="metrics.cpu">${metrics.cpu > 0 ? metrics.cpu + '%' : 'N/A'}</span>
999
1006
  </div>
1000
1007
  <div class="detail-item">
1001
1008
  <span>Uptime</span>
1002
- <span data-key="metrics.uptime">${worker.uptime || 'N/A'}</span>
1009
+ <span data-key="metrics.uptime">${metrics.uptime > 0 ? formatUptime(metrics.uptime) : 'N/A'}</span>
1003
1010
  </div>
1004
1011
  </div>
1005
1012
  `;
@@ -1135,6 +1142,8 @@ function renderWorkers(data) {
1135
1142
  const tbody = document.getElementById('workers-tbody');
1136
1143
  if (!tbody) return;
1137
1144
 
1145
+ updateDashboardIdentity(data.appName);
1146
+
1138
1147
  const expandedWorkers = new Set(
1139
1148
  Array.from(tbody.querySelectorAll('.expandable-row.open'))
1140
1149
  .map((row) => row.getAttribute('id')?.replace('details-', ''))
@@ -1182,6 +1191,18 @@ function renderWorkers(data) {
1182
1191
  updatePagination(data.pagination);
1183
1192
  }
1184
1193
 
1194
+ function updateDashboardIdentity(appName) {
1195
+ if (typeof appName !== 'string' || appName.trim() === '') return;
1196
+
1197
+ const normalizedAppName = appName.trim();
1198
+ document.title = `${normalizedAppName} Workers Dashboard`;
1199
+
1200
+ const heading = document.querySelector('.header-top h1');
1201
+ if (heading) {
1202
+ heading.textContent = `${normalizedAppName}'s Workers`;
1203
+ }
1204
+ }
1205
+
1185
1206
  function toggleDetails(rowId) {
1186
1207
  const row = document.getElementById(rowId);
1187
1208
  if (row) {
@@ -1346,7 +1367,6 @@ async function toggleAutoStart(name, driver, enabled) {
1346
1367
  }
1347
1368
 
1348
1369
  function showAddWorkerModal() {
1349
- // TODO: Implement add worker modal
1350
1370
  alert('Add Worker functionality coming soon!');
1351
1371
  }
1352
1372
 
@@ -1766,7 +1786,7 @@ function setupEventStream() {
1766
1786
 
1767
1787
  try {
1768
1788
  const payload = JSON.parse(evt.data);
1769
- if (payload && payload.type === 'snapshot') {
1789
+ if (payload?.type === 'snapshot') {
1770
1790
  const now = Date.now();
1771
1791
  if (now - lastSseRefresh < 4000) return;
1772
1792
  lastSseRefresh = now;
@@ -1,12 +0,0 @@
1
- type DurableObjectState = {
2
- storage: {
3
- get: (key: string) => Promise<unknown>;
4
- put: (key: string, value: unknown) => Promise<void>;
5
- };
6
- };
7
- export declare class ZinTrustWorkerShutdownDurableObject {
8
- private readonly state;
9
- constructor(state: DurableObjectState);
10
- fetch(request: Request): Promise<Response>;
11
- }
12
- export {};
@@ -1,41 +0,0 @@
1
- import { Logger } from '@zintrust/core';
2
- const loadState = async (state) => {
3
- const stored = (await state.storage.get('shutdown'));
4
- return stored ?? { shuttingDown: false };
5
- };
6
- const saveState = async (state, value) => {
7
- await state.storage.put('shutdown', value);
8
- };
9
- // eslint-disable-next-line no-restricted-syntax
10
- export class ZinTrustWorkerShutdownDurableObject {
11
- state;
12
- constructor(state) {
13
- this.state = state;
14
- }
15
- async fetch(request) {
16
- const url = new URL(request.url);
17
- const path = url.pathname;
18
- if (request.method === 'GET' && path === '/status') {
19
- const current = await loadState(this.state);
20
- return new Response(JSON.stringify(current), {
21
- status: 200,
22
- headers: { 'content-type': 'application/json' },
23
- });
24
- }
25
- if (request.method === 'POST' && path === '/shutdown') {
26
- const payload = (await request.json().catch(() => ({})));
27
- const next = {
28
- shuttingDown: true,
29
- startedAt: new Date().toISOString(),
30
- reason: payload.reason ?? 'manual',
31
- };
32
- await saveState(this.state, next);
33
- Logger.info('Worker shutdown requested via Durable Object', next);
34
- return new Response(JSON.stringify({ ok: true }), {
35
- status: 202,
36
- headers: { 'content-type': 'application/json' },
37
- });
38
- }
39
- return new Response('Not Found', { status: 404 });
40
- }
41
- }
@@ -1,202 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
- <title>ZinTrust Workers Dashboard</title>
7
- <link rel="stylesheet" href="workers/styles.css" />
8
- </head>
9
- <body>
10
- <div class="container">
11
- <div class="header">
12
- <div class="header-top">
13
- <div style="display: flex; align-items: center; gap: 16px">
14
- <div class="logo-frame">
15
- <img src="workers/zintrust.svg" alt="ZinTrust" class="logo-img" />
16
- </div>
17
- <h1>ZinTrust Workers</h1>
18
- </div>
19
- <div class="header-actions">
20
- <button id="theme-toggle" class="theme-toggle">
21
- <svg class="icon" viewBox="0 0 24 24">
22
- <circle cx="12" cy="12" r="5" />
23
- <path
24
- d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"
25
- />
26
- </svg>
27
- Theme
28
- </button>
29
- <button id="auto-refresh-toggle" class="btn" onclick="toggleAutoRefresh()">
30
- <svg id="auto-refresh-icon" class="icon" viewBox="0 0 24 24">
31
- <polygon points="5 3 19 12 5 21 5 3" />
32
- </svg>
33
- <span id="auto-refresh-label">Auto Refresh</span>
34
- </button>
35
- <button class="btn" onclick="fetchData()">
36
- <svg class="icon" viewBox="0 0 24 24">
37
- <path d="M23 4v6h-6" />
38
- <path d="M1 20v-6h6" />
39
- <path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15" />
40
- </svg>
41
- Refresh
42
- </button>
43
- <button class="btn btn-primary" onclick="showAddWorkerModal()">
44
- <svg class="icon" viewBox="0 0 24 24">
45
- <line x1="12" y1="5" x2="12" y2="19" />
46
- <line x1="5" y1="12" x2="19" y2="12" />
47
- </svg>
48
- Add Worker
49
- </button>
50
- </div>
51
- </div>
52
-
53
- <div class="nav-bar">
54
- <nav class="nav-links">
55
- <a href="/queue-monitor" class="nav-link">Queue Monitor</a>
56
- <a href="/telemetry" class="nav-link">Telemetry</a>
57
- <a href="/metrics" class="nav-link">Metrics</a>
58
- </nav>
59
- </div>
60
-
61
- <div class="filters-bar">
62
- <div class="filter-group">
63
- <span>Status:</span>
64
- <select id="status-filter">
65
- <option value="">All Status</option>
66
- <option value="running">Running</option>
67
- <option value="stopped">Stopped</option>
68
- <option value="error">Error</option>
69
- <option value="paused">Paused</option>
70
- </select>
71
- </div>
72
- <div class="filter-group">
73
- <span>Driver:</span>
74
- <select id="driver-filter">
75
- <option value="">All Drivers</option>
76
- </select>
77
- </div>
78
- <div class="filter-group">
79
- <span>Sort:</span>
80
- <select id="sort-select">
81
- <option value="name">Sort by Name</option>
82
- <option value="status" selected>Sort by Status</option>
83
- <option value="driver">Sort by Driver</option>
84
- <option value="health">Sort by Health</option>
85
- <option value="version">Sort by Version</option>
86
- <option value="processed">Sort by Performance</option>
87
- </select>
88
- </div>
89
- <div style="flex-grow: 1"></div>
90
- <div class="search-box">
91
- <svg class="search-icon" viewBox="0 0 24 24">
92
- <circle cx="11" cy="11" r="8"></circle>
93
- <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
94
- </svg>
95
- <input type="text" id="search-input" placeholder="Search workers..." />
96
- </div>
97
- </div>
98
- </div>
99
-
100
- <div id="loading" style="text-align: center; padding: 40px; color: var(--muted)">
101
- <div>Loading workers...</div>
102
- </div>
103
-
104
- <div
105
- id="error"
106
- style="display: none; text-align: center; padding: 40px; color: var(--danger)"
107
- >
108
- <div>Failed to load workers data</div>
109
- <button class="btn" onclick="fetchData()" style="margin-top: 16px">Retry</button>
110
- </div>
111
-
112
- <div id="workers-content" style="display: none">
113
- <div class="summary-bar" id="queue-summary">
114
- <div class="summary-item">
115
- <span class="summary-label">Queue Driver</span>
116
- <span class="summary-value" id="queue-driver">-</span>
117
- </div>
118
- <div class="summary-item">
119
- <span class="summary-label">Queues</span>
120
- <span class="summary-value" id="queue-total">0</span>
121
- </div>
122
- <div class="summary-item">
123
- <span class="summary-label">Jobs</span>
124
- <span class="summary-value" id="queue-jobs">0</span>
125
- </div>
126
- <div class="summary-item">
127
- <span class="summary-label">Processing</span>
128
- <span class="summary-value" id="queue-processing">0</span>
129
- </div>
130
- <div class="summary-item">
131
- <span class="summary-label">Failed</span>
132
- <span class="summary-value" id="queue-failed">0</span>
133
- </div>
134
- <div class="summary-item">
135
- <span class="summary-label">Drivers</span>
136
- <div class="drivers-list" id="drivers-list"></div>
137
- </div>
138
- </div>
139
- <div class="table-container">
140
- <div class="table-wrapper">
141
- <table>
142
- <thead>
143
- <tr>
144
- <th style="width: 250px">Worker</th>
145
- <th style="width: 120px">Status</th>
146
- <th style="width: 120px">Health</th>
147
- <th style="width: 100px">Driver</th>
148
- <th style="width: 100px">Version</th>
149
- <th style="width: 320px">Performance</th>
150
- <th style="width: 180px">Actions</th>
151
- </tr>
152
- </thead>
153
- <tbody id="workers-tbody">
154
- <!-- Workers will be populated here -->
155
- </tbody>
156
- </table>
157
- </div>
158
-
159
- <div class="pagination">
160
- <div class="pagination-info" id="pagination-info">Showing 0-0 of 0 workers</div>
161
- <div class="pagination-controls">
162
- <button class="page-btn" id="prev-btn" onclick="loadPage('prev')" disabled>
163
- <svg
164
- viewBox="0 0 24 24"
165
- fill="none"
166
- stroke="currentColor"
167
- stroke-linecap="round"
168
- stroke-linejoin="round"
169
- >
170
- <polyline points="15 18 9 12 15 6"></polyline>
171
- </svg>
172
- </button>
173
- <div id="page-numbers" style="display: flex; gap: 8px"></div>
174
- <button class="page-btn" id="next-btn" onclick="loadPage('next')" disabled>
175
- <svg
176
- viewBox="0 0 24 24"
177
- fill="none"
178
- stroke="currentColor"
179
- stroke-linecap="round"
180
- stroke-linejoin="round"
181
- >
182
- <polyline points="9 18 15 12 9 6"></polyline>
183
- </svg>
184
- </button>
185
-
186
- <div class="page-size-selector">
187
- <span>Show:</span>
188
- <select id="limit-select" onchange="changeLimit(this.value)">
189
- <option value="10">10</option>
190
- <option value="25">25</option>
191
- <option value="50">50</option>
192
- <option value="100">100</option>
193
- </select>
194
- </div>
195
- </div>
196
- </div>
197
- </div>
198
- </div>
199
- </div>
200
- <script src="workers/main.js"></script>
201
- </body>
202
- </html>