Skip to content

vidya381/weather-insight

Repository files navigation

Weather Insight β˜€οΈπŸŒ§οΈ

Weather tracking app with ML-powered insights. Track multiple cities, detect temperature anomalies, analyze trends, and find recurring weather patterns.

React FastAPI PostgreSQL Machine Learning

Live Demo: https://weather-insight-ml.vercel.app

What it does

Tracks weather for multiple cities and runs ML analysis on historical data. Backend collects weather data every hour and stores it for analysis. Frontend shows current weather, 5-day forecasts, and ML insights.

Features

Dashboard

  • Add up to 10 favorite cities, set one as primary
  • Current weather and 5-day hourly forecast
  • Guest mode (stores cities in localStorage, migrates to DB on signup)
  • Weather-themed animated backgrounds

ML Insights (requires historical data)

  • Anomaly Detection - Flags unusual temperatures using Z-score analysis (needs 10+ days of data)
  • Trend Analysis - Predicts next 7 days of temps using linear regression (needs 30+ days)
  • Pattern Clustering - Groups similar weather conditions with K-Means (needs 90+ days)

Auth

  • JWT tokens with bcrypt password hashing
  • Can use the app without login (guest mode)

Performance

  • Caches weather data for 10 minutes (no duplicate API calls)
  • Background job collects weather every hour for favorited cities
  • React.memo and useMemo to prevent unnecessary re-renders

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    React Frontend (Vite)                    β”‚
β”‚  Dashboard β”‚ ML Insights β”‚ Auth β”‚ City Search               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚ REST API (Axios)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    FastAPI Backend                          β”‚
β”‚  Routes β†’ Services β†’ Repositories β†’ Models                  β”‚
β”‚  + ML Pipeline (NumPy, Pandas, Scikit-learn)                β”‚
β”‚  + Background Jobs (APScheduler)                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              PostgreSQL Database (SQLAlchemy)               β”‚
β”‚  Users β”‚ Cities β”‚ Weather Data β”‚ ML Results                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   OpenWeather API                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

For detailed architecture: See ARCHITECTURE.md

Technology Stack

Frontend

  • Framework: React 18.3 with Vite
  • Routing: React Router v7
  • State: React Context API with custom hooks
  • Styling: CSS Modules with responsive design (320px+)
  • Charts: Recharts for data visualization
  • HTTP: Axios with interceptors
  • Icons: React Icons

Backend

  • Framework: FastAPI (Python 3.14)
  • Database: PostgreSQL with SQLAlchemy ORM
  • Migrations: Alembic
  • Auth: JWT tokens with bcrypt (12 salt rounds)
  • ML: NumPy, Pandas, Scikit-learn
  • Scheduler: APScheduler (hourly data collection, daily cleanup)
  • External API: OpenWeather API

Machine Learning

  • Anomaly Detection: Z-Score statistical method
  • Trend Analysis: Linear Regression with confidence intervals
  • Pattern Clustering: K-Means with StandardScaler normalization

Project Structure

weather-insight/
β”œβ”€β”€ src/                      # Frontend React app
β”‚   β”œβ”€β”€ components/           # React components
β”‚   β”œβ”€β”€ pages/                # Route pages (Dashboard, MLInsights, Auth)
β”‚   β”œβ”€β”€ context/              # React Context (AuthContext)
β”‚   β”œβ”€β”€ hooks/                # Custom hooks (useCachedWeather)
β”‚   β”œβ”€β”€ api/                  # API client modules
β”‚   └── utils/                # Utilities (guestCities, localStorage)
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ routes/           # API endpoints
β”‚   β”‚   β”œβ”€β”€ services/         # Business logic
β”‚   β”‚   β”œβ”€β”€ repositories/     # Data access layer
β”‚   β”‚   β”œβ”€β”€ models/           # SQLAlchemy models
β”‚   β”‚   β”œβ”€β”€ ml/               # ML algorithms
β”‚   β”‚   └── jobs/             # Background jobs
β”‚   └── alembic/              # Database migrations
β”œβ”€β”€ ARCHITECTURE.md           # System architecture details
β”œβ”€β”€ ML_MODELS.md              # ML algorithm explanations
└── backend/API_ENDPOINTS.md  # Complete API reference

More Documentation

Technical Details

Performance

  • Custom React hooks cache weather data for 10 minutes
  • Deduplicates simultaneous requests (multiple components requesting same city = 1 API call)
  • React.memo on heavy components (WeatherBackground, weather cards)
  • Lazy loads pages (Dashboard, MLInsights split into separate bundles)

Data Pipeline

  • APScheduler runs hourly job to fetch weather for favorited cities
  • Daily cleanup removes old data (180 days for weather, 90 days for ML results)
  • Alembic manages database migrations

Security

  • bcrypt password hashing (12 salt rounds)
  • JWT tokens (24-hour expiration)
  • Pydantic validates all API inputs
  • CORS only allows specific origins

Local Setup

Prerequisites

  • Node.js 18+
  • Python 3.14+
  • PostgreSQL 14+
  • OpenWeather API key

Frontend Setup

npm install
npm run dev  # http://localhost:5173

Backend Setup

cd backend
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

# Configure .env
cp .env.example .env
# Add: DATABASE_URL, SECRET_KEY, OPENWEATHER_API_KEY

# Run migrations
alembic upgrade head

# Start server
python -m app.main  # http://localhost:8000

API Documentation

How it Works

Caching

  • First request fetches from API and caches for 10 minutes
  • Subsequent requests use cache (instant load)
  • Cache auto-expires after 10 minutes

Guest Mode

  • Try the app without creating an account
  • Cities stored in localStorage (expires after 30 days)
  • Data automatically migrates to DB when you sign up

ML Data Requirements

  • Anomaly detection needs 10+ days of data
  • Trend analysis needs 30+ days
  • Pattern clustering needs 90+ days
  • Backend collects weather every hour for all favorited cities

Built with React, FastAPI, PostgreSQL, and Scikit-learn. Uses background jobs for hourly data collection and daily cleanup. See ARCHITECTURE.md for system design and ML_MODELS.md for algorithm details.


About

Weather tracking app with ML features - anomaly detection, temperature trends, and pattern analysis. Collects hourly weather data and runs predictions on historical patterns.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors