2023-09-21 16:43:11 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2024-04-14 14:51:16 +01:00
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
|
2023-09-21 16:43:11 +01:00
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
|
|
|
|
)
|
|
|
|
|
2024-04-17 14:56:57 +01:00
|
|
|
func DeleteModel(c BasePack, id string) (err error) {
|
|
|
|
c.GetLogger().Warnf("Removing model with id: %s", id)
|
|
|
|
_, err = c.GetDb().Exec("delete from models where id=$1;", id)
|
2024-03-06 23:33:54 +00:00
|
|
|
if err != nil {
|
2024-04-17 14:56:57 +01:00
|
|
|
return
|
2024-03-06 23:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
model_path := path.Join("./savedData", id)
|
2024-04-17 14:56:57 +01:00
|
|
|
c.GetLogger().Warnf("Removing folder of model with id: %s at %s", id, model_path)
|
2024-03-06 23:33:54 +00:00
|
|
|
err = os.RemoveAll(model_path)
|
|
|
|
if err != nil {
|
2024-04-17 14:56:57 +01:00
|
|
|
return
|
2024-03-06 23:33:54 +00:00
|
|
|
}
|
2024-04-17 14:56:57 +01:00
|
|
|
return
|
|
|
|
}
|
2024-03-06 23:33:54 +00:00
|
|
|
|
2024-04-17 14:56:57 +01:00
|
|
|
func deleteModelJSON(c *Context, id string) *Error {
|
|
|
|
err := DeleteModel(c, id)
|
|
|
|
if err != nil {
|
|
|
|
return c.E500M("Failed to delete models", err)
|
|
|
|
}
|
2024-03-06 23:33:54 +00:00
|
|
|
return c.SendJSON(id)
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
func handleDelete(handle *Handle) {
|
2024-04-14 15:19:32 +01:00
|
|
|
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 {
|
2024-03-09 10:52:08 +00:00
|
|
|
var model struct {
|
|
|
|
Id string
|
|
|
|
Name string
|
|
|
|
Status int
|
2023-09-21 16:43:11 +01:00
|
|
|
}
|
2024-04-14 14:51:16 +01:00
|
|
|
err := GetDBOnce(c, &model, "models where id=$1 and user_id=$2;", dat.Id, c.User.Id)
|
2024-03-09 10:52:08 +00:00
|
|
|
if err == NotFoundError {
|
|
|
|
return c.SendJSONStatus(http.StatusNotFound, "Model not found!")
|
|
|
|
} else if err != nil {
|
2024-04-14 15:19:32 +01:00
|
|
|
return c.E500M("Faield to get model", err)
|
2023-09-21 16:43:11 +01:00
|
|
|
}
|
|
|
|
|
2024-05-10 02:13:02 +01:00
|
|
|
switch ModelStatus(model.Status) {
|
2024-03-06 23:33:54 +00:00
|
|
|
case FAILED_TRAINING:
|
|
|
|
fallthrough
|
|
|
|
case FAILED_PREPARING_ZIP_FILE:
|
|
|
|
fallthrough
|
|
|
|
case FAILED_PREPARING_TRAINING:
|
|
|
|
fallthrough
|
2023-09-21 16:43:11 +01:00
|
|
|
case FAILED_PREPARING:
|
2024-03-09 10:52:08 +00:00
|
|
|
return deleteModelJSON(c, dat.Id)
|
2023-10-06 12:13:19 +01:00
|
|
|
|
2024-03-06 23:33:54 +00:00
|
|
|
case READY:
|
|
|
|
fallthrough
|
2024-04-08 14:17:13 +01:00
|
|
|
case READY_RETRAIN_FAILED:
|
|
|
|
fallthrough
|
|
|
|
case READY_ALTERATION_FAILED:
|
|
|
|
fallthrough
|
2023-09-21 16:43:11 +01:00
|
|
|
case CONFIRM_PRE_TRAINING:
|
2024-03-09 10:52:08 +00:00
|
|
|
if dat.Name == nil {
|
|
|
|
return c.JsonBadRequest("Provided name does not match the model name")
|
2023-09-21 16:43:11 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 10:52:08 +00:00
|
|
|
if *dat.Name != model.Name {
|
|
|
|
return c.JsonBadRequest("Provided name does not match the model name")
|
2023-09-21 16:43:11 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 10:52:08 +00:00
|
|
|
return deleteModelJSON(c, dat.Id)
|
2023-09-21 16:43:11 +01:00
|
|
|
default:
|
2024-03-09 10:52:08 +00:00
|
|
|
c.Logger.Warn("Do not know how to handle model in status", "status", model.Status)
|
|
|
|
return c.JsonBadRequest("Model in invalid status")
|
2023-09-21 16:43:11 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|