Skip to content

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Switchyard — Railway Station Control

A live operations dashboard for a railway station. An admin selects a state, city, and station, then sees real‑time platform occupancy, the train board, and smart platform suggestions generated by a rule‑based allocation engine.

Live demo: https://switch-yard.vercel.app API: https://switchyard-scpr.onrender.com · API docs: https://switchyard-scpr.onrender.com/docs

Note: the backend runs on a free tier that sleeps after ~15 minutes idle, so the first request after a quiet period can take 30–50 seconds to wake up. It's fast after that.


What it does

  • Cascading selectors — pick State → City → Station, all loaded from the database.
  • Four live stat cards — total platforms, free, occupied, and trains arriving in the next hour. Each card is clickable for a breakdown.
  • Platform status board — a colour‑coded tile per platform (green = free, amber = arriving, red = occupied).
  • Train board — one table of every train (arrival, departure, expected time, platform, status) with a merged column showing the engine's platform suggestion, and the reason on hover.
  • Platform allocation engine — when a train is delayed and clashes with another on its platform, the engine proposes the cheapest fix (relocate the delayed train, relocate the other train, or hold one) measured in added‑delay minutes.
  • Dark mode, a live clock, and auto‑refresh every 20 seconds.

Tech stack

Layer Tech
Frontend React, Vite, Tailwind CSS v4
Backend FastAPI, SQLAlchemy, Pydantic
Database PostgreSQL
Hosting Vercel (frontend), Render (backend + managed Postgres)

Repository structure

SwitchYard/
├── backend/                     # FastAPI + SQLAlchemy API
│   ├── app/
│   │   ├── api/routes/dashboard.py   # HTTP endpoints
│   │   ├── config/settings.py        # env-driven config
│   │   ├── db/                        # models, session, crud, seed + table scripts
│   │   ├── logic/allocator.py        # platform-allocation engine
│   │   └── main.py                   # app entry point + CORS
│   ├── Dockerfile
│   ├── requirements.txt
│   └── .env.example
├── frontend/                    # React + Vite + Tailwind dashboard
│   └── src/
│       ├── App.jsx                   # state, data loading, layout
│       ├── api.js                    # backend URL + fetch helpers
│       └── components/               # Header, Selectors, StatCards, PlatformBoard, TrainBoard, DetailPanel
├── docker-compose.yml           # local Postgres for development
└── README.md

Running locally

Prerequisites: Python 3.11+, Node 20+, and Docker (for the local database).

1. Start the database

From the repository root:

docker compose up -d db

This runs PostgreSQL on localhost:5432 (user postgres, password postgres, database switchyard).

2. Backend

cd backend
python -m venv venv
# Windows: venv\Scripts\activate     macOS/Linux: source venv/bin/activate
pip install -r requirements.txt

# create your .env from the template
cp .env.example .env               # Windows: Copy-Item .env.example .env

# create the tables and load sample data
python -m app.db.reset_table
python -m app.db.seed_data

# run the API
uvicorn app.main:app --reload

The API is now on http://localhost:8000 — check http://localhost:8000/docs.

3. Frontend

cd frontend
npm install
npm run dev

Open http://localhost:5173. In development it calls http://localhost:8000 by default, so no extra config is needed.

The sample data is anchored to the moment you seed it and stays realistic for about half a day. If the board ever shows everything as "departed", just re-run python -m app.db.seed_data to refresh it.


Environment variables

Backend

Variable Purpose Example
DATABASE_URL PostgreSQL connection string postgresql://postgres:postgres@localhost:5432/switchyard
FRONTEND_ORIGIN Allowed CORS origin(s), comma‑separated https://switch-yard.vercel.app

Frontend

Variable Purpose Example
VITE_API_URL Base URL of the backend API (no trailing /api) https://switchyard-scpr.onrender.com

The allocation engine

backend/app/logic/allocator.py is pure Python — no ML, every decision traceable to a timestamp comparison. For each delayed train that clashes on its platform it:

  1. builds an occupancy map of every platform's bookings,
  2. detects conflicts using a half‑open interval overlap test,
  3. assembles candidate fixes — relocate the delayed train to the nearest free platform, relocate the on‑time train it clashes with, or hold the on‑time train so they share the platform in turn,
  4. prices each option in added‑delay minutes and keeps the cheapest,

then writes a suggestion (with a human‑readable reason) that the dashboard shows in the train board.


Deployment

The frontend is a static Vite build hosted on Vercel (root directory frontend, with VITE_API_URL pointed at the backend). The backend is a Docker service on Render (root directory backend, using backend/Dockerfile) connected to a Render managed PostgreSQL instance via DATABASE_URL, with FRONTEND_ORIGIN set to the Vercel URL so CORS only permits the real site. Both platforms redeploy automatically on every push to main.


License

This project is for learning and demonstration purposes.

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages