# manikumar-dodla/agriguard architecture

> Architecture diagram of the GitHub repository manikumar-dodla/agriguard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/manikumar-dodla/agriguard
- Repository: https://github.com/manikumar-dodla/agriguard

## Overview

##### Project Type and Purpose
- AgriGuard is a full-stack, data-driven web application that empowers farmers with crop recommendations, irrigation guidance, flood alerts, and AI-powered chat assistance.
- It was built for the NASA Space Apps Challenge 2024 and integrates multiple external data sources (NASA POWER, Open Meteo, OpenWeather) and an AI model (Hugging Face Mistral).

##### File Structure
- Top-level directories:
- backend/ – Node.js server (server.js, package.json), Python service (app.py) hosting a Keras disease model (plant_disease_model.keras), .env for secrets
- public/ – Static assets for the front end
- src/ – React code (App.jsx, pages/, components/, context/)
- Configuration: vite.config.js, tailwind.config.js, eslint.config.js
- Key patterns: Single React SPA with Vite build, Node as API gateway, Python microservice for ML inference, context/provider pattern for user state, API integration layers in components/pages.

##### README for Insights
- Frontend uses React + Tailwind + Recharts
- Backend APIs call NASA POWER, Open Meteo, OpenWeather, Hugging Face
- Firebase is used (src/pages/firebase.js) for authentication/data storage

##### Main Components
a. User & UI Layer
- Browser / Mobile App (HTTP/HTTPS)
- React SPA (src/)
b. API Layer (Backend)
- Node.js Server (server.js) – Express routes for weather, crop suggestions, flood alerts, chatbot proxy
- Python ML Service (app.py) – Loads plant_disease_model.keras for disease predictions
c. Data Stores & Auth
- Firebase (Auth + Firestore/Realtime DB)
- Environment variables (.env) for API keys
d. External Services
- NASA POWER API (precipitation, groundwater)
- Open Meteo River Discharge API (flood alerts)
- OpenWeather API (weather data)
- Hugging Face Mistral AI API (chatbot)
e. Third-Party Libraries
- Recharts for data visualization
- Tailwind CSS for styling

##### Relationships & Interactions
- The React SPA initiates HTTP requests to the Node.js server.
- Node.js routes forward or aggregate data from:
- NASA POWER (crop & irrigation data)
- Open Meteo (flood alerts)
- OpenWeather (weather snapshots)
- Hugging Face (chatbot queries)
- Python ML Service (disease detection inference)
- Node.js may read/write to Firebase for user profiles, favorites, expert consultations.
- Python ML Service returns JSON predictions to Node.js.
- Data flows back to React, where Recharts renders graphs and components display results.

##### Architectural Patterns & Principles
- Separation of concerns: React UI vs. Node API vs. Python microservice
- API Gateway pattern: Node.js aggregates external API calls and internal services
- Microservice for ML inference: isolates heavy model loading and prediction logic
- Context API for global user state in React
- Environment-driven configuration for keys and endpoints

##### What
- Clear, descriptive labels for each component and service
- Notes on protocols (HTTP/HTTPS, REST) and authentication (Firebase tokens, API keys)

##### Detail
- Environment (.env) feeding API keys into Node.js and Python services
- Separate the AI Chatbot flow from the data-visualization flow for clarity

## Diagram (Mermaid)

```mermaid
graph TD
    %% User
    User["User (Browser/Mobile)"]:::user

    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        React["React SPA"]:::frontend
        Pages["Pages Directory"]:::frontend
        Components["Components Directory"]:::frontend
        Context["UserContext.jsx"]:::frontend
        Assets["Static Assets"]:::frontend
        IndexHTML["index.html"]:::frontend
    end

    %% Backend/API Layer
    subgraph "API Gateway Layer"
        direction TB
        Node["Node.js API Gateway"]:::backend
    end

    %% ML Microservice
    subgraph "Machine Learning Service"
        direction TB
        PythonService["Python ML Service"]:::ml
        ModelFile["plant_disease_model.keras"]:::ml
    end

    %% Data Store & Auth
    subgraph "Data & Auth"
        direction TB
        Firebase["Firebase Auth & Database"]:::datastore
    end

    %% External Services
    subgraph "External Services"
        direction TB
        NASA["NASA POWER API"]:::external
        Meteo["Open Meteo API"]:::external
        Weather["OpenWeather API"]:::external
        HF["Hugging Face Mistral"]:::external
    end

    %% Connections
    User -->|HTTP/HTTPS| React
    React -->|"GET/POST Requests"| Node
    Node -->|"Inference Request"| PythonService
    PythonService -->|"JSON Predictions"| Node
    Node <-->|Read/Write| Firebase
    Node -->|"GET Crop & Irrigation Data"| NASA
    Node -->|"GET Flood Alerts"| Meteo
    Node -->|"GET Weather Data"| Weather
    Node -->|"POST Chatbot Query"| HF
    NASA -->|"Response Data"| Node
    Meteo -->|"Response Data"| Node
    Weather -->|"Response Data"| Node
    HF -->|"Chat Responses"| Node
    Node -->|HTTP/HTTPS JSON| React

    %% Click Events - Frontend
    click React "https://github.com/manikumar-dodla/agriguard/tree/main/src/"
    click Pages "https://github.com/manikumar-dodla/agriguard/tree/main/src/pages/"
    click Components "https://github.com/manikumar-dodla/agriguard/tree/main/src/components/"
    click Context "https://github.com/manikumar-dodla/agriguard/blob/main/src/context/UserContext.jsx"
    click Assets "https://github.com/manikumar-dodla/agriguard/tree/main/public/assets/"
    click IndexHTML "https://github.com/manikumar-dodla/agriguard/blob/main/index.html"
    click React "https://github.com/manikumar-dodla/agriguard/blob/main/src/main.jsx"
    click React "https://github.com/manikumar-dodla/agriguard/blob/main/src/App.jsx"

    %% Click Events - Backend
    click Node "https://github.com/manikumar-dodla/agriguard/blob/main/backend/server.js"
    click Node "https://github.com/manikumar-dodla/agriguard/blob/main/backend/package.json"
    click Node "https://github.com/manikumar-dodla/agriguard/blob/main/backend/package-lock.json"

    %% Click Events - ML Service
    click PythonService "https://github.com/manikumar-dodla/agriguard/blob/main/backend/app.py"
    click ModelFile "https://github.com/manikumar-dodla/agriguard/blob/main/backend/plant_disease_model.keras"

    %% Click Events - Environment & Config
    click Node "https://github.com/manikumar-dodla/agriguard/blob/main/backend/.env"
    click React "https://github.com/manikumar-dodla/agriguard/blob/main/vite.config.js"
    click React "https://github.com/manikumar-dodla/agriguard/blob/main/tailwind.config.js"
    click React "https://github.com/manikumar-dodla/agriguard/blob/main/eslint.config.js"
    click React "https://github.com/manikumar-dodla/agriguard/blob/main/postcss.config.js"
    click React "https://github.com/manikumar-dodla/agriguard/blob/main/package.json"
    click React "https://github.com/manikumar-dodla/agriguard/blob/main/package-lock.json"

    %% Click Events - Firebase
    click Firebase "https://github.com/manikumar-dodla/agriguard/blob/main/src/pages/firebase.js"

    %% Styles
    classDef user fill:#e2e3e5,stroke:#6c757d,color:#343a40;
    classDef frontend fill:#cce5ff,stroke:#004085,color:#004085;
    classDef backend fill:#d4edda,stroke:#155724,color:#155724;
    classDef ml fill:#d1ecf1,stroke:#0c5460,color:#0c5460;
    classDef datastore fill:#fff3cd,stroke:#856404,color:#856404;
    classDef external fill:#f8d7da,stroke:#721c24,color:#721c24;
```
