feat: migrate task jobs to workflow notifications + timeline
This commit is contained in:
150
components/notifications/task-stage-helpers.ts
Normal file
150
components/notifications/task-stage-helpers.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
import type { Task, TaskStage, TaskStageEvent, TaskType } from '@/lib/types';
|
||||
|
||||
export type StageTimelineItem = {
|
||||
stage: TaskStage;
|
||||
label: string;
|
||||
state: 'completed' | 'active' | 'pending';
|
||||
detail: string | null;
|
||||
timestamp: string | null;
|
||||
};
|
||||
|
||||
const TASK_TYPE_LABELS: Record<TaskType, string> = {
|
||||
sync_filings: 'Filing sync',
|
||||
refresh_prices: 'Price refresh',
|
||||
analyze_filing: 'Filing analysis',
|
||||
portfolio_insights: 'Portfolio insight'
|
||||
};
|
||||
|
||||
const STAGE_LABELS: Record<TaskStage, string> = {
|
||||
queued: 'Queued',
|
||||
running: 'Running',
|
||||
completed: 'Completed',
|
||||
failed: 'Failed',
|
||||
'sync.fetch_filings': 'Fetch filings',
|
||||
'sync.fetch_metrics': 'Fetch filing metrics',
|
||||
'sync.persist_filings': 'Persist filings',
|
||||
'sync.hydrate_statements': 'Hydrate statements',
|
||||
'refresh.load_holdings': 'Load holdings',
|
||||
'refresh.fetch_quotes': 'Fetch quotes',
|
||||
'refresh.persist_prices': 'Persist prices',
|
||||
'analyze.load_filing': 'Load filing',
|
||||
'analyze.fetch_document': 'Fetch primary document',
|
||||
'analyze.extract': 'Extract context',
|
||||
'analyze.generate_report': 'Generate report',
|
||||
'analyze.persist_report': 'Persist report',
|
||||
'insights.load_holdings': 'Load holdings',
|
||||
'insights.generate': 'Generate insight',
|
||||
'insights.persist': 'Persist insight'
|
||||
};
|
||||
|
||||
const TASK_STAGE_ORDER: Record<TaskType, TaskStage[]> = {
|
||||
sync_filings: [
|
||||
'queued',
|
||||
'running',
|
||||
'sync.fetch_filings',
|
||||
'sync.fetch_metrics',
|
||||
'sync.persist_filings',
|
||||
'sync.hydrate_statements',
|
||||
'completed'
|
||||
],
|
||||
refresh_prices: [
|
||||
'queued',
|
||||
'running',
|
||||
'refresh.load_holdings',
|
||||
'refresh.fetch_quotes',
|
||||
'refresh.persist_prices',
|
||||
'completed'
|
||||
],
|
||||
analyze_filing: [
|
||||
'queued',
|
||||
'running',
|
||||
'analyze.load_filing',
|
||||
'analyze.fetch_document',
|
||||
'analyze.extract',
|
||||
'analyze.generate_report',
|
||||
'analyze.persist_report',
|
||||
'completed'
|
||||
],
|
||||
portfolio_insights: [
|
||||
'queued',
|
||||
'running',
|
||||
'insights.load_holdings',
|
||||
'insights.generate',
|
||||
'insights.persist',
|
||||
'completed'
|
||||
]
|
||||
};
|
||||
|
||||
export function taskTypeLabel(taskType: TaskType) {
|
||||
return TASK_TYPE_LABELS[taskType];
|
||||
}
|
||||
|
||||
export function stageLabel(stage: TaskStage) {
|
||||
return STAGE_LABELS[stage] ?? stage;
|
||||
}
|
||||
|
||||
export function buildStageTimeline(task: Task, events: TaskStageEvent[]): StageTimelineItem[] {
|
||||
const baseOrder = TASK_STAGE_ORDER[task.task_type] ?? ['queued', 'running', 'completed'];
|
||||
const orderedStages = [...baseOrder];
|
||||
|
||||
if (task.status === 'failed' && !orderedStages.includes('failed')) {
|
||||
orderedStages.push('failed');
|
||||
}
|
||||
|
||||
const latestEventByStage = new Map<TaskStage, TaskStageEvent>();
|
||||
for (const event of events) {
|
||||
latestEventByStage.set(event.stage, event);
|
||||
}
|
||||
|
||||
return orderedStages.map((stage) => {
|
||||
const event = latestEventByStage.get(stage);
|
||||
|
||||
if (task.status === 'queued' || task.status === 'running') {
|
||||
if (stage === task.stage) {
|
||||
return {
|
||||
stage,
|
||||
label: stageLabel(stage),
|
||||
state: 'active' as const,
|
||||
detail: event?.stage_detail ?? task.stage_detail,
|
||||
timestamp: event?.created_at ?? null
|
||||
};
|
||||
}
|
||||
|
||||
if (event) {
|
||||
return {
|
||||
stage,
|
||||
label: stageLabel(stage),
|
||||
state: 'completed' as const,
|
||||
detail: event.stage_detail,
|
||||
timestamp: event.created_at
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
stage,
|
||||
label: stageLabel(stage),
|
||||
state: 'pending' as const,
|
||||
detail: null,
|
||||
timestamp: null
|
||||
};
|
||||
}
|
||||
|
||||
if (stage === task.stage || event) {
|
||||
return {
|
||||
stage,
|
||||
label: stageLabel(stage),
|
||||
state: 'completed' as const,
|
||||
detail: event?.stage_detail ?? task.stage_detail,
|
||||
timestamp: event?.created_at ?? task.finished_at
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
stage,
|
||||
label: stageLabel(stage),
|
||||
state: 'pending' as const,
|
||||
detail: null,
|
||||
timestamp: null
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user