A small FastAPI service for optimal task selection over time intervals using the Weighted Interval Scheduling algorithm.
The repository is named Interval-Scheduler-API, while the packaged Python project and CLI are named smart-scheduler.
This project lets you submit tasks with:
- a unique ID
- a start time
- an end time
- a priority score
- optional metadata
It then computes the best non-overlapping schedule that maximizes total priority.
The service is designed as a lightweight scheduling API with:
- a FastAPI HTTP interface
- an in-memory task store
- a reusable scheduling algorithm module
- unit tests for both the algorithm and API behavior
- Weighted interval scheduling for optimal non-overlapping task selection
- FastAPI-based REST API
- In-memory thread-safe task storage
- Preview scheduling without persisting tasks
- Automatic UUID generation when
task_idis omitted - Validation for invalid intervals and negative priorities
- Timezone-aware datetime enforcement at the API layer
- Python 3.11+
- FastAPI
- Uvicorn
- Pytest
Interval-Scheduler-API/
├── pyproject.toml
├── src/
│ └── smart_scheduler/
│ ├── __init__.py
│ ├── algo.py # weighted interval scheduling implementation
│ ├── api.py # FastAPI app and request/response models
│ ├── cli.py # CLI entrypoint for running the API server
│ ├── models.py # task domain model and validation
│ └── store.py # thread-safe in-memory task store
└── tests/
├── test_algo.py
└── test_api.py
Each task represents a fixed time interval with a weight:
task_id: strstart: datetimeend: datetimepriority: intmeta: dict | null
Intervals are treated as half-open:
[start, end)
That means a task ending at 11:00 is compatible with another task starting at 11:00.
The core solver implements Weighted Interval Scheduling:
- Sort tasks by end time
- For each task, find the latest compatible previous task
- Use dynamic programming to compute the maximum achievable priority
- Reconstruct the chosen task set
This gives an optimal solution for the non-overlapping subset under the current priority model.
Create a virtual environment and install the package in editable mode:
python3.11 -m venv .venv
source .venv/bin/activate
python -m pip install -U pip
python -m pip install -e ".[dev]"If python3.11 is not available on your system, use your local Python 3.11+ interpreter instead.
Start the development server using the installed CLI:
smart-scheduler --host 127.0.0.1 --port 8000You can also run Uvicorn directly:
uvicorn smart_scheduler.api:app --host 127.0.0.1 --port 8000Once running, the API is typically available at:
http://127.0.0.1:8000
Returns a simple health check response.
Example response:
{
"status": "ok"
}Create or upsert a task in the in-memory store.
Request body:
{
"task_id": "T1",
"start": "2026-01-16T10:00:00+05:30",
"end": "2026-01-16T11:00:00+05:30",
"priority": 10,
"meta": {
"type": "meeting"
}
}Notes:
task_idis optional; if omitted, the API generates a UUID- API requests require timezone-aware datetimes
endmust be strictly afterstartprioritymust be non-negative
List all stored tasks.
Delete a stored task by ID.
Run scheduling against the currently stored tasks.
Example response:
{
"total_priority": 19,
"tasks": [
{
"task_id": "T1",
"start": "2026-01-16T10:00:00+05:30",
"end": "2026-01-16T11:00:00+05:30",
"priority": 10,
"meta": null
},
{
"task_id": "T3",
"start": "2026-01-16T11:00:00+05:30",
"end": "2026-01-16T12:00:00+05:30",
"priority": 9,
"meta": null
}
]
}Run scheduling on an ad hoc payload without writing tasks into the store.
Request body:
{
"tasks": [
{
"task_id": "A",
"start": "2026-01-16T10:00:00+05:30",
"end": "2026-01-16T11:00:00+05:30",
"priority": 10
},
{
"task_id": "B",
"start": "2026-01-16T10:30:00+05:30",
"end": "2026-01-16T12:00:00+05:30",
"priority": 15
},
{
"task_id": "C",
"start": "2026-01-16T12:00:00+05:30",
"end": "2026-01-16T13:00:00+05:30",
"priority": 9
}
]
}Add a few tasks:
curl -X POST http://127.0.0.1:8000/tasks \
-H "Content-Type: application/json" \
-d '{
"task_id": "T1",
"start": "2026-01-16T10:00:00+05:30",
"end": "2026-01-16T11:00:00+05:30",
"priority": 10
}'curl -X POST http://127.0.0.1:8000/tasks \
-H "Content-Type: application/json" \
-d '{
"task_id": "T2",
"start": "2026-01-16T10:30:00+05:30",
"end": "2026-01-16T12:00:00+05:30",
"priority": 15
}'curl -X POST http://127.0.0.1:8000/tasks \
-H "Content-Type: application/json" \
-d '{
"task_id": "T3",
"start": "2026-01-16T12:00:00+05:30",
"end": "2026-01-16T13:00:00+05:30",
"priority": 9
}'Then compute the optimal schedule:
curl -X POST http://127.0.0.1:8000/scheduleExpected behavior:
T1andT3can both be selected together for total priority19T2conflicts withT1, so the algorithm chooses the best compatible combination
Install dependencies:
python -m pip install -e ".[dev]"Run tests:
pytestThe codebase has a solid structure for a small scheduling service, but the current checked-in revision has an issue in src/smart_scheduler/algo.py:
- the file currently contains an indentation error
- because of that, the package cannot be imported successfully in its current state
- test collection fails until that syntax issue is fixed
So the project design, API layout, and tests are present, but the current snapshot needs a small code correction before the service can run successfully.
- Storage is in-memory only; data is lost on restart
- No authentication or authorization is included
- No persistence layer or database integration is implemented
- No recurring-task support is present
- No resource constraints beyond time overlap are modeled
- API scheduling is single-objective: maximize total priority
- add persistent storage with SQLite or PostgreSQL
- expose update endpoints explicitly
- add pagination and filtering for task listing
- support recurring tasks and calendars
- add OpenAPI examples and richer validation messages
- fix the current indentation issue and add CI to prevent syntax regressions