87 lines
3.6 KiB
Go
87 lines
3.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type TaskService struct {
|
|
uc *biz.TaskUsecase
|
|
}
|
|
|
|
func NewTaskService(uc *biz.TaskUsecase) *TaskService {
|
|
return &TaskService{uc: uc}
|
|
}
|
|
func taskDomain(v *dto.TaskRequest) *biz.TimedTask {
|
|
return &biz.TimedTask{ID: v.ID, Name: v.Name, Description: v.Description, Spec: v.Spec, WithSeconds: v.WithSeconds, ExecutorType: v.ExecutorType, MethodName: v.MethodName, Params: v.Params, HTTPURL: v.HTTPURL, HTTPMethod: v.HTTPMethod, HTTPHeader: v.HTTPHeader, HTTPBody: v.HTTPBody, HTTPAllowPrivate: v.HTTPAllowPrivate, Enabled: v.Enabled}
|
|
}
|
|
func (s *TaskService) CreateRequest(ctx context.Context, req *dto.TaskRequest) (uint, error) {
|
|
value := taskDomain(req)
|
|
if err := s.Create(ctx, value); err != nil {
|
|
return 0, err
|
|
}
|
|
return value.ID, nil
|
|
}
|
|
func (s *TaskService) UpdateRequest(ctx context.Context, req *dto.TaskRequest) error {
|
|
return s.Update(ctx, taskDomain(req))
|
|
}
|
|
func (s *TaskService) ListRequest(ctx context.Context, page, size int, name, executorType string, enabled *bool, next map[uint]time.Time) ([]map[string]any, int64, error) {
|
|
return s.Tasks(ctx, page, size, &biz.TimedTask{Name: name, ExecutorType: executorType, EnabledFilter: enabled}, next)
|
|
}
|
|
func (s *TaskService) Create(ctx context.Context, v *biz.TimedTask) error {
|
|
return s.uc.Create(ctx, v)
|
|
}
|
|
func (s *TaskService) Update(ctx context.Context, v *biz.TimedTask) error {
|
|
return s.uc.Update(ctx, v)
|
|
}
|
|
func (s *TaskService) Delete(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteTask(ctx, id)
|
|
}
|
|
func (s *TaskService) Toggle(ctx context.Context, id uint, enabled bool) error {
|
|
return s.uc.ToggleTask(ctx, id, enabled)
|
|
}
|
|
func taskDTO(v *biz.TimedTask, next *time.Time) map[string]any {
|
|
return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "name": v.Name, "description": v.Description, "spec": v.Spec, "withSeconds": v.WithSeconds, "executorType": v.ExecutorType, "methodName": v.MethodName, "params": json.RawMessage(v.Params), "httpUrl": v.HTTPURL, "httpMethod": v.HTTPMethod, "httpHeader": json.RawMessage(v.HTTPHeader), "httpBody": v.HTTPBody, "httpAllowPrivate": v.HTTPAllowPrivate, "enabled": v.Enabled, "nextRunAt": next}
|
|
}
|
|
func (s *TaskService) Tasks(ctx context.Context, page, size int, q *biz.TimedTask, next map[uint]time.Time) ([]map[string]any, int64, error) {
|
|
items, total, err := s.uc.ListTasks(ctx, page, size, q)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
var ptr *time.Time
|
|
if value, ok := next[v.ID]; ok {
|
|
copy := value
|
|
ptr = ©
|
|
}
|
|
out = append(out, taskDTO(v, ptr))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
|
|
func (s *TaskService) Logs(ctx context.Context, page, size int, taskID uint, status string) ([]map[string]any, int64, error) {
|
|
items, total, err := s.uc.ListTaskLogs(ctx, page, size, taskID, status)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "DeletedAt": nil, "taskId": v.TaskID, "taskName": v.TaskName, "triggerType": v.TriggerType, "startedAt": v.StartedAt, "finishedAt": v.FinishedAt, "durationMs": v.DurationMS, "status": v.Status, "errorMsg": v.ErrorMsg, "output": v.Output})
|
|
}
|
|
return out, total, nil
|
|
}
|
|
|
|
func (s *TaskService) RegisteredMethods() []map[string]any {
|
|
methods := biz.RegisteredTaskMethods()
|
|
out := make([]map[string]any, 0, len(methods))
|
|
for _, method := range methods {
|
|
out = append(out, map[string]any{"name": method.Name, "description": method.Description})
|
|
}
|
|
return out
|
|
}
|