@zucker-framework/dashboard 1.0.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.
package/dist/index.mjs ADDED
@@ -0,0 +1,379 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/dashboard.ts
5
+ import { Injectable } from "@nestjs/common";
6
+ function _ts_decorate(decorators, target, key, desc) {
7
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
8
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
9
+ r = Reflect.decorate(decorators, target, key, desc);
10
+ } else {
11
+ for (var i = decorators.length - 1; i >= 0; i--) {
12
+ if (d = decorators[i]) {
13
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
14
+ }
15
+ }
16
+ }
17
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
18
+ }
19
+ __name(_ts_decorate, "_ts_decorate");
20
+ var StaticMeasurement = class {
21
+ static {
22
+ __name(this, "StaticMeasurement");
23
+ }
24
+ definition;
25
+ dimensions = /* @__PURE__ */ new Map();
26
+ constructor(definition) {
27
+ this.definition = definition;
28
+ }
29
+ addDimension(dimension) {
30
+ this.dimensions.set(dimension.definition.id, dimension);
31
+ return this;
32
+ }
33
+ getDimensions() {
34
+ return Array.from(this.dimensions.values());
35
+ }
36
+ getDimension(id) {
37
+ return this.dimensions.get(id);
38
+ }
39
+ };
40
+ var CompositeDashboardObject = class CompositeDashboardObject2 {
41
+ static {
42
+ __name(this, "CompositeDashboardObject");
43
+ }
44
+ definition;
45
+ providers = [];
46
+ addProvider(provider) {
47
+ if (!this.definition) {
48
+ this.definition = provider.getObjectDefinition();
49
+ }
50
+ this.providers.push(provider);
51
+ }
52
+ getMeasurements() {
53
+ return this.providers.flatMap((p) => p.getMeasurements());
54
+ }
55
+ getMeasurement(id) {
56
+ for (const provider of this.providers) {
57
+ const m = provider.getMeasurement(id);
58
+ if (m) return m;
59
+ }
60
+ return void 0;
61
+ }
62
+ };
63
+ var CompositeDashboard = class CompositeDashboard2 {
64
+ static {
65
+ __name(this, "CompositeDashboard");
66
+ }
67
+ definition;
68
+ objects = /* @__PURE__ */ new Map();
69
+ subDashboards = [];
70
+ constructor(definition) {
71
+ this.definition = definition;
72
+ }
73
+ addProvider(provider) {
74
+ const objId = provider.getObjectDefinition().id;
75
+ let obj = this.objects.get(objId);
76
+ if (!obj) {
77
+ obj = new CompositeDashboardObject();
78
+ this.objects.set(objId, obj);
79
+ }
80
+ obj.addProvider(provider);
81
+ }
82
+ addDashboard(dashboard) {
83
+ this.subDashboards.push(dashboard);
84
+ }
85
+ getObjects() {
86
+ const objs = Array.from(this.objects.values());
87
+ for (const sub of this.subDashboards) {
88
+ objs.push(...sub.getObjects());
89
+ }
90
+ return objs;
91
+ }
92
+ getObject(id) {
93
+ const obj = this.objects.get(id);
94
+ if (obj) return obj;
95
+ for (const sub of this.subDashboards) {
96
+ const found = sub.getObject(id);
97
+ if (found) return found;
98
+ }
99
+ return void 0;
100
+ }
101
+ };
102
+ var DashboardManager = class {
103
+ static {
104
+ __name(this, "DashboardManager");
105
+ }
106
+ dashboards = /* @__PURE__ */ new Map();
107
+ /**
108
+ * Register a MeasurementProvider.
109
+ * Automatically creates/composites the dashboard and object hierarchy.
110
+ */
111
+ registerProvider(provider) {
112
+ const def = provider.getDashboardDefinition();
113
+ let dashboard = this.dashboards.get(def.id);
114
+ if (!dashboard) {
115
+ dashboard = new CompositeDashboard(def);
116
+ this.dashboards.set(def.id, dashboard);
117
+ }
118
+ dashboard.addProvider(provider);
119
+ }
120
+ /**
121
+ * Register a full Dashboard (will be merged into composite).
122
+ */
123
+ registerDashboard(dashboard) {
124
+ const def = dashboard.definition;
125
+ let composite = this.dashboards.get(def.id);
126
+ if (!composite) {
127
+ composite = new CompositeDashboard(def);
128
+ this.dashboards.set(def.id, composite);
129
+ }
130
+ composite.addDashboard(dashboard);
131
+ }
132
+ /**
133
+ * Register a simple dashboard definition (backward compat).
134
+ */
135
+ register(definition) {
136
+ if (!this.dashboards.has(definition.id)) {
137
+ this.dashboards.set(definition.id, new CompositeDashboard(definition));
138
+ }
139
+ }
140
+ unregister(id) {
141
+ this.dashboards.delete(id);
142
+ }
143
+ getDashboard(id) {
144
+ return this.dashboards.get(id);
145
+ }
146
+ getDashboards() {
147
+ return Array.from(this.dashboards.values());
148
+ }
149
+ /**
150
+ * Get all registered dashboard definitions.
151
+ */
152
+ getDefinitions() {
153
+ return Array.from(this.dashboards.values()).map((d) => d.definition);
154
+ }
155
+ };
156
+ DashboardManager = _ts_decorate([
157
+ Injectable()
158
+ ], DashboardManager);
159
+
160
+ // src/measurement.ts
161
+ var MeasurementParameter = class _MeasurementParameter {
162
+ static {
163
+ __name(this, "MeasurementParameter");
164
+ }
165
+ params;
166
+ constructor(params = {}) {
167
+ this.params = {
168
+ ...params
169
+ };
170
+ }
171
+ static of(params) {
172
+ return new _MeasurementParameter(params);
173
+ }
174
+ get(name) {
175
+ return this.params[name];
176
+ }
177
+ getString(name, defaultValue = "") {
178
+ const val = this.params[name];
179
+ return val !== void 0 && val !== null ? String(val) : defaultValue;
180
+ }
181
+ getNumber(name, defaultValue = 0) {
182
+ const val = this.params[name];
183
+ if (typeof val === "number") return val;
184
+ if (typeof val === "string") {
185
+ const n = Number(val);
186
+ return isNaN(n) ? defaultValue : n;
187
+ }
188
+ return defaultValue;
189
+ }
190
+ values() {
191
+ return {
192
+ ...this.params
193
+ };
194
+ }
195
+ };
196
+ var SimpleMeasurementValue = class _SimpleMeasurementValue {
197
+ static {
198
+ __name(this, "SimpleMeasurementValue");
199
+ }
200
+ value;
201
+ timeString;
202
+ timestamp;
203
+ tags;
204
+ constructor(value, timestamp, tags) {
205
+ this.value = value;
206
+ this.timestamp = timestamp;
207
+ this.timeString = _SimpleMeasurementValue.formatTime(timestamp);
208
+ this.tags = tags;
209
+ }
210
+ static of(value, timestamp) {
211
+ const ts = timestamp instanceof Date ? timestamp : typeof timestamp === "number" ? new Date(timestamp) : /* @__PURE__ */ new Date();
212
+ return new _SimpleMeasurementValue(value, ts);
213
+ }
214
+ static formatTime(date) {
215
+ const pad = /* @__PURE__ */ __name((n) => String(n).padStart(2, "0"), "pad");
216
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
217
+ }
218
+ /**
219
+ * Compare by timestamp ascending.
220
+ */
221
+ static compareAsc(a, b) {
222
+ const ta = a.timestamp?.getTime() ?? 0;
223
+ const tb = b.timestamp?.getTime() ?? 0;
224
+ return ta - tb;
225
+ }
226
+ /**
227
+ * Compare by timestamp descending (newest first, default sort).
228
+ */
229
+ static compareDesc(a, b) {
230
+ const ta = a.timestamp?.getTime() ?? 0;
231
+ const tb = b.timestamp?.getTime() ?? 0;
232
+ return tb - ta;
233
+ }
234
+ };
235
+
236
+ // src/system-monitor.ts
237
+ import * as os from "os";
238
+ var SystemMonitorProvider = class {
239
+ static {
240
+ __name(this, "SystemMonitorProvider");
241
+ }
242
+ measurements;
243
+ constructor() {
244
+ this.measurements = /* @__PURE__ */ new Map();
245
+ this.measurements.set("cpu", this.createCpuMeasurement());
246
+ this.measurements.set("memory", this.createMemoryMeasurement());
247
+ this.measurements.set("process", this.createProcessMeasurement());
248
+ }
249
+ getDashboardDefinition() {
250
+ return {
251
+ id: "system",
252
+ name: "System Monitor"
253
+ };
254
+ }
255
+ getObjectDefinition() {
256
+ return {
257
+ id: "stats",
258
+ name: "System Statistics"
259
+ };
260
+ }
261
+ getMeasurements() {
262
+ return Array.from(this.measurements.values());
263
+ }
264
+ getMeasurement(id) {
265
+ return this.measurements.get(id);
266
+ }
267
+ createCpuMeasurement() {
268
+ return new StaticMeasurement({
269
+ id: "cpu",
270
+ name: "CPU",
271
+ dimensions: [
272
+ {
273
+ id: "usage",
274
+ name: "CPU Usage"
275
+ },
276
+ {
277
+ id: "count",
278
+ name: "CPU Count"
279
+ }
280
+ ]
281
+ }).addDimension(this.createDimension("usage", "CPU Usage", async () => {
282
+ const cpus2 = os.cpus();
283
+ let totalIdle = 0;
284
+ let totalTick = 0;
285
+ for (const cpu of cpus2) {
286
+ totalIdle += cpu.times.idle;
287
+ totalTick += cpu.times.user + cpu.times.nice + cpu.times.sys + cpu.times.irq + cpu.times.idle;
288
+ }
289
+ return SimpleMeasurementValue.of(totalTick > 0 ? 1 - totalIdle / totalTick : 0);
290
+ })).addDimension(this.createDimension("count", "CPU Count", async () => {
291
+ return SimpleMeasurementValue.of(os.cpus().length);
292
+ }));
293
+ }
294
+ createMemoryMeasurement() {
295
+ return new StaticMeasurement({
296
+ id: "memory",
297
+ name: "Memory",
298
+ dimensions: [
299
+ {
300
+ id: "total",
301
+ name: "Total Memory"
302
+ },
303
+ {
304
+ id: "used",
305
+ name: "Used Memory"
306
+ },
307
+ {
308
+ id: "free",
309
+ name: "Free Memory"
310
+ },
311
+ {
312
+ id: "usageRate",
313
+ name: "Memory Usage Rate"
314
+ }
315
+ ]
316
+ }).addDimension(this.createDimension("total", "Total Memory", async () => {
317
+ return SimpleMeasurementValue.of(os.totalmem());
318
+ })).addDimension(this.createDimension("used", "Used Memory", async () => {
319
+ return SimpleMeasurementValue.of(os.totalmem() - os.freemem());
320
+ })).addDimension(this.createDimension("free", "Free Memory", async () => {
321
+ return SimpleMeasurementValue.of(os.freemem());
322
+ })).addDimension(this.createDimension("usageRate", "Memory Usage Rate", async () => {
323
+ const total = os.totalmem();
324
+ const free = os.freemem();
325
+ return SimpleMeasurementValue.of(total > 0 ? (total - free) / total : 0);
326
+ }));
327
+ }
328
+ createProcessMeasurement() {
329
+ return new StaticMeasurement({
330
+ id: "process",
331
+ name: "Process",
332
+ dimensions: [
333
+ {
334
+ id: "uptime",
335
+ name: "Uptime"
336
+ },
337
+ {
338
+ id: "rss",
339
+ name: "RSS Memory"
340
+ },
341
+ {
342
+ id: "heapUsed",
343
+ name: "Heap Used"
344
+ },
345
+ {
346
+ id: "heapTotal",
347
+ name: "Heap Total"
348
+ }
349
+ ]
350
+ }).addDimension(this.createDimension("uptime", "Uptime", async () => {
351
+ return SimpleMeasurementValue.of(process.uptime());
352
+ })).addDimension(this.createDimension("rss", "RSS Memory", async () => {
353
+ return SimpleMeasurementValue.of(process.memoryUsage().rss);
354
+ })).addDimension(this.createDimension("heapUsed", "Heap Used", async () => {
355
+ return SimpleMeasurementValue.of(process.memoryUsage().heapUsed);
356
+ })).addDimension(this.createDimension("heapTotal", "Heap Total", async () => {
357
+ return SimpleMeasurementValue.of(process.memoryUsage().heapTotal);
358
+ }));
359
+ }
360
+ createDimension(id, name, getter) {
361
+ return {
362
+ definition: {
363
+ id,
364
+ name
365
+ },
366
+ async getValue(params) {
367
+ const p = params instanceof MeasurementParameter ? params : new MeasurementParameter(params);
368
+ return getter(p);
369
+ }
370
+ };
371
+ }
372
+ };
373
+ export {
374
+ DashboardManager,
375
+ MeasurementParameter,
376
+ SimpleMeasurementValue,
377
+ StaticMeasurement,
378
+ SystemMonitorProvider
379
+ };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@zucker-framework/dashboard",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "module": "dist/index.mjs",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "LICENSE",
10
+ "NOTICE",
11
+ "THIRD_PARTY_NOTICES"
12
+ ],
13
+ "dependencies": {
14
+ "@zucker-framework/core": "1.0.0"
15
+ },
16
+ "devDependencies": {
17
+ "@types/node": "^20.19.43"
18
+ },
19
+ "peerDependencies": {
20
+ "@nestjs/common": "^12.0.0",
21
+ "@nestjs/core": "^12.0.0"
22
+ },
23
+ "license": "Apache-2.0",
24
+ "author": "Lind",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/Lind-pro/zucker-framework.git",
28
+ "directory": "packages/dashboard"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public",
32
+ "registry": "https://registry.npmjs.org/"
33
+ },
34
+ "scripts": {
35
+ "build": "tsup src/index.ts --format cjs,esm --dts",
36
+ "clean": "rm -rf dist",
37
+ "lint": "eslint src/"
38
+ }
39
+ }