fyp/logic/tasks/utils/utils.go
Andre Henriques 0ac6ac8dce runner-go (#102)
Reviewed-on: #102
Co-authored-by: Andre Henriques <andr3h3nriqu3s@gmail.com>
Co-committed-by: Andre Henriques <andr3h3nriqu3s@gmail.com>
2024-05-10 02:13:02 +01:00

123 lines
3.7 KiB
Go

package tasks_utils
import (
"time"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
"github.com/goccy/go-json"
)
type Task struct {
Id string `db:"id" json:"id"`
UserId string `db:"user_id" json:"user_id"`
ModelId *string `db:"model_id" json:"model_id"`
Status int `db:"status" json:"status"`
StatusMessage string `db:"status_message" json:"status_message"`
UserConfirmed int `db:"user_confirmed" json:"user_confirmed"`
Compacted int `db:"compacted" json:"compacted"`
TaskType int `db:"task_type" json:"type"`
ExtraTaskInfo string `db:"extra_task_info" json:"extra_task_info"`
Result string `db:"result" json:"result"`
CreatedOn time.Time `db:"created_on" json:"created"`
}
// Find better way todo this
type TaskT struct {
Id string `db:"t.id" json:"id"`
UserId string `db:"t.user_id" json:"user_id"`
ModelId *string `db:"t.model_id" json:"model_id"`
Status int `db:"t.status" json:"status"`
StatusMessage string `db:"t.status_message" json:"status_message"`
UserConfirmed int `db:"t.user_confirmed" json:"user_confirmed"`
Compacted int `db:"t.compacted" json:"compacted"`
TaskType int `db:"t.task_type" json:"type"`
ExtraTaskInfo string `db:"t.extra_task_info" json:"extra_task_info"`
Result string `db:"t.result" json:"result"`
CreatedOn time.Time `db:"t.created_on" json:"created"`
}
type TaskDependents struct {
Id string `db:"id" json:"id"`
MainId string `db:"main_id" json:"main_id"`
DependentId string `db:"dependent_id" json:"dependent_id"`
}
type TaskStatus int
const (
TASK_FAILED_RUNNING TaskStatus = -2
TASK_FAILED_CREATION = -1
TASK_PREPARING = 0
TASK_TODO = 1
TASK_PICKED_UP = 2
TASK_RUNNING = 3
TASK_DONE = 4
)
type TaskType int
const (
TASK_TYPE_CLASSIFICATION TaskType = 1 + iota
TASK_TYPE_TRAINING
TASK_TYPE_RETRAINING
TASK_TYPE_DELETE_USER
)
type TaskAgreement int
const (
TASK_AGREE_DISAGRE TaskAgreement = -1 + iota
TASK_AGREE_NONE
TASK_AGREE_AGREE
)
func (t Task) UpdateStatus(base BasePack, status TaskStatus, message string) (err error) {
return UpdateTaskStatus(base, t.Id, status, message)
}
func (t Task) SetAgreement(base BasePack, agreement TaskAgreement) (err error) {
_, err = base.GetDb().Exec("update tasks set user_confirmed=$1 where id=$2", agreement, t.Id)
return
}
/**
* Call the UpdateStatus function and logs on the case of failure!
* This varient does not return any error message
*/
func (t Task) UpdateStatusLog(base BasePack, status TaskStatus, message string) {
err := t.UpdateStatus(base, status, message)
if err != nil {
base.GetLogger().Error("Failed to update task status", "error", err, "task", t.Id)
}
}
func UpdateTaskStatus(base BasePack, id string, status TaskStatus, message string) (err error) {
_, err = base.GetDb().Exec("update tasks set status=$1, status_message=$2 where id=$3", status, message, id)
return
}
func (t Task) SetResult(base BasePack, result any) (err error) {
text, err := json.Marshal(result)
if err != nil {
return
}
return t.SetResultText(base, string(text))
}
func (t Task) SetResultText(base BasePack, text string) (err error) {
_, err = base.GetDb().Exec("update tasks set result=$1 where id=$2", []byte(text), t.Id)
return
}
func (t Task) Depend(base BasePack, depend_id string) (err error) {
var dependency = struct {
Main string `db:"main_id"`
Dependent string `db:"dependent_id"`
}{
Main: t.Id,
Dependent: depend_id,
}
_, err = InsertReturnId(base.GetDb(), &dependency, "tasks_dependencies", "id")
return
}