usePerformanceMetrics.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import type { NginxPerformanceInfo } from '@/api/ngx'
  2. export function usePerformanceMetrics(nginxInfo: Ref<NginxPerformanceInfo | undefined>) {
  3. // Format numbers to a more readable form
  4. function formatNumber(num: number): string {
  5. if (num >= 1000000) {
  6. return `${(num / 1000000).toFixed(2)}M`
  7. }
  8. else if (num >= 1000) {
  9. return `${(num / 1000).toFixed(2)}K`
  10. }
  11. return num.toString()
  12. }
  13. // Active connections percentage
  14. const activeConnectionsPercent = computed(() => {
  15. if (!nginxInfo.value) {
  16. return 0
  17. }
  18. const maxConnections = nginxInfo.value.worker_connections * nginxInfo.value.worker_processes
  19. return Number(((nginxInfo.value.active / maxConnections) * 100).toFixed(2))
  20. })
  21. // Worker processes usage percentage
  22. const workerProcessesPercent = computed(() => {
  23. if (!nginxInfo.value) {
  24. return 0
  25. }
  26. return Number(((nginxInfo.value.workers / nginxInfo.value.worker_processes) * 100).toFixed(2))
  27. })
  28. // Requests per connection
  29. const requestsPerConnection = computed(() => {
  30. if (!nginxInfo.value || nginxInfo.value.handled === 0) {
  31. return 0
  32. }
  33. return (nginxInfo.value.requests / nginxInfo.value.handled).toFixed(2)
  34. })
  35. // Maximum requests per second
  36. const maxRPS = computed(() => {
  37. if (!nginxInfo.value) {
  38. return 0
  39. }
  40. return nginxInfo.value.worker_processes * nginxInfo.value.worker_connections
  41. })
  42. // Process composition data
  43. const processTypeData = computed(() => {
  44. if (!nginxInfo.value) {
  45. return []
  46. }
  47. return [
  48. { type: $gettext('Worker Processes'), value: nginxInfo.value.workers, color: '#1890ff' },
  49. { type: $gettext('Master Process'), value: nginxInfo.value.master, color: '#52c41a' },
  50. { type: $gettext('Cache Processes'), value: nginxInfo.value.cache, color: '#faad14' },
  51. { type: $gettext('Other Processes'), value: nginxInfo.value.other, color: '#f5222d' },
  52. ]
  53. })
  54. // Resource utilization
  55. const resourceUtilization = computed(() => {
  56. if (!nginxInfo.value) {
  57. return 0
  58. }
  59. const cpuFactor = Math.min(nginxInfo.value.cpu_usage / 100, 1)
  60. const maxConnections = nginxInfo.value.worker_connections * nginxInfo.value.worker_processes
  61. const connectionFactor = Math.min(nginxInfo.value.active / maxConnections, 1)
  62. return Math.round((cpuFactor * 0.5 + connectionFactor * 0.5) * 100)
  63. })
  64. // Table data
  65. const statusData = computed(() => {
  66. if (!nginxInfo.value) {
  67. return []
  68. }
  69. return [
  70. {
  71. key: '1',
  72. name: $gettext('Active connections'),
  73. value: formatNumber(nginxInfo.value.active),
  74. },
  75. {
  76. key: '2',
  77. name: $gettext('Total handshakes'),
  78. value: formatNumber(nginxInfo.value.accepts),
  79. },
  80. {
  81. key: '3',
  82. name: $gettext('Total connections'),
  83. value: formatNumber(nginxInfo.value.handled),
  84. },
  85. {
  86. key: '4',
  87. name: $gettext('Total requests'),
  88. value: formatNumber(nginxInfo.value.requests),
  89. },
  90. {
  91. key: '5',
  92. name: $gettext('Read requests'),
  93. value: nginxInfo.value.reading,
  94. },
  95. {
  96. key: '6',
  97. name: $gettext('Responses'),
  98. value: nginxInfo.value.writing,
  99. },
  100. {
  101. key: '7',
  102. name: $gettext('Waiting processes'),
  103. value: nginxInfo.value.waiting,
  104. },
  105. ]
  106. })
  107. // Worker processes data
  108. const workerData = computed(() => {
  109. if (!nginxInfo.value) {
  110. return []
  111. }
  112. return [
  113. {
  114. key: '1',
  115. name: $gettext('Number of worker processes'),
  116. value: nginxInfo.value.workers,
  117. },
  118. {
  119. key: '2',
  120. name: $gettext('Master process'),
  121. value: nginxInfo.value.master,
  122. },
  123. {
  124. key: '3',
  125. name: $gettext('Cache manager processes'),
  126. value: nginxInfo.value.cache,
  127. },
  128. {
  129. key: '4',
  130. name: $gettext('Other Nginx processes'),
  131. value: nginxInfo.value.other,
  132. },
  133. {
  134. key: '5',
  135. name: $gettext('Nginx CPU usage rate'),
  136. value: `${nginxInfo.value.cpu_usage.toFixed(2)}%`,
  137. },
  138. {
  139. key: '6',
  140. name: $gettext('Nginx Memory usage'),
  141. value: `${nginxInfo.value.memory_usage.toFixed(2)} MB`,
  142. },
  143. ]
  144. })
  145. // Configuration data
  146. const configData = computed(() => {
  147. if (!nginxInfo.value) {
  148. return []
  149. }
  150. return [
  151. {
  152. key: '1',
  153. name: $gettext('Number of worker processes'),
  154. value: nginxInfo.value.worker_processes,
  155. },
  156. {
  157. key: '2',
  158. name: $gettext('Maximum number of connections per worker process'),
  159. value: nginxInfo.value.worker_connections,
  160. },
  161. ]
  162. })
  163. return {
  164. formatNumber,
  165. activeConnectionsPercent,
  166. workerProcessesPercent,
  167. requestsPerConnection,
  168. maxRPS,
  169. processTypeData,
  170. resourceUtilization,
  171. statusData,
  172. workerData,
  173. configData,
  174. }
  175. }