# nikotpab/class-scheduling-engine architecture

> Architecture diagram of the GitHub repository nikotpab/class-scheduling-engine, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nikotpab/class-scheduling-engine
- Repository: https://github.com/nikotpab/class-scheduling-engine

## Overview

1. **Overall shape**  
   This is a backend microservice for academic timetable generation. The codebase is organized as a clean, layered architecture: API at the edge, application use cases in the middle, a domain model that holds the scheduling rules, and infrastructure adapters for database, solver, queueing, and WebSockets.

2. **Request entrypoints**  
   FastAPI is the HTTP entry layer in `src/api`. The main routes are:
   - `schedules` for job submission, polling, listing, and deletion
   - `config` for penalty-weight configuration
   - `ws` for push notifications tied to a schedule job  
   The API returns a `job_id` immediately for generation requests, which implies schedule generation is asynchronous rather than blocking the HTTP request.

3. **Application layer as orchestration**  
   `src/application/use_cases` contains the two primary workflows:
   - `GenerateSchedule`
   - `GetSchedule`  
   These use cases mediate between request schemas and domain/infrastructure services. They are the main orchestration boundary where input is validated, the solver is invoked, and the resulting schedule/job state is persisted or retrieved.

4. **Domain model and business rules**  
   `src/domain/entities` defines the core scheduling objects: `Teacher`, `Subject`, `Room`, `Timeslot`, and `Schedule`.  
   `src/domain/services/scheduling_service.py` appears to encapsulate the scheduling business rules, while `src/domain/value_objects/penalty_weights.py` captures the tunable objective weights. This is where the hard constraints and soft penalties belong conceptually, independent of FastAPI or the database.

5. **Ports define the dependency boundaries**  
   The domain exposes abstract interfaces in `src/domain/ports`:
   - `i_solver`
   - `i_schedule_repository`
   - `i_event_publisher`  
   These ports make the system extensible: the domain/application layers depend on abstractions, while infrastructure provides the concrete PuLP solver, database repository, and event delivery mechanism.

6. **Solver subsystem**  
   `src/infrastructure/solvers` contains the optimization backend. The README indicates the main solver is PuLP/CBC for exact mixed-integer linear programming, with a `TabuSearchSolverStub` as an extensibility point. A `solver_factory` selects the solver implementation based on the request/config.  
   This subsystem is the computational core of the service and is responsible for enforcing hard constraints and minimizing penalty terms.

7. **Persistence subsystem**  
   `src/infrastructure/persistence` uses async SQLAlchemy for runtime access and Alembic for schema evolution. `models.py` maps the storage schema, `database.py` manages DB connectivity, and `schedule_repository.py` implements the repository port.  
   The database is PostgreSQL 16, and the repository is the boundary between domain scheduling jobs/results and durable storage.

8. **Async execution and messaging**  
   `src/infrastructure/messaging` contains Celery integration, backed by Redis. Generation jobs are offloaded to a worker via Celery tasks, which fits the API’s immediate `job_id` response pattern. The queue layer decouples HTTP latency from potentially long-running MILP solving.

9. **Real-time notifications**  
   `src/infrastructure/websockets/connection_manager.py` supports WebSocket connections keyed by `job_id`. When a Celery task completes, the system can push job-completion notifications to connected clients instead of relying only on polling. This is the real-time complement to the `/schedules/{job_id}` status endpoint.

10. **Configuration and runtime wiring**  
    `src/infrastructure/config/settings.py` centralizes environment-based configuration using `pydantic-settings`. The API layer uses `src/api/dependencies.py` to wire repositories, solver selection, and services together.  
    Docker Compose and the Dockerfile package the full stack: API, worker, PostgreSQL, Redis, and Flower for monitoring.

11. **Constraint model reflected in the README**  
    The solver is built around academic timetabling constraints such as teacher availability, room capacity, no double-booking, and campus-transfer restrictions. The README also highlights teacher categories like `REGULAR`, `PROCOM`, `PROJEX`, and `PROHES`, which materially affect the solver objective and feasible assignments.

12. **Testing and deployment boundaries**  
    Unit tests cover the domain and solver logic, while integration tests exercise the API stack. Alembic migrations track schema changes. This separation matches the architecture: pure business logic can be tested without infrastructure, while full-stack behavior is verified against the composed services.

Main entry points: [`src/application/use_cases/generate_schedule.py`](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/application/use_cases/generate_schedule.py) (Generate), [`src/domain/services/scheduling_service.py`](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/services/scheduling_service.py) (Scheduling service), [`src/api/main.py`](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/main.py) (FastAPI app), [`src/domain/ports/i_schedule_repository.py`](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/ports/i_schedule_repository.py) (Repo port)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_api["API layer"]
  node_node_api_main["FastAPI app<br/>entrypoint<br/>[main.py]"]
  node_node_api_deps["Dependencies<br/>wiring<br/>[dependencies.py]"]
  node_node_api_schedules["Schedules API<br/>router<br/>[schedules.py]"]
  node_node_api_config["Config API<br/>router<br/>[config.py]"]
  node_node_api_ws["WebSocket API<br/>router<br/>[ws.py]"]
end

subgraph group_group_app["Application layer"]
  node_node_app_generate["Generate<br/>use case"]
  node_node_app_get["Get schedule<br/>use case<br/>[get_schedule.py]"]
  node_node_app_inputs["Input schemas<br/>dto<br/>[inputs.py]"]
  node_node_app_outputs["Output schemas<br/>dto<br/>[outputs.py]"]
end

subgraph group_group_domain["Domain model"]
  node_node_dom_sched_service["Scheduling service<br/>domain service"]
  node_node_dom_penalties["Penalty weights<br/>value object<br/>[penalty_weights.py]"]
  node_node_dom_entities{{"Entities<br/>domain model<br/>[schedule.py]"}}
  node_node_dom_ports_solver["Solver port<br/>[i_solver.py]"]
  node_node_dom_ports_repo["Repo port"]
  node_node_dom_ports_events["Event port"]
end

subgraph group_group_infra["Infrastructure"]
  node_node_infra_settings["Settings<br/>config<br/>[settings.py]"]
  node_node_infra_solver_factory["Solver factory<br/>adapter<br/>[solver_factory.py]"]
  node_node_infra_pulp["PuLP solver<br/>optimizer<br/>[pulp_solver.py]"]
  node_node_infra_repo[("Schedule repo")]
  node_node_infra_db[("Database<br/>persistence<br/>[database.py]")]
  node_node_infra_celery["Celery tasks<br/>worker<br/>[tasks.py]"]
  node_node_infra_redis["Celery app<br/>queue config<br/>[celery_app.py]"]
  node_node_infra_ws_mgr["Conn manager<br/>websocket hub"]
end

subgraph group_group_tests["Tests"]
  node_node_tests_unit["Unit tests<br/>test suite"]
  node_node_tests_integration["Integration tests<br/>test suite<br/>[test_api.py]"]
end

node_node_api_main -->|"registers"| node_node_api_schedules
node_node_api_main -->|"registers"| node_node_api_config
node_node_api_main -->|"registers"| node_node_api_ws
node_node_api_schedules -->|"calls"| node_node_app_generate
node_node_api_schedules -->|"calls"| node_node_app_get
node_node_api_config -->|"uses"| node_node_dom_penalties
node_node_api_ws -->|"uses"| node_node_infra_ws_mgr
node_node_api_deps -->|"binds"| node_node_dom_ports_solver
node_node_api_deps -->|"binds"| node_node_dom_ports_repo
node_node_api_deps -->|"binds"| node_node_dom_ports_events
node_node_app_generate -->|"validates"| node_node_app_inputs
node_node_app_generate -->|"orchestrates"| node_node_dom_sched_service
node_node_app_generate -->|"invokes"| node_node_dom_ports_solver
node_node_app_generate -->|"persists"| node_node_dom_ports_repo
node_node_app_generate -->|"publishes"| node_node_dom_ports_events
node_node_app_get -->|"reads"| node_node_dom_ports_repo
node_node_dom_sched_service -->|"operates on"| node_node_dom_entities
node_node_dom_sched_service -->|"applies"| node_node_dom_penalties
node_node_dom_sched_service -->|"depends on"| node_node_dom_ports_solver
node_node_infra_solver_factory -->|"creates"| node_node_infra_pulp
node_node_infra_pulp -->|"solves for"| node_node_dom_entities
node_node_infra_repo -->|"uses"| node_node_infra_db
node_node_infra_repo -->|"implements"| node_node_dom_ports_repo
node_node_infra_celery -->|"runs"| node_node_app_generate
node_node_infra_celery -->|"queues via"| node_node_infra_redis
node_node_infra_ws_mgr -->|"implements"| node_node_dom_ports_events
node_node_tests_unit -.->|"covers"| node_node_dom_entities
node_node_tests_unit -.->|"covers"| node_node_infra_pulp
node_node_tests_integration -.->|"covers"| node_node_api_main

click node_node_api_main "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/main.py"
click node_node_api_deps "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/dependencies.py"
click node_node_api_schedules "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/routers/schedules.py"
click node_node_api_config "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/routers/config.py"
click node_node_api_ws "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/routers/ws.py"
click node_node_app_generate "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/application/use_cases/generate_schedule.py"
click node_node_app_get "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/application/use_cases/get_schedule.py"
click node_node_app_inputs "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/application/schemas/inputs.py"
click node_node_app_outputs "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/application/schemas/outputs.py"
click node_node_dom_sched_service "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/services/scheduling_service.py"
click node_node_dom_penalties "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/value_objects/penalty_weights.py"
click node_node_dom_entities "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/entities/schedule.py"
click node_node_dom_ports_solver "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/ports/i_solver.py"
click node_node_dom_ports_repo "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/ports/i_schedule_repository.py"
click node_node_dom_ports_events "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/ports/i_event_publisher.py"
click node_node_infra_settings "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/config/settings.py"
click node_node_infra_solver_factory "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/solvers/solver_factory.py"
click node_node_infra_pulp "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/solvers/pulp_solver.py"
click node_node_infra_repo "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/persistence/schedule_repository.py"
click node_node_infra_db "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/persistence/database.py"
click node_node_infra_celery "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/messaging/tasks.py"
click node_node_infra_redis "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/messaging/celery_app.py"
click node_node_infra_ws_mgr "https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/websockets/connection_manager.py"
click node_node_tests_unit "https://github.com/nikotpab/class-scheduling-engine/blob/main/tests/unit/test_domain_entities.py"
click node_node_tests_integration "https://github.com/nikotpab/class-scheduling-engine/blob/main/tests/integration/test_api.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_api_main,node_node_api_deps,node_node_api_schedules,node_node_api_config,node_node_api_ws toneBlue
class node_node_app_generate,node_node_app_get,node_node_app_inputs,node_node_app_outputs toneAmber
class node_node_dom_sched_service,node_node_dom_penalties,node_node_dom_entities,node_node_dom_ports_solver,node_node_dom_ports_repo,node_node_dom_ports_events toneMint
class node_node_infra_settings,node_node_infra_solver_factory,node_node_infra_pulp,node_node_infra_repo,node_node_infra_db,node_node_infra_celery,node_node_infra_redis,node_node_infra_ws_mgr toneRose
class node_node_tests_unit,node_node_tests_integration toneIndigo
```

## Components

### API layer

HTTP entrypoints and dependency wiring.

- [FastAPI app](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/main.py): entrypoint · Composes the HTTP service and registers routers. · `src/api/main.py`
- [Dependencies](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/dependencies.py): wiring · Builds application services and infrastructure adapters for the API. · `src/api/dependencies.py`
- [Schedules API](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/routers/schedules.py): router · Submit, poll, list, and delete schedule jobs. · `src/api/routers/schedules.py`
- [Config API](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/routers/config.py): router · Exposes penalty-weight configuration. · `src/api/routers/config.py`
- [WebSocket API](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/api/routers/ws.py): router · Pushes schedule-job updates to connected clients. · `src/api/routers/ws.py`

### Application layer

Use cases and transport schemas.

- [Generate](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/application/use_cases/generate_schedule.py): use case · Orchestrates schedule creation and persistence. · `src/application/use_cases/generate_schedule.py`
- [Get schedule](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/application/use_cases/get_schedule.py): use case · Retrieves existing schedule/job state. · `src/application/use_cases/get_schedule.py`
- [Input schemas](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/application/schemas/inputs.py): dto · Request models for schedule generation and related actions. · `src/application/schemas/inputs.py`
- [Output schemas](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/application/schemas/outputs.py): dto · Response models returned by the API. · `src/application/schemas/outputs.py`

### Domain model

Core scheduling concepts and business rules.

- [Scheduling service](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/services/scheduling_service.py): domain service · Encapsulates scheduling rules and constraint logic. · `src/domain/services/scheduling_service.py`
- [Penalty weights](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/value_objects/penalty_weights.py): value object · Tunables for soft constraints and optimization objective. · `src/domain/value_objects/penalty_weights.py`
- [Entities](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/entities/schedule.py): domain model · Teachers, subjects, rooms, timeslots, and schedules. · `src/domain/entities/schedule.py`
- [Solver port](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/ports/i_solver.py): port · Abstract optimization interface. · `src/domain/ports/i_solver.py`
- [Repo port](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/ports/i_schedule_repository.py): port · Abstract schedule persistence interface. · `src/domain/ports/i_schedule_repository.py`
- [Event port](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/domain/ports/i_event_publisher.py): port · Abstract notification publishing interface. · `src/domain/ports/i_event_publisher.py`

### Infrastructure

Concrete adapters for persistence, solver, messaging, and websockets.

- [Settings](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/config/settings.py): config · Environment-based runtime configuration. · `src/infrastructure/config/settings.py`
- [Solver factory](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/solvers/solver_factory.py): adapter · Selects the solver implementation for a request or config. · `src/infrastructure/solvers/solver_factory.py`
- [PuLP solver](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/solvers/pulp_solver.py): optimizer · MILP solver implementation for timetable generation. · `src/infrastructure/solvers/pulp_solver.py`
- [Schedule repo](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/persistence/schedule_repository.py): repository · SQLAlchemy-backed implementation of the schedule repository port. · `src/infrastructure/persistence/schedule_repository.py`
- [Database](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/persistence/database.py): persistence · Database session and connectivity layer. · `src/infrastructure/persistence/database.py`
- [Celery tasks](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/messaging/tasks.py): worker · Background job execution for schedule generation. · `src/infrastructure/messaging/tasks.py`
- [Celery app](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/messaging/celery_app.py): queue config · Queue wiring for asynchronous execution. · `src/infrastructure/messaging/celery_app.py`
- [Conn manager](https://github.com/nikotpab/class-scheduling-engine/blob/main/src/infrastructure/websockets/connection_manager.py): websocket hub · Tracks WebSocket connections keyed by job ID. · `src/infrastructure/websockets/connection_manager.py`

### Tests

Unit and integration coverage.

- [Unit tests](https://github.com/nikotpab/class-scheduling-engine/blob/main/tests/unit/test_domain_entities.py): test suite · Covers domain entities and solver behavior. · `tests/unit/test_domain_entities.py`
- [Integration tests](https://github.com/nikotpab/class-scheduling-engine/blob/main/tests/integration/test_api.py): test suite · Exercises the API stack end to end. · `tests/integration/test_api.py`

## Connections

- FastAPI app → Schedules API: registers · Main app mounts the schedules routes.
- FastAPI app → Config API: registers · Main app mounts configuration routes.
- FastAPI app → WebSocket API: registers · Main app mounts websocket routes.
- Schedules API → Generate: calls · Submission endpoints start schedule generation.
- Schedules API → Get schedule: calls · Poll/list/delete endpoints retrieve schedule state.
- Config API → Penalty weights: uses · Configuration endpoints expose penalty tuning.
- WebSocket API → Conn manager: uses · WebSocket routes coordinate connections through the manager.
- Dependencies → Solver port: binds · Wires a concrete solver behind the solver port.
- Dependencies → Repo port: binds · Wires persistence behind the repository port.
- Dependencies → Event port: binds · Wires event delivery behind the publisher port.
- Generate → Input schemas: validates · Use case accepts request DTOs.
- Generate → Scheduling service: orchestrates · Coordinates business rules around scheduling.
- Generate → Solver port: invokes · Delegates optimization to the solver abstraction.
- Generate → Repo port: persists · Stores generated schedule/job state.
- Generate → Event port: publishes · Emits job progress or completion events.
- Get schedule → Repo port: reads · Fetches schedule/job state from persistence.
- Scheduling service → Entities: operates on · Business rules are expressed over core scheduling entities.
- Scheduling service → Penalty weights: applies · Uses penalty weights to score schedules.
- Scheduling service → Solver port: depends on · Service remains independent of solver implementation.
- Solver factory → PuLP solver: creates · Factory selects the concrete optimization backend.
- PuLP solver → Entities: solves for · Solver produces schedules over domain objects.
- Schedule repo → Database: uses · Repository relies on DB connectivity and sessions.
- Schedule repo → Repo port: implements · Concrete adapter for the repository port.
- Celery tasks → Generate: runs · Background worker executes generation workflows.
- Celery tasks → Celery app: queues via · Celery is configured for asynchronous task execution.
- Conn manager → Event port: implements · WebSocket delivery can satisfy event publication.
- Unit tests → Entities: covers · Unit tests validate core domain objects.
- Unit tests → PuLP solver: covers · Unit tests validate solver behavior.
- Integration tests → FastAPI app: covers · Integration tests exercise the composed API stack.
