project model

This commit is contained in:
w-mj 2023-03-30 21:27:54 +08:00
parent 92dd999e4a
commit cf02a4b41b
No known key found for this signature in database
GPG Key ID: 3A2CB5BE2F835897
3 changed files with 94 additions and 2 deletions

View File

@ -1,7 +1,7 @@
package k8s_manager
import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"reflect"
)
@ -44,7 +44,8 @@ func InitConfig() {
optionalFields := map[string]string{}
for _, f := range mustFields {
if getField(config, f) == "" {
panic(fmt.Sprintf("field %s must be set.", f))
log.Warningf("filed %s has not been set", f)
// panic(fmt.Sprintf("field %s must be set.", f))
}
}
for f, v := range optionalFields {

View File

@ -0,0 +1,61 @@
package model
import (
"gorm.io/gorm"
"time"
)
type ProjectModel struct {
gorm.Model
ProjectName string
UserName string
GroupName string
Type string
Comment string
ImageID int
Image ImageModel
TrainData []*DataModel `gorm:"many2many:projects_data"`
}
func (*ProjectModel) TableName() string {
return "projects"
}
type ImageModel struct {
gorm.Model
Image string
Comment string
Command string
Env string
Port string
UserName string
IsPublished bool
}
func (*ImageModel) TableName() string {
return "images"
}
type DataModel struct {
gorm.Model
Type string
Name string
Comment string
AccessInfo string
}
func (*DataModel) TableName() string {
return "data"
}
type RunningProject struct {
Project *ProjectModel
StartTime *time.Time
UserName string
Nodes int
Cpus int
Memory int
Gpus int
GpuMemory int
}

30
test/testdb.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"encoding/json"
"fmt"
k8s_manager "k8s-manager/src"
"k8s-manager/src/model"
)
func CreateTables() {
db := k8s_manager.DB()
err := db.Migrator().AutoMigrate(
&model.ProjectModel{},
&model.ImageModel{},
&model.DataModel{},
)
if err != nil {
fmt.Println(err)
return
}
}
func main() {
k8s_manager.InitConfig()
db := k8s_manager.DB()
p := model.ProjectModel{}
db.Preload("TrainData").Where("user_name = ?", "wmj").Find(&p)
j, _ := json.Marshal(&p)
fmt.Println(string(j))
}