2023-09-21 16:43:11 +01:00
package models
import (
"fmt"
"net/http"
2023-09-22 19:22:36 +01:00
model_classes "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/classes"
2023-09-26 20:15:28 +01:00
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
2024-03-01 23:03:25 +00:00
"git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
2023-09-21 16:43:11 +01:00
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
)
2024-03-01 23:03:25 +00:00
func handleJson ( w http . ResponseWriter , r * http . Request , c * Context ) * Error {
if ! CheckAuthLevel ( 1 , w , r , c ) {
return nil
}
id , err := GetIdFromUrl ( r , "id" )
if err != nil {
return c . JsonBadRequest ( "Model not found" )
}
type rowmodel struct {
Name string ` json:"name" `
Status int ` json:"status" `
Id string ` json:"id" `
Width * int ` json:"width" `
Height * int ` json:"height" `
Color_mode * string ` json:"color_mode" `
Format string ` json:"format" `
Model_type int ` json:"model_type" `
}
var model rowmodel = rowmodel { }
err = utils . GetDBOnce ( c , & model , "models where id=$1 and user_id=$2" , id , c . User . Id )
if err == NotFoundError {
return c . SendJSONStatus ( 404 , "Model not found" )
} else if err != nil {
return c . Error500 ( err )
}
return c . SendJSON ( model )
/ *
switch model . Status {
case TRAINING :
}
* /
}
2023-09-21 16:43:11 +01:00
func handleEdit ( handle * Handle ) {
2024-03-01 23:03:25 +00:00
handle . Get ( "/models/edit/classes" , func ( w http . ResponseWriter , r * http . Request , c * Context ) * Error {
2023-09-21 16:43:11 +01:00
if ! CheckAuthLevel ( 1 , w , r , c ) {
return nil
}
2024-03-01 23:03:25 +00:00
if c . Mode != JSON {
return c . ErrorCode ( nil , 400 , AnyMap { } )
}
id , err := GetIdFromUrl ( r , "id" )
if err != nil {
return c . SendJSONStatus ( http . StatusNotFound , "Model not found" )
}
model , err := GetBaseModel ( c . Db , id )
if err == ModelNotFoundError {
return c . SendJSONStatus ( http . StatusNotFound , "Model not found" )
} else if err != nil {
return c . Error500 ( err )
}
wrong_number , err := model_classes . GetNumberOfWrongDataPoints ( c . Db , model . Id )
if err != nil {
return c . Error500 ( err )
}
cls , err := model_classes . ListClasses ( handle . Db , id )
if err != nil {
return c . Error500 ( err )
}
has_data , err := model_classes . ModelHasDataPoints ( handle . Db , id )
if err != nil {
return Error500 ( err )
}
type ReturnType struct {
Classes [ ] model_classes . ModelClass ` json:"classes" `
HasData bool ` json:"has_data" `
NumberOfInvalidImages int ` json:"number_of_invalid_images" `
}
return c . SendJSON ( ReturnType {
Classes : cls ,
HasData : has_data ,
NumberOfInvalidImages : wrong_number ,
} )
} )
2024-03-02 12:45:49 +00:00
handle . Get ( "/models/edit/definitions" , func ( w http . ResponseWriter , r * http . Request , c * Context ) * Error {
if ! CheckAuthLevel ( 1 , w , r , c ) {
return nil
}
if c . Mode != JSON {
return c . ErrorCode ( nil , 400 , AnyMap { } )
}
id , err := GetIdFromUrl ( r , "id" )
if err != nil {
return c . SendJSONStatus ( http . StatusNotFound , "Model not found" )
}
model , err := GetBaseModel ( c . Db , id )
if err == ModelNotFoundError {
return c . SendJSONStatus ( http . StatusNotFound , "Model not found" )
} else if err != nil {
return c . Error500 ( err )
}
type defrow struct {
Id string
Status int
EpochProgress int
Epoch int
Accuracy float64
}
defs := [ ] defrow { }
if model . ModelType == 2 {
def_rows , err := c . Db . Query ( "select md.id, md.status, md.epoch, h.epoch_progress, h.accuracy from model_definition as md inner join exp_model_head as h on h.def_id = md.id where md.model_id=$1 order by md.created_on asc" , model . Id )
if err != nil {
return c . Error500 ( err )
}
defer def_rows . Close ( )
for def_rows . Next ( ) {
var def defrow
err = def_rows . Scan ( & def . Id , & def . Status , & def . Epoch , & def . EpochProgress , & def . Accuracy )
if err != nil {
return c . Error500 ( err )
}
defs = append ( defs , def )
}
} else {
def_rows , err := c . Db . Query ( "select id, status, epoch, epoch_progress, accuracy from model_definition where model_id=$1 order by created_on asc" , model . Id )
if err != nil {
return c . Error500 ( err )
}
defer def_rows . Close ( )
for def_rows . Next ( ) {
var def defrow
err = def_rows . Scan ( & def . Id , & def . Status , & def . Epoch , & def . EpochProgress , & def . Accuracy )
if err != nil {
return c . Error500 ( err )
}
defs = append ( defs , def )
}
}
type layerdef struct {
id string
LayerType int ` json:"layer_type" `
Shape string ` json:"shape" `
}
layers := [ ] layerdef { }
for _ , def := range defs {
if def . Status == MODEL_DEFINITION_STATUS_TRAINING {
rows , err := c . Db . Query ( "select id, layer_type, shape from model_definition_layer where def_id=$1 order by layer_order asc;" , def . Id )
if err != nil {
return c . Error500 ( err )
}
defer rows . Close ( )
for rows . Next ( ) {
var layerdef layerdef
err = rows . Scan ( & layerdef . id , & layerdef . LayerType , & layerdef . Shape )
if err != nil {
return c . Error500 ( err )
}
layers = append ( layers , layerdef )
}
if model . ModelType == 2 {
type lastLayerType struct {
Id string
Range_start int
Range_end int
}
var lastLayer lastLayerType
err := GetDBOnce ( c , & lastLayer , "exp_model_head where def_id=$1 and status=3;" , def . Id )
if err != nil {
return c . Error500 ( err )
}
layers = append ( layers , layerdef {
id : lastLayer . Id ,
LayerType : LAYER_DENSE ,
Shape : fmt . Sprintf ( "%d, 1" , lastLayer . Range_end - lastLayer . Range_start + 1 ) ,
} )
}
break
}
}
type Definitions struct {
Id string ` json:"id" `
Status int ` json:"status" `
EpochProgress int ` json:"epoch_progress" `
Epoch int ` json:"epoch" `
Accuracy float64 ` json:"accuracy" `
Layers * [ ] layerdef ` json:"layers" `
}
defsToReturn := make ( [ ] Definitions , len ( defs ) , len ( defs ) )
setLayers := false
for i , def := range defs {
var lay * [ ] layerdef = nil
if def . Status == MODEL_DEFINITION_STATUS_TRAINING && ! setLayers {
lay = & layers
setLayers = true
}
defsToReturn [ i ] = Definitions {
Id : def . Id ,
Status : def . Status ,
EpochProgress : def . EpochProgress ,
Epoch : def . Epoch ,
Accuracy : def . Accuracy ,
Layers : lay ,
}
}
return c . SendJSON ( defsToReturn )
} )
2024-03-01 23:03:25 +00:00
handle . Get ( "/models/edit" , func ( w http . ResponseWriter , r * http . Request , c * Context ) * Error {
if ! CheckAuthLevel ( 1 , w , r , c ) {
return nil
}
if c . Mode == JSON {
return handleJson ( w , r , c )
}
2023-09-21 16:43:11 +01:00
id , err := GetIdFromUrl ( r , "id" )
if err != nil {
return ErrorCode ( nil , http . StatusNotFound , AnyMap {
"NotFoundMessage" : "Model not found" ,
"GoBackLink" : "/models" ,
} )
}
// TODO handle admin users
2024-02-08 18:20:58 +00:00
rows , err := handle . Db . Query ( "select name, status, width, height, color_mode, format, model_type from models where id=$1 and user_id=$2;" , id , c . User . Id )
2023-09-21 16:43:11 +01:00
if err != nil {
return Error500 ( err )
}
2023-10-20 12:37:56 +01:00
defer rows . Close ( )
2023-09-21 16:43:11 +01:00
if ! rows . Next ( ) {
return ErrorCode ( nil , http . StatusNotFound , AnyMap {
"NotFoundMessage" : "Model not found" ,
"GoBackLink" : "/models" ,
} )
}
type rowmodel struct {
Name string
Status int
Id string
Width * int
Height * int
Color_mode * string
2023-10-20 12:37:56 +01:00
Format string
2024-02-08 18:20:58 +00:00
Type int
2023-09-21 16:43:11 +01:00
}
var model rowmodel = rowmodel { }
model . Id = id
2024-02-08 18:20:58 +00:00
err = rows . Scan ( & model . Name , & model . Status , & model . Width , & model . Height , & model . Color_mode , & model . Format , & model . Type )
2023-09-21 16:43:11 +01:00
if err != nil {
return Error500 ( err )
}
// Handle errors
// All errors will be negative
if model . Status < 0 {
LoadBasedOnAnswer ( c . Mode , w , "/models/edit.html" , c . AddMap ( AnyMap {
"Model" : model ,
} ) )
return nil
}
switch model . Status {
case PREPARING :
LoadBasedOnAnswer ( c . Mode , w , "/models/edit.html" , c . AddMap ( AnyMap {
"Model" : model ,
} ) )
case CONFIRM_PRE_TRAINING :
2023-10-10 12:28:49 +01:00
2023-10-20 12:37:56 +01:00
wrong_number , err := model_classes . GetNumberOfWrongDataPoints ( c . Db , model . Id )
if err != nil {
return c . Error500 ( err )
}
2023-10-10 12:28:49 +01:00
2023-10-20 12:37:56 +01:00
cls , err := model_classes . ListClasses ( handle . Db , id )
if err != nil {
return c . Error500 ( err )
}
2023-09-26 20:15:28 +01:00
2023-10-20 12:37:56 +01:00
has_data , err := model_classes . ModelHasDataPoints ( handle . Db , id )
if err != nil {
return Error500 ( err )
}
2023-09-22 19:22:36 +01:00
2023-09-21 16:43:11 +01:00
LoadBasedOnAnswer ( c . Mode , w , "/models/edit.html" , c . AddMap ( AnyMap {
2023-10-20 12:37:56 +01:00
"Model" : model ,
"Classes" : cls ,
"HasData" : has_data ,
"NumberOfInvalidImages" : wrong_number ,
2023-09-21 16:43:11 +01:00
} ) )
2023-09-27 21:20:39 +01:00
case READY :
LoadBasedOnAnswer ( c . Mode , w , "/models/edit.html" , c . AddMap ( AnyMap {
"Model" : model ,
} ) )
2023-10-20 12:37:56 +01:00
case TRAINING :
type defrow struct {
2023-10-24 22:35:11 +01:00
Id string
2023-10-20 12:37:56 +01:00
Status int
EpochProgress int
2023-10-24 22:35:11 +01:00
Epoch int
2023-10-21 00:26:52 +01:00
Accuracy float64
2023-10-20 12:37:56 +01:00
}
defs := [ ] defrow { }
2024-03-01 23:03:25 +00:00
if model . Type == 2 {
def_rows , err := c . Db . Query ( "select md.id, md.status, md.epoch, h.epoch_progress, h.accuracy from model_definition as md inner join exp_model_head as h on h.def_id = md.id where md.model_id=$1 order by md.created_on asc" , model . Id )
if err != nil {
return c . Error500 ( err )
}
defer def_rows . Close ( )
2024-02-10 12:14:26 +00:00
2024-03-01 23:03:25 +00:00
for def_rows . Next ( ) {
var def defrow
err = def_rows . Scan ( & def . Id , & def . Status , & def . Epoch , & def . EpochProgress , & def . Accuracy )
if err != nil {
return c . Error500 ( err )
}
defs = append ( defs , def )
}
} else {
def_rows , err := c . Db . Query ( "select id, status, epoch, epoch_progress, accuracy from model_definition where model_id=$1 order by created_on asc" , model . Id )
if err != nil {
return c . Error500 ( err )
}
defer def_rows . Close ( )
for def_rows . Next ( ) {
var def defrow
err = def_rows . Scan ( & def . Id , & def . Status , & def . Epoch , & def . EpochProgress , & def . Accuracy )
if err != nil {
return c . Error500 ( err )
}
defs = append ( defs , def )
}
}
2023-10-20 12:37:56 +01:00
2023-10-24 22:35:11 +01:00
type layerdef struct {
2024-02-08 18:20:58 +00:00
id string
2023-10-24 22:35:11 +01:00
LayerType int
Shape string
}
layers := [ ] layerdef { }
for _ , def := range defs {
if def . Status == MODEL_DEFINITION_STATUS_TRAINING {
2024-02-08 18:20:58 +00:00
rows , err := c . Db . Query ( "select id, layer_type, shape from model_definition_layer where def_id=$1 order by layer_order asc;" , def . Id )
2023-10-24 22:35:11 +01:00
if err != nil {
return c . Error500 ( err )
}
defer rows . Close ( )
for rows . Next ( ) {
var layerdef layerdef
2024-02-08 18:20:58 +00:00
err = rows . Scan ( & layerdef . id , & layerdef . LayerType , & layerdef . Shape )
2023-10-24 22:35:11 +01:00
if err != nil {
return c . Error500 ( err )
}
layers = append ( layers , layerdef )
}
2024-02-08 18:20:58 +00:00
if model . Type == 2 {
type lastLayerType struct {
Id string
Range_start int
Range_end int
}
2024-03-01 23:03:25 +00:00
var lastLayer lastLayerType
2024-02-08 18:20:58 +00:00
err := GetDBOnce ( c , & lastLayer , "exp_model_head where def_id=$1 and status=3;" , def . Id )
if err != nil {
return c . Error500 ( err )
}
layers = append ( layers , layerdef {
2024-03-01 23:03:25 +00:00
id : lastLayer . Id ,
2024-02-08 18:20:58 +00:00
LayerType : LAYER_DENSE ,
2024-03-01 23:03:25 +00:00
Shape : fmt . Sprintf ( "%d, 1" , lastLayer . Range_end - lastLayer . Range_start + 1 ) ,
2024-02-08 18:20:58 +00:00
} )
}
2023-10-24 22:35:11 +01:00
break
}
}
sep_mod := 100
if len ( layers ) > 8 {
sep_mod = 100 - ( len ( layers ) - 8 ) * 10
}
if sep_mod < 10 {
sep_mod = 10
}
2023-10-20 12:37:56 +01:00
LoadBasedOnAnswer ( c . Mode , w , "/models/edit.html" , c . AddMap ( AnyMap {
2023-10-24 22:35:11 +01:00
"Model" : model ,
"Defs" : defs ,
"Layers" : layers ,
"SepMod" : sep_mod ,
2023-10-20 12:37:56 +01:00
} ) )
case PREPARING_ZIP_FILE :
LoadBasedOnAnswer ( c . Mode , w , "/models/edit.html" , c . AddMap ( AnyMap {
"Model" : model ,
} ) )
2023-09-21 16:43:11 +01:00
default :
fmt . Printf ( "Unkown Status: %d\n" , model . Status )
return Error500 ( nil )
}
return nil
} )
2024-03-01 23:03:25 +00:00
2023-09-21 16:43:11 +01:00
}