服務器狀態

This commit is contained in:
2023-05-21 08:08:46 +08:00
parent e946608376
commit 05ae37d6e2
6 changed files with 57 additions and 5 deletions

View File

@@ -17,13 +17,17 @@ TEST:
- [x] 刪除數據集 - [x] 刪除數據集
- [x] 修改數據集 - [x] 修改數據集
- [x] 查詢數據集 - [x] 查詢數據集
- [x] GET [/api/servers](/api/servers) 服務器列表
- [x] GET [/api/models](/api/models) 模型列表 - [x] GET [/api/models](/api/models) 模型列表
- [x] 創建模型(訓練模型)
- [x] 刪除模型
- [x] 修改模型
- [x] 查詢模型
- [x] GET [/api/images](/api/images) 圖片列表 - [x] GET [/api/images](/api/images) 圖片列表
- [x] GET [/api/tasks](/api/tasks) 任務列表 - [x] GET [/api/tasks](/api/tasks) 任務列表
- [x] GET [/api/tags](/api/tags) 標籤列表 - [x] GET [/api/tags](/api/tags) 標籤列表
- [x] GET [/api/params](/api/params) 參數列表 - [x] GET [/api/params](/api/params) 參數列表
- [x] GET [/api/account](/api/account) 賬戶信息 - [x] GET [/api/account](/api/account) 賬戶信息
- [x] GET [/api/servers](/api/servers) 服務器列表
TEST: TEST:

View File

@@ -11,6 +11,7 @@ type Server struct {
Type string `json:"type"` // (訓練|推理) Type string `json:"type"` // (訓練|推理)
IP string `json:"ip"` IP string `json:"ip"`
Port int `json:"port"` Port int `json:"port"`
Status string `json:"status"` // (異常|初始化|就緒|工作中|關閉中)
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`

View File

@@ -22,7 +22,7 @@ func (list ImageList) Value() (driver.Value, error) {
return json.Marshal(list) return json.Marshal(list)
} }
// 數據集 // 數據集模型
type Dataset struct { type Dataset struct {
ID int `json:"id" gorm:"primary_key"` ID int `json:"id" gorm:"primary_key"`
Name string `json:"name"` Name string `json:"name"`
@@ -129,6 +129,7 @@ func DatasetsItemPatch(w http.ResponseWriter, r *http.Request) {
}) })
} }
// 刪除數據集
func DatasetsItemDelete(w http.ResponseWriter, r *http.Request) { func DatasetsItemDelete(w http.ResponseWriter, r *http.Request) {
models.AccountRead(w, r, func(account *models.Account) { models.AccountRead(w, r, func(account *models.Account) {
// 獲取數據集 // 獲取數據集

View File

@@ -77,9 +77,6 @@ func ImagesItemPatch(w http.ResponseWriter, r *http.Request) {
log.Println(err) log.Println(err)
return return
} }
//image.ID = utils.ParamInt(mux.Vars(r)["id"], 0)
//image.Update()
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(image)) w.Write(utils.ToJSON(image))
} }

View File

@@ -54,6 +54,9 @@ func ModelsPost(w http.ResponseWriter, r *http.Request) {
log.Println(err) log.Println(err)
return return
} }
// 直接提交訓練任務
go model.Train()
// 返回創建的模型
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(model)) w.Write(utils.ToJSON(model))
}) })

View File

@@ -1,11 +1,15 @@
package routers package routers
import ( import (
"encoding/json"
"fmt"
"io/ioutil"
"main/configs" "main/configs"
"main/models" "main/models"
"main/utils" "main/utils"
"net/http" "net/http"
"github.com/google/uuid"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@@ -17,6 +21,13 @@ func ServersGet(w http.ResponseWriter, r *http.Request) {
db := configs.ORMDB() db := configs.ORMDB()
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 {
// 驗證服務器狀態
resp, err := http.Get(fmt.Sprintf("http://%s:%d/docs", server.IP, server.Port))
if err != nil || resp.StatusCode != http.StatusOK {
server.Status = "異常"
} else {
server.Status = "正常"
}
listview.List = append(listview.List, server) listview.List = append(listview.List, server)
} }
listview.Next = listview.Page*listview.PageSize < int(listview.Total) listview.Next = listview.Page*listview.PageSize < int(listview.Total)
@@ -25,6 +36,41 @@ func ServersGet(w http.ResponseWriter, r *http.Request) {
func ServersPost(w http.ResponseWriter, r *http.Request) { func ServersPost(w http.ResponseWriter, r *http.Request) {
var server models.Server var server models.Server
// 獲取參數
body, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err := json.Unmarshal(body, &server); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
// 如果不指定類型,禁止創建服務器, 必須指定類型:訓練|推理
if server.Type != "訓練" && server.Type != "推理" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("必須指定類型:訓練|推理"))
return
}
// 如果不指定名稱則使用uuid生成隨機名稱
if server.Name == "" {
server.Name = uuid.New().String()
}
// 如果不指定 port則使用默認 port
if server.Port == 0 {
server.Port = 7860
}
// 如果不指定IP則自動創建新服務器
if server.IP == "" {
// TODO: 創建新服務器
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(server))
return
}
configs.ORMDB().Create(&server) configs.ORMDB().Create(&server)
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(server)) w.Write(utils.ToJSON(server))