wip: task controllers
This commit is contained in:
@ -6,6 +6,10 @@ type ESDoc struct {
|
||||
Content map[string]any `json:"_source"`
|
||||
}
|
||||
|
||||
func (d *ESDoc) ToMap() map[string]any {
|
||||
return map[string]any{"_id": d.DocId, "_index": d.Index, "_source": d.Content}
|
||||
}
|
||||
|
||||
type ESResponse struct {
|
||||
ScrollId string `json:"_scroll_id"`
|
||||
Took int `json:"took"`
|
||||
|
@ -17,4 +17,5 @@ type OpLogger interface {
|
||||
}
|
||||
|
||||
type TaskRow interface {
|
||||
Preview() map[string]any
|
||||
}
|
||||
|
@ -3,6 +3,9 @@ package model
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/loveuer/nfflow/internal/sqlType"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type InputType int64
|
||||
@ -21,6 +24,42 @@ const (
|
||||
OutputTypeMQ
|
||||
)
|
||||
|
||||
type PipeType int64
|
||||
|
||||
func (p PipeType) Value() int64 { return int64(p) }
|
||||
|
||||
func (p PipeType) Code() string {
|
||||
switch p {
|
||||
case PipeTypeLoadashMap:
|
||||
return "pipe_map"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func (p PipeType) Label() string {
|
||||
switch p {
|
||||
case PipeTypeLoadashMap:
|
||||
return "Map"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
func (p PipeType) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{"value": p.Value(), "code": p.Code(), "label": p.Label()})
|
||||
}
|
||||
|
||||
func (p PipeType) All() []Enum {
|
||||
return []Enum{PipeTypeLoadashMap}
|
||||
}
|
||||
|
||||
const (
|
||||
PipeTypeLoadashMap PipeType = iota + 1
|
||||
)
|
||||
|
||||
var _ Enum = PipeType(0)
|
||||
|
||||
type TaskStatus int64
|
||||
|
||||
func (t TaskStatus) MarshalJSON() ([]byte, error) {
|
||||
@ -150,27 +189,90 @@ type Task struct {
|
||||
TaskLog string `json:"task_log" gorm:"task_log"`
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
||||
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
|
||||
DeletedAt int64 `json:"deleted_at" gorm:"index;column:deleted_at;default:0"`
|
||||
InputType InputType `json:"input_type"`
|
||||
InputConfig sqlType.JSONB `json:"input_config"`
|
||||
type TaskInput struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
TaskId uint64 `json:"task_id" gorm:"column:task_id"`
|
||||
Type InputType `json:"type" gorm:"column:type"`
|
||||
Config sqlType.JSONB `json:"config" gorm:"config"`
|
||||
}
|
||||
|
||||
type Output struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
||||
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
|
||||
DeletedAt int64 `json:"deleted_at" gorm:"index;column:deleted_at;default:0"`
|
||||
OutputType OutputType
|
||||
OutputConfig sqlType.JSONB
|
||||
func (t *Task) GetInput(tx *gorm.DB) (*TaskInput, error) {
|
||||
var (
|
||||
err error
|
||||
ti = new(TaskInput)
|
||||
)
|
||||
|
||||
if err = tx.Model(&TaskInput{}).
|
||||
Where("task_id", t.Id).
|
||||
Take(ti).
|
||||
Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ti, nil
|
||||
}
|
||||
|
||||
type Pipe struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
||||
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
|
||||
DeletedAt int64 `json:"deleted_at" gorm:"index;column:deleted_at;default:0"`
|
||||
type TaskOutput struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
TaskId uint64 `json:"task_id" gorm:"column:task_id"`
|
||||
Type OutputType `json:"type" gorm:"column:type"`
|
||||
Config sqlType.JSONB `json:"config" gorm:"config"`
|
||||
}
|
||||
|
||||
func (t *Task) GetOutputs(tx *gorm.DB) ([]*TaskOutput, error) {
|
||||
var (
|
||||
err error
|
||||
outputs = make([]*TaskOutput, 0)
|
||||
)
|
||||
|
||||
if err = tx.Model(&TaskOutput{}).
|
||||
Where("task_id", t.Id).
|
||||
Find(&outputs).
|
||||
Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return outputs, nil
|
||||
}
|
||||
|
||||
type TaskPipe struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
TaskId uint64 `json:"task_id" gorm:"column:task_id"`
|
||||
Pid uint64 `json:"pid" gorm:"column:pid"`
|
||||
Type PipeType `json:"type" gorm:"column:type"`
|
||||
Config sqlType.JSONB `json:"config" gorm:"config"`
|
||||
Next []*TaskPipe `json:"next" gorm:"-"`
|
||||
}
|
||||
|
||||
func (t *Task) GetPipes(tx *gorm.DB) ([]*TaskPipe, error) {
|
||||
var (
|
||||
err error
|
||||
list = make([]*TaskPipe, 0)
|
||||
)
|
||||
|
||||
if err = tx.Model(&TaskPipe{}).
|
||||
Where("task_id", t.Id).
|
||||
Find(&list).
|
||||
Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := lo.SliceToMap(list, func(item *TaskPipe) (uint64, *TaskPipe) {
|
||||
item.Next = make([]*TaskPipe, 0)
|
||||
return item.Id, item
|
||||
})
|
||||
|
||||
m[0] = &TaskPipe{Next: make([]*TaskPipe, 0)}
|
||||
|
||||
for idx := range list {
|
||||
x := list[idx]
|
||||
if _, exist := m[x.Pid]; !exist {
|
||||
logrus.Warnf("GetPipes: pipe=[task_id=%d id=%d pid=%d type=%s] pid not found", x.TaskId, x.Id, x.Pid, x.Type.Code())
|
||||
continue
|
||||
}
|
||||
|
||||
m[x.Pid].Next = append(m[x.Pid].Next, x)
|
||||
}
|
||||
|
||||
return m[0].Next, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user