fyp/logic/stats/tasks.go

96 lines
2.7 KiB
Go

package stats
import (
"time"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/utils"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
)
func handleTasksStats(handle *Handle) {
type ModelTasksStatsRequest struct {
ModelId string `json:"model_id" validate:"required"`
}
PostAuthJson(handle, "/stats/task/model/day", User_Normal, func(c *Context, dat *ModelTasksStatsRequest) *Error {
model, err := GetBaseModel(c, dat.ModelId)
if err == ModelNotFoundError {
return c.JsonBadRequest("Model not found!")
} else if err != nil {
return c.E500M("Failed to get model", err)
}
now := time.Now()
now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
tasks, err := GetDbMultitple[Task](c, "tasks where model_id=$1 and created_on > $2 order by created_on asc;", model.Id, now)
if err != nil {
return c.E500M("Failed to get task informations", err)
}
type DataPoint struct {
Non_classfication_tasks_su int `json:"nc_success"`
Non_classfication_tasks_err int `json:"nc_error"`
Classfication_su int `json:"c_success"`
Classfication_fal int `json:"c_failure"`
Classfication_un int `json:"c_unknown"`
Classfication_err int `json:"c_error"`
Classfication_pre int `json:"c_pre_running"`
Classfication_running int `json:"c_running"`
}
total := DataPoint{}
hours := make([]DataPoint, 24)
for i := 0; i < 24; i++ {
hours[i] = DataPoint{}
}
for i := range tasks {
task := tasks[i]
hour := task.CreatedOn.Hour()
if task.TaskType == int(TASK_TYPE_CLASSIFICATION) {
if task.Status == 4 {
if task.UserConfirmed == 1 {
total.Classfication_su += 1
hours[hour].Classfication_su += 1
} else if task.UserConfirmed == -1 {
total.Classfication_fal += 1
hours[hour].Classfication_fal += 1
} else {
total.Classfication_un += 1
hours[hour].Classfication_un += 1
}
} else if task.Status < 0 {
total.Classfication_err += 1
hours[hour].Classfication_err += 1
} else if task.Status < 2 {
total.Classfication_pre += 1
hours[hour].Classfication_pre += 1
} else if task.Status < 4 || task.Status == 5 {
total.Classfication_running += 1
hours[hour].Classfication_running += 1
}
} else {
if task.Status >= 0 {
total.Non_classfication_tasks_su += 1
hours[hour].Non_classfication_tasks_su += 1
} else {
total.Non_classfication_tasks_err += 1
hours[hour].Non_classfication_tasks_err += 1
}
}
}
data := struct {
Total DataPoint `json:"total"`
Hours []DataPoint `json:"hours"`
}{
Total: total,
Hours: hours,
}
return c.SendJSON(data)
})
}