Feature: Disease & Pest Prediction
Relevant source files
The following files were used as context for generating this wiki page:
The Disease & Pest Prediction feature is an AI-powered system designed to provide proactive warnings and management recommendations for common grape diseases and pests. It analyzes weather data, farm-specific conditions, and historical patterns to generate daily risk scores and actionable alerts, enabling farmers to take preventive measures, optimize treatments, and reduce crop loss. This system is a core component of the "Phase 3: Advanced AI Intelligence" initiative, moving the application from a reactive tool to a proactive advisory service.
The system is composed of two main services: a client-side DiseasePredictionModel for immediate risk assessment and a backend PestPredictionService that leverages weather forecasts and historical data for more in-depth, persistent predictions.
Sources: context/PRD.md:200-206, src/lib/disease-prediction.ts, src/lib/pest-prediction-service.ts, app/help/page.tsx:132-137
System Architecture
The prediction system combines two distinct but complementary models to assess and alert farmers about potential threats. The DiseasePredictionModel provides a synchronous, in-memory calculation based on current inputs, while the PestPredictionService offers an asynchronous, more comprehensive analysis that includes weather forecasts and community data, with results persisted in the database.
The following diagram illustrates the high-level data flow for generating pest and disease predictions.
Prediction Services
The core logic is split between two primary services, each with a specific focus.
DiseasePredictionModel
This is a static class that performs synchronous, in-memory disease risk calculations based on a comprehensive set of inputs. It is designed for on-the-fly analysis without database interaction.
Sources: src/lib/disease-prediction.ts:179-181
Key Components
predictDiseaseRisk(inputs: DiseaseRiskInputs): The main static method that orchestrates the analysis. It iterates through a predefined set of disease models, calculates the risk for each, and aggregates the results.DISEASE_MODELS: A constant object containing the specific parameters for each disease, such as optimal temperature, humidity, and susceptible growth stages.- Risk Calculation: The risk score for each disease is a weighted sum of several factors:
- Temperature Factor
- Humidity Factor
- Moisture Factor (Rainfall & Leaf Wetness)
- Growth Stage Susceptibility
- Grape Variety Resistance
- Vineyard Conditions (e.g., canopy density)
- Previous Treatments
Sources: src/lib/disease-prediction.ts:80-177, src/lib/disease-prediction.ts:200-246
Data Structures
The model relies on a set of well-defined interfaces for its inputs and outputs.
This diagram shows the DiseasePredictionModel class and its relationship with the main input and output data structures.
Sources: src/lib/disease-prediction.ts:6-77
PestPredictionService
This service provides a more robust, asynchronous prediction mechanism that integrates with external services like a weather provider (OpenMeteoWeatherService) and the Supabase database. Its predictions are persisted and can be retrieved for dashboards and alerts.
Sources: src/lib/pest-prediction-service.ts:13-14
Prediction Generation Flow
The generatePredictions method follows a clear sequence to produce risk assessments.
This sequence diagram details the steps taken by PestPredictionService to generate and store predictions.
Sources: src/lib/pest-prediction-service.ts:241-267
Key Components
generatePredictions(farmId, farmData): The primary method that fetches weather forecasts and historical data to calculate and save predictions.PEST_RISK_FACTORS: A configuration object defining risk parameters for pests and diseases like Downy Mildew, Powdery Mildew, and Thrips. It includes optimal weather conditions, historical multipliers, and seasonal factors.TREATMENT_RECOMMENDATIONS: A database of chemical, organic, and cultural treatment options for each pest/disease, including details on products, dosage, and cost.- Community Intelligence: The service enhances predictions by incorporating a
communityRiskfactor, which is calculated based on active predictions from other farms in the same region. Risk increases by 10% for each active community report.
Sources: src/lib/pest-prediction-service.ts:17-238, src/lib/pest-prediction-service.ts:413-435
Data Models
The feature relies on both application-level TypeScript interfaces and a database schema for persistence.
pest_disease_predictions Table
Predictions generated by the PestPredictionService are stored in the pest_disease_predictions table in Supabase.
This ER diagram shows the schema for the pest_disease_predictions table and its relation to the farms table.
Sources: context/CLAUDE.md:65-87, src/lib/pest-prediction-service.ts:457-476
Key Interfaces
The system uses several key interfaces to manage data flow.
| Interface | Description | Source File |
|---|---|---|
DiseaseRiskInputs | Defines the input data required by DiseasePredictionModel, including weather, grape variety, and more. | src/lib/disease-prediction.ts:11-32 |
DiseasePredictionResults | The output of the DiseasePredictionModel, containing overall risk, alerts, and a treatment calendar. | src/lib/disease-prediction.ts:48-59 |
PestDiseasePrediction | The primary data structure for a single prediction, used by PestPredictionService and stored in the DB. | context/TYPES_AND_INTERFACES.md:188-228 |
PestRiskFactors | Defines the configuration for calculating risk for each pest, including weights and thresholds. | src/lib/pest-prediction-service.ts:6-9 |
Risk Models & Factors
The prediction logic is driven by configurable models for each disease and pest.
Disease Models (disease-prediction.ts)
The DISEASE_MODELS object defines the conditions conducive to diseases like Powdery Mildew, Downy Mildew, and Botrytis.
| Disease | Optimal Temp (°C) | Optimal Humidity (%) | Rain Required | Leaf Wetness (hrs) |
|---|---|---|---|---|
| Powdery Mildew | 20 - 27 | 40 - 70 | No | 0 |
| Downy Mildew | 13 - 23 | 85 - 100 | Yes | 6 |
| Botrytis | 15 - 25 | 80 - 100 | Yes | 12 |
| Black Rot | 20 - 30 | 80 - 100 | Yes | 8 |
| Anthracnose | 2 - 30 | 85 - 100 | Yes | 10 |
Source: src/lib/disease-prediction.ts:80-177
Pest Risk Factors (pest-prediction-service.ts)
The PEST_RISK_FACTORS object defines a weighted model that also includes seasonal adjustments and historical data.
Example: Downy Mildew Risk Factors
| Factor | Parameter | Weight |
|---|---|---|
| Temperature | Optimal range: 15-25°C | 0.3 |
| Humidity | Threshold: >85% | 0.4 |
| Rainfall | >10mm over 3 days | 0.3 |
| Modifiers | ||
| Historical Multiplier | Multiplies risk by 1.2 if historical | N/A |
| Seasonal Factor | Varies by month (e.g., June: 1.0, Dec: 0.1) | N/A |
Source: src/lib/pest-prediction-service.ts:18-37
Conclusion
The Disease & Pest Prediction feature is a sophisticated, multi-faceted system that provides critical, time-sensitive advice to farmers. By combining a rapid, client-side assessment model with a more comprehensive, data-rich backend service, it delivers a layered approach to risk management. The system's architecture is designed to be extensible, with clear data structures and configurable risk models, allowing it to adapt and improve as more data is collected. This feature is central to VineSight's goal of empowering farmers with proactive, AI-driven insights for sustainable and profitable agriculture.