status.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { NumberFormatter } from '@/utils';
  2. export class CurTotal {
  3. current: number;
  4. total: number;
  5. constructor(current: number, total: number) {
  6. this.current = current;
  7. this.total = total;
  8. }
  9. get percent(): number {
  10. if (this.total === 0) return 0;
  11. return NumberFormatter.toFixed((this.current / this.total) * 100, 2);
  12. }
  13. get color(): string {
  14. const p = this.percent;
  15. if (p < 80) return '#1677ff';
  16. if (p < 90) return '#faad14';
  17. return '#ff4d4f';
  18. }
  19. }
  20. const XRAY_STATE_COLORS: Record<string, string> = {
  21. running: 'green',
  22. stop: 'orange',
  23. error: 'red',
  24. };
  25. export interface NetIO {
  26. up: number;
  27. down: number;
  28. }
  29. export interface NetTraffic {
  30. sent: number;
  31. recv: number;
  32. }
  33. export interface PublicIP {
  34. ipv4: string | number;
  35. ipv6: string | number;
  36. }
  37. export interface AppStats {
  38. threads: number;
  39. mem: number;
  40. uptime: number;
  41. }
  42. export interface XrayInfo {
  43. state: 'running' | 'stop' | 'error' | string;
  44. errorMsg: string;
  45. version: string;
  46. color: string;
  47. }
  48. interface StatusInput {
  49. cpu?: number;
  50. cpuCores?: number;
  51. logicalPro?: number;
  52. cpuSpeedMhz?: number;
  53. disk?: { current?: number; total?: number };
  54. loads?: number[];
  55. mem?: { current?: number; total?: number };
  56. netIO?: NetIO;
  57. netTraffic?: NetTraffic;
  58. publicIP?: PublicIP;
  59. swap?: { current?: number; total?: number };
  60. tcpCount?: number;
  61. udpCount?: number;
  62. uptime?: number;
  63. appUptime?: number;
  64. appStats?: AppStats;
  65. xray?: Partial<XrayInfo>;
  66. }
  67. export class Status {
  68. cpu: CurTotal = new CurTotal(0, 0);
  69. cpuCores = 0;
  70. logicalPro = 0;
  71. cpuSpeedMhz = 0;
  72. disk: CurTotal = new CurTotal(0, 0);
  73. loads: number[] = [0, 0, 0];
  74. mem: CurTotal = new CurTotal(0, 0);
  75. netIO: NetIO = { up: 0, down: 0 };
  76. netTraffic: NetTraffic = { sent: 0, recv: 0 };
  77. publicIP: PublicIP = { ipv4: 0, ipv6: 0 };
  78. swap: CurTotal = new CurTotal(0, 0);
  79. tcpCount = 0;
  80. udpCount = 0;
  81. uptime = 0;
  82. appUptime = 0;
  83. appStats: AppStats = { threads: 0, mem: 0, uptime: 0 };
  84. xray: XrayInfo = { state: 'stop', errorMsg: '', version: '', color: '' };
  85. constructor(data?: StatusInput | null) {
  86. if (data == null) return;
  87. this.cpu = new CurTotal(data.cpu ?? 0, 100);
  88. this.cpuCores = data.cpuCores ?? 0;
  89. this.logicalPro = data.logicalPro ?? 0;
  90. this.cpuSpeedMhz = data.cpuSpeedMhz ?? 0;
  91. this.disk = new CurTotal(data.disk?.current ?? 0, data.disk?.total ?? 0);
  92. this.loads = (data.loads || [0, 0, 0]).map((v) => NumberFormatter.toFixed(v, 2));
  93. this.mem = new CurTotal(data.mem?.current ?? 0, data.mem?.total ?? 0);
  94. this.netIO = data.netIO ?? this.netIO;
  95. this.netTraffic = data.netTraffic ?? this.netTraffic;
  96. this.publicIP = data.publicIP ?? this.publicIP;
  97. this.swap = new CurTotal(data.swap?.current ?? 0, data.swap?.total ?? 0);
  98. this.tcpCount = data.tcpCount ?? 0;
  99. this.udpCount = data.udpCount ?? 0;
  100. this.uptime = data.uptime ?? 0;
  101. this.appUptime = data.appUptime ?? 0;
  102. this.appStats = data.appStats ?? this.appStats;
  103. this.xray = { ...this.xray, ...(data.xray || {}) };
  104. this.xray.color = XRAY_STATE_COLORS[this.xray.state] ?? 'gray';
  105. }
  106. }