Comprehensive analytics engine with KPI dashboards, predictive insights, spend analysis, vendor intelligence, and AI-powered recommendations for procurement optimization.
The Analytics & AI Intelligence module is the decision-support backbone of ProKure. It aggregates procurement data across all modules, computes real-time KPIs, detects anomalies, predicts prices and delivery risks, and provides AI-driven vendor recommendations. The module powers executive dashboards, scheduled reports, and a Phase 2 natural language query interface.
The analytics module uses a dedicated PostgreSQL analytics schema for structured metrics and materialized views, plus MongoDB collections for AI model management, training datasets, and flexible document storage.
Stores computed KPI values at regular intervals for dashboard display and trend analysis.
Indexes: (tenant_id, kpi_code, snapshot_date), (tenant_id, period_type, snapshot_date), (dimension_key, dimension_value)
Aggregated spend data by vendor, category, project, department, and period for drill-down analysis.
Indexes: (tenant_id, analysis_period_start), (vendor_id), (category_id), (project_id)
Historical price records from bids, POs, contracts, and market data for trend analysis and prediction training.
Indexes: (tenant_id, material_id, effective_date), (vendor_id, material_id), (source_type)
ML-generated price forecasts with confidence scores and post-facto accuracy tracking.
Indexes: (tenant_id, material_id, prediction_date), (model_version)
AI-generated vendor suggestions ranked by composite score for purchase requisitions.
Indexes: (tenant_id, pr_id), (recommended_vendor_id), (material_id)
Detected anomalies across procurement data with severity classification and acknowledgement tracking.
Indexes: (tenant_id, anomaly_type, detected_at), (entity_type, entity_id), (severity, is_acknowledged)
End-to-end procurement cycle timestamps and duration calculations per PR/PO.
Indexes: (tenant_id, pr_id), (po_type), (project_id), (total_cycle_days)
ML-powered project budget forecasts with variance analysis and risk classification.
Indexes: (tenant_id, project_id, forecast_date)
Predictive delivery risk scores for open POs based on multi-factor ML model.
Indexes: (tenant_id, po_id), (vendor_id), (risk_level)
Refreshes: Every 6 hours via Hangfire
Refreshes: Every 6 hours
Refreshes: Daily
Refreshes: Every 2 hours
Training data prepared for ML model training. TTL: None (persistent)
Registry of all trained AI/ML models with versioning and deployment status. TTL: None
Audit trail of all predictions made by AI models. TTL: 180 days
Natural language queries and their parsed SQL translations. TTL: 365 days
Mobile push notification tracking for analytics alerts. TTL: 90 days
Per-user dashboard preferences, pinned KPIs, and alert settings. TTL: None
The KPI engine calculates six core metrics every 5 minutes via Hangfire, stores snapshots for trend analysis, caches results in Redis, and pushes updates to connected dashboards via SignalR.
Average days from PR creation to PO release. Target: < 14 days. Alert: > 21 days.
-- Average days from PR creation to PO release SELECT AVG(total_cycle_days) AS avg_cycle_days FROM analytics.procurement_cycle_metrics WHERE tenant_id = :tenant_id AND pr_created_at >= :period_start AND pr_created_at < :period_end; -- Target: < 14 days -- Alert: > 21 days
Savings = (L1 Bid Amount - Awarded Amount) / L1 Bid Amount * 100. Target: > 5%.
-- Savings = (L1 Bid Amount - Awarded Amount) / L1 Bid Amount * 100 SELECT SUM(l1_bid_total - awarded_total) / NULLIF(SUM(l1_bid_total), 0) * 100 AS savings_pct FROM analytics.spend_analysis WHERE tenant_id = :tenant_id AND analysis_period_start >= :start; -- Target: > 5%
Deliveries on or before committed date / Total deliveries * 100. Target: > 95%.
-- Deliveries on or before committed date / Total deliveries * 100 SELECT COUNT(*) FILTER (WHERE actual_delivery_date <= committed_delivery_date) * 100.0 / NULLIF(COUNT(*), 0) AS on_time_pct FROM analytics.delivery_risk_scores WHERE tenant_id = :tenant_id AND actual_delivery_date IS NOT NULL; -- Target: > 95%
GRN items passing quality check on first inspection. Target: > 90%.
SELECT COUNT(*) FILTER (WHERE qc_result = 'PASSED') * 100.0 / NULLIF(COUNT(*), 0) AS first_pass_pct FROM procurement.grn_items WHERE tenant_id = :tenant_id; -- Target: > 90%
Average days from invoice submission to approval. Target: < 7 days.
SELECT AVG(EXTRACT(EPOCH FROM (approved_at - submitted_at)) / 86400) AS avg_days FROM finance.invoices WHERE tenant_id = :tenant_id AND status = 'APPROVED'; -- Target: < 7 days
Mean vendor performance score across all active vendors. Target: > 4.0 / 5.0.
SELECT AVG(overall_score) AS avg_rating FROM vendor.vendor_scores WHERE tenant_id = :tenant_id AND period_type = 'MONTHLY'; -- Target: > 4.0 / 5.0
Hangfire Scheduler (every 5 min)
|
v
Calculate KPIs (SQL queries)
|
v
Store in kpi_snapshots
|
v
Cache in Redis (5-min TTL)
|
v
Dashboard API reads from Redis
|
v
SignalR pushes updates to UI
The Spend Analysis Engine aggregates procurement spend across vendors, categories, projects, and departments. It classifies spend types and refreshes materialized views on a schedule.
PO Released --> Trigger spend_analysis update
|
v
Aggregate by: Vendor, Category, Project, Department, Period
|
v
Calculate: Total Spend, Savings, Maverick %, Contract %
|
v
Store in spend_analysis table
|
v
Refresh mv_vendor_spend_summary (6h)
|
v
Refresh mv_category_spend_summary (6h)
| Spend Type | Definition | Column |
|---|---|---|
| Contract Spend | PO linked to an active contract | contract_spend |
| Direct Spend | PO via DIRECT path (no RFQ) | direct_spend |
| Spot Spend | PO via SPOT path | spot_spend |
| Maverick Spend | PO outside approved contracts/catalogs | maverick_spend |
ML-powered price forecasting uses historical bid, PO, and contract data to predict future material prices with confidence scoring.
1. Data Collection +-- analytics.price_history (historical bids, POs, contracts) 2. Feature Engineering +-- Material category +-- Vendor region +-- Order quantity +-- Season/month +-- Historical price trend (3mo, 6mo, 12mo) +-- Market index (if available) +-- Inflation rate 3. Model Training (Monthly via Hangfire) +-- Algorithm: Ensemble (Linear Regression + ARIMA) +-- Train/Test: 80/20 split +-- Validation: Cross-validation (5-fold) +-- Store model in ai_model_registry 4. Prediction +-- Input: material_id, quantity, target_date +-- Output: predicted_price, confidence_score +-- Store in price_predictions table 5. Accuracy Tracking +-- Compare predicted vs actual (post-PO) +-- Update accuracy_pct +-- Retrain if accuracy < 80%
AI-driven vendor suggestions based on a weighted composite scoring algorithm combining performance, price, delivery, and quality factors.
Vendor Score = Sum(Weight x Normalized Score)| Factor | Weight | Calculation |
|---|---|---|
| Performance Rating | 35% | vendor_scores.overall_score |
| Price Competitiveness | 30% | 1 - (avg_price / max_price) |
| Delivery Reliability | 20% | on_time_deliveries / total |
| Quality Score | 15% | first_pass_qc_rate |
PR Approved --> Extract material_ids
|
v
Query vendor_recommendations cache
|
v
If stale (>24h) --> Recalculate:
+-- Get eligible vendors per material
+-- Calculate score per vendor
+-- Rank top 5
+-- Store in vendor_recommendations
|
v
Return ranked list to UI
|
v
User accepts/rejects --> Track in is_accepted
Automated detection of unusual patterns in procurement data using both rule-based and ML-based approaches.
| Anomaly Type | Detection Method | Trigger Condition | Severity |
|---|---|---|---|
| PRICE_SPIKE | Z-score > 2.5 on bid price vs material avg | Bid price > mean + 2.5 sigma | HIGH |
| QUANTITY_UNUSUAL | IQR method on PR quantities | Qty outside Q1-1.5xIQR to Q3+1.5xIQR | MEDIUM |
| VENDOR_PATTERN | Frequency analysis | Same vendor awarded >60% in category | MEDIUM |
| APPROVAL_DELAY | SLA breach detection | Approval pending > configured SLA hours | HIGH |
| DELIVERY_RISK | ML risk score | risk_score > 0.7 | HIGH |
| SPEND_SURGE | Month-over-month comparison | Spend > 150% of 3-month rolling avg | CRITICAL |
Real-Time Events (PO, Bid, Invoice)
|
v
Anomaly Detection Service
+-- Rule-Based Engine (immediate)
| +-- Z-score, IQR, threshold checks
+-- ML-Based Engine (batch, daily)
+-- Isolation Forest, clustering
|
v
Store in anomaly_detections
|
v
If severity HIGH/CRITICAL --> Push notification
|
v
Dashboard: Anomaly inbox for review
Predictive delivery risk model scores open POs daily to proactively identify potential delays.
Risk Score (0.0 - 1.0) = Sum(Factor Weight x Factor Score)| Factor | Weight | Data Source |
|---|---|---|
| Vendor History | 30% | Past on-time % for this vendor |
| Material Lead Time | 20% | Historical lead time data |
| Order Complexity | 15% | Line items, custom specs |
| Vendor Capacity | 15% | Concurrent POs count |
| Season/Weather | 10% | Month, region factors |
| Distance | 10% | Vendor location to delivery |
| Score Range | Risk Level | Color |
|---|---|---|
| 0.0 - 0.3 | LOW | Green |
| 0.3 - 0.5 | MEDIUM | Yellow |
| 0.5 - 0.7 | HIGH | Orange |
| 0.7 - 1.0 | CRITICAL | Red |
ML-powered budget forecasting predicts final project costs by combining committed, spent, and predicted remaining amounts.
Forecasted Total = Committed + Spent + Predicted Remaining
Where:
- Committed = SUM(approved PO amounts not yet invoiced)
- Spent = SUM(paid invoice amounts)
- Predicted Remaining = ML prediction based on:
+-- Historical burn rate
+-- Pending PR amounts
+-- Open RFQ expected values
+-- Category price trends
Users can ask procurement questions in plain English. The NLP engine parses intent, extracts entities, generates SQL, and returns results.
// Example 1 User Query: "Show top 5 vendors by spend this quarter" Intent: SPEND_ANALYSIS Entities: [top_n: 5, dimension: vendor, period: this_quarter] SQL: SELECT vendor_name, total_spend FROM analytics.mv_vendor_spend_summary WHERE tenant_id = :t ORDER BY total_spend_ytd DESC LIMIT 5 // Example 2 User Query: "Which materials had price increase >10% last month?" Intent: PRICE_TREND Entities: [metric: price_change, threshold: 10%, period: last_month] SQL: SELECT material_name, price_change_pct FROM analytics.price_history ... // Example 3 User Query: "What's the delivery risk for PO-2026-00145?" Intent: DELIVERY_STATUS Entities: [entity: PO, id: PO-2026-00145] SQL: SELECT * FROM analytics.delivery_risk_scores WHERE po_id = ...
Push notification rules and mobile-specific analytics features for on-the-go procurement management.
| Notification Type | Trigger | Recipient |
|---|---|---|
| KPI_ALERT | KPI breaches threshold | Dashboard owner |
| ANOMALY_ALERT | Critical anomaly detected | Procurement admin |
| APPROVAL_PENDING | Approval waiting >4h | Approver |
| DELIVERY_UPDATE | ASN dispatched | Receiving team |
| BUDGET_WARNING | Budget >80% | Project manager |
Standard report catalog with data sources, refresh schedules, and export formats.
| Report ID | Report Name | Data Source | Refresh | Export |
|---|---|---|---|---|
| RPT-001 | PR Status Summary | procurement.purchase_requests | Real-time | CSV, PDF |
| RPT-002 | RFQ Analysis | procurement.rfqs + bids | Real-time | CSV, PDF, XLSX |
| RPT-003 | PO Register | procurement.purchase_orders | Real-time | CSV, PDF, XLSX |
| RPT-004 | Vendor Performance | vendor.vendor_scores | Monthly | |
| RPT-005 | Spend Analysis | analytics.spend_analysis | 6h | CSV, XLSX |
| RPT-006 | Price Trend | analytics.price_history | Daily | CSV, PDF |
| RPT-007 | Procurement Cycle Time | analytics.procurement_cycle_metrics | Daily | |
| RPT-008 | Budget vs Actual | analytics.budget_forecasts | 2h | |
| RPT-009 | Anomaly Report | analytics.anomaly_detections | Real-time | |
| RPT-010 | Delivery Risk | analytics.delivery_risk_scores | Daily | |
| RPT-011 | MSME Compliance | vendor + finance tables | Monthly | |
| RPT-012 | Invoice Aging | finance.invoices | Daily | CSV, PDF |
| RPT-013 | Savings Dashboard | analytics.spend_analysis | 6h | |
| RPT-014 | Vendor Recommendation | analytics.vendor_recommendations | 24h | |
| RPT-015 | Project Budget Status | analytics.mv_project_budget_status | 2h |
Redis-based caching layer for analytics endpoints with configurable TTLs per data type.
Cache Key Pattern: prokure:{tenant_id}:analytics:{type}:{params_hash} KPI Dashboard: TTL = 5 minutes Spend Summary: TTL = 30 minutes Vendor Rankings: TTL = 1 hour Price Predictions: TTL = 6 hours Report Cache: TTL = custom (stored in report_cache MongoDB) Materialized Views: Refreshed by Hangfire schedule
RESTful API endpoints for the analytics module with caching details.
| Method | Endpoint | Description | Cache |
|---|---|---|---|
| GET | /api/analytics/dashboard | Executive KPI dashboard | 5min Redis |
| GET | /api/analytics/kpis/{code} | Single KPI with history | 5min |
| GET | /api/analytics/spend | Spend analysis with filters | 30min |
| GET | /api/analytics/spend/drill-down | Drill by vendor/category/period | 30min |
| GET | /api/analytics/price-trend/{materialId} | Price history + prediction | 6h |
| GET | /api/analytics/vendor-recommendations/{prId} | AI vendor suggestions | 24h |
| GET | /api/analytics/anomalies | Anomaly inbox | Real-time |
| POST | /api/analytics/anomalies/{id}/acknowledge | Mark anomaly reviewed | - |
| GET | /api/analytics/delivery-risk/{poId} | PO delivery risk score | Daily |
| GET | /api/analytics/budget-forecast/{projectId} | Budget forecast | 2h |
| GET | /api/analytics/cycle-metrics | Procurement cycle analysis | Daily |
| POST | /api/analytics/reports/generate | Generate report | - |
| GET | /api/analytics/reports/{id}/download | Download report | - |
| POST | /api/analytics/nlp-query | Natural language query (Phase 2) | - |
| POST | /api/analytics/crm-sync | CRM sync-back | - |
| GET | /api/analytics/vendor-rating/{vendorId} | Vendor rating details | 1h |
Background job definitions for analytics data processing, model training, and report generation.
| Job | Schedule | Action |
|---|---|---|
| RefreshKpiSnapshots | Every 5 minutes | Calculate all 6 KPIs, store snapshots |
| RefreshSpendAnalysis | Every 6 hours | Aggregate spend by all dimensions |
| RefreshMaterializedViews | mv_vendor: 6h, mv_category: 6h, mv_monthly: daily, mv_budget: 2h | Refresh PostgreSQL materialized views |
| CalculateVendorRatings | Monthly (1st) | Recalculate all vendor scores |
| TrainPriceModels | Monthly (15th) | Retrain price prediction models |
| DetectAnomalies | Daily (2 AM) | Run ML anomaly detection batch |
| ScoreDeliveryRisks | Daily (6 AM) | Score all open POs |
| ForecastBudgets | Daily (4 AM) | Update project budget forecasts |
| GenerateScheduledReports | Per report config | Generate and email scheduled reports |
| CleanupPredictionLogs | Weekly (Sunday) | TTL cleanup for MongoDB collections |
| RefreshVendorRecommendations | Daily (3 AM) | Pre-compute recommendations for open PRs |
| SyncCrmData | On event + Daily (midnight) | Sync project/PO data to CRM |
Core business rules governing the analytics and AI intelligence module.
AI agents that can autonomously execute procurement workflows with human-in-the-loop oversight. Inspired by SAP Joule, Coupa Navi, and Ivalua IVA capabilities for next-generation intelligent procurement.
Registered AI agents with their capabilities, permissions, and configuration.
Indexes: (tenant_id, agent_code), (agent_type, is_active)
Execution history of AI agent actions with full audit trail.
Indexes: (tenant_id, agent_id, started_at), (execution_status), (trigger_entity_type, trigger_entity_id)
AI-generated recommendations for human review (actions not auto-executed).
Indexes: (tenant_id, status, recommendation_type), (entity_type, entity_id), (expires_at)
Actions executed autonomously by AI agents without human approval.
Indexes: (tenant_id, action_type, executed_at), (agent_execution_id), (target_entity_type, target_entity_id)
User feedback on AI actions for continuous learning and improvement.
Indexes: (tenant_id, agent_id, feedback_type), (incorporated), (created_at)
Agentic AI Procurement Workflow
================================
1. TRIGGER EVENT
PR Approved / RFQ Deadline / Bid Received / etc.
|
v
2. AGENT ACTIVATION
+-- Select appropriate agent (RFQ_CREATOR, BID_EVALUATOR, etc.)
+-- Load agent configuration and thresholds
+-- Gather context data
|
v
3. AI REASONING (Chain-of-Thought)
+-- Analyze context
+-- Consider business rules
+-- Evaluate options
+-- Generate proposed actions
|
v
4. CONFIDENCE CHECK
+-- If confidence >= threshold AND value <= auto_limit
| |
| +-- AUTO-EXECUTE
| +-- Log to ai_autonomous_actions
| +-- Notify user (optional)
|
+-- Else
|
+-- CREATE RECOMMENDATION
+-- Log to ai_recommendations
+-- Await human approval
|
v
5. FEEDBACK LOOP
+-- Collect user feedback
+-- Store in ai_learning_feedback
+-- Periodic model fine-tuning
| Agent | Trigger | Actions | Auto-Execute |
|---|---|---|---|
| RFQ_CREATOR | PR Approved | Create RFQ, Select vendors, Set timeline | If value < ₹50K |
| VENDOR_SELECTOR | RFQ Draft | Recommend vendors, Check compliance | Recommendations only |
| BID_EVALUATOR | Bid Received | Score bids, Detect anomalies, Rank | Recommendations only |
| PO_GENERATOR | Bid Awarded | Create PO, Set terms, Route approval | If value < ₹25K |
| COMMUNICATION_AGENT | Various | Send reminders, Request updates | Yes (communications) |
| EXCEPTION_HANDLER | Error/Delay | Diagnose issue, Suggest resolution | Recommendations only |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/ai/agents | List configured agents |
| PUT | /api/ai/agents/{id}/config | Update agent configuration |
| GET | /api/ai/executions | List agent executions |
| GET | /api/ai/recommendations | List pending recommendations |
| POST | /api/ai/recommendations/{id}/accept | Accept recommendation |
| POST | /api/ai/recommendations/{id}/reject | Reject recommendation |
| GET | /api/ai/actions | List autonomous actions |
| POST | /api/ai/actions/{id}/reverse | Reverse an action |
| POST | /api/ai/feedback | Submit feedback |
| POST | /api/ai/chat | Conversational AI interface |
| POST | /api/ai/agents/{code}/trigger | Manually trigger agent |
Detailed logs of AI agent operations for debugging and auditing.
Conversation history for chat-based AI interface.