Spaces:
Running
Running
File size: 5,900 Bytes
db5f035 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | # Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Schedule API routes."""
from uuid import UUID
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from src.core.auth import resolve_db_user_id
from src.database import get_db
from src.schemas.schedule import (
ScheduleCreate,
ScheduleListResponse,
ScheduleResponse,
ScheduleUpdate,
)
from src.services.scheduler import SchedulerService
router = APIRouter(prefix="/schedules", tags=["schedules"])
@router.get("/", response_model=ScheduleListResponse)
async def list_my_schedules(
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> ScheduleListResponse:
service = SchedulerService(db)
schedules, total = await service.list_my_schedules(owner_id)
return ScheduleListResponse(schedules=schedules, total=total)
@router.post("/", response_model=ScheduleResponse, status_code=201)
async def create_schedule(
data: ScheduleCreate,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> ScheduleResponse:
service = SchedulerService(db)
return await service.create_schedule(owner_id, data)
@router.get("/estimate-cost")
async def estimate_workflow_cost(
workflow_id: UUID,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> dict:
"""Estimate the credit cost of running a workflow."""
from src.services.workflow_service import WorkflowService
from src.services.workflow_execution import WorkflowExecutionService
wf_svc = WorkflowService(db)
wf = await wf_svc.get_workflow(workflow_id)
engine = WorkflowExecutionService(db)
total = engine._estimate_cost(wf)
steps = []
for step in wf.steps or []:
step_cost = 1
if step.skill and step.skill.avg_credits:
step_cost = int(step.skill.avg_credits)
elif step.agent and step.agent.pricing:
step_cost = step.agent.pricing.get("credits", 1)
steps.append({
"agent_name": step.agent.name if step.agent else None,
"skill_name": step.skill.name if step.skill else None,
"estimated_credits": step_cost,
})
return {
"workflow_id": str(workflow_id),
"workflow_name": wf.name,
"estimated_credits": total,
"steps": steps,
}
@router.get("/{schedule_id}", response_model=ScheduleResponse)
async def get_schedule(
schedule_id: UUID,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> ScheduleResponse:
service = SchedulerService(db)
schedule = await service.get_schedule(schedule_id)
if schedule.owner_id != owner_id:
from src.core.exceptions import ForbiddenError
raise ForbiddenError("You do not own this schedule")
return schedule
@router.get("/{schedule_id}/runs")
async def list_schedule_runs(
schedule_id: UUID,
page: int = 1,
per_page: int = 20,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> dict:
"""List workflow runs triggered by this schedule."""
service = SchedulerService(db)
schedule = await service.get_schedule(schedule_id)
if schedule.owner_id != owner_id:
from src.core.exceptions import ForbiddenError
raise ForbiddenError("You do not own this schedule")
from src.models.workflow import WorkflowRun
from sqlalchemy import func, select
count_stmt = select(func.count()).select_from(WorkflowRun).where(
WorkflowRun.schedule_id == schedule_id
)
total = (await db.execute(count_stmt)).scalar_one()
offset = (page - 1) * per_page
stmt = (
select(WorkflowRun)
.where(WorkflowRun.schedule_id == schedule_id)
.order_by(WorkflowRun.created_at.desc())
.offset(offset)
.limit(per_page)
)
result = await db.execute(stmt)
runs = list(result.scalars().all())
return {
"runs": [
{
"id": str(r.id),
"status": r.status,
"credits_spent": float(r.total_credits_charged or 0),
"started_at": r.created_at.isoformat() if r.created_at else None,
"completed_at": r.completed_at.isoformat() if r.completed_at else None,
"error": r.error,
}
for r in runs
],
"total": total,
"page": page,
"per_page": per_page,
}
@router.put("/{schedule_id}", response_model=ScheduleResponse)
async def update_schedule(
schedule_id: UUID,
data: ScheduleUpdate,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> ScheduleResponse:
service = SchedulerService(db)
return await service.update_schedule(schedule_id, owner_id, data)
@router.delete("/{schedule_id}", status_code=204)
async def delete_schedule(
schedule_id: UUID,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> None:
service = SchedulerService(db)
await service.delete_schedule(schedule_id, owner_id)
@router.post("/{schedule_id}/pause", response_model=ScheduleResponse)
async def pause_schedule(
schedule_id: UUID,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> ScheduleResponse:
service = SchedulerService(db)
return await service.pause_schedule(schedule_id, owner_id)
@router.post("/{schedule_id}/resume", response_model=ScheduleResponse)
async def resume_schedule(
schedule_id: UUID,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> ScheduleResponse:
service = SchedulerService(db)
return await service.resume_schedule(schedule_id, owner_id)
|