server total
This commit is contained in:
		@@ -1,7 +1,10 @@
 | 
				
			|||||||
package models
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"main/configs"
 | 
						"main/configs"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -19,6 +22,30 @@ type Server struct {
 | 
				
			|||||||
	UpdatedAt time.Time                `json:"updated_at" gorm:"autoUpdateTime"`
 | 
						UpdatedAt time.Time                `json:"updated_at" gorm:"autoUpdateTime"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (server *Server) CheckStatus() bool {
 | 
				
			||||||
 | 
						resp, err := http.Get(fmt.Sprintf("http://%s:%d/status", server.IP, server.Port))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 解碼json
 | 
				
			||||||
 | 
						var data map[string]interface{}
 | 
				
			||||||
 | 
						if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fmt.Println("data:", data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 檢查服務器是否正常
 | 
				
			||||||
 | 
						if data["status"] != "ok" {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 檢查服務器是否正常
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	configs.ORMDB().AutoMigrate(&Server{})
 | 
						configs.ORMDB().AutoMigrate(&Server{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -30,4 +57,16 @@ func init() {
 | 
				
			|||||||
		Port:   7860,
 | 
							Port:   7860,
 | 
				
			||||||
		Status: "閒置",
 | 
							Status: "閒置",
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 檢查所有服務器的狀態, 無效的服務器設置為異常
 | 
				
			||||||
 | 
						var servers []Server
 | 
				
			||||||
 | 
						configs.ORMDB().Find(&servers)
 | 
				
			||||||
 | 
						for _, server := range servers {
 | 
				
			||||||
 | 
							if server.Status != "異常" {
 | 
				
			||||||
 | 
								if !server.CheckStatus() {
 | 
				
			||||||
 | 
									server.Status = "異常"
 | 
				
			||||||
 | 
									configs.ORMDB().Save(&server)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -138,7 +138,6 @@ func ModelItemGet(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var model = models.Model{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
 | 
						var model = models.Model{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
 | 
				
			||||||
	fmt.Println(model)
 | 
					 | 
				
			||||||
	if err := configs.ORMDB().Take(&model, utils.ParamInt(mux.Vars(r)["id"], 0)).Error; err != nil {
 | 
						if err := configs.ORMDB().Take(&model, utils.ParamInt(mux.Vars(r)["id"], 0)).Error; err != nil {
 | 
				
			||||||
		w.WriteHeader(http.StatusNotFound)
 | 
							w.WriteHeader(http.StatusNotFound)
 | 
				
			||||||
		w.Write([]byte(err.Error()))
 | 
							w.Write([]byte(err.Error()))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,6 +19,9 @@ func ServersGet(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	listview.PageSize = utils.ParamInt(r.URL.Query().Get("pageSize"), 10)
 | 
						listview.PageSize = utils.ParamInt(r.URL.Query().Get("pageSize"), 10)
 | 
				
			||||||
	var server_list []models.Server
 | 
						var server_list []models.Server
 | 
				
			||||||
	db := configs.ORMDB()
 | 
						db := configs.ORMDB()
 | 
				
			||||||
 | 
						// 獲取服務器總數
 | 
				
			||||||
 | 
						db.Model(&models.Server{}).Count(&listview.Total)
 | 
				
			||||||
 | 
						// 獲取服務器列表
 | 
				
			||||||
	db.Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&server_list)
 | 
						db.Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&server_list)
 | 
				
			||||||
	for _, server := range server_list {
 | 
						for _, server := range server_list {
 | 
				
			||||||
		// 驗證服務器狀態
 | 
							// 驗證服務器狀態
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										62
									
								
								test.sh
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								test.sh
									
									
									
									
									
								
							@@ -4,68 +4,82 @@
 | 
				
			|||||||
start_time=$(date +%s)
 | 
					start_time=$(date +%s)
 | 
				
			||||||
rm -f data/sqlite3.db
 | 
					rm -f data/sqlite3.db
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 流程測試, 啓動服務, 設定進程名 go_test
 | 
					# 流程測試, 啓動服務, 設定進程名 go_test, 並將日誌隱藏
 | 
				
			||||||
go run main.go -procname go_test &
 | 
					go run main.go -procname go_test > /dev/null 2>&1 &
 | 
				
			||||||
sleep 2
 | 
					sleep 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 退出服務的函數
 | 
					# 退出服務的函數
 | 
				
			||||||
function exit_service() { pkill -f go_test && rm -f data/sqlite3.db && echo "退出服務: $1" && exit 1; }
 | 
					function exit_service() { pkill -f go_test && rm -f data/sqlite3.db && echo "退出服務: $1" && exit 1; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 驗證結果的函數
 | 
				
			||||||
 | 
					function message() {
 | 
				
			||||||
 | 
					    response=$1
 | 
				
			||||||
 | 
					    info=$2
 | 
				
			||||||
 | 
					    if [[ ${response: -3} -eq 200 ]]; then
 | 
				
			||||||
 | 
					        if [[ $# -eq 3 ]] && [[ $3 == true ]]; then
 | 
				
			||||||
 | 
					            echo "[測試成功] $info: $response"
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					            echo "[測試成功] $info"
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					        pkill -f go_test && rm -f data/sqlite3.db && echo "[$info]測試失敗: $response" && exit 1
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 創建用戶 (POST /api/users)
 | 
					# 創建用戶 (POST /api/users)
 | 
				
			||||||
response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","password":"test","email":""}' -s -w "%{http_code}" http://localhost:8080/api/users)
 | 
					response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","password":"test","email":""}' -s -w "%{http_code}" http://localhost:8080/api/users)
 | 
				
			||||||
[[ ${response: -3} -eq 200 ]] && { echo "創建用戶成功: ${response%???}"; } || exit_service "創建用戶失敗: ${response%???}"
 | 
					message "$response" "創建用戶"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## 獲取用戶列表 (GET /api/users)
 | 
					# 獲取用戶列表 (GET /api/users)
 | 
				
			||||||
#response=$(curl -X GET -H "Content-Type: application/json" -s -w "%{http_code}" http://localhost:8080/api/users)
 | 
					response=$(curl -X GET -H "Content-Type: application/json" -s -w "%{http_code}" http://localhost:8080/api/users)
 | 
				
			||||||
#[[ ${response: -3} -eq 200 ]] && { echo "獲取用戶列表成功: ${response%???}"; } || exit_service "獲取用戶列表失敗: ${response%???}"
 | 
					message "$response" "用戶列表"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 登錄 (POST /api/sessions)
 | 
					# 登錄 (POST /api/sessions)
 | 
				
			||||||
response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","password":"test"}' -s -w "%{http_code}" http://localhost:8080/api/sessions)
 | 
					response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","password":"test"}' -s -w "%{http_code}" http://localhost:8080/api/sessions)
 | 
				
			||||||
[[ ${response: -3} -eq 200 ]] && { echo "登錄成功: ${response%???}"; } || exit_service "登錄失敗: ${response%???}"
 | 
					message "$response" "登錄"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 不使用jq, 從一段json中取出id字段的值
 | 
					# 不使用jq, 從一段json中取出id字段的值
 | 
				
			||||||
session_id=$(echo "$response" | head -n -1 | grep -o '"id": "[^"]*' | cut -d '"' -f 4)
 | 
					session_id=$(echo "$response" | head -n -1 | grep -o '"id": "[^"]*' | cut -d '"' -f 4)
 | 
				
			||||||
echo "session_id: $session_id"
 | 
					#echo "session_id: $session_id"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 創建數據集, 應當在cookie中攜帶session_id (POST /api/datasets)
 | 
					# 創建數據集, 應當在cookie中攜帶session_id (POST /api/datasets)
 | 
				
			||||||
response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","description":"test"}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets)
 | 
					response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","description":"test"}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets)
 | 
				
			||||||
[[ ${response: -3} -eq 200 ]] && { echo "創建數據集成功: ${response%???}"; } || exit_service "創建數據集失敗: ${response%???}"
 | 
					message "$response" "創建數據集"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 取數據集id的值, 值爲 int
 | 
					# 取數據集id的值, 值爲 int
 | 
				
			||||||
dataset_id=$(echo "${response%???}" | grep -o '"id": [0-9]*' | awk '{print $2}')
 | 
					dataset_id=$(echo "${response%???}" | grep -o '"id": [0-9]*' | awk '{print $2}')
 | 
				
			||||||
echo "dataset_id: $dataset_id"
 | 
					#echo "dataset_id: $dataset_id"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## 獲取數據集列表 (GET /api/datasets)
 | 
					# 獲取數據集列表 (GET /api/datasets)
 | 
				
			||||||
#response=$(curl -X GET -H "Content-Type: application/json" -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets)
 | 
					response=$(curl -X GET -H "Content-Type: application/json" -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets)
 | 
				
			||||||
#[[ ${response: -3} -eq 200 ]] && { echo "獲取數據集列表成功: ${response%???}"; } || exit_service "獲取數據集列表失敗: ${response%???}"
 | 
					message "$response" "數據集列表"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 修改數據集, images 中增加 url (PATCH /api/datasets/:id)
 | 
					# 修改數據集, images 中增加 url (PATCH /api/datasets/:id)
 | 
				
			||||||
response=$(curl -X PATCH -H "Content-Type: application/json" -d '{"images":["https://img.gameui.net/article-7258-1677745322000@1x456.webp","https://img.gameui.net/article-6477-1682109454000@1x456.webp"]}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets/$dataset_id)
 | 
					response=$(curl -X PATCH -H "Content-Type: application/json" -d '{"images":["https://img.gameui.net/article-7258-1677745322000@1x456.webp","https://img.gameui.net/article-6477-1682109454000@1x456.webp"]}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets/$dataset_id)
 | 
				
			||||||
[[ ${response: -3} -eq 200 ]] && { echo "修改數據集成功: ${response%???}"; } || exit_service "修改數據集失敗: ${response%???}"
 | 
					message "$response" "修改數據集"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## 訓練模型 (POST /api/models)
 | 
					# 訓練模型 (POST /api/models)
 | 
				
			||||||
#response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","type":"dreambooth","trigger_words":"miao~","base_model":"sd1.5","epochs":20,"description":"test","dataset_id":'$dataset_id'}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/models)
 | 
					response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","type":"dreambooth","trigger_words":"miao~","base_model":"sd1.5","epochs":20,"description":"test","dataset_id":'$dataset_id'}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/models)
 | 
				
			||||||
#[[ ${response: -3} -eq 200 ]] && { echo "訓練模型任務已創建: ${response%???}"; } || exit_service "訓練模型任務創建失敗: ${response%???}"
 | 
					message "$response" "訓練模型"
 | 
				
			||||||
#
 | 
					
 | 
				
			||||||
#
 | 
					
 | 
				
			||||||
## 取模型id的值, 值爲 int
 | 
					# 取模型id的值, 值爲 int
 | 
				
			||||||
#model_id=$(echo "${response%???}" | grep -o '"id": [0-9]*' | awk '{print $2}')
 | 
					model_id=$(echo "${response%???}" | grep -o '"id": [0-9]*' | awk '{print $2}')
 | 
				
			||||||
#echo "model_id: $model_id"
 | 
					#echo "model_id: $model_id"
 | 
				
			||||||
#
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 模型列表 (GET /api/models)
 | 
					# 模型列表 (GET /api/models)
 | 
				
			||||||
response=$(curl -X GET -H "Content-Type: application/json" -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/models)
 | 
					response=$(curl -X GET -H "Content-Type: application/json" -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/models)
 | 
				
			||||||
 [[ ${response: -3} -eq 200 ]] && { echo "獲取模型列表成功: ${response%???}"; } || exit_service "獲取模型列表失敗: ${response%???}"
 | 
					message "$response" "模型列表" true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## 獲取模型訓練進度 (GET /api/models/:id)
 | 
					## 獲取模型訓練進度 (GET /api/models/:id)
 | 
				
			||||||
@@ -92,7 +106,7 @@ response=$(curl -X PATCH -H "Content-Type: application/json" -d '{"images":["htt
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
# 服務器列表
 | 
					# 服務器列表
 | 
				
			||||||
response=$(curl -X GET -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/servers)
 | 
					response=$(curl -X GET -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/servers)
 | 
				
			||||||
[[ ${response: -3} -eq 200 ]] && { echo "獲取服務器列表成功: ${response%???}"; } || exit_service "獲取服務器列表失敗: ${response%???}"
 | 
					message "$response" "服務器列表" true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sleep 10
 | 
					sleep 10
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user