File size: 952 Bytes
7a0a17c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

FastAPI app for the scheduling optimizer.

"""

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional

from solver import solve_single_machine

app = FastAPI(title="Scheduling Optimizer API", version="0.1.0")


class Task(BaseModel):
    id: str
    duration: int = 1


class ScheduleRequest(BaseModel):
    tasks: List[Task]
    horizon: Optional[int] = 10000


@app.get("/health")
def health():
    return {"status": "ok"}


@app.post("/schedule")
def schedule(req: ScheduleRequest):
    tasks = [{"id": t.id, "duration": t.duration} for t in req.tasks]
    if not tasks:
        raise HTTPException(status_code=400, detail="tasks must not be empty")
    result = solve_single_machine(tasks, horizon=req.horizon or 10000)
    if "error" in result:
        raise HTTPException(status_code=422, detail=result.get("error", "Solver failed"))
    return result