116 lines
3.6 KiB
Go
116 lines
3.6 KiB
Go
// Package orm define database data struct
|
|
package orm
|
|
|
|
import (
|
|
"github.com/gofrs/uuid"
|
|
)
|
|
|
|
// AsyncTaskType defines the type of asynchronous task
|
|
type AsyncTaskType string
|
|
|
|
const (
|
|
// AsyncTaskTypeTopologyAnalysis represents topology analysis task
|
|
AsyncTaskTypeTopologyAnalysis AsyncTaskType = "TOPOLOGY_ANALYSIS"
|
|
// AsyncTaskTypePerformanceAnalysis represents performance analysis task
|
|
AsyncTaskTypePerformanceAnalysis AsyncTaskType = "PERFORMANCE_ANALYSIS"
|
|
// AsyncTaskTypeEventAnalysis represents event analysis task
|
|
AsyncTaskTypeEventAnalysis AsyncTaskType = "EVENT_ANALYSIS"
|
|
// AsyncTaskTypeBatchImport represents batch import task
|
|
AsyncTaskTypeBatchImport AsyncTaskType = "BATCH_IMPORT"
|
|
)
|
|
|
|
// AsyncTaskStatus defines the status of asynchronous task
|
|
type AsyncTaskStatus string
|
|
|
|
const (
|
|
// AsyncTaskStatusSubmitted represents task has been submitted to queue
|
|
AsyncTaskStatusSubmitted AsyncTaskStatus = "SUBMITTED"
|
|
// AsyncTaskStatusRunning represents task is currently executing
|
|
AsyncTaskStatusRunning AsyncTaskStatus = "RUNNING"
|
|
// AsyncTaskStatusCompleted represents task completed successfully
|
|
AsyncTaskStatusCompleted AsyncTaskStatus = "COMPLETED"
|
|
// AsyncTaskStatusFailed represents task failed with error
|
|
AsyncTaskStatusFailed AsyncTaskStatus = "FAILED"
|
|
)
|
|
|
|
// AsyncTask defines the core task entity stored in database for task lifecycle tracking
|
|
type AsyncTask struct {
|
|
TaskID uuid.UUID `gorm:"column:task_id;primaryKey;type:uuid;default:gen_random_uuid()"`
|
|
TaskType AsyncTaskType `gorm:"column:task_type;type:varchar(50);not null"`
|
|
Status AsyncTaskStatus `gorm:"column:status;type:varchar(20);not null;index"`
|
|
Params JSONMap `gorm:"column:params;type:jsonb"`
|
|
CreatedAt int64 `gorm:"column:created_at;not null;index"`
|
|
FinishedAt *int64 `gorm:"column:finished_at;index"`
|
|
Progress *int `gorm:"column:progress"` // 0-100, nullable
|
|
}
|
|
|
|
// TableName returns the table name for AsyncTask model
|
|
func (a *AsyncTask) TableName() string {
|
|
return "async_task"
|
|
}
|
|
|
|
// SetSubmitted marks the task as submitted
|
|
func (a *AsyncTask) SetSubmitted() {
|
|
a.Status = AsyncTaskStatusSubmitted
|
|
}
|
|
|
|
// SetRunning marks the task as running
|
|
func (a *AsyncTask) SetRunning() {
|
|
a.Status = AsyncTaskStatusRunning
|
|
}
|
|
|
|
// SetCompleted marks the task as completed with finished timestamp
|
|
func (a *AsyncTask) SetCompleted(timestamp int64) {
|
|
a.Status = AsyncTaskStatusCompleted
|
|
a.FinishedAt = ×tamp
|
|
a.setProgress(100)
|
|
}
|
|
|
|
// SetFailed marks the task as failed with finished timestamp
|
|
func (a *AsyncTask) SetFailed(timestamp int64) {
|
|
a.Status = AsyncTaskStatusFailed
|
|
a.FinishedAt = ×tamp
|
|
}
|
|
|
|
// setProgress updates the task progress (0-100)
|
|
func (a *AsyncTask) setProgress(value int) {
|
|
if value < 0 {
|
|
value = 0
|
|
}
|
|
if value > 100 {
|
|
value = 100
|
|
}
|
|
a.Progress = &value
|
|
}
|
|
|
|
// UpdateProgress updates the task progress with validation
|
|
func (a *AsyncTask) UpdateProgress(value int) {
|
|
a.setProgress(value)
|
|
}
|
|
|
|
// IsCompleted checks if the task is completed
|
|
func (a *AsyncTask) IsCompleted() bool {
|
|
return a.Status == AsyncTaskStatusCompleted
|
|
}
|
|
|
|
// IsRunning checks if the task is running
|
|
func (a *AsyncTask) IsRunning() bool {
|
|
return a.Status == AsyncTaskStatusRunning
|
|
}
|
|
|
|
// IsFailed checks if the task failed
|
|
func (a *AsyncTask) IsFailed() bool {
|
|
return a.Status == AsyncTaskStatusFailed
|
|
}
|
|
|
|
// IsValidTaskType checks if the task type is valid
|
|
func IsValidAsyncTaskType(taskType string) bool {
|
|
switch AsyncTaskType(taskType) {
|
|
case AsyncTaskTypeTopologyAnalysis, AsyncTaskTypePerformanceAnalysis,
|
|
AsyncTaskTypeEventAnalysis, AsyncTaskTypeBatchImport:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|