page1.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. let cpuChart = null;
  2. let memChart = null;
  3. let diskChart = null;
  4. let sysStatusTimer = null;
  5. function getSystemInfo(){
  6. fetch("/api/system/info")
  7. .then(r=>{
  8. if(!r.ok) throw new Error("HTTP "+r.status);
  9. return r.json();
  10. })
  11. .then(d=>{
  12. cpuChart.setOption({
  13. graphic:[{
  14. type:"text",
  15. left:"center",
  16. top:"75%",
  17. style:{
  18. text:`${d.cpu_cores||"--"}核 ${d.cpu_freq||"--"}MHz`,
  19. fill:"#aaa",
  20. fontSize:15
  21. }
  22. }],
  23. series:[{data:[{value:d.cpu_usage}]}]
  24. });
  25. memChart.setOption({
  26. graphic:[{
  27. type:"text",
  28. left:"center",
  29. top:"75%",
  30. style:{
  31. text:`${d.mem_used||"--"} / ${d.mem_total||"--"}`,
  32. fill:"#aaa",
  33. fontSize:15
  34. }
  35. }],
  36. series:[{data:[{value:d.mem_percent}]}]
  37. });
  38. diskChart.setOption({
  39. title:{
  40. text:`${d.disk_percent}%`,
  41. subtext:`${d.disk_used||"--"} / ${d.disk_total||"--"}`,
  42. left:"center",
  43. top:"38%",
  44. textStyle:{
  45. fontSize:38,
  46. fontWeight:"bold"
  47. },
  48. subtextStyle:{
  49. fontSize:15
  50. }
  51. },
  52. series:[{
  53. data:[
  54. {value:d.disk_percent,name:"已使用"},
  55. {value:100-d.disk_percent,name:"剩余"}
  56. ]
  57. }]
  58. });
  59. })
  60. .catch(e=>console.error("system info failed:",e));
  61. }
  62. function gaugeOption(){
  63. return {
  64. series:[{
  65. type:"gauge",
  66. radius:"95%",
  67. center:["50%","55%"],
  68. min:0,
  69. max:100,
  70. startAngle:210,
  71. endAngle:-30,
  72. axisLine:{
  73. lineStyle:{
  74. width:22
  75. }
  76. },
  77. progress:{
  78. show:true,
  79. width:22
  80. },
  81. pointer:{show:false},
  82. axisTick:{show:false},
  83. splitLine:{show:false},
  84. axisLabel:{show:false},
  85. detail:{
  86. fontSize:42,
  87. fontWeight:"bold",
  88. offsetCenter:[0,"0%"],
  89. formatter:"{value}%"
  90. },
  91. title:{show:false},
  92. data:[{value:0}]
  93. }]
  94. };
  95. }
  96. function startSysStatusRefresh(){
  97. if(sysStatusTimer) return;
  98. getSystemInfo();
  99. sysStatusTimer = setInterval(() => {
  100. getSystemInfo();
  101. }, 3000);
  102. }
  103. function stopSysStatusRefresh(){
  104. if(sysStatusTimer){
  105. clearInterval(sysStatusTimer);
  106. sysStatusTimer=null;
  107. }
  108. }
  109. function initPage1(){
  110. cpuChart=echarts.init(document.getElementById("cpu-chart"));
  111. memChart=echarts.init(document.getElementById("mem-chart"));
  112. diskChart=echarts.init(document.getElementById("disk-chart"));
  113. cpuChart.setOption(gaugeOption());
  114. memChart.setOption(gaugeOption());
  115. diskChart.setOption({
  116. title:{
  117. text:"0%",
  118. subtext:"-- / --",
  119. left:"center",
  120. top:"38%",
  121. textStyle:{
  122. fontSize:38,
  123. fontWeight:"bold"
  124. },
  125. subtextStyle:{
  126. fontSize:15
  127. }
  128. },
  129. series:[{
  130. type:"pie",
  131. radius:["65%","85%"],
  132. label:{show:false},
  133. data:[
  134. {value:0,name:"已使用"},
  135. {value:100,name:"剩余"}
  136. ]
  137. }]
  138. });
  139. startSysStatusRefresh();
  140. setTimeout(()=>{
  141. cpuChart.resize();
  142. memChart.resize();
  143. diskChart.resize();
  144. },500);
  145. }
  146. function exitPage1(){
  147. stopSysStatusRefresh();
  148. cpuChart?.dispose();
  149. memChart?.dispose();
  150. diskChart?.dispose();
  151. cpuChart=null;
  152. memChart=null;
  153. diskChart=null;
  154. }