Dashboards & Analytics
Relevant source files
The following files were used as context for generating this wiki page:
The Dashboards & Analytics system in DRX CRM provides real-time business intelligence and operational oversight for small businesses and administrators. The system is designed to transform raw transactional, inventory, and customer data into actionable insights through various specialized interfaces, including the main User Dashboard, the AI Intelligence Dashboard, and the Enterprise Admin Console.
The primary goal of these analytics is to ensure financial correctness, maintain inventory traceability, and provide AI-powered recommendations for pricing and restocking. By utilizing concurrent data fetching and client-side processing, the system maintains high performance even with large datasets.
Sources: README.md:10-15, app/(app)/dashboard/page.tsx:43-55
Dashboard Architecture & Data Flow
The dashboard system follows a centralized data-fetching pattern where business-specific metrics are aggregated and processed before being rendered in the UI.
Data Aggregation Logic
The main dashboard fetches data concurrently from multiple Supabase tables to minimize loading times. It aggregates information regarding sales, active customer counts, inventory levels, and outstanding credit balances.
The diagram shows the concurrent request architecture used to populate the main dashboard interface.
Sources: app/(app)/dashboard/page.tsx:43-85
Core Metrics Calculation
Data retrieved from the backend undergoes client-side transformation to calculate key performance indicators (KPIs).
| Metric | Calculation Logic | Data Source |
|---|---|---|
| Total Sales (30d) | Sum of total_amount where order_status is "completed" and created_at is within the last 30 days. | sales table |
| Active Customers | Exact count of records where is_active is true. | customers table |
| Inventory Items | Total count of distinct items in the catalog. | inventory table |
| Credit Outstanding | Sum of current_balance across all customer credit profiles. | credit_limits table |
Sources: app/(app)/dashboard/page.tsx:56-85, tests/dashboard-data-processing.unit.test.ts:16-34
AI Intelligence & Insights
The AI Intelligence Dashboard provides predictive analytics and automated business monitoring. It focuses on identifying critical operational risks and optimization opportunities.
Intelligence Metrics
The system monitors specialized AI tables to surface recommendations:
- Pricing Recommendations: Pending price updates generated by AI to optimize margins.
- Critical Alerts: High-severity inventory alerts that have not been acknowledged.
- Urgent Restocks: Forecasted stockouts with high or critical restock urgency.
Sources: components/ai/ai-intelligence-dashboard.tsx:36-55
Analysis Execution Flow
Users can manually trigger a system-wide AI analysis through a dedicated interface.
The sequence diagram illustrates the process of triggering and receiving AI-driven business analysis.
Sources: components/ai/ai-intelligence-dashboard.tsx:64-85
Admin & Enterprise Analytics
For system-wide oversight, the Admin Console aggregates statistics across all businesses and system logs.
Administrative Statistics
The Admin Console tracks system health and scale through four primary categories:
- Businesses: Total count of business profiles registered in the system.
- Users: Total count of user roles across all tenants.
- Audit Logs: Total volume of system audit entries for compliance tracking.
- AI Failures: Count of failed AI insight generations for technical monitoring.
Sources: app/(app)/admin/page.tsx:38-60
Implementation Details
Data Processing Functions
The system uses utility functions to ensure data integrity, particularly handling null values or missing data gracefully.
// From tests/dashboard-data-processing.unit.test.ts
const processDashboardData = (data: any) => {
const {
salesData = [],
customerCount = 0,
inventoryCount = 0,
creditData = [],
recentSales = [],
} = data;
const totalSales = salesData?.reduce((sum, sale) => sum + (sale.total_amount || 0), 0) || 0;
const creditOutstanding = creditData?.reduce((sum, credit) => sum + (credit.current_balance || 0), 0) || 0;
return {
totalSales,
activeCustomers: customerCount,
inventoryItems: inventoryCount,
creditOutstanding,
recentActivity: recentSales ? [...recentSales] : [],
};
};
Sources: tests/dashboard-data-processing.unit.test.ts:16-34, tests/data-processing-vitest.test.ts:40-55
Error Handling & States
Dashboards implement multi-state rendering to handle various data scenarios:
- Loading State: Uses
DashboardSkeletonwhile data is being fetched. - Error State: Displays an
ErrorStatecomponent with a retry mechanism if API calls fail. - Empty State: Shown when no business is found or no sales activity exists.
Sources: app/(app)/dashboard/page.tsx:96-125
Summary
The Dashboards & Analytics module serves as the central intelligence hub for DRX CRM. By aggregating real-time data from sales, inventory, and credit systems, it provides both granular operational metrics for business owners and high-level health statistics for system administrators. The integration of AI-driven insights further enhances the system by providing proactive recommendations for pricing and inventory management, ensuring businesses can operate with high efficiency and data-driven precision.