|
| 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