Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit e98bbdc

Browse files
committed
Feat: add VisualDataSubsystem
1 parent 659cf3b commit e98bbdc

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import InstanceSubsystem from "./service/system_instance";
5151
import { initDependent } from "./service/install";
5252
import "./service/async_task_service";
5353
import "./service/async_task_service/quick_install";
54+
import "./service/system_visual_data";
5455

5556
// Initialize HTTP service
5657
const koaApp = koa.initKoa();

src/routers/info_router.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import i18next from "i18next";
1212
import logger from "../service/log";
1313
import fs from "fs-extra";
1414
import { LOCAL_PRESET_LANG_PATH } from "../const";
15+
import VisualDataSubsystem from "../service/system_visual_data";
1516

1617
// Get the basic information of the daemon system
1718
routerApp.on("info/overview", async (ctx) => {
@@ -33,7 +34,8 @@ routerApp.on("info/overview", async (ctx) => {
3334
running,
3435
total
3536
},
36-
system: systemInfo()
37+
system: systemInfo(),
38+
cpuMemChart: VisualDataSubsystem.getSystemChartArray()
3739
};
3840
protocol.response(ctx, info);
3941
});

src/service/system_visual_data.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (C) 2023 MCSManager <[email protected]>
2+
3+
import { systemInfo } from "../common/system_info";
4+
5+
// Visual data subsystem: responsible for collecting system data and event data, and providing some methods to display them
6+
class LineQueue<T> {
7+
private readonly arr = new Array<T>();
8+
9+
constructor(public readonly maxSize: number, defaultValue?: T) {
10+
for (let index = 0; index < maxSize; index++) {
11+
this.arr.push(defaultValue);
12+
}
13+
}
14+
15+
push(data: T) {
16+
if (this.arr.length < this.maxSize) {
17+
this.arr.push(data);
18+
} else {
19+
this.arr.shift();
20+
this.arr.push(data);
21+
}
22+
}
23+
24+
getArray() {
25+
return this.arr;
26+
}
27+
}
28+
29+
interface ISystemChartInfo {
30+
cpu: number;
31+
mem: number;
32+
}
33+
34+
class VisualDataSubsystem {
35+
public readonly countMap: Map<string, number> = new Map<string, number>();
36+
37+
public readonly systemChart = new LineQueue<ISystemChartInfo>(60, { cpu: 0, mem: 0 });
38+
39+
private requestCount = 0;
40+
41+
constructor() {
42+
setInterval(() => {
43+
const info = systemInfo();
44+
if (info) {
45+
this.systemChart.push({
46+
cpu: Number((info.cpuUsage * 100).toFixed(0)),
47+
mem: Number((info.memUsage * 100).toFixed(0))
48+
});
49+
} else {
50+
this.systemChart.push({
51+
cpu: 0,
52+
mem: 0
53+
});
54+
}
55+
}, 1000 * 10);
56+
}
57+
58+
addRequestCount() {
59+
this.requestCount++;
60+
}
61+
62+
getSystemChartArray() {
63+
return this.systemChart.getArray();
64+
}
65+
66+
// Trigger counting event
67+
emitCountEvent(eventName: string) {
68+
const v = this.countMap.get(eventName);
69+
if (v) {
70+
this.countMap.set(eventName, v + 1);
71+
} else {
72+
this.countMap.set(eventName, 1);
73+
}
74+
}
75+
76+
// Trigger counting event
77+
eventCount(eventName: string) {
78+
return this.countMap.get(eventName);
79+
}
80+
81+
// Trigger log event
82+
emitLogEvent(eventName: string, source: any) {
83+
const time = new Date().toLocaleString();
84+
}
85+
}
86+
87+
export default new VisualDataSubsystem();

0 commit comments

Comments
 (0)