fix task article link drawer
This commit is contained in:
@@ -54,27 +54,30 @@ type ScheduleTaskRequest struct {
|
||||
}
|
||||
|
||||
type ScheduleTaskResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
WorkspaceID *int64 `json:"workspace_id"`
|
||||
PromptRuleID int64 `json:"prompt_rule_id"`
|
||||
PromptRuleName *string `json:"prompt_rule_name"`
|
||||
BrandID *int64 `json:"brand_id"`
|
||||
BrandName *string `json:"brand_name"`
|
||||
Name string `json:"name"`
|
||||
CronExpr string `json:"cron_expr"`
|
||||
AutoPublish bool `json:"auto_publish"`
|
||||
PublishAccountIDs []string `json:"publish_account_ids"`
|
||||
AutoPublishPlatforms []string `json:"auto_publish_platforms"`
|
||||
CoverAssetURL *string `json:"cover_asset_url"`
|
||||
CoverImageAssetID *int64 `json:"cover_image_asset_id"`
|
||||
EnableWebSearch bool `json:"enable_web_search"`
|
||||
GenerateCount int `json:"generate_count"`
|
||||
StartAt *string `json:"start_at"`
|
||||
EndAt *string `json:"end_at"`
|
||||
NextRunAt *string `json:"next_run_at"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
ID int64 `json:"id"`
|
||||
WorkspaceID *int64 `json:"workspace_id"`
|
||||
PromptRuleID int64 `json:"prompt_rule_id"`
|
||||
PromptRuleName *string `json:"prompt_rule_name"`
|
||||
BrandID *int64 `json:"brand_id"`
|
||||
BrandName *string `json:"brand_name"`
|
||||
Name string `json:"name"`
|
||||
CronExpr string `json:"cron_expr"`
|
||||
AutoPublish bool `json:"auto_publish"`
|
||||
PublishAccountIDs []string `json:"publish_account_ids"`
|
||||
AutoPublishPlatforms []string `json:"auto_publish_platforms"`
|
||||
CoverAssetURL *string `json:"cover_asset_url"`
|
||||
CoverImageAssetID *int64 `json:"cover_image_asset_id"`
|
||||
EnableWebSearch bool `json:"enable_web_search"`
|
||||
GenerateCount int `json:"generate_count"`
|
||||
StartAt *string `json:"start_at"`
|
||||
EndAt *string `json:"end_at"`
|
||||
NextRunAt *string `json:"next_run_at"`
|
||||
Status string `json:"status"`
|
||||
GenerationStatus *string `json:"generation_status"`
|
||||
ExecutionTime *string `json:"execution_time"`
|
||||
GeneratedArticles []InstantTaskArticleLink `json:"generated_articles"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type ScheduleTaskListParams struct {
|
||||
@@ -404,6 +407,9 @@ func (s *ScheduleTaskService) loadScheduleTasks(ctx context.Context, tenantID in
|
||||
if err := s.populateScheduleAutoPublishPlatforms(ctx, tenantID, &item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.populateScheduleLatestGeneratedArticles(ctx, tenantID, &item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
return &ScheduleTaskListResponse{Items: items, Total: total}, nil
|
||||
@@ -432,6 +438,9 @@ func (s *ScheduleTaskService) loadScheduleTaskDetail(ctx context.Context, tenant
|
||||
if err := s.populateScheduleAutoPublishPlatforms(ctx, tenantID, &item); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if err := s.populateScheduleLatestGeneratedArticles(ctx, tenantID, &item); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return &item, true, nil
|
||||
}
|
||||
|
||||
@@ -447,6 +456,122 @@ func (s *ScheduleTaskService) populateScheduleAutoPublishPlatforms(ctx context.C
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ScheduleTaskService) populateScheduleLatestGeneratedArticles(ctx context.Context, tenantID int64, item *ScheduleTaskResponse) error {
|
||||
if item == nil || item.ID <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
WITH latest_run AS (
|
||||
SELECT COALESCE(NULLIF(gt.input_params_json ->> 'scheduled_for', ''), to_char(date_trunc('second', gt.created_at), 'YYYY-MM-DD"T"HH24:MI:SS"Z"')) AS run_key
|
||||
FROM generation_tasks gt
|
||||
WHERE gt.tenant_id = $1
|
||||
AND gt.task_type = 'custom_generation'
|
||||
AND COALESCE(NULLIF(gt.input_params_json ->> 'generation_mode', ''), '') = 'schedule'
|
||||
AND NULLIF(gt.input_params_json ->> 'schedule_task_id', '')::bigint = $2
|
||||
ORDER BY gt.created_at DESC, gt.id DESC
|
||||
LIMIT 1
|
||||
)
|
||||
SELECT gt.id,
|
||||
COALESCE(a.id, gt.article_id) AS article_id,
|
||||
COALESCE(av.title, NULLIF(a.wizard_state_json ->> 'title', ''), NULLIF(gt.input_params_json ->> 'title', ''), NULLIF(gt.input_params_json ->> 'task_name', '')) AS article_title,
|
||||
COALESCE(a.generate_status, gt.status) AS article_generate_status,
|
||||
COALESCE(a.created_at, gt.created_at) AS article_created_at,
|
||||
COALESCE(gt.started_at, gt.completed_at, gt.created_at) AS execution_time
|
||||
FROM generation_tasks gt
|
||||
JOIN latest_run lr ON COALESCE(NULLIF(gt.input_params_json ->> 'scheduled_for', ''), to_char(date_trunc('second', gt.created_at), 'YYYY-MM-DD"T"HH24:MI:SS"Z"')) = lr.run_key
|
||||
LEFT JOIN articles a ON a.id = gt.article_id AND a.deleted_at IS NULL
|
||||
LEFT JOIN article_versions av ON av.id = a.current_version_id
|
||||
WHERE gt.tenant_id = $1
|
||||
AND gt.task_type = 'custom_generation'
|
||||
AND COALESCE(NULLIF(gt.input_params_json ->> 'generation_mode', ''), '') = 'schedule'
|
||||
AND NULLIF(gt.input_params_json ->> 'schedule_task_id', '')::bigint = $2
|
||||
ORDER BY gt.created_at, gt.id
|
||||
`, tenantID, item.ID)
|
||||
if err != nil {
|
||||
return response.ErrInternal(50010, "query_failed", "failed to load schedule generated articles")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
articles := make([]InstantTaskArticleLink, 0)
|
||||
statuses := make([]string, 0)
|
||||
var executionTime *string
|
||||
for rows.Next() {
|
||||
var taskID int64
|
||||
var articleID *int64
|
||||
var title *string
|
||||
var generateStatus string
|
||||
var createdAt interface{}
|
||||
var executionAt interface{}
|
||||
if err := rows.Scan(&taskID, &articleID, &title, &generateStatus, &createdAt, &executionAt); err != nil {
|
||||
return response.ErrInternal(50010, "scan_failed", err.Error())
|
||||
}
|
||||
if executionTime == nil {
|
||||
executionTime = stringPtrFromDBValue(executionAt)
|
||||
}
|
||||
statuses = append(statuses, generateStatus)
|
||||
if articleID != nil && *articleID > 0 {
|
||||
articles = append(articles, InstantTaskArticleLink{
|
||||
ArticleID: *articleID,
|
||||
Title: title,
|
||||
GenerateStatus: generateStatus,
|
||||
CreatedAt: fmt.Sprintf("%v", createdAt),
|
||||
})
|
||||
} else {
|
||||
_ = taskID
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return response.ErrInternal(50010, "scan_failed", "failed to iterate schedule generated articles")
|
||||
}
|
||||
|
||||
item.GeneratedArticles = articles
|
||||
item.ExecutionTime = executionTime
|
||||
status := aggregateGeneratedArticleStatus(statuses)
|
||||
if status != "" {
|
||||
item.GenerationStatus = &status
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func aggregateGeneratedArticleStatus(statuses []string) string {
|
||||
if len(statuses) == 0 {
|
||||
return ""
|
||||
}
|
||||
completed := 0
|
||||
failed := 0
|
||||
running := 0
|
||||
queued := 0
|
||||
for _, status := range statuses {
|
||||
switch status {
|
||||
case "completed":
|
||||
completed++
|
||||
case "failed":
|
||||
failed++
|
||||
case "generating", "running":
|
||||
running++
|
||||
case "queued", "pending":
|
||||
queued++
|
||||
}
|
||||
}
|
||||
if running > 0 {
|
||||
return "running"
|
||||
}
|
||||
if queued > 0 {
|
||||
return "queued"
|
||||
}
|
||||
if completed > 0 && failed > 0 {
|
||||
return "partial_success"
|
||||
}
|
||||
if failed == len(statuses) {
|
||||
return "failed"
|
||||
}
|
||||
if completed == len(statuses) {
|
||||
return "completed"
|
||||
}
|
||||
return statuses[len(statuses)-1]
|
||||
}
|
||||
|
||||
func scanScheduleTaskResponse(scanner interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}) (ScheduleTaskResponse, error) {
|
||||
|
||||
Reference in New Issue
Block a user