Prechádzať zdrojové kódy

Merge pull request #953 from akinoccc/dev

feat: add field of nginx worker processes mode(auto or manual)
Akino 3 týždňov pred
rodič
commit
b9e825971d

+ 5 - 0
app/src/api/ngx.ts

@@ -51,6 +51,7 @@ export interface NginxPerformanceInfo {
   memory_usage: number // 内存使用率(MB)
   worker_processes: number // worker_processes 配置
   worker_connections: number // worker_connections 配置
+  process_mode: string // worker进程配置模式:'auto'或'manual'
 }
 
 const ngx = {
@@ -74,6 +75,10 @@ const ngx = {
     return http.get('/nginx/detail_status')
   },
 
+  toggle_stub_status(enable: boolean): Promise<{ stub_status_enabled: boolean, error: string }> {
+    return http.post('/nginx/stub_status', { enable })
+  },
+
   reload() {
     return http.post('/nginx/reload')
   },

+ 1 - 2
app/src/composables/useNginxPerformance.ts

@@ -1,6 +1,5 @@
 import type { NginxPerformanceInfo } from '@/api/ngx'
 import ngx from '@/api/ngx'
-import { fromNow } from '@/lib/helper'
 
 export function useNginxPerformance() {
   const loading = ref(false)
@@ -17,7 +16,7 @@ export function useNginxPerformance() {
   const formattedUpdateTime = computed(() => {
     if (!lastUpdateTime.value)
       return $gettext('Unknown')
-    return fromNow(lastUpdateTime.value.toLocaleString())
+    return lastUpdateTime.value.toLocaleString()
   })
 
   // Update the last update time

+ 6 - 9
app/src/views/dashboard/NginxDashBoard.vue

@@ -1,12 +1,11 @@
 <script setup lang="ts">
+import ngx from '@/api/ngx'
 import { useNginxPerformance } from '@/composables/useNginxPerformance'
 import { useSSE } from '@/composables/useSSE'
 import { NginxStatus } from '@/constants'
 import { useUserStore } from '@/pinia'
 import { useGlobalStore } from '@/pinia/moudule/global'
 import { ClockCircleOutlined, ReloadOutlined } from '@ant-design/icons-vue'
-import axios from 'axios'
-import { storeToRefs } from 'pinia'
 import ConnectionMetricsCard from './components/ConnectionMetricsCard.vue'
 import PerformanceStatisticsCard from './components/PerformanceStatisticsCard.vue'
 import PerformanceTablesCard from './components/PerformanceTablesCard.vue'
@@ -39,16 +38,14 @@ async function toggleStubStatus() {
   try {
     stubStatusLoading.value = true
     stubStatusError.value = ''
-    const response = await axios.post('/api/nginx/stub_status', {
-      enable: !stubStatusEnabled.value,
-    })
+    const response = await ngx.toggle_stub_status(!stubStatusEnabled.value)
 
-    if (response.data.stub_status_enabled !== undefined) {
-      stubStatusEnabled.value = response.data.stub_status_enabled
+    if (response.stub_status_enabled !== undefined) {
+      stubStatusEnabled.value = response.stub_status_enabled
     }
 
-    if (response.data.error) {
-      stubStatusError.value = response.data.error
+    if (response.error) {
+      stubStatusError.value = response.error
     }
     else {
       fetchInitialData().then(connectSSE)

+ 5 - 1
app/src/views/dashboard/components/PerformanceTablesCard.vue

@@ -196,7 +196,11 @@ const maxRPS = computed(() => {
                 {{ $gettext('Maximum worker process number:') }}
                 <strong>{{ nginxInfo.worker_processes }}</strong>
                 <span class="text-gray-500 text-xs ml-2">
-                  {{ nginxInfo.worker_processes === nginxInfo.workers ? $gettext('auto = CPU cores') : $gettext('manually set') }}
+                  {{
+                    nginxInfo.process_mode === 'auto'
+                      ? $gettext('auto = CPU cores')
+                      : $gettext('manually set')
+                  }}
                 </span>
               </p>
               <p class="mb-0">

+ 6 - 2
internal/nginx/config_info.go

@@ -10,8 +10,9 @@ import (
 )
 
 type NginxConfigInfo struct {
-	WorkerProcesses   int `json:"worker_processes"`
-	WorkerConnections int `json:"worker_connections"`
+	WorkerProcesses   int    `json:"worker_processes"`
+	WorkerConnections int    `json:"worker_connections"`
+	ProcessMode       string `json:"process_mode"`
 }
 
 // GetNginxWorkerConfigInfo Get Nginx config info of worker_processes and worker_connections
@@ -19,6 +20,7 @@ func GetNginxWorkerConfigInfo() (*NginxConfigInfo, error) {
 	result := &NginxConfigInfo{
 		WorkerProcesses:   1,
 		WorkerConnections: 1024,
+		ProcessMode:       "manual",
 	}
 
 	// Get worker_processes config
@@ -33,8 +35,10 @@ func GetNginxWorkerConfigInfo() (*NginxConfigInfo, error) {
 	if matches := wpRe.FindStringSubmatch(string(output)); len(matches) > 1 {
 		if matches[1] == "auto" {
 			result.WorkerProcesses = runtime.NumCPU()
+			result.ProcessMode = "auto"
 		} else {
 			result.WorkerProcesses, _ = strconv.Atoi(matches[1])
+			result.ProcessMode = "manual"
 		}
 	}
 

+ 8 - 5
internal/nginx/performance.go

@@ -43,13 +43,16 @@ func GetPerformanceData() NginxPerformanceResponse {
 		logger.Warn("Failed to get Nginx config info:", err)
 	}
 
+	// 确保ProcessMode字段能够正确传递
+	perfInfo := NginxPerformanceInfo{
+		StubStatusData:   *statusInfo,
+		NginxProcessInfo: *processInfo,
+		NginxConfigInfo:  *configInfo,
+	}
+
 	return NginxPerformanceResponse{
 		StubStatusEnabled: stubStatusEnabled,
 		Running:           running,
-		Info: NginxPerformanceInfo{
-			StubStatusData:   *statusInfo,
-			NginxProcessInfo: *processInfo,
-			NginxConfigInfo:  *configInfo,
-		},
+		Info:              perfInfo,
 	}
 }

+ 3 - 1
internal/nginx/stub_status.go

@@ -11,6 +11,7 @@ import (
 	"time"
 
 	"github.com/pkg/errors"
+	"github.com/uozi-tech/cosy/logger"
 )
 
 // StubStatusInfo Store the stub_status module status
@@ -32,7 +33,7 @@ type StubStatusData struct {
 const (
 	StubStatusPort       = 51828
 	StubStatusPath       = "/stub_status"
-	StubStatusHost       = "localhost"
+	StubStatusHost       = "127.0.0.1"
 	StubStatusProtocol   = "http"
 	StubStatusAllow      = "127.0.0.1"
 	StubStatusDeny       = "all"
@@ -53,6 +54,7 @@ func GetStubStatusData() (bool, *StubStatusData, error) {
 
 	// Get the stub_status status information
 	enabled, statusURL := IsStubStatusEnabled()
+	logger.Info("GetStubStatusData", "enabled", enabled, "statusURL", statusURL)
 	if !enabled {
 		return false, result, fmt.Errorf("stub_status is not enabled")
 	}