closes #44
This commit is contained in:
		
							parent
							
								
									b5ba0d295a
								
							
						
					
					
						commit
						bc801648a3
					
				@ -6,6 +6,7 @@ import (
 | 
			
		||||
	"strconv"
 | 
			
		||||
 | 
			
		||||
	. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
 | 
			
		||||
	. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func HandleList(handle *Handle) {
 | 
			
		||||
@ -48,6 +49,11 @@ func HandleList(handle *Handle) {
 | 
			
		||||
            return Error500(nil)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        model, err := GetBaseModel(c.Db, model_id)
 | 
			
		||||
        if err != nil {
 | 
			
		||||
            return Error500(err)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        rows, err := handle.Db.Query("select id, file_path, model_mode, status from model_data_point where class_id=$1 limit 10 offset $2;", id, page * 10)
 | 
			
		||||
        if err != nil {
 | 
			
		||||
            return Error500(err)
 | 
			
		||||
@ -95,7 +101,7 @@ func HandleList(handle *Handle) {
 | 
			
		||||
            "Page": page,
 | 
			
		||||
            "Id": id,
 | 
			
		||||
            "Name": name,
 | 
			
		||||
            "ModelId": model_id,
 | 
			
		||||
            "Model": model,
 | 
			
		||||
        }))
 | 
			
		||||
        return nil
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
@ -24,7 +24,7 @@ func handleEdit(handle *Handle) {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// TODO handle admin users
 | 
			
		||||
		rows, err := handle.Db.Query("select name, status, width, height, color_mode from models where id=$1 and user_id=$2;", id, c.User.Id)
 | 
			
		||||
		rows, err := handle.Db.Query("select name, status, width, height, color_mode, format from models where id=$1 and user_id=$2;", id, c.User.Id)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return Error500(err)
 | 
			
		||||
		}
 | 
			
		||||
@ -44,12 +44,13 @@ func handleEdit(handle *Handle) {
 | 
			
		||||
			Width      *int
 | 
			
		||||
			Height     *int
 | 
			
		||||
			Color_mode *string
 | 
			
		||||
			Format     string
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		var model rowmodel = rowmodel{}
 | 
			
		||||
		model.Id = id
 | 
			
		||||
 | 
			
		||||
		err = rows.Scan(&model.Name, &model.Status, &model.Width, &model.Height, &model.Color_mode)
 | 
			
		||||
		err = rows.Scan(&model.Name, &model.Status, &model.Width, &model.Height, &model.Color_mode, &model.Format)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return Error500(err)
 | 
			
		||||
		}
 | 
			
		||||
@ -71,10 +72,14 @@ func handleEdit(handle *Handle) {
 | 
			
		||||
		case CONFIRM_PRE_TRAINING:
 | 
			
		||||
 | 
			
		||||
			wrong_number, err := model_classes.GetNumberOfWrongDataPoints(c.Db, model.Id)
 | 
			
		||||
            if err != nil { return c.Error500(err) }
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return c.Error500(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			cls, err := model_classes.ListClasses(handle.Db, id)
 | 
			
		||||
            if err != nil { return c.Error500(err) }
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return c.Error500(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			has_data, err := model_classes.ModelHasDataPoints(handle.Db, id)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
 | 
			
		||||
@ -387,8 +387,8 @@ func trainModel(c *Context, model *BaseModel) {
 | 
			
		||||
	ModelUpdateStatus(c, model.Id, READY)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func removeFailedDataPoints(db *sql.DB, model *BaseModel) (err error) {
 | 
			
		||||
	rows, err := db.Query("select mdp.id from model_data_point as mdp join model_classes as mc on mc.id=mdp.class_id where mc.model_id=$1 and mdp.status=-1;", model.Id)
 | 
			
		||||
func removeFailedDataPoints(c *Context, model *BaseModel) (err error) {
 | 
			
		||||
	rows, err := c.Db.Query("select mdp.id from model_data_point as mdp join model_classes as mc on mc.id=mdp.class_id where mc.model_id=$1 and mdp.status=-1;", model.Id)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
@ -402,13 +402,18 @@ func removeFailedDataPoints(db *sql.DB, model *BaseModel) (err error) {
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		err = os.RemoveAll(path.Join(base_path, dataPointId+model.Format))
 | 
			
		||||
 | 
			
		||||
        p := path.Join(base_path, dataPointId + "." + model.Format)
 | 
			
		||||
        
 | 
			
		||||
        c.Logger.Warn("Removing image", "path", p)
 | 
			
		||||
 | 
			
		||||
		err = os.RemoveAll(p)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = db.Exec("delete from model_data_point as mdp using model_classes as mc where mdp.class_id = mc.id and mc.model_id=$1 and mdp.status=-1;", model.Id)
 | 
			
		||||
	_, err = c.Db.Exec("delete from model_data_point as mdp using model_classes as mc where mdp.class_id = mc.id and mc.model_id=$1 and mdp.status=-1;", model.Id)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -471,7 +476,7 @@ func generateDefinitions(c *Context, model *BaseModel, number_of_models int) *Er
 | 
			
		||||
		return c.Error500(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = removeFailedDataPoints(c.Db, model)
 | 
			
		||||
	err = removeFailedDataPoints(c, model)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return c.Error500(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -16,16 +16,16 @@ import (
 | 
			
		||||
	"github.com/charmbracelet/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Mul (n1 int, n2 int) int {
 | 
			
		||||
func Mul(n1 int, n2 int) int {
 | 
			
		||||
	return n1 * n2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Add (n1 int, n2 int) int {
 | 
			
		||||
func Add(n1 int, n2 int) int {
 | 
			
		||||
	return n1 + n2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func baseLoadTemplate(base string, path string) (*template.Template, any) {
 | 
			
		||||
    funcs := map[string]any {
 | 
			
		||||
	funcs := map[string]any{
 | 
			
		||||
		"startsWith": strings.HasPrefix,
 | 
			
		||||
		"replace":    strings.Replace,
 | 
			
		||||
		"mul":        Mul,
 | 
			
		||||
@ -98,14 +98,14 @@ func LoadHtml(writer http.ResponseWriter, path string, data interface{}) {
 | 
			
		||||
 | 
			
		||||
func LoadDefineTemplate(writer http.ResponseWriter, path string, base string, data AnyMap) {
 | 
			
		||||
	if data == nil {
 | 
			
		||||
        data = map[string]interface{} {
 | 
			
		||||
		data = map[string]interface{}{
 | 
			
		||||
			"Error": true,
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		data["Error"] = true
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    funcs := map[string]any {
 | 
			
		||||
	funcs := map[string]any{
 | 
			
		||||
		"startsWith": strings.HasPrefix,
 | 
			
		||||
		"mul":        Mul,
 | 
			
		||||
		"replace":    strings.Replace,
 | 
			
		||||
@ -365,7 +365,7 @@ func (c Context) Error400(err error, message string, w http.ResponseWriter, path
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c Context) SetReportCaller(report bool) {
 | 
			
		||||
    if (report) {
 | 
			
		||||
	if report {
 | 
			
		||||
		c.Logger.SetCallerOffset(2)
 | 
			
		||||
		c.Logger.SetReportCaller(true)
 | 
			
		||||
	} else {
 | 
			
		||||
@ -375,7 +375,7 @@ func (c Context) SetReportCaller(report bool) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c Context) ErrorCode(err error, code int, data AnyMap) *Error {
 | 
			
		||||
    if (code == 400) {
 | 
			
		||||
	if code == 400 {
 | 
			
		||||
		c.SetReportCaller(true)
 | 
			
		||||
		c.Logger.Warn("When returning BadRequest(400) please use context.Error400\n")
 | 
			
		||||
		c.SetReportCaller(false)
 | 
			
		||||
@ -556,6 +556,42 @@ func (x Handle) ReadFiles(pathTest string, baseFilePath string, fileType string,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (x Handle) ReadTypesFiles(pathTest string, baseFilePath string, fileTypes []string, contentTypes []string) {
 | 
			
		||||
	http.HandleFunc(pathTest, func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		user_path := r.URL.Path[len(pathTest):]
 | 
			
		||||
 | 
			
		||||
		// fmt.Printf("Requested path: %s\n", user_path)
 | 
			
		||||
 | 
			
		||||
		found := false
 | 
			
		||||
        index := -1;
 | 
			
		||||
 | 
			
		||||
		for i, fileType := range fileTypes {
 | 
			
		||||
			if strings.HasSuffix(user_path, fileType) {
 | 
			
		||||
				found = true
 | 
			
		||||
                index = i;
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if !found {
 | 
			
		||||
			w.WriteHeader(http.StatusNotFound)
 | 
			
		||||
			w.Write([]byte("File not found"))
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		bytes, err := os.ReadFile(path.Join(baseFilePath, pathTest, user_path))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println(err)
 | 
			
		||||
			w.WriteHeader(http.StatusInternalServerError)
 | 
			
		||||
			w.Write([]byte("Failed to load file"))
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		w.Header().Set("Content-Type", contentTypes[index])
 | 
			
		||||
		w.Write(bytes)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHandler(db *sql.DB) *Handle {
 | 
			
		||||
 | 
			
		||||
	var gets []HandleFunc
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								main.go
									
									
									
									
									
								
							@ -34,17 +34,16 @@ func main() {
 | 
			
		||||
	//TODO check if file structure exists to save data
 | 
			
		||||
	handle := NewHandler(db)
 | 
			
		||||
 | 
			
		||||
    _, 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", models_utils.FAILED_TRAINING, models_utils.TRAINING)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	// TODO Handle this in other way
 | 
			
		||||
    handle.StaticFiles("/styles/", ".css", "text/css");
 | 
			
		||||
    handle.StaticFiles("/js/", ".js", "text/javascript");
 | 
			
		||||
    handle.ReadFiles("/imgs/", "views", ".png", "image/png;");
 | 
			
		||||
    handle.ReadFiles("/savedData/", ".", ".png", "image/png;");
 | 
			
		||||
	handle.StaticFiles("/styles/", ".css", "text/css")
 | 
			
		||||
	handle.StaticFiles("/js/", ".js", "text/javascript")
 | 
			
		||||
	handle.ReadFiles("/imgs/", "views", ".png", "image/png;")
 | 
			
		||||
	handle.ReadTypesFiles("/savedData/", ".", []string{".png", ".jpeg"}, []string{"image/png", "image/jpeg"})
 | 
			
		||||
 | 
			
		||||
	handle.GetHTML("/", AnswerTemplate("index.html", nil, 0))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -81,7 +81,7 @@
 | 
			
		||||
                    </td>
 | 
			
		||||
                    <td class="text-center">
 | 
			
		||||
                        {{ if startsWith .FilePath "id://" }}
 | 
			
		||||
                            <img src="/savedData/{{ $.ModelId }}/data/{{ .Id }}.png" height="30px" width="30px" style="object-fit: contain;" />
 | 
			
		||||
                            <img src="/savedData/{{ $.Model.Id }}/data/{{ .Id }}.{{ $.Model.Format }}" height="30px" width="30px" style="object-fit: contain;" />
 | 
			
		||||
                        {{ else }}
 | 
			
		||||
                            TODO
 | 
			
		||||
                            img {{ .FilePath }}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user