Feature: Reporting & Data Export
Relevant source files
The following files were used as context for generating this wiki page:
The Reporting and Data Export feature is a core component of VineSight, designed to provide users with comprehensive insights into their farm operations and to facilitate compliance and financial analysis. It allows farmers to view, analyze, and export their logged data—including irrigation, spraying, harvesting, and expenses—into standardized formats like PDF and CSV. This functionality replaces manual record-keeping and supports data-driven decision-making.
Sources: context/PRD.md:47-50, src/app/reports/page.tsx, src/lib/export-service.ts
Architecture Overview
The feature is composed of a user-facing React component, which interacts with dedicated backend services for data aggregation and file generation. The UnifiedReportsPage component acts as the primary user interface, while ExportService and ReportingService handle the business logic for creating exportable files.
The following diagram illustrates the high-level data flow for generating and exporting a report.
Reporting User Interface
The primary user interface for this feature is the UnifiedReportsPage component, located at src/app/reports/page.tsx. This page allows users to configure, preview, and export their farm data.
Sources: src/app/reports/page.tsx
Key UI Components & State
- Farm and Date Selection: Users can select a specific farm and a date range for the report.
- Report Preview: A summary of key metrics is generated, including total records, water usage, total harvest, and net profit.
- Data Tabs: Records are categorized into tabs for Irrigation, Spray, Harvest, and Expense, with pagination for browsing.
- Export Buttons: Dedicated buttons to trigger PDF or CSV export.
The component manages its state using React hooks:
| State Variable | Type | Description |
|---|---|---|
farms | Farm[] | List of all farms available to the user. |
selectedFarm | Farm | The currently selected farm for reporting. |
reportData | ReportPreview | The aggregated data and summary for the preview. |
exportOptions | Partial<ExportOptions> | Configuration for the export, including date range, format, and report type. |
loading | boolean | Indicates when data is being fetched or an export is in progress. |
currentPage | number | Manages pagination for the data preview tabs. |
activeTab | keyof RecordData | Tracks the currently selected data category (e.g., 'irrigation'). |
Sources: src/app/reports/page.tsx:39-60
Data Flow for Report Preview
When a user selects a farm or changes the date range, the generatePreview function is called. This triggers a sequence of operations to fetch and process the data for display.
This diagram shows the sequence of events when a user generates a report preview on the reports page.
Sources: src/app/reports/page.tsx:100-108, src/app/reports/page.tsx:128-239
Local vs. Service-Based Export
The reports/page.tsx component contains logic to handle exports differently based on the environment.
- Development/Bypass Mode: It uses local functions (
exportToCSV,exportToPDF) that operate on thereportDataalready loaded in the component's state. This is useful for testing and rapid development. - Production Mode: It delegates the export process to the
ExportService, passing the configuredExportOptions.
Sources: src/app/reports/page.tsx:472-512
Data Export Service
The ExportService class is the primary engine for generating data exports. It is designed to be a self-contained module that fetches data from SupabaseService and formats it into either CSV or PDF.
Sources: src/lib/export-service.ts
Core Class and Methods
This diagram shows the static methods within the ExportService class, outlining its public and private API for handling data exports.
Sources: src/lib/export-servicets:32-683
Export Configuration
The exportData method is configured using the ExportOptions interface.
| Parameter | Type | Description |
|---|---|---|
farmId | number | The ID of the farm to export data for. |
dateRange | { from: string, to: string } | The start and end dates for the data query. |
includeTypes | string[] | An array of record types to include (e.g., ['irrigation', 'spray']). |
format | 'csv' | 'pdf' | The desired output format. |
reportType | 'operations' | 'financial' | 'compliance' | 'comprehensive' | The type of report, which determines the data sections included. |
Sources: src/lib/export-service.ts:16-25
Data Gathering Process
The gatherExportData method is responsible for fetching all necessary records from the database via SupabaseService based on the ExportOptions.
This flowchart illustrates how gatherExportData conditionally fetches and filters different types of farm records.
Sources: src/lib/export-service.ts:73-143
PDF Generation
PDFs are generated using jsPDF and jspdf-autotable. The service dynamically adds sections to the PDF based on the reportType.
- Header: Includes the report title, farm name, and date range.
- Farm Details: A table summarizing the farm's properties.
- Data Sections: Tables for operational, financial, or compliance data are added with themed headers.
- Summary Statistics: A final table summarizing key metrics like total revenue, expenses, and net profit.
- Pagination: The service includes logic to automatically add new pages to prevent content overflow.
Sources: src/lib/export-service.ts:289-609
Reporting Service & Data Types
The ReportingService is another service dedicated to generating more structured, formal reports, such as for compliance purposes. Note: This service is marked as "temporarily disabled for deployment" in the source code.
Sources: src/lib/reporting-service.ts:20-31
Report Data Structures
The system defines a rich set of TypeScript interfaces in src/lib/reporting-types.ts to model various reports and their contents.
This diagram shows the inheritance and composition relationships between the main report interfaces.
Sources: src/lib/reporting-types.ts:1-45
Key Report Types
The reporting-types.ts file defines several specific data structures for compliance and analysis:
PesticideUsageRecord: Detailed log of pesticide applications, including active ingredients, rates, and safety intervals.OrganicComplianceData: Tracks information relevant to organic certification, such as inputs used and buffer zone compliance.WaterUsageCompliance: Monitors water usage against allocated amounts and tracks quality test results.SoilHealthReport: Contains detailed soil sample analysis, including pH, nutrients, and organic matter.HarvestRecord: Logs harvest details, including quantity, quality, sugar content, and buyer information.
Sources: src/lib/reporting-types.ts:47-149
Conclusion
The Reporting & Data Export feature is a robust system that empowers users to transform raw operational data into actionable insights and professional documentation. It leverages a modular architecture with a clear separation between the user interface and backend services. The use of dedicated services like ExportService and ReportingService, along with well-defined data types, creates a scalable and maintainable system for handling various report formats and complex data aggregation requirements. The dual-path export logic in the UI also demonstrates a thoughtful approach to balancing development speed with production-ready reliability.