status.ts 3.3 KB

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