# dheerajok/cropadvisor architecture

> Architecture diagram of the GitHub repository dheerajok/cropadvisor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dheerajok/cropadvisor
- Repository: https://github.com/dheerajok/cropadvisor

## Overview

1. **Overall shape**
   Agro-Insight is organized as a **Next.js App Router** application. The repository contains both the user-facing pages and the server-side API routes inside the same app, so the main architectural boundary is between **React pages/components** and **`app/api/*` route handlers**.

2. **User-facing modules**
   The top-level pages are split into feature areas: **dashboard**, **crop recommendation**, **disease detection**, **fertilizer**, **weather**, **farming guide**, and **learn more**. These are mostly presentation and interaction layers that collect user input, show results, and navigate between tools.

3. **API surface**
   The backend logic exposed by this repo lives under `app/api/`:
   - `weather/current`
   - `soil/analyze`
   - `crop/recommend`
   - `fertilizer/recommend`
   - `disease/detect`
   - `disease/history`
   
   Each route is a server-side entry point for a single domain operation, which makes these the key integration boundaries for the UI.

4. **Domain decomposition**
   The application is split into clear agricultural domains:
   - **Weather**: current conditions and likely forecast support
   - **Soil analysis**: soil measurements and derived interpretation
   - **Crop recommendation**: recommendation engine based on soil/climate/location inputs
   - **Fertilizer recommendation**: nutrient and crop-specific guidance
   - **Disease detection**: image-based diagnosis plus history tracking
   - **Guides/content**: static knowledge and best-practice information
   
   This separation is important for diagramming because each feature is independently invoked but semantically related through farming workflows.

5. **Data flow from UI to server**
   The typical flow is: **user interacts with a feature page → page calls a matching API route → route performs analysis/recommendation → response is rendered back in the page**. The repo’s structure suggests the pages are thin clients over domain-specific server endpoints, rather than embedding all logic in the browser.

6. **Persistence layer**
   There is a dedicated database helper in `app/db/mongodb.js` and model files for:
   - `CropRecommendation`
   - `DiseaseDetection`
   - `FertilizerRecommendation`
   
   That indicates a **MongoDB-backed persistence layer** used at least for storing recommendation/detection records and retrieving disease history. For an architecture diagram, this is the main stateful subsystem outside the Next.js runtime.

7. **Disease detection pipeline**
   `app/api/disease/detect/route.js` plus `app/utils/diseaseParser.js` and the `DiseaseDetection` model imply a two-step backend path: **accept an image or detection payload, normalize/parse the result, then persist or return structured diagnosis data**. The separate `history` route is the read-side of that same subsystem.

8. **Weather integration**
   The README references **OpenWeatherMap**, and the `weather/current` API route is the obvious integration point. This route likely acts as a server-side adapter that hides the external weather API from the UI and returns app-specific weather data.

9. **Machine-learning / inference boundary**
   The README describes AI-based crop and disease features, and the file layout keeps those operations behind API routes rather than in pages. Architecturally, this means **model inference happens on the server side**, with the frontend only handling inputs like soil values or leaf images and rendering the predicted output.

10. **Shared presentation shell**
   `app/layout.js`, `app/globals.css`, and the `dashboard` page provide the application shell and shared styling. This is the common UI layer that wraps all feature pages, while the actual business logic remains in the feature routes.

11. **Static and informational content**
   `farming-guide`, `learn-more`, and the root `page.js` are mostly informational/navigation surfaces. They support onboarding and education rather than core data processing, so they sit at the edge of the architecture.

12. **Important repo note**
   The README mentions a separate Python/Flask or FastAPI backend, but this tree does not include a `backend/` directory. In the code present here, the **Next.js app itself appears to host the API layer**, so a diagram should reflect the repository as a self-contained web app with serverless-style routes, not a split frontend/backend codebase.

13. **Key boundaries to draw**
   The most useful diagram boundaries are:
   - **Browser/UI pages**
   - **Next.js API routes**
   - **MongoDB persistence**
   - **External services/model inference inputs**
   
   That separation captures how farmers’ inputs move from pages into server-side recommendation/detection logic and then into stored history or returned results.

Main entry points: [`app/layout.js`](https://github.com/dheerajok/cropadvisor/blob/main/app/layout.js) (App shell), [`app/dashboard/page.js`](https://github.com/dheerajok/cropadvisor/blob/main/app/dashboard/page.js) (Dashboard), [`app/db/mongodb.js`](https://github.com/dheerajok/cropadvisor/blob/main/app/db/mongodb.js) (MongoDB), [`app/crop-recommendation/page.js`](https://github.com/dheerajok/cropadvisor/blob/main/app/crop-recommendation/page.js) (Crop rec)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["UI shell"]
  node_root_page["Home<br/>landing page<br/>[page.js]"]
  node_dashboard_page["Dashboard<br/>overview page<br/>[page.js]"]
  node_crop_page["Crop rec<br/>feature page<br/>[page.js]"]
  node_disease_page["Disease detect<br/>feature page<br/>[page.js]"]
  node_fertilizer_page["Fertilizer<br/>feature page<br/>[page.js]"]
  node_weather_page["Weather<br/>feature page<br/>[page.js]"]
  node_guide_page["Farming guide<br/>content page<br/>[page.js]"]
  node_learn_page["Learn more<br/>content page<br/>[page.js]"]
end

subgraph group_api["API routes"]
  node_weather_api["Weather current<br/>route handler<br/>[route.js]"]
  node_soil_api["Soil analyze<br/>route handler<br/>[route.js]"]
  node_crop_api["Crop recommend<br/>route handler<br/>[route.js]"]
  node_fertilizer_api["Fertilizer recommend<br/>route handler<br/>[route.js]"]
  node_detect_api["Disease detect<br/>route handler<br/>[route.js]"]
  node_history_api["Disease history<br/>route handler<br/>[route.js]"]
end

subgraph group_data["Data layer"]
  node_mongodb[("MongoDB<br/>db helper<br/>[mongodb.js]")]
  node_crop_model["Crop model<br/>mongo model"]
  node_disease_model["Disease model<br/>mongo model"]
  node_fertilizer_model["Fertilizer model<br/>mongo model"]
end

subgraph group_shared["Shared code"]
  node_layout["App shell<br/>layout<br/>[layout.js]"]
  node_styles["Global styles<br/>stylesheet<br/>[globals.css]"]
  node_disease_parser["Disease parser<br/>[diseaseParser.js]"]
end

node_root_page -->|"wrapped by"| node_layout
node_dashboard_page -->|"wrapped by"| node_layout
node_crop_page -->|"wrapped by"| node_layout
node_disease_page -->|"wrapped by"| node_layout
node_fertilizer_page -->|"wrapped by"| node_layout
node_weather_page -->|"wrapped by"| node_layout
node_guide_page -->|"wrapped by"| node_layout
node_learn_page -->|"wrapped by"| node_layout
node_layout -->|"loads"| node_styles
node_dashboard_page -.->|"navigates to"| node_crop_page
node_dashboard_page -.->|"navigates to"| node_disease_page
node_dashboard_page -.->|"navigates to"| node_fertilizer_page
node_dashboard_page -.->|"navigates to"| node_weather_page
node_crop_page -->|"calls"| node_crop_api
node_crop_page -->|"calls"| node_soil_api
node_fertilizer_page -->|"calls"| node_fertilizer_api
node_weather_page -->|"calls"| node_weather_api
node_disease_page -->|"calls"| node_detect_api
node_disease_page -->|"calls"| node_history_api
node_crop_api -->|"persists"| node_mongodb
node_fertilizer_api -->|"persists"| node_mongodb
node_detect_api -->|"normalizes"| node_disease_parser
node_detect_api -->|"persists"| node_mongodb
node_history_api -->|"reads"| node_mongodb
node_crop_api -->|"uses"| node_crop_model
node_fertilizer_api -->|"uses"| node_fertilizer_model
node_detect_api -->|"uses"| node_disease_model
node_weather_api -.->|"optional state"| node_mongodb

click node_root_page "https://github.com/dheerajok/cropadvisor/blob/main/app/page.js"
click node_dashboard_page "https://github.com/dheerajok/cropadvisor/blob/main/app/dashboard/page.js"
click node_crop_page "https://github.com/dheerajok/cropadvisor/blob/main/app/crop-recommendation/page.js"
click node_disease_page "https://github.com/dheerajok/cropadvisor/blob/main/app/disease-detection/page.js"
click node_fertilizer_page "https://github.com/dheerajok/cropadvisor/blob/main/app/fertilizer/page.js"
click node_weather_page "https://github.com/dheerajok/cropadvisor/blob/main/app/weather/page.js"
click node_guide_page "https://github.com/dheerajok/cropadvisor/blob/main/app/farming-guide/page.js"
click node_learn_page "https://github.com/dheerajok/cropadvisor/blob/main/app/learn-more/page.js"
click node_layout "https://github.com/dheerajok/cropadvisor/blob/main/app/layout.js"
click node_styles "https://github.com/dheerajok/cropadvisor/blob/main/app/globals.css"
click node_disease_parser "https://github.com/dheerajok/cropadvisor/blob/main/app/utils/diseaseParser.js"
click node_weather_api "https://github.com/dheerajok/cropadvisor/blob/main/app/api/weather/current/route.js"
click node_soil_api "https://github.com/dheerajok/cropadvisor/blob/main/app/api/soil/analyze/route.js"
click node_crop_api "https://github.com/dheerajok/cropadvisor/blob/main/app/api/crop/recommend/route.js"
click node_fertilizer_api "https://github.com/dheerajok/cropadvisor/blob/main/app/api/fertilizer/recommend/route.js"
click node_detect_api "https://github.com/dheerajok/cropadvisor/blob/main/app/api/disease/detect/route.js"
click node_history_api "https://github.com/dheerajok/cropadvisor/blob/main/app/api/disease/history/route.js"
click node_mongodb "https://github.com/dheerajok/cropadvisor/blob/main/app/db/mongodb.js"
click node_crop_model "https://github.com/dheerajok/cropadvisor/blob/main/app/model/CropRecommendation.js"
click node_disease_model "https://github.com/dheerajok/cropadvisor/blob/main/app/model/DiseaseDetection.js"
click node_fertilizer_model "https://github.com/dheerajok/cropadvisor/blob/main/app/model/FertilizerRecommendation.js"

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_root_page,node_dashboard_page,node_crop_page,node_disease_page,node_fertilizer_page,node_weather_page,node_guide_page,node_learn_page toneBlue
class node_weather_api,node_soil_api,node_crop_api,node_fertilizer_api,node_detect_api,node_history_api toneAmber
class node_mongodb,node_crop_model,node_disease_model,node_fertilizer_model toneMint
class node_layout,node_styles,node_disease_parser toneRose
```

## Components

### UI shell

App Router pages and shared presentation layer

- [Home](https://github.com/dheerajok/cropadvisor/blob/main/app/page.js): landing page · Entry point and primary navigation surface · `app/page.js`
- [Dashboard](https://github.com/dheerajok/cropadvisor/blob/main/app/dashboard/page.js): overview page · Main application overview and feature launcher · `app/dashboard/page.js`
- [Crop rec](https://github.com/dheerajok/cropadvisor/blob/main/app/crop-recommendation/page.js): feature page · Crop recommendation input and results UI · `app/crop-recommendation/page.js`
- [Disease detect](https://github.com/dheerajok/cropadvisor/blob/main/app/disease-detection/page.js): feature page · Leaf image diagnosis interface · `app/disease-detection/page.js`
- [Fertilizer](https://github.com/dheerajok/cropadvisor/blob/main/app/fertilizer/page.js): feature page · Fertilizer guidance interface · `app/fertilizer/page.js`
- [Weather](https://github.com/dheerajok/cropadvisor/blob/main/app/weather/page.js): feature page · Weather lookup and display screen · `app/weather/page.js`
- [Farming guide](https://github.com/dheerajok/cropadvisor/blob/main/app/farming-guide/page.js): content page · Educational farming guidance content · `app/farming-guide/page.js`
- [Learn more](https://github.com/dheerajok/cropadvisor/blob/main/app/learn-more/page.js): content page · Onboarding and project information page · `app/learn-more/page.js`

### API routes

Server-side domain endpoints under app/api

- [Weather current](https://github.com/dheerajok/cropadvisor/blob/main/app/api/weather/current/route.js): route handler · Server-side weather data adapter · `app/api/weather/current/route.js`
- [Soil analyze](https://github.com/dheerajok/cropadvisor/blob/main/app/api/soil/analyze/route.js): route handler · Server-side soil analysis endpoint · `app/api/soil/analyze/route.js`
- [Crop recommend](https://github.com/dheerajok/cropadvisor/blob/main/app/api/crop/recommend/route.js): route handler · Server-side crop recommendation endpoint · `app/api/crop/recommend/route.js`
- [Fertilizer recommend](https://github.com/dheerajok/cropadvisor/blob/main/app/api/fertilizer/recommend/route.js): route handler · Server-side fertilizer recommendation endpoint · `app/api/fertilizer/recommend/route.js`
- [Disease detect](https://github.com/dheerajok/cropadvisor/blob/main/app/api/disease/detect/route.js): route handler · Server-side disease detection endpoint · `app/api/disease/detect/route.js`
- [Disease history](https://github.com/dheerajok/cropadvisor/blob/main/app/api/disease/history/route.js): route handler · Read-side disease detection history endpoint · `app/api/disease/history/route.js`

### Data layer

MongoDB helper and persisted models

- [MongoDB](https://github.com/dheerajok/cropadvisor/blob/main/app/db/mongodb.js): db helper · Shared database connection helper · `app/db/mongodb.js`
- [Crop model](https://github.com/dheerajok/cropadvisor/blob/main/app/model/CropRecommendation.js): mongo model · Stored crop recommendation records · `app/model/CropRecommendation.js`
- [Disease model](https://github.com/dheerajok/cropadvisor/blob/main/app/model/DiseaseDetection.js): mongo model · Stored disease detection records · `app/model/DiseaseDetection.js`
- [Fertilizer model](https://github.com/dheerajok/cropadvisor/blob/main/app/model/FertilizerRecommendation.js): mongo model · Stored fertilizer recommendation records · `app/model/FertilizerRecommendation.js`

### Shared code

Cross-cutting utilities and global app files

- [App shell](https://github.com/dheerajok/cropadvisor/blob/main/app/layout.js): layout · Shared Next.js layout and framing · `app/layout.js`
- [Global styles](https://github.com/dheerajok/cropadvisor/blob/main/app/globals.css): stylesheet · Application-wide styling · `app/globals.css`
- [Disease parser](https://github.com/dheerajok/cropadvisor/blob/main/app/utils/diseaseParser.js): utility · Normalizes disease detection output · `app/utils/diseaseParser.js`

## Connections

- Home → App shell: wrapped by · Home page uses the shared app shell
- Dashboard → App shell: wrapped by · Dashboard renders inside the shared shell
- Crop rec → App shell: wrapped by · Feature page uses shared layout
- Disease detect → App shell: wrapped by · Feature page uses shared layout
- Fertilizer → App shell: wrapped by · Feature page uses shared layout
- Weather → App shell: wrapped by · Feature page uses shared layout
- Farming guide → App shell: wrapped by · Content page uses shared layout
- Learn more → App shell: wrapped by · Content page uses shared layout
- App shell → Global styles: loads · Layout applies the global stylesheet
- Dashboard → Crop rec: navigates to · Dashboard links into crop recommendation
- Dashboard → Disease detect: navigates to · Dashboard links into disease detection
- Dashboard → Fertilizer: navigates to · Dashboard links into fertilizer guidance
- Dashboard → Weather: navigates to · Dashboard links into weather lookup
- Crop rec → Crop recommend: calls · Crop UI submits inputs to the recommendation endpoint
- Crop rec → Soil analyze: calls · Crop UI can request soil analysis as part of recommendation
- Fertilizer → Fertilizer recommend: calls · Fertilizer UI requests recommendations from the server
- Weather → Weather current: calls · Weather UI fetches current conditions
- Disease detect → Disease detect: calls · Disease UI submits image or diagnosis payload
- Disease detect → Disease history: calls · Disease UI reads past detections
- Crop recommend → MongoDB: persists · Crop recommendations can be stored through MongoDB helper
- Fertilizer recommend → MongoDB: persists · Fertilizer recommendations can be stored through MongoDB helper
- Disease detect → Disease parser: normalizes · Detection output is parsed into structured data
- Disease detect → MongoDB: persists · Detection results are stored in MongoDB
- Disease history → MongoDB: reads · History endpoint queries stored detection records
- Crop recommend → Crop model: uses · Crop route works with crop recommendation records
- Fertilizer recommend → Fertilizer model: uses · Fertilizer route works with fertilizer recommendation records
- Disease detect → Disease model: uses · Detection route works with disease records
- Weather current → MongoDB: optional state · Weather route may use shared persistence or caching
