Spaces:
Sleeping
Sleeping
File size: 32,266 Bytes
383cb38 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 | import asyncio
from collections import defaultdict
from datetime import datetime, timedelta
import logging
from typing import Any, Dict, List, Optional, Tuple
from fastapi import APIRouter, Depends, HTTPException, Request
import pandas as pd
import plotly.graph_objects as go
from pydantic import BaseModel, Field
logger = logging.getLogger(__name__)
# Analytics Models
class AnalyticsTimeRange(BaseModel):
"""Analytics Time Range"""
start_date: str = Field(..., description="Start date (YYYY-MM-DD)")
end_date: str = Field(..., description="End date (YYYY-MM-DD)")
granularity: str = Field(
"daily", description="Time granularity (hourly, daily, weekly, monthly)"
)
class ChatMetrics(BaseModel):
"""Chat Conversation Metrics"""
total_conversations: int = Field(0, description="Total conversations")
active_conversations: int = Field(0, description="Active conversations")
average_response_time: float = Field(0.0, description="Average response time in ms")
user_satisfaction_score: float = Field(
0.0, description="User satisfaction score (1-5)"
)
messages_per_conversation: float = Field(
0.0, description="Average messages per conversation"
)
total_messages: int = Field(0, description="Total messages")
active_users: int = Field(0, description="Active users")
conversation_duration_avg: float = Field(
0.0, description="Average conversation duration in seconds"
)
class VoiceMetrics(BaseModel):
"""Voice Integration Metrics"""
voice_commands_processed: int = Field(0, description="Voice commands processed")
average_processing_time: float = Field(
0.0, description="Average processing time in ms"
)
recognition_accuracy: float = Field(0.0, description="Speech recognition accuracy")
tts_requests: int = Field(0, description="Text-to-speech requests")
voice_messages_sent: int = Field(0, description="Voice messages sent")
command_success_rate: float = Field(0.0, description="Command success rate")
popular_commands: List[str] = Field(
default_factory=list, description="Popular voice commands"
)
class FileMetrics(BaseModel):
"""File Processing Metrics"""
files_uploaded: int = Field(0, description="Files uploaded")
images_processed: int = Field(0, description="Images processed")
documents_analyzed: int = Field(0, description="Documents analyzed")
audio_files_transcribed: int = Field(0, description="Audio files transcribed")
total_storage_used_mb: float = Field(0.0, description="Total storage used in MB")
average_file_size_kb: float = Field(0.0, description="Average file size in KB")
file_processing_success_rate: float = Field(
0.0, description="File processing success rate"
)
class PerformanceMetrics(BaseModel):
"""System Performance Metrics"""
uptime_percentage: float = Field(0.0, description="Uptime percentage")
average_response_time_ms: float = Field(
0.0, description="Average response time in ms"
)
concurrent_users: int = Field(0, description="Concurrent users")
api_requests_per_minute: int = Field(0, description="API requests per minute")
error_rate: float = Field(0.0, description="Error rate percentage")
memory_usage_mb: int = Field(0, description="Memory usage in MB")
cpu_usage_percent: float = Field(0.0, description="CPU usage percentage")
class UserBehaviorMetrics(BaseModel):
"""User Behavior Analytics"""
user_retention_rate: float = Field(0.0, description="User retention rate")
feature_adoption_rate: float = Field(0.0, description="Feature adoption rate")
session_duration_avg: float = Field(0.0, description="Average session duration")
daily_active_users: int = Field(0, description="Daily active users")
monthly_active_users: int = Field(0, description="Monthly active users")
user_engagement_score: float = Field(0.0, description="User engagement score")
popular_features: List[str] = Field(
default_factory=list, description="Popular features"
)
class BusinessMetrics(BaseModel):
"""Business Performance Metrics"""
roi_percentage: float = Field(0.0, description="Return on investment percentage")
cost_savings: float = Field(0.0, description="Cost savings in USD")
productivity_improvement: float = Field(
0.0, description="Productivity improvement percentage"
)
support_ticket_reduction: float = Field(
0.0, description="Support ticket reduction percentage"
)
user_satisfaction_trend: List[float] = Field(
default_factory=list, description="User satisfaction trend"
)
feature_usage_growth: float = Field(
0.0, description="Feature usage growth percentage"
)
class AnalyticsSummary(BaseModel):
"""Comprehensive Analytics Summary"""
timestamp: str = Field(..., description="Analytics generation timestamp")
time_range: AnalyticsTimeRange
chat_metrics: ChatMetrics
voice_metrics: VoiceMetrics
file_metrics: FileMetrics
performance_metrics: PerformanceMetrics
user_behavior_metrics: UserBehaviorMetrics
business_metrics: BusinessMetrics
overall_health_score: float = Field(
0.0, description="Overall system health score (0-100)"
)
class TrendAnalysis(BaseModel):
"""Trend Analysis Results"""
metric_name: str = Field(..., description="Metric name")
current_value: float = Field(0.0, description="Current value")
previous_value: float = Field(0.0, description="Previous period value")
change_percentage: float = Field(0.0, description="Change percentage")
trend_direction: str = Field(
"stable", description="Trend direction (up, down, stable)"
)
confidence_score: float = Field(0.0, description="Trend confidence score")
class AnomalyDetection(BaseModel):
"""Anomaly Detection Results"""
metric_name: str = Field(..., description="Metric name")
detected_at: str = Field(..., description="Detection timestamp")
severity: str = Field(
"low", description="Anomaly severity (low, medium, high, critical)"
)
description: str = Field(..., description="Anomaly description")
suggested_action: str = Field(..., description="Suggested action")
class EnterpriseAnalyticsDashboard:
"""Enterprise Analytics Dashboard Service"""
def __init__(self):
self.router = APIRouter()
self.analytics_data = defaultdict(list)
self.setup_routes()
def setup_routes(self):
"""Setup analytics dashboard routes"""
self.router.add_api_route(
"/analytics/dashboard/summary",
self.get_dashboard_summary,
methods=["POST"],
summary="Get comprehensive analytics summary",
)
self.router.add_api_route(
"/analytics/dashboard/chat-metrics",
self.get_chat_metrics,
methods=["POST"],
summary="Get chat conversation metrics",
)
self.router.add_api_route(
"/analytics/dashboard/voice-metrics",
self.get_voice_metrics,
methods=["POST"],
summary="Get voice integration metrics",
)
self.router.add_api_route(
"/analytics/dashboard/file-metrics",
self.get_file_metrics,
methods=["POST"],
summary="Get file processing metrics",
)
self.router.add_api_route(
"/analytics/dashboard/performance-metrics",
self.get_performance_metrics,
methods=["POST"],
summary="Get system performance metrics",
)
self.router.add_api_route(
"/analytics/dashboard/user-behavior",
self.get_user_behavior_metrics,
methods=["POST"],
summary="Get user behavior analytics",
)
self.router.add_api_route(
"/analytics/dashboard/business-metrics",
self.get_business_metrics,
methods=["POST"],
summary="Get business performance metrics",
)
self.router.add_api_route(
"/analytics/dashboard/trends",
self.get_trend_analysis,
methods=["POST"],
summary="Get trend analysis",
)
self.router.add_api_route(
"/analytics/dashboard/anomalies",
self.get_anomaly_detection,
methods=["POST"],
summary="Get anomaly detection results",
)
self.router.add_api_route(
"/analytics/dashboard/visualization/{chart_type}",
self.get_visualization_data,
methods=["POST"],
summary="Get visualization data for charts",
)
self.router.add_api_route(
"/analytics/dashboard/export",
self.export_analytics_data,
methods=["POST"],
summary="Export analytics data",
)
async def get_dashboard_summary(
self, time_range: AnalyticsTimeRange
) -> AnalyticsSummary:
"""Get comprehensive analytics dashboard summary"""
try:
# Generate mock analytics data
chat_metrics = await self._generate_chat_metrics(time_range)
voice_metrics = await self._generate_voice_metrics(time_range)
file_metrics = await self._generate_file_metrics(time_range)
performance_metrics = await self._generate_performance_metrics(time_range)
user_behavior_metrics = await self._generate_user_behavior_metrics(
time_range
)
business_metrics = await self._generate_business_metrics(time_range)
# Calculate overall health score
health_score = self._calculate_health_score(
chat_metrics, performance_metrics, user_behavior_metrics
)
return AnalyticsSummary(
timestamp=datetime.utcnow().isoformat(),
time_range=time_range,
chat_metrics=chat_metrics,
voice_metrics=voice_metrics,
file_metrics=file_metrics,
performance_metrics=performance_metrics,
user_behavior_metrics=user_behavior_metrics,
business_metrics=business_metrics,
overall_health_score=health_score,
)
except Exception as e:
logger.error(f"Failed to generate dashboard summary: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate analytics summary"
)
async def get_chat_metrics(self, time_range: AnalyticsTimeRange) -> ChatMetrics:
"""Get chat conversation metrics"""
try:
return await self._generate_chat_metrics(time_range)
except Exception as e:
logger.error(f"Failed to generate chat metrics: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate chat metrics"
)
async def get_voice_metrics(self, time_range: AnalyticsTimeRange) -> VoiceMetrics:
"""Get voice integration metrics"""
try:
return await self._generate_voice_metrics(time_range)
except Exception as e:
logger.error(f"Failed to generate voice metrics: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate voice metrics"
)
async def get_file_metrics(self, time_range: AnalyticsTimeRange) -> FileMetrics:
"""Get file processing metrics"""
try:
return await self._generate_file_metrics(time_range)
except Exception as e:
logger.error(f"Failed to generate file metrics: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate file metrics"
)
async def get_performance_metrics(
self, time_range: AnalyticsTimeRange
) -> PerformanceMetrics:
"""Get system performance metrics"""
try:
return await self._generate_performance_metrics(time_range)
except Exception as e:
logger.error(f"Failed to generate performance metrics: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate performance metrics"
)
async def get_user_behavior_metrics(
self, time_range: AnalyticsTimeRange
) -> UserBehaviorMetrics:
"""Get user behavior analytics"""
try:
return await self._generate_user_behavior_metrics(time_range)
except Exception as e:
logger.error(f"Failed to generate user behavior metrics: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate user behavior metrics"
)
async def get_business_metrics(
self, time_range: AnalyticsTimeRange
) -> BusinessMetrics:
"""Get business performance metrics"""
try:
return await self._generate_business_metrics(time_range)
except Exception as e:
logger.error(f"Failed to generate business metrics: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate business metrics"
)
async def get_trend_analysis(
self, time_range: AnalyticsTimeRange
) -> List[TrendAnalysis]:
"""Get trend analysis for key metrics"""
try:
return await self._generate_trend_analysis(time_range)
except Exception as e:
logger.error(f"Failed to generate trend analysis: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate trend analysis"
)
async def get_anomaly_detection(
self, time_range: AnalyticsTimeRange
) -> List[AnomalyDetection]:
"""Get anomaly detection results"""
try:
return await self._generate_anomaly_detection(time_range)
except Exception as e:
logger.error(f"Failed to generate anomaly detection: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate anomaly detection"
)
async def get_visualization_data(
self, chart_type: str, time_range: AnalyticsTimeRange
) -> Dict[str, Any]:
"""Get visualization data for charts"""
try:
return await self._generate_visualization_data(chart_type, time_range)
except Exception as e:
logger.error(f"Failed to generate visualization data: {e}")
raise HTTPException(
status_code=500, detail="Failed to generate visualization data"
)
async def export_analytics_data(
self, time_range: AnalyticsTimeRange, format: str = "json"
) -> Dict[str, Any]:
"""Export analytics data in specified format"""
try:
return await self._export_analytics_data(time_range, format)
except Exception as e:
logger.error(f"Failed to export analytics data: {e}")
raise HTTPException(
status_code=500, detail="Failed to export analytics data"
)
async def _generate_chat_metrics(
self, time_range: AnalyticsTimeRange
) -> ChatMetrics:
"""Generate chat conversation metrics"""
# Mock data - in production, query from database
return ChatMetrics(
total_conversations=1500,
active_conversations=45,
average_response_time=180.5,
user_satisfaction_score=4.7,
messages_per_conversation=8.3,
total_messages=12450,
active_users=89,
conversation_duration_avg=420.2,
)
async def _generate_voice_metrics(
self, time_range: AnalyticsTimeRange
) -> VoiceMetrics:
"""Generate voice integration metrics"""
# Mock data - in production, query from database
return VoiceMetrics(
voice_commands_processed=450,
average_processing_time=1200.5,
recognition_accuracy=0.92,
tts_requests=280,
voice_messages_sent=670,
command_success_rate=0.88,
popular_commands=[
"create_task",
"schedule_meeting",
"search_information",
"send_message",
"set_reminder",
],
)
async def _generate_file_metrics(
self, time_range: AnalyticsTimeRange
) -> FileMetrics:
"""Generate file processing metrics"""
# Mock data - in production, query from database
return FileMetrics(
files_uploaded=670,
images_processed=230,
documents_analyzed=310,
audio_files_transcribed=130,
total_storage_used_mb=245.7,
average_file_size_kb=1560.3,
file_processing_success_rate=0.96,
)
async def _generate_performance_metrics(
self, time_range: AnalyticsTimeRange
) -> PerformanceMetrics:
"""Generate system performance metrics"""
# Mock data - in production, collect from monitoring system
return PerformanceMetrics(
uptime_percentage=99.9,
average_response_time_ms=180.2,
concurrent_users=25,
api_requests_per_minute=45,
error_rate=0.02,
memory_usage_mb=245,
cpu_usage_percent=12.5,
)
async def _generate_user_behavior_metrics(
self, time_range: AnalyticsTimeRange
) -> UserBehaviorMetrics:
"""Generate user behavior analytics"""
# Mock data - in production, analyze user behavior patterns
return UserBehaviorMetrics(
user_retention_rate=0.85,
feature_adoption_rate=0.72,
session_duration_avg=1200.5,
daily_active_users=150,
monthly_active_users=450,
user_engagement_score=4.3,
popular_features=[
"chat",
"voice_commands",
"file_upload",
"workflow_automation",
"search",
],
)
async def _generate_business_metrics(
self, time_range: AnalyticsTimeRange
) -> BusinessMetrics:
"""Generate business performance metrics"""
# Mock data - in production, calculate from business data
return BusinessMetrics(
roi_percentage=45.7,
cost_savings=125000.0,
productivity_improvement=32.5,
support_ticket_reduction=58.3,
user_satisfaction_trend=[4.2, 4.3, 4.5, 4.6, 4.7],
feature_usage_growth=28.9,
)
async def _generate_trend_analysis(
self, time_range: AnalyticsTimeRange
) -> List[TrendAnalysis]:
"""Generate trend analysis for key metrics"""
trends = [
TrendAnalysis(
metric_name="user_satisfaction_score",
current_value=4.7,
previous_value=4.5,
change_percentage=4.4,
trend_direction="up",
confidence_score=0.92,
),
TrendAnalysis(
metric_name="average_response_time",
current_value=180.5,
previous_value=195.2,
change_percentage=-7.5,
trend_direction="down",
confidence_score=0.88,
),
TrendAnalysis(
metric_name="active_users",
current_value=89,
previous_value=85,
change_percentage=4.7,
trend_direction="up",
confidence_score=0.85,
),
TrendAnalysis(
metric_name="error_rate",
current_value=0.02,
previous_value=0.03,
change_percentage=-33.3,
trend_direction="down",
confidence_score=0.90,
),
]
return trends
async def _generate_anomaly_detection(
self, time_range: AnalyticsTimeRange
) -> List[AnomalyDetection]:
"""Generate anomaly detection results"""
anomalies = [
AnomalyDetection(
metric_name="api_response_time",
detected_at=datetime.utcnow().isoformat(),
severity="medium",
description="API response time increased by 45% in the last hour",
suggested_action="Check server load and database performance",
),
AnomalyDetection(
metric_name="memory_usage",
detected_at=datetime.utcnow().isoformat(),
severity="low",
description="Memory usage spike detected during peak hours",
suggested_action="Monitor memory usage and consider scaling",
),
]
return anomalies
async def _generate_visualization_data(
self, chart_type: str, time_range: AnalyticsTimeRange
) -> Dict[str, Any]:
"""Generate visualization data for charts"""
if chart_type == "user_engagement":
return {
"chart_type": "line",
"title": "User Engagement Over Time",
"data": {
"labels": ["Week 1", "Week 2", "Week 3", "Week 4", "Current"],
"datasets": [
{
"label": "Daily Active Users",
"data": [120, 135, 142, 148, 150],
"borderColor": "rgb(75, 192, 192)",
"backgroundColor": "rgba(75, 192, 192, 0.2)",
}
],
},
}
elif chart_type == "response_time":
return {
"chart_type": "bar",
"title": "Average Response Time by Feature",
"data": {
"labels": ["Chat", "Voice", "File Upload", "Search", "Workflow"],
"datasets": [
{
"label": "Response Time (ms)",
"data": [180, 1200, 450, 320, 890],
"backgroundColor": [
"rgba(255, 99, 132, 0.8)",
"rgba(54, 162, 235, 0.8)",
"rgba(255, 205, 86, 0.8)",
"rgba(75, 192, 192, 0.8)",
"rgba(153, 102, 255, 0.8)",
],
}
],
},
}
elif chart_type == "feature_usage":
return {
"chart_type": "doughnut",
"title": "Feature Usage Distribution",
"data": {
"labels": [
"Chat",
"Voice Commands",
"File Processing",
"Workflows",
"Search",
],
"datasets": [
{
"data": [45, 25, 15, 10, 5],
"backgroundColor": [
"#FF6384",
"#36A2EB",
"#FFCE56",
"#4BC0C0",
"#9966FF",
],
}
],
},
}
else:
return {
"chart_type": "line",
"title": "Default Chart",
"data": {"labels": [], "datasets": []},
}
async def _export_analytics_data(
self, time_range: AnalyticsTimeRange, format: str = "json"
) -> Dict[str, Any]:
"""Export analytics data in specified format"""
summary = await self.get_dashboard_summary(time_range)
if format == "csv":
# Generate CSV data
import csv
import io
output = io.StringIO()
writer = csv.writer(output)
# Write header
writer.writerow(["Metric Category", "Metric Name", "Value", "Timestamp"])
# Write data
metrics_data = [
(
"Chat",
"Total Conversations",
summary.chat_metrics.total_conversations,
summary.timestamp,
),
(
"Chat",
"Active Conversations",
summary.chat_metrics.active_conversations,
summary.timestamp,
),
(
"Chat",
"Average Response Time",
summary.chat_metrics.average_response_time,
summary.timestamp,
),
(
"Voice",
"Commands Processed",
summary.voice_metrics.voice_commands_processed,
summary.timestamp,
),
(
"Voice",
"Recognition Accuracy",
summary.voice_metrics.recognition_accuracy,
summary.timestamp,
),
(
"File",
"Files Uploaded",
summary.file_metrics.files_uploaded,
summary.timestamp,
),
(
"File",
"Storage Used (MB)",
summary.file_metrics.total_storage_used_mb,
summary.timestamp,
),
(
"Performance",
"Uptime Percentage",
summary.performance_metrics.uptime_percentage,
summary.timestamp,
),
(
"Performance",
"Error Rate",
summary.performance_metrics.error_rate,
summary.timestamp,
),
(
"Business",
"ROI Percentage",
summary.business_metrics.roi_percentage,
summary.timestamp,
),
(
"Business",
"Cost Savings",
summary.business_metrics.cost_savings,
summary.timestamp,
),
]
for category, name, value, timestamp in metrics_data:
writer.writerow([category, name, value, timestamp])
return {
"format": "csv",
"filename": f"analytics_export_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.csv",
"data": output.getvalue(),
"record_count": len(metrics_data),
}
else:
# Default JSON format
return {
"format": "json",
"filename": f"analytics_export_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.json",
"data": summary.dict(),
"record_count": 1,
}
def _calculate_health_score(
self,
chat_metrics: ChatMetrics,
performance_metrics: PerformanceMetrics,
user_behavior_metrics: UserBehaviorMetrics,
) -> float:
"""Calculate overall system health score"""
# Weighted average of key metrics
uptime_score = performance_metrics.uptime_percentage
response_time_score = max(
0, 100 - (performance_metrics.average_response_time_ms / 10)
)
user_satisfaction_score = (
chat_metrics.user_satisfaction_score * 20
) # Convert 1-5 to 0-100
error_rate_score = max(0, 100 - (performance_metrics.error_rate * 1000))
engagement_score = (
user_behavior_metrics.user_engagement_score * 20
) # Convert 1-5 to 0-100
weights = {
"uptime": 0.25,
"response_time": 0.20,
"user_satisfaction": 0.25,
"error_rate": 0.15,
"engagement": 0.15,
}
health_score = (
uptime_score * weights["uptime"]
+ response_time_score * weights["response_time"]
+ user_satisfaction_score * weights["user_satisfaction"]
+ error_rate_score * weights["error_rate"]
+ engagement_score * weights["engagement"]
)
return round(health_score, 2)
# Initialize enterprise analytics dashboard
enterprise_analytics_dashboard = EnterpriseAnalyticsDashboard()
# Analytics API Router for inclusion in main application
router = enterprise_analytics_dashboard.router
# Additional analytics endpoints
@router.get("/analytics/dashboard/health")
async def analytics_dashboard_health():
"""Health check for analytics dashboard"""
return {
"status": "healthy",
"service": "enterprise_analytics_dashboard",
"available_metrics": [
"chat_metrics",
"voice_metrics",
"file_metrics",
"performance_metrics",
"user_behavior_metrics",
"business_metrics",
],
"supported_charts": [
"user_engagement",
"response_time",
"feature_usage",
],
"export_formats": ["json", "csv"],
}
@router.get("/analytics/dashboard/realtime")
async def get_realtime_metrics():
"""Get real-time analytics metrics"""
# Mock real-time data
return {
"timestamp": datetime.utcnow().isoformat(),
"active_conversations": 25,
"concurrent_users": 89,
"api_requests_per_minute": 45,
"memory_usage_mb": 245,
"cpu_usage_percent": 12.5,
"response_time_ms": 180.2,
"error_rate": 0.02,
}
@router.post("/analytics/dashboard/predictive")
async def get_predictive_analytics(time_range: AnalyticsTimeRange):
"""Get predictive analytics and forecasts"""
# Mock predictive data
return {
"timestamp": datetime.utcnow().isoformat(),
"time_range": time_range,
"predictions": {
"user_growth": {
"next_week": 165,
"next_month": 195,
"confidence": 0.85,
},
"storage_usage": {
"next_week": 280.5,
"next_month": 345.2,
"confidence": 0.92,
},
"api_requests": {
"next_week": 52,
"next_month": 68,
"confidence": 0.78,
},
},
"recommendations": [
"Consider scaling storage capacity in 2 weeks",
"Monitor API rate limits for increased usage",
"Optimize database queries for better performance",
],
}
@router.get("/analytics/dashboard/comparison")
async def get_comparison_analytics(current_period: str, previous_period: str):
"""Get comparison analytics between periods"""
# Mock comparison data
return {
"current_period": current_period,
"previous_period": previous_period,
"comparisons": {
"active_users": {"current": 150, "previous": 135, "change": 11.1},
"user_satisfaction": {"current": 4.7, "previous": 4.5, "change": 4.4},
"response_time": {"current": 180.5, "previous": 195.2, "change": -7.5},
"error_rate": {"current": 0.02, "previous": 0.03, "change": -33.3},
},
"insights": [
"User satisfaction improved by 4.4% compared to previous period",
"Response time decreased by 7.5%, indicating performance improvements",
"Error rate reduced by 33.3%, showing increased system stability",
],
}
logger.info("Enterprise Analytics Dashboard initialized")
|