From e7eeccd09ea3c41f8a7a9ae4d6eae83267578152 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Sun, 14 Apr 2024 15:19:32 +0100 Subject: [PATCH] chore: did more clean up --- logic/db_types/classes.go | 8 + logic/{models/utils => db_types}/types.go | 10 +- logic/models/add.go | 2 +- logic/models/classes/data_point.go | 6 +- logic/models/classes/list.go | 49 +-- logic/models/classes/types.go | 8 - logic/models/data.go | 5 +- logic/models/delete.go | 26 +- logic/models/edit.go | 1 - logic/models/list.go | 14 +- logic/models/run.go | 1 - logic/models/test.go | 2 +- logic/models/train/reset.go | 21 +- logic/models/train/train.go | 9 +- logic/tasks/handleUpload.go | 31 +- logic/tasks/list.go | 1 - logic/utils/handler.go | 21 +- main.go | 4 +- users.go | 2 +- .../src/routes/models/edit/ModelTable.svelte | 414 +++++++++--------- 20 files changed, 283 insertions(+), 352 deletions(-) create mode 100644 logic/db_types/classes.go rename logic/{models/utils => db_types}/types.go (92%) delete mode 100644 logic/models/classes/types.go diff --git a/logic/db_types/classes.go b/logic/db_types/classes.go new file mode 100644 index 0000000..49eee43 --- /dev/null +++ b/logic/db_types/classes.go @@ -0,0 +1,8 @@ +package dbtypes + +type DATA_POINT_MODE int + +const ( + DATA_POINT_MODE_TRAINING DATA_POINT_MODE = 1 + DATA_POINT_MODE_TESTING = 2 +) diff --git a/logic/models/utils/types.go b/logic/db_types/types.go similarity index 92% rename from logic/models/utils/types.go rename to logic/db_types/types.go index 3db6c18..29d60a8 100644 --- a/logic/models/utils/types.go +++ b/logic/db_types/types.go @@ -1,4 +1,4 @@ -package models_utils +package dbtypes import ( "database/sql" @@ -100,10 +100,10 @@ func GetBaseModel(db *sql.DB, id string) (base *BaseModel, err error) { } func (m BaseModel) CanEval() bool { - if m.Status != READY && m.Status != READY_RETRAIN && m.Status != READY_RETRAIN_FAILED && m.Status != READY_ALTERATION && m.Status != READY_ALTERATION_FAILED { - return false - } - return true + if m.Status != READY && m.Status != READY_RETRAIN && m.Status != READY_RETRAIN_FAILED && m.Status != READY_ALTERATION && m.Status != READY_ALTERATION_FAILED { + return false + } + return true } func StringToImageMode(colorMode string) int { diff --git a/logic/models/add.go b/logic/models/add.go index 5d65ea5..cb4b442 100644 --- a/logic/models/add.go +++ b/logic/models/add.go @@ -11,7 +11,7 @@ import ( "os" "path" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) diff --git a/logic/models/classes/data_point.go b/logic/models/classes/data_point.go index eb38bf7..ecd16fe 100644 --- a/logic/models/classes/data_point.go +++ b/logic/models/classes/data_point.go @@ -3,6 +3,8 @@ package model_classes import ( "database/sql" "errors" + + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" ) var FailedToGetIdAfterInsertError = errors.New("Failed to Get Id After Insert Error") @@ -23,6 +25,6 @@ func AddDataPoint(db *sql.DB, class_id string, file_path string, mode DATA_POINT } func UpdateDataPointStatus(db *sql.DB, data_point_id string, status int, message *string) (err error) { - _, err = db.Exec("update model_data_point set status=$1, status_message=$2 where id=$3", status, message, data_point_id) - return + _, err = db.Exec("update model_data_point set status=$1, status_message=$2 where id=$3", status, message, data_point_id) + return } diff --git a/logic/models/classes/list.go b/logic/models/classes/list.go index 735d555..83028ba 100644 --- a/logic/models/classes/list.go +++ b/logic/models/classes/list.go @@ -1,43 +1,26 @@ package model_classes import ( - "strconv" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) func HandleList(handle *Handle) { - handle.Get("/models/data/list", func(c *Context) *Error { - if !c.CheckAuthLevel(1) { - return nil - } - - id, err := GetIdFromUrl(c, "id") - if err != nil { - return c.JsonBadRequest("Model Class not found!") - } - - page := 0 - if c.R.URL.Query().Has("page") { - page_url := c.R.URL.Query().Get("page") - page_url_number, err := strconv.Atoi(page_url) - if err != nil { - return c.JsonBadRequest("Page is not a number") - } - page = page_url_number - } - + type DataList struct { + Id string `json:"id" validate:"required"` + Page int `json:"page"` + } + PostAuthJson(handle, "/models/data/list", User_Normal, func(c *Context, dat *DataList) *Error { var class_row struct { Name string Model_id string } - err = GetDBOnce(c, &class_row, "model_classes where id=$1", id) + err := GetDBOnce(c, &class_row, "model_classes where id=$1", dat.Id) if err == NotFoundError { return c.JsonBadRequest("Model Class not found!") } else if err != nil { - return c.Error500(err) + return c.E500M("Failed to get classes", err) } type baserow struct { @@ -47,23 +30,21 @@ func HandleList(handle *Handle) { Status int `json:"status"` } - rows, err := GetDbMultitple[baserow](c, "model_data_point where class_id=$1 limit 11 offset $2", id, page*10) + rows, err := GetDbMultitple[baserow](c, "model_data_point where class_id=$1 limit 11 offset $2", dat.Id, dat.Page*10) if err != nil { - return c.Error500(err) - } - - type ReturnType struct { - ImageList []*baserow `json:"image_list"` - Page int `json:"page"` - ShowNext bool `json:"showNext"` + return c.E500M("Failed to get classes", err) } max_len := min(11, len(rows)) c.ShowMessage = false - return c.SendJSON(ReturnType{ + return c.SendJSON(struct { + ImageList []*baserow `json:"image_list"` + Page int `json:"page"` + ShowNext bool `json:"showNext"` + }{ ImageList: rows[0:max_len], - Page: page, + Page: dat.Page, ShowNext: len(rows) == 11, }) }) diff --git a/logic/models/classes/types.go b/logic/models/classes/types.go deleted file mode 100644 index b1d96a8..0000000 --- a/logic/models/classes/types.go +++ /dev/null @@ -1,8 +0,0 @@ -package model_classes - -type DATA_POINT_MODE int - -const ( - DATA_POINT_MODE_TRAINING DATA_POINT_MODE = 1 - DATA_POINT_MODE_TESTING = 2 -) diff --git a/logic/models/data.go b/logic/models/data.go index 20f88ab..0cf7729 100644 --- a/logic/models/data.go +++ b/logic/models/data.go @@ -14,7 +14,6 @@ import ( . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" model_classes "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/classes" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) @@ -68,9 +67,9 @@ func fileProcessor( parts := strings.Split(file.Name, "/") - mode := model_classes.DATA_POINT_MODE_TRAINING + mode := DATA_POINT_MODE_TRAINING if parts[0] == "testing" { - mode = model_classes.DATA_POINT_MODE_TESTING + mode = DATA_POINT_MODE_TESTING } data_point_id, err := model_classes.AddDataPoint(c.Db, ids[parts[1]], "id://", mode) diff --git a/logic/models/delete.go b/logic/models/delete.go index dd54d65..e7fc286 100644 --- a/logic/models/delete.go +++ b/logic/models/delete.go @@ -6,7 +6,6 @@ import ( "path" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) @@ -14,44 +13,35 @@ func deleteModelJSON(c *Context, id string) *Error { c.Logger.Warnf("Removing model with id: %s", id) _, err := c.Db.Exec("delete from models where id=$1;", id) if err != nil { - return c.Error500(err) + return c.E500M("Failed to delete models", err) } model_path := path.Join("./savedData", id) c.Logger.Warnf("Removing folder of model with id: %s at %s", id, model_path) err = os.RemoveAll(model_path) if err != nil { - return c.Error500(err) + return c.E500M("Failed to remove data", err) } return c.SendJSON(id) } func handleDelete(handle *Handle) { - handle.Delete("/models/delete", func(c *Context) *Error { - if !c.CheckAuthLevel(1) { - return nil - } - var dat struct { - Id string `json:"id" validate:"required"` - Name *string `json:"name,omitempty"` - } - - if err_ := c.ToJSON(&dat); err_ != nil { - return err_ - } - + type DeleteModel struct { + Id string `json:"id" validate:"required"` + Name *string `json:"name,omitempty"` + } + DeleteAuthJson(handle, "/models/delete", User_Normal, func(c *Context, dat *DeleteModel) *Error { var model struct { Id string Name string Status int } - err := GetDBOnce(c, &model, "models where id=$1 and user_id=$2;", dat.Id, c.User.Id) if err == NotFoundError { return c.SendJSONStatus(http.StatusNotFound, "Model not found!") } else if err != nil { - return c.Error500(err) + return c.E500M("Faield to get model", err) } switch model.Status { diff --git a/logic/models/edit.go b/logic/models/edit.go index f07f1a3..0a6efa8 100644 --- a/logic/models/edit.go +++ b/logic/models/edit.go @@ -5,7 +5,6 @@ import ( . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" model_classes "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/classes" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) diff --git a/logic/models/list.go b/logic/models/list.go index ddafc1c..3006e1a 100644 --- a/logic/models/list.go +++ b/logic/models/list.go @@ -6,15 +6,7 @@ import ( ) // Auth level set when path is definied as 1 -func handleStats(c *Context) *Error { - var b struct { - Id string `json:"id" validate:"required"` - } - - if _err := c.ToJSON(&b); _err != nil { - return _err - } - +func handleStats(c *Context, b *JustId) *Error { type Row struct { Name string `db:"mc.name" json:"name"` Training string `db:"count(mdp.id) filter (where mdp.model_mode=1)" json:"training"` @@ -23,7 +15,7 @@ func handleStats(c *Context) *Error { rows, err := GetDbMultitple[Row](c, "model_data_point as mdp inner join model_classes as mc on mc.id=mdp.class_id where mc.model_id=$1 group by mc.name order by mc.name asc;", b.Id) if err != nil { - return c.Error500(err) + return c.E500M("Failed to get stats", err) } c.ShowMessage = false @@ -50,5 +42,5 @@ func handleList(handle *Handle) { return c.SendJSON(got) }) - handle.PostAuth("/models/class/stats", 1, handleStats) + PostAuthJson(handle, "/models/class/stats", User_Normal, handleStats) } diff --git a/logic/models/run.go b/logic/models/run.go index 1d72bac..eda033c 100644 --- a/logic/models/run.go +++ b/logic/models/run.go @@ -6,7 +6,6 @@ import ( "path" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/utils" tf "github.com/galeone/tensorflow/tensorflow/go" diff --git a/logic/models/test.go b/logic/models/test.go index e1a3891..fcb2ce5 100644 --- a/logic/models/test.go +++ b/logic/models/test.go @@ -6,7 +6,7 @@ import ( _ "image/png" "os" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) diff --git a/logic/models/train/reset.go b/logic/models/train/reset.go index 2bdd759..bc92eeb 100644 --- a/logic/models/train/reset.go +++ b/logic/models/train/reset.go @@ -4,29 +4,17 @@ import ( "os" "path" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) func handleRest(handle *Handle) { - handle.Delete("/models/train/reset", func(c *Context) *Error { - if !c.CheckAuthLevel(1) { - return nil - } - var dat struct { - Id string `json:"id"` - } - - if err := c.ToJSON(&dat); err != nil { - return err - } - + DeleteAuthJson(handle, "/models/train/reset", User_Normal, func(c *Context, dat *JustId) *Error { model, err := GetBaseModel(c.Db, dat.Id) if err == ModelNotFoundError { return c.JsonBadRequest("Model not found") } else if err != nil { - // TODO improve response - return c.Error500(err) + return c.E500M("Failed to get model", err) } if model.Status != FAILED_PREPARING_TRAINING && model.Status != FAILED_TRAINING { @@ -37,8 +25,7 @@ func handleRest(handle *Handle) { _, err = c.Db.Exec("delete from model_definition where model_id=$1", model.Id) if err != nil { - // TODO improve response - return c.Error500(err) + return c.E500M("Failed to delete model", err) } ModelUpdateStatus(c, model.Id, CONFIRM_PRE_TRAINING) diff --git a/logic/models/train/train.go b/logic/models/train/train.go index 72370ba..b6ad695 100644 --- a/logic/models/train/train.go +++ b/logic/models/train/train.go @@ -16,7 +16,6 @@ import ( . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" model_classes "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/classes" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" "github.com/charmbracelet/log" ) @@ -80,7 +79,7 @@ func generateCvs(c *Context, run_path string, model_id string) (count int, err e } count = co.Count - data, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2;", model_id, model_classes.DATA_POINT_MODE_TRAINING) + data, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2;", model_id, DATA_POINT_MODE_TRAINING) if err != nil { return } @@ -139,7 +138,7 @@ func generateCvsExp(c *Context, run_path string, model_id string, doPanic bool) return generateCvsExp(c, run_path, model_id, true) } - data, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2 and mc.status=$3;", model_id, model_classes.DATA_POINT_MODE_TRAINING, MODEL_CLASS_STATUS_TRAINING) + data, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2 and mc.status=$3;", model_id, DATA_POINT_MODE_TRAINING, MODEL_CLASS_STATUS_TRAINING) if err != nil { return } @@ -303,7 +302,7 @@ func generateCvsExpandExp(c *Context, run_path string, model_id string, offset i return generateCvsExpandExp(c, run_path, model_id, offset, true) } - data, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2 and mc.status=$3;", model_id, model_classes.DATA_POINT_MODE_TRAINING, MODEL_CLASS_STATUS_TRAINING) + data, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2 and mc.status=$3;", model_id, DATA_POINT_MODE_TRAINING, MODEL_CLASS_STATUS_TRAINING) if err != nil { return } @@ -337,7 +336,7 @@ func generateCvsExpandExp(c *Context, run_path string, model_id string, offset i // This is to load some extra data so that the model has more things to train on // - data_other, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2 and mc.status=$3 limit $4;", model_id, model_classes.DATA_POINT_MODE_TRAINING, MODEL_CLASS_STATUS_TRAINED, count*10) + data_other, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2 and mc.status=$3 limit $4;", model_id, DATA_POINT_MODE_TRAINING, MODEL_CLASS_STATUS_TRAINED, count*10) if err != nil { return } diff --git a/logic/tasks/handleUpload.go b/logic/tasks/handleUpload.go index ead9947..615a30a 100644 --- a/logic/tasks/handleUpload.go +++ b/logic/tasks/handleUpload.go @@ -9,7 +9,6 @@ import ( . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) @@ -48,7 +47,7 @@ func handleUpload(handler *Handle) { ModelId string `json:"id" validate:"required"` } - _err := c.ParseJson(&requestData, json_data) + _err := c.ParseJson(&requestData, json_data) if _err != nil { return _err } @@ -88,9 +87,9 @@ func handleUpload(handler *Handle) { } id, err := InsertReturnId(c, &newTask, "tasks", "id") - if err != nil { + if err != nil { return c.E500M("Error 500", err) - } + } save_path := path.Join("savedData", model.Id, "tasks") os.MkdirAll(save_path, os.ModePerm) @@ -99,26 +98,28 @@ func handleUpload(handler *Handle) { img_file, err := os.Create(img_path) if err != nil { - if _err := UpdateTaskStatus(c,id, -1, "Failed to create the file"); _err != nil { - c.Logger.Error("Failed to update tasks") - } + if _err := UpdateTaskStatus(c, id, -1, "Failed to create the file"); _err != nil { + c.Logger.Error("Failed to update tasks") + } return c.E500M("Failed to create the file", err) } defer img_file.Close() img_file.Write(file) if !TestImgForModel(c, model, img_path) { - if _err := UpdateTaskStatus(c, id, -1, "The provided image is not a valid image for this model"); _err != nil { - c.Logger.Error("Failed to update tasks") - } + if _err := UpdateTaskStatus(c, id, -1, "The provided image is not a valid image for this model"); _err != nil { + c.Logger.Error("Failed to update tasks") + } return c.JsonBadRequest(struct { - Message string `json:"message"` - Id string `json:"task_id"` - } { "Provided image does not match the model", id}) + Message string `json:"message"` + Id string `json:"task_id"` + }{"Provided image does not match the model", id}) } - UpdateStatus(c, "tasks", id, 1) + UpdateStatus(c, "tasks", id, 1) - return c.SendJSON(struct {Id string `json:"id"`}{id}) + return c.SendJSON(struct { + Id string `json:"id"` + }{id}) }) } diff --git a/logic/tasks/list.go b/logic/tasks/list.go index 3af8dba..019621f 100644 --- a/logic/tasks/list.go +++ b/logic/tasks/list.go @@ -3,7 +3,6 @@ package tasks import ( . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" dbtypes "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) diff --git a/logic/utils/handler.go b/logic/utils/handler.go index 17c0675..32d84b1 100644 --- a/logic/utils/handler.go +++ b/logic/utils/handler.go @@ -11,7 +11,6 @@ import ( "time" dbtypes "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" "github.com/charmbracelet/log" "github.com/go-playground/validator/v10" "github.com/goccy/go-json" @@ -67,7 +66,7 @@ func (x *Handle) Get(path string, fn func(c *Context) *Error) { x.gets = append(x.gets, HandleFunc{path, fn}) } -func (x *Handle) GetAuth(path string, authLevel int, fn func(c *Context) *Error) { +func (x *Handle) GetAuth(path string, authLevel dbtypes.UserType, fn func(c *Context) *Error) { inner_fn := func(c *Context) *Error { if !c.CheckAuthLevel(authLevel) { return nil @@ -80,7 +79,7 @@ func (x *Handle) Post(path string, fn func(c *Context) *Error) { x.posts = append(x.posts, HandleFunc{path, fn}) } -func (x *Handle) PostAuth(path string, authLevel int, fn func(c *Context) *Error) { +func (x *Handle) PostAuth(path string, authLevel dbtypes.UserType, fn func(c *Context) *Error) { inner_fn := func(c *Context) *Error { if !c.CheckAuthLevel(authLevel) { return nil @@ -92,7 +91,7 @@ func (x *Handle) PostAuth(path string, authLevel int, fn func(c *Context) *Error func PostAuthJson[T interface{}](x *Handle, path string, authLevel dbtypes.UserType, fn func(c *Context, obj *T) *Error) { inner_fn := func(c *Context) *Error { - if !c.CheckAuthLevel(int(authLevel)) { + if !c.CheckAuthLevel(authLevel) { return nil } @@ -112,7 +111,7 @@ func (x *Handle) Delete(path string, fn func(c *Context) *Error) { x.deletes = append(x.deletes, HandleFunc{path, fn}) } -func (x *Handle) DeleteAuth(path string, authLevel int, fn func(c *Context) *Error) { +func (x *Handle) DeleteAuth(path string, authLevel dbtypes.UserType, fn func(c *Context) *Error) { inner_fn := func(c *Context) *Error { if !c.CheckAuthLevel(authLevel) { return nil @@ -122,7 +121,7 @@ func (x *Handle) DeleteAuth(path string, authLevel int, fn func(c *Context) *Err x.posts = append(x.posts, HandleFunc{path, inner_fn}) } -func DeleteAuthJson[T interface{}](x *Handle, path string, authLevel int, fn func(c *Context, obj *T) *Error) { +func DeleteAuthJson[T interface{}](x *Handle, path string, authLevel dbtypes.UserType, fn func(c *Context, obj *T) *Error) { inner_fn := func(c *Context) *Error { if !c.CheckAuthLevel(authLevel) { return nil @@ -160,13 +159,13 @@ func handleLoop(array []HandleFunc, context *Context) { handleError(&Error{404, "Endpoint not found"}, context) } -func (c *Context) CheckAuthLevel(authLevel int) bool { +func (c *Context) CheckAuthLevel(authLevel dbtypes.UserType) bool { if authLevel > 0 { if c.User == nil { contextlessLogoff(c.Writer) return false } - if c.User.UserType < authLevel { + if c.User.UserType < int(authLevel) { c.Writer.WriteHeader(http.StatusUnauthorized) e := c.SendJSON("Not Authorized") if e != nil { @@ -321,15 +320,15 @@ func (c Context) JsonErrorBadRequest(err error, dat any) *Error { return c.SendJSONStatus(http.StatusBadRequest, dat) } -func (c *Context) GetModelFromId(id_path string) (*BaseModel, *Error) { +func (c *Context) GetModelFromId(id_path string) (*dbtypes.BaseModel, *Error) { id, err := GetIdFromUrl(c, id_path) if err != nil { return nil, c.SendJSONStatus(http.StatusNotFound, "Model not found") } - model, err := GetBaseModel(c.Db, id) - if err == ModelNotFoundError { + model, err := dbtypes.GetBaseModel(c.Db, id) + if err == dbtypes.ModelNotFoundError { return nil, c.SendJSONStatus(http.StatusNotFound, "Model not found") } else if err != nil { return nil, c.Error500(err) diff --git a/main.go b/main.go index 7c5490d..494aa8a 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,8 @@ import ( "github.com/charmbracelet/log" _ "github.com/lib/pq" + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models" - models_utils "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/runner" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" @@ -44,7 +44,7 @@ func main() { handle := NewHandler(db, config) // TODO remove this before commiting - _, err = db.Exec("update models set status=$1 where status=$2", models_utils.FAILED_TRAINING, models_utils.TRAINING) + _, err = db.Exec("update models set status=$1 where status=$2", FAILED_TRAINING, TRAINING) if err != nil && clear_db { log.Warn("Database might not be on") panic(err) diff --git a/users.go b/users.go index 21dc06f..5cf633b 100644 --- a/users.go +++ b/users.go @@ -188,7 +188,7 @@ func usersEndpints(db *sql.DB, handle *Handle) { }) // TODO allow admin users to update this data - handle.GetAuth("/user/info", int(dbtypes.User_Normal), func(c *Context) *Error { + handle.GetAuth("/user/info", dbtypes.User_Normal, func(c *Context) *Error { user, err := dbtypes.UserFromToken(c.Db, *c.Token) if err != nil { return c.E500M("Falied to get user data", err) diff --git a/webpage/src/routes/models/edit/ModelTable.svelte b/webpage/src/routes/models/edit/ModelTable.svelte index 2ad0fdd..cf6c316 100644 --- a/webpage/src/routes/models/edit/ModelTable.svelte +++ b/webpage/src/routes/models/edit/ModelTable.svelte @@ -2,21 +2,21 @@ export type Image = { file_path: string; mode: number; - status: number; - id: string; + status: number; + id: string; }; {#if classes.length == 0} @@ -106,47 +104,48 @@
-
- {#each classes as item} - - {/each} -
- +
+ {#each classes as item} + + {/each} +
+
- {#if selected_class == undefined && isActive('-----New Class-----')} -
-

- Add New Class -

-
-
- -
- Please provide a file that has the training and testing data
- The file must have 2 folders one with testing images and one with training images.
- Each of the folders will contain the classes of the model. The folders must be the same in testing and training. - The class folders must have the images for the classes. -
+		{#if selected_class == undefined && isActive('-----New Class-----')}
+			
+

Add New Class

+ +
+ +
+ Please provide a file that has the training and testing data
+ The file must have 2 folders one with testing images and one with training images. +
+ Each of the folders will contain the classes of the model. The folders must be the same + in testing and training. The class folders must have the images for the classes. +
     training\
         class1\
             img1.png
@@ -172,147 +171,132 @@
             ...
         ...
 
-
- - - - Upload Zip File - -
- - - File selected - -
-
-
- - {#if file} - {#await uploading} - - {:then} - - {/await} - {/if} - -
- {/if} - {#if selected_class} -
- {#if model.model_type == 2} - {#if selected_class?.status == 1} -

- Class to train -

- {:else if selected_class?.status == 2} -

- Class training -

- {:else if selected_class?.status == 3} -

- Class trained -

- {/if} - {/if} - - - - - - - - - - - - {#each image_list as image} - - - - - - - - {/each} - -
File Path Mode - - - - - -
- {#if image.file_path == 'id://'} - Managed - {:else} - {image.file_path} - {/if} - - {#if image.mode == 2} - Testing - {:else} - Training - {/if} - - {#if image.file_path == 'id://'} - - {:else} - TODO img {image.file_path} - {/if} - - {#if image.status == 1} - - {:else} - - {/if} - - -
-
-
- {#if page > 0} - - {/if} -
+
+ + + Upload Zip File +
+ + File selected +
+
+
+ + {#if file} + {#await uploading} + + {:then} + + {/await} + {/if} + +
+ {/if} + {#if selected_class} +
+ {#if model.model_type == 2} + {#if selected_class?.status == 1} +

Class to train

+ {:else if selected_class?.status == 2} +

Class training

+ {:else if selected_class?.status == 3} +

Class trained

+ {/if} + {/if} + + + + + + + + + + + + {#each image_list as image} + + + + + + + + {/each} + +
File Path Mode + + + + + +
+ {#if image.file_path == 'id://'} + Managed + {:else} + {image.file_path} + {/if} + + {#if image.mode == 2} + Testing + {:else} + Training + {/if} + + {#if image.file_path == 'id://'} + + {:else} + TODO img {image.file_path} + {/if} + + {#if image.status == 1} + + {:else} + + {/if} + + +
+
+
+ {#if page > 0} + + {/if} +
-
- {page} -
+
+ {page} +
-
- {#if showNext} - - {/if} -
-
-
- {/if} +
+ {#if showNext} + + {/if} +
+ + + {/if}
{/if}