usePerformanceMetrics.ts 4.9 KB

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