""" Operations Dashboard Data: task queue, human tasks, stats """ from sqlor.dbpools import DBPools async def main(): dbname = get_module_dbname('pipeline_core') async with DBPools().sqlorContext(dbname) as sor: # Task stats by state task_stats = await sor.sqlExe(""" SELECT state, COUNT(*) as cnt FROM pipeline_tasks GROUP BY state ORDER BY cnt DESC """, {}) stats = {'submitted':0, 'running':0, 'completed':0, 'failed':0, 'cancelled':0} for r in task_stats: stats[r.state] = r.cnt # Recent tasks (last 20) tasks = await sor.sqlExe(""" SELECT t.id, t.title, t.state, t.pipeline_id, t.created_at FROM pipeline_tasks t ORDER BY t.created_at DESC LIMIT 20 """, {}) task_list = [] for r in tasks: task_list.append({ 'id': r.id, 'title': r.title, 'state': r.state, 'pipeline_id': r.pipeline_id, 'created_at': str(r.created_at)[:19] }) # Pending human tasks human_tasks = await sor.sqlExe(""" SELECT h.id, h.task_id, h.task_type, h.step_name, h.status, h.assignee_role, h.created_at, h.expired_at, t.title as task_title FROM pipeline_human_tasks h LEFT JOIN pipeline_tasks t ON h.task_id = t.id WHERE h.status IN ('pending', 'in_progress') ORDER BY h.created_at DESC LIMIT 20 """, {}) human_list = [] for r in human_tasks: human_list.append({ 'id': r.id, 'task_id': r.task_id, 'task_type': r.task_type, 'step_name': r.step_name, 'status': r.status, 'assignee_role': r.assignee_role, 'task_title': r.task_title, 'created_at': str(r.created_at)[:19], 'expired_at': str(r.expired_at)[:19] if r.expired_at else None }) return { 'stats': stats, 'tasks': task_list, 'human_tasks': human_list, 'total_tasks': sum(stats.values()), 'active_tasks': stats['running'], 'pending_reviews': len(human_list) }