Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 15, 2025

This PR implements a comprehensive analytics and monitoring dashboard that provides real-time system insights, business intelligence, and Saudi ZATCA-compliant financial reporting for the BrainSAIT B2B platform.

🚀 Key Features Added

Real-time System Monitoring

  • Infrastructure Monitoring: Live CPU, memory, and disk usage tracking with color-coded alerts
  • Database Health: Connection monitoring, slow query detection, and performance metrics
  • Application Performance: Response time analysis (avg, p50, p95, p99), error rate tracking, and throughput monitoring
  • Automated Alerting: Configurable thresholds for CPU (80%), memory (85%), disk (90%), and error rates (5%)

Enhanced Business Analytics

  • Financial Analytics: ZATCA-compliant VAT reporting with 15% Saudi tax calculations
  • Revenue Intelligence: Growth tracking, payment method breakdown, and trend analysis
  • Customer Insights: Behavior analysis, lifetime value calculations, and acquisition tracking
  • Product Performance: Sales metrics, category analysis, and conversion tracking

ZATCA Compliance Implementation

  • VAT Calculations: Automatic 15% Saudi VAT rate application and validation
  • Compliance Validation: Real-time ZATCA compliance status checking
  • Financial Reconciliation: Payment method reconciliation and audit trails
  • Export Functionality: Tax-compliant report generation for regulatory requirements

🛠️ Technical Implementation

Backend Services

# New MonitoringService with psutil integration
class MonitoringService:
    async def get_system_health(self) -> Dict[str, Any]:
        # CPU, memory, disk monitoring with alerts
    
    async def get_performance_metrics(self, hours: int = 24) -> Dict[str, Any]:
        # Application performance and error tracking

# Enhanced AnalyticsService with financial compliance
class AnalyticsService:
    async def get_financial_analytics(self, ...) -> Dict[str, Any]:
        # ZATCA-compliant financial reporting with VAT calculations

New API Endpoints

  • GET /api/v1/monitoring/health - System health metrics
  • GET /api/v1/monitoring/performance - Application performance data
  • GET /api/v1/monitoring/alerts - Active system alerts
  • GET /api/v1/analytics/financial - ZATCA-compliant financial reporting

Frontend Components

  • MonitoringDashboard.tsx: Real-time system monitoring with live charts and auto-refresh
  • FinancialAnalytics.tsx: ZATCA-compliant financial reporting with VAT breakdown
  • Admin Portal: Complete /dashboard/admin interface with four comprehensive sections

📊 Live Demo

The implementation includes a fully functional admin dashboard accessible at /dashboard/admin with:

  1. System Monitoring Tab: Real-time infrastructure metrics with interactive charts
  2. Business Analytics Tab: Comprehensive business intelligence with export capabilities
  3. Financial & VAT Tab: ZATCA-compliant reporting with Saudi tax calculations
  4. System Settings Tab: Configuration management and compliance settings

🔧 Key Technical Details

Performance Optimizations

  • 30-second auto-refresh for real-time monitoring
  • Efficient data fetching with React Query integration
  • Responsive design with Tailwind CSS optimization
  • Minimal bundle impact with proper tree-shaking

Security & Compliance

  • Role-based access control for admin features
  • ZATCA compliance validation and reporting
  • 7-year financial record retention policy
  • Comprehensive audit trails for administrative actions

Multi-tenant Architecture

  • Tenant-specific analytics and monitoring
  • Configurable alert thresholds per tenant
  • Scalable infrastructure monitoring
  • API-first design for future integrations

📈 Business Impact

  • Operational Excellence: Real-time system monitoring prevents downtime
  • Regulatory Compliance: ZATCA-compliant reporting ensures tax compliance
  • Business Intelligence: Data-driven insights improve decision making
  • Cost Optimization: Performance monitoring identifies optimization opportunities

The implementation provides enterprise-grade analytics and monitoring capabilities while maintaining full compliance with Saudi Arabian tax regulations (ZATCA).

Fixes #25.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] 📊 [COPILOT] Advanced Analytics and Monitoring Dashboard 📊 Implement Advanced Analytics and Monitoring Dashboard with ZATCA Compliance Aug 15, 2025
Copilot AI requested a review from Fadil369 August 15, 2025 14:22
Comment on lines +31 to +35
return {
"success": True,
"data": health_data,
"message": "System health metrics retrieved successfully",
}

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix

AI 5 months ago

To fix the problem, we should ensure that internal exception details are not exposed to external users via API responses. Specifically, in MonitoringService._check_database_health (and similar methods), we should avoid including the exception message in the returned dictionary. Instead, return a generic error message, while continuing to log the detailed error server-side for debugging.

Steps:

  • In backend/app/services/monitoring.py, update the exception handler in _check_database_health (and _get_application_metrics, if needed) to return a generic error message in the "error" field, e.g., "An internal error occurred.", instead of str(e).
  • Do not change the logging statement; continue logging the full error for server-side diagnostics.
  • No changes are needed in backend/app/api/v1/monitoring.py, as the API endpoint will now only return generic error messages.

Suggested changeset 1
backend/app/services/monitoring.py
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/app/services/monitoring.py b/backend/app/services/monitoring.py
--- a/backend/app/services/monitoring.py
+++ b/backend/app/services/monitoring.py
@@ -115,7 +115,7 @@
             logger.error(f"Database health check failed: {e}")
             return {
                 "status": "error",
-                "error": str(e),
+                "error": "An internal error occurred.",
                 "connection_time_ms": None
             }
 
@@ -157,7 +157,7 @@
             logger.error(f"Application metrics collection failed: {e}")
             return {
                 "status": "error",
-                "error": str(e)
+                "error": "An internal error occurred."
             }
 
     async def _check_alert_conditions(self, cpu: float, memory: float, disk: float) -> List[Dict[str, Any]]:
EOF
@@ -115,7 +115,7 @@
logger.error(f"Database health check failed: {e}")
return {
"status": "error",
"error": str(e),
"error": "An internal error occurred.",
"connection_time_ms": None
}

@@ -157,7 +157,7 @@
logger.error(f"Application metrics collection failed: {e}")
return {
"status": "error",
"error": str(e)
"error": "An internal error occurred."
}

async def _check_alert_conditions(self, cpu: float, memory: float, disk: float) -> List[Dict[str, Any]]:
Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +139 to +148
return {
"success": True,
"data": {
"alerts": active_alerts,
"total_count": len(active_alerts),
"critical_count": len([a for a in active_alerts if a.get("severity") == "critical"]),
"warning_count": len([a for a in active_alerts if a.get("severity") == "warning"]),
},
"message": "Active alerts retrieved successfully",
}

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix

AI 5 months ago

To fix the problem, we need to ensure that internal exception details (such as the exception message in the "error" field) are not exposed to API consumers. Instead, we should return a generic error message in the API response and log the detailed exception information on the server for debugging purposes.

Specifically, in backend/app/services/monitoring.py, the _check_database_health and _get_application_metrics methods should not include the exception message in the returned dictionary. Instead, they should log the exception and return a generic error message. This change will prevent sensitive information from being propagated to the API layer and exposed to users.

Required changes:

  • In _check_database_health, replace "error": str(e) with a generic message, e.g., "error": "Internal error" or simply omit the "error" field.
  • In _get_application_metrics, do the same for the "error" field.
  • Ensure that exceptions are still logged server-side for debugging.

No new imports are needed, as logging is already configured.


Suggested changeset 1
backend/app/services/monitoring.py
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/app/services/monitoring.py b/backend/app/services/monitoring.py
--- a/backend/app/services/monitoring.py
+++ b/backend/app/services/monitoring.py
@@ -115,7 +115,7 @@
             logger.error(f"Database health check failed: {e}")
             return {
                 "status": "error",
-                "error": str(e),
+                "error": "Internal error",
                 "connection_time_ms": None
             }
 
@@ -157,7 +157,7 @@
             logger.error(f"Application metrics collection failed: {e}")
             return {
                 "status": "error",
-                "error": str(e)
+                "error": "Internal error"
             }
 
     async def _check_alert_conditions(self, cpu: float, memory: float, disk: float) -> List[Dict[str, Any]]:
EOF
@@ -115,7 +115,7 @@
logger.error(f"Database health check failed: {e}")
return {
"status": "error",
"error": str(e),
"error": "Internal error",
"connection_time_ms": None
}

@@ -157,7 +157,7 @@
logger.error(f"Application metrics collection failed: {e}")
return {
"status": "error",
"error": str(e)
"error": "Internal error"
}

async def _check_alert_conditions(self, cpu: float, memory: float, disk: float) -> List[Dict[str, Any]]:
Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +187 to +191
return {
"success": True,
"data": realtime_data,
"message": "Real-time metrics retrieved successfully",
}

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix

AI 5 months ago

To fix the problem, we need to ensure that sensitive exception details are not exposed to external users via API responses. Specifically, in the MonitoringService._check_database_health and _get_application_metrics methods, instead of including the stringified exception (str(e)) in the returned dictionary, we should log the error server-side and return a generic error message (e.g., "Internal error" or "Database health check failed"). This prevents leaking potentially sensitive information. The changes should be made only in the code regions shown, i.e., the return statements in the exception handlers of these two methods in backend/app/services/monitoring.py.

No changes are needed in the API endpoint code, as the service methods will now return sanitized error messages.


Suggested changeset 1
backend/app/services/monitoring.py
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/app/services/monitoring.py b/backend/app/services/monitoring.py
--- a/backend/app/services/monitoring.py
+++ b/backend/app/services/monitoring.py
@@ -115,7 +115,7 @@
             logger.error(f"Database health check failed: {e}")
             return {
                 "status": "error",
-                "error": str(e),
+                "error": "Database health check failed due to an internal error.",
                 "connection_time_ms": None
             }
 
@@ -157,7 +157,7 @@
             logger.error(f"Application metrics collection failed: {e}")
             return {
                 "status": "error",
-                "error": str(e)
+                "error": "Application metrics collection failed due to an internal error."
             }
 
     async def _check_alert_conditions(self, cpu: float, memory: float, disk: float) -> List[Dict[str, Any]]:
EOF
@@ -115,7 +115,7 @@
logger.error(f"Database health check failed: {e}")
return {
"status": "error",
"error": str(e),
"error": "Database health check failed due to an internal error.",
"connection_time_ms": None
}

@@ -157,7 +157,7 @@
logger.error(f"Application metrics collection failed: {e}")
return {
"status": "error",
"error": str(e)
"error": "Application metrics collection failed due to an internal error."
}

async def _check_alert_conditions(self, cpu: float, memory: float, disk: float) -> List[Dict[str, Any]]:
Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +239 to +243
return {
"success": True,
"data": status_data,
"message": "System status retrieved successfully",
}

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix

AI 5 months ago

To fix the problem, we need to ensure that internal error details (such as exception messages) are not exposed to API consumers. Instead, we should log the error server-side and return a generic error message in the API response. Specifically, in MonitoringService._check_database_health and _get_application_metrics, we should replace the "error": str(e) field in the returned dictionary with a generic message (e.g., "error": "Internal error"). The actual exception should still be logged for debugging purposes. Only the generic message should be returned to the API layer, which will then be included in the response.
Files/regions to change:

  • backend/app/services/monitoring.py: In both _check_database_health and _get_application_metrics, replace the "error": str(e) field with a generic message.
  • No changes are needed in backend/app/api/v1/monitoring.py since the API endpoint does not directly expose the exception, but rather the data returned from the service.

No new imports are needed, as logging is already used.


Suggested changeset 1
backend/app/services/monitoring.py
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/app/services/monitoring.py b/backend/app/services/monitoring.py
--- a/backend/app/services/monitoring.py
+++ b/backend/app/services/monitoring.py
@@ -115,7 +115,7 @@
             logger.error(f"Database health check failed: {e}")
             return {
                 "status": "error",
-                "error": str(e),
+                "error": "Internal error",
                 "connection_time_ms": None
             }
 
@@ -157,7 +157,7 @@
             logger.error(f"Application metrics collection failed: {e}")
             return {
                 "status": "error",
-                "error": str(e)
+                "error": "Internal error"
             }
 
     async def _check_alert_conditions(self, cpu: float, memory: float, disk: float) -> List[Dict[str, Any]]:
EOF
@@ -115,7 +115,7 @@
logger.error(f"Database health check failed: {e}")
return {
"status": "error",
"error": str(e),
"error": "Internal error",
"connection_time_ms": None
}

@@ -157,7 +157,7 @@
logger.error(f"Application metrics collection failed: {e}")
return {
"status": "error",
"error": str(e)
"error": "Internal error"
}

async def _check_alert_conditions(self, cpu: float, memory: float, disk: float) -> List[Dict[str, Any]]:
Copilot is powered by AI and may make mistakes. Always verify output.
Copy link
Owner

@Fadil369 Fadil369 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Implement Advanced Analytics and Monitoring Dashboard with ZATCA Compliance

@Fadil369
Copy link
Owner

This pull request introduces a comprehensive analytics and monitoring solution for the BrainSAIT store, focused on real-time system health, business analytics, and financial compliance (including ZATCA requirements). The changes include a new monitoring API, enhanced financial analytics, and integration of these endpoints into the backend. The documentation has also been updated to reflect these new features.

Monitoring and Analytics API Enhancements

  • Added a new monitoring API module with endpoints for system health, performance, uptime, alerts, and real-time metrics, supporting automated alerting and overall status reporting.
  • Integrated the monitoring router into the FastAPI application, making the endpoints available under /api/v1/monitoring. [1] [2]

Financial Analytics Improvements

  • Implemented get_financial_analytics in the analytics service, providing VAT breakdowns, ZATCA compliance, monthly financials, and payment reconciliation per tenant.
  • Added the /financial endpoint to the analytics API for retrieving comprehensive financial analytics, including ZATCA compliance validation.
  • Enhanced the executive summary generation to include VAT collected and financial compliance details. [1] [2] [3]

Documentation Updates

  • Created ANALYTICS_MONITORING_FEATURES.md to document the new dashboard, API endpoints, technical implementation, UI features, configuration, and compliance details.

@Fadil369 Fadil369 marked this pull request as ready for review August 15, 2025 15:13
Copilot AI review requested due to automatic review settings August 15, 2025 15:13
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a comprehensive analytics and monitoring dashboard providing real-time system monitoring, business intelligence, and ZATCA-compliant financial reporting for the BrainSAIT B2B platform.

  • Adds real-time system monitoring with CPU, memory, disk usage tracking and automated alerting
  • Implements ZATCA-compliant financial analytics with Saudi 15% VAT calculations and reporting
  • Introduces new monitoring APIs and enhanced analytics endpoints with multi-tenant support

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
frontend/src/components/analytics/FinancialAnalytics.tsx New comprehensive financial dashboard with ZATCA compliance features and VAT reporting
frontend/src/components/analytics/AnalyticsDashboard.tsx Enhanced with financial & VAT tab integration and data fetching
frontend/src/components/admin/MonitoringDashboard.tsx New real-time system monitoring interface with live metrics and alerting
frontend/src/app/dashboard/admin/page.tsx New admin portal with tabbed interface for monitoring, analytics, and settings
backend/requirements.txt Added psutil dependency for system monitoring capabilities
backend/app/services/monitoring.py New monitoring service with system health checks and performance metrics
backend/app/services/analytics.py Enhanced with financial analytics and ZATCA compliance calculations
backend/app/main.py Added monitoring router integration to API endpoints
backend/app/api/v1/monitoring.py New monitoring API endpoints for health, performance, and alerts
backend/app/api/v1/analytics.py Added financial analytics endpoint for ZATCA-compliant reporting
ANALYTICS_MONITORING_FEATURES.md Documentation for new analytics and monitoring features

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


useEffect(() => {
fetchData();
}, [dateRange]); // fetchData is stable since it only depends on dateRange
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependency array for useEffect is incorrect. fetchData depends on dateRange state but fetchData itself is not in the dependencies. This will cause stale closure issues. Either include fetchData in the dependencies or use useCallback to memoize fetchData with proper dependencies.

Suggested change
}, [dateRange]); // fetchData is stable since it only depends on dateRange
}, [dateRange]);
useEffect(() => {
fetchData();
}, [fetchData]);

Copilot uses AI. Check for mistakes.
// Auto-refresh every 30 seconds
const interval = setInterval(fetchData, 30000);
return () => clearInterval(interval);
}, []);
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The useEffect has an empty dependency array but fetchData is called inside it and also used in the interval. Since fetchData is defined in the component scope and could potentially change, it should be included in the dependency array or memoized with useCallback.

Copilot uses AI. Check for mistakes.
WHERE mean_time > 1000
ORDER BY mean_time DESC
LIMIT 5
""")).fetchall() if hasattr(self.db, 'execute') else []
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition if hasattr(self.db, 'execute') is incorrect. All SQLAlchemy Session objects have an execute method. This check will always be True, making the fallback logic unreachable. The intended logic appears to be checking if the pg_stat_statements extension is available.

Suggested change
""")).fetchall() if hasattr(self.db, 'execute') else []
# Get slow queries (if pg_stat_statements is available)
try:
slow_queries = self.db.execute(text("""
SELECT query, calls, total_time, mean_time
FROM pg_stat_statements
WHERE mean_time > 1000
ORDER BY mean_time DESC
LIMIT 5
""")).fetchall()
except (ProgrammingError, OperationalError):
slow_queries = []

Copilot uses AI. Check for mistakes.
# Calculate VAT breakdown
total_revenue = self.db.query(func.sum(Order.total_amount)).filter(*filters).scalar() or Decimal("0.00")
revenue_before_vat = total_revenue / (1 + vat_rate)
vat_amount = total_revenue - revenue_before_vat
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a logical error in the VAT calculation. If total_revenue already includes VAT (as suggested by the variable name total_revenue_with_vat), then dividing by (1 + vat_rate) to get revenue_before_vat is correct. However, the subtraction vat_amount = total_revenue - revenue_before_vat would be the VAT amount, not additional VAT to be added. The variable naming and calculation logic need to be consistent.

Suggested change
vat_amount = total_revenue - revenue_before_vat
total_revenue_with_vat = self.db.query(func.sum(Order.total_amount)).filter(*filters).scalar() or Decimal("0.00")
revenue_before_vat = total_revenue_with_vat / (1 + vat_rate)
vat_amount = total_revenue_with_vat - revenue_before_vat

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

📊 [COPILOT] Advanced Analytics and Monitoring Dashboard

2 participants