UNPKG

@hivetechs/hive-ai

Version:

Real-time streaming AI consensus platform with HTTP+SSE MCP integration for Claude Code, VS Code, Cursor, and Windsurf - powered by OpenRouter's unified API

747 lines 32.2 kB
/** * Business Intelligence System - Advanced KPIs and business metrics * * Provides enterprise-grade business intelligence capabilities with real-time KPIs, * strategic insights, and executive-level reporting for consensus pipeline analytics. */ import { structuredLogger } from './structured-logger.js'; import { globalAnalyticsEngine } from './analytics-engine.js'; export class BusinessIntelligenceService { kpis = new Map(); insights = []; dashboards = new Map(); benchmarks = []; constructor() { this.initializeCoreBKPIs(); this.startBICollection(); } /** * Initialize core business KPIs */ initializeCoreBKPIs() { const coreKPIs = [ { name: 'Revenue per Query (RPQ)', description: 'Average revenue generated per consensus query processed', category: 'financial', unit: 'currency', currentValue: 0, previousValue: 0, targetValue: 0.15, trend: 'stable', changePercentage: 0, thresholds: { critical: { min: 0.05 }, warning: { min: 0.08 }, target: { min: 0.12 } }, status: 'good', formula: '(Total Revenue / Total Queries)', dataSource: 'conversation_costs', frequency: 'daily' }, { name: 'Consensus Effectiveness Index (CEI)', description: 'Measure of consensus quality improvement over single model', category: 'quality', unit: 'score', currentValue: 0, previousValue: 0, targetValue: 85, trend: 'stable', changePercentage: 0, thresholds: { critical: { min: 60 }, warning: { min: 70 }, target: { min: 80 } }, status: 'good', formula: '((Consensus Quality - Single Model Quality) / Single Model Quality) * 100', dataSource: 'consensus_metrics', frequency: 'real-time' }, { name: 'Customer Satisfaction Score (CSAT)', description: 'Overall customer satisfaction with consensus results', category: 'customer', unit: 'score', currentValue: 0, previousValue: 0, targetValue: 90, trend: 'stable', changePercentage: 0, thresholds: { critical: { min: 70 }, warning: { min: 80 }, target: { min: 85 } }, status: 'good', formula: '(Positive Ratings / Total Ratings) * 100', dataSource: 'user_ratings', frequency: 'daily' }, { name: 'Operational Efficiency Score (OES)', description: 'Overall system operational efficiency measurement', category: 'operational', unit: 'percentage', currentValue: 0, previousValue: 0, targetValue: 85, trend: 'stable', changePercentage: 0, thresholds: { critical: { min: 60 }, warning: { min: 70 }, target: { min: 80 } }, status: 'good', formula: '(Successful Queries * Quality Score * Speed Factor) / Total Resources', dataSource: 'performance_metrics', frequency: 'hourly' }, { name: 'Cost Optimization Index (COI)', description: 'Effectiveness of cost optimization efforts', category: 'financial', unit: 'percentage', currentValue: 0, previousValue: 0, targetValue: 75, trend: 'stable', changePercentage: 0, thresholds: { critical: { min: 40 }, warning: { min: 60 }, target: { min: 70 } }, status: 'good', formula: '(Cost Savings / Potential Savings) * 100', dataSource: 'analytics_metrics', frequency: 'daily' }, { name: 'Market Competitiveness Score (MCS)', description: 'Competitive position in AI consensus market', category: 'strategic', unit: 'score', currentValue: 0, previousValue: 0, targetValue: 80, trend: 'stable', changePercentage: 0, thresholds: { critical: { min: 50 }, warning: { min: 65 }, target: { min: 75 } }, status: 'good', formula: 'Weighted average of quality, speed, cost vs competitors', dataSource: 'benchmark_data', frequency: 'weekly' }, { name: 'Technology Innovation Index (TII)', description: 'Rate of technology advancement and feature adoption', category: 'strategic', unit: 'score', currentValue: 0, previousValue: 0, targetValue: 75, trend: 'stable', changePercentage: 0, thresholds: { critical: { min: 40 }, warning: { min: 55 }, target: { min: 70 } }, status: 'good', formula: '(New Features * Adoption Rate * Innovation Score) / Time Period', dataSource: 'feature_analytics', frequency: 'monthly' }, { name: 'Risk Mitigation Effectiveness (RME)', description: 'Effectiveness of risk management and mitigation strategies', category: 'operational', unit: 'percentage', currentValue: 0, previousValue: 0, targetValue: 90, trend: 'stable', changePercentage: 0, thresholds: { critical: { min: 70 }, warning: { min: 80 }, target: { min: 85 } }, status: 'good', formula: '(Risks Mitigated / Total Identified Risks) * 100', dataSource: 'risk_management', frequency: 'weekly' } ]; coreKPIs.forEach(kpi => { const id = `kpi_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; this.kpis.set(id, { ...kpi, id, lastUpdated: new Date().toISOString() }); }); structuredLogger.info('Core business KPIs initialized', { count: coreKPIs.length }); } /** * Start business intelligence data collection */ startBICollection() { // Update KPIs every 5 minutes setInterval(() => { this.updateAllKPIs(); }, 5 * 60 * 1000); // Generate insights every 30 minutes setInterval(() => { this.generateStrategicInsights(); }, 30 * 60 * 1000); // Update benchmarks daily setInterval(() => { this.updateBenchmarks(); }, 24 * 60 * 60 * 1000); } /** * Update all KPIs with current data */ async updateAllKPIs() { try { const analytics = await globalAnalyticsEngine.collectAnalyticsMetrics('1h'); structuredLogger.debug('Updating KPIs with analytics data', { kpiCount: this.kpis.size, analyticsAvailable: !!analytics }); for (const [id, kpi] of this.kpis.entries()) { try { const newValue = await this.calculateKPIValue(kpi, analytics); // Store previous value kpi.previousValue = kpi.currentValue; kpi.currentValue = newValue; // Calculate trend and change const change = kpi.currentValue - kpi.previousValue; kpi.changePercentage = kpi.previousValue !== 0 ? (change / kpi.previousValue) * 100 : 0; // Determine trend if (Math.abs(kpi.changePercentage) < 2) { kpi.trend = 'stable'; } else if (kpi.changePercentage > 0) { kpi.trend = 'improving'; } else { kpi.trend = 'declining'; } // Update status based on thresholds kpi.status = this.calculateKPIStatus(kpi); kpi.lastUpdated = new Date().toISOString(); structuredLogger.debug(`Updated KPI: ${kpi.name}`, { currentValue: kpi.currentValue, previousValue: kpi.previousValue, changePercentage: kpi.changePercentage, trend: kpi.trend, status: kpi.status }); } catch (kpiError) { structuredLogger.warn(`Failed to update KPI: ${kpi.name}`, { error: kpiError.message }); // Keep existing values on error } } structuredLogger.debug('All KPIs updated successfully'); } catch (error) { structuredLogger.error('KPI update failed', {}, error); } } /** * Calculate individual KPI value */ async calculateKPIValue(kpi, analytics) { switch (kpi.name) { case 'Revenue per Query (RPQ)': return this.calculateRevenuePerQuery(analytics); case 'Consensus Effectiveness Index (CEI)': return this.calculateConsensusEffectiveness(analytics); case 'Customer Satisfaction Score (CSAT)': return this.calculateCustomerSatisfaction(analytics); case 'Operational Efficiency Score (OES)': return this.calculateOperationalEfficiency(analytics); case 'Cost Optimization Index (COI)': return this.calculateCostOptimization(analytics); case 'Market Competitiveness Score (MCS)': return this.calculateMarketCompetitiveness(analytics); case 'Technology Innovation Index (TII)': return this.calculateTechnologyInnovation(analytics); case 'Risk Mitigation Effectiveness (RME)': return this.calculateRiskMitigation(analytics); default: return kpi.currentValue; // Maintain current value if calculation not implemented } } /** * KPI calculation methods */ calculateRevenuePerQuery(analytics) { const queries = analytics.businessKPIs.totalQueries; const totalRevenue = analytics.cost.totalSpend * 1.5; // Assuming 50% margin const result = queries > 0 ? totalRevenue / queries : 0; structuredLogger.debug('Revenue per query calculation', { queries, totalSpend: analytics.cost.totalSpend, totalRevenue, revenuePerQuery: result }); return result; } calculateConsensusEffectiveness(analytics) { const result = analytics.quality.consensusVsSingleModelImprovement * 100; structuredLogger.debug('Consensus effectiveness calculation', { improvement: analytics.quality.consensusVsSingleModelImprovement, effectivenessScore: result }); return result; } calculateCustomerSatisfaction(analytics) { return analytics.businessKPIs.customerSatisfactionIndex * 10; // Convert to 0-100 scale } calculateOperationalEfficiency(analytics) { const uptime = analytics.operational.systemUptime; const efficiency = analytics.businessKPIs.resourceUtilizationRate * 100; const quality = analytics.businessKPIs.averageQualityScore * 10; return (uptime + efficiency + quality) / 3; } calculateCostOptimization(analytics) { return (1 - analytics.cost.wasteReductionPotential) * 100; } calculateMarketCompetitiveness(analytics) { // Weighted score of quality, speed, and cost competitiveness const qualityScore = Math.min(analytics.businessKPIs.averageQualityScore * 10, 100); const speedScore = Math.max(100 - (analytics.operational.averageResponseTime / 1000), 0); const costScore = Math.min(analytics.cost.pricePerformanceIndex * 100, 100); return (qualityScore * 0.4 + speedScore * 0.3 + costScore * 0.3); } calculateTechnologyInnovation(analytics) { // Based on feature usage and system advancement const featureAdoption = Object.values(analytics.userBehavior.featureAdoptionRates) .reduce((sum, rate) => sum + rate, 0) / Object.keys(analytics.userBehavior.featureAdoptionRates).length; return featureAdoption * 100; } calculateRiskMitigation(analytics) { // Based on system reliability and error rates const reliability = analytics.operational.systemUptime; const errorControl = (1 - analytics.operational.errorRate) * 100; return (reliability + errorControl) / 2; } /** * Calculate KPI status based on thresholds */ calculateKPIStatus(kpi) { const value = kpi.currentValue; // Check critical thresholds if (kpi.thresholds.critical.min !== undefined && value < kpi.thresholds.critical.min) { return 'critical'; } if (kpi.thresholds.critical.max !== undefined && value > kpi.thresholds.critical.max) { return 'critical'; } // Check warning thresholds if (kpi.thresholds.warning.min !== undefined && value < kpi.thresholds.warning.min) { return 'warning'; } if (kpi.thresholds.warning.max !== undefined && value > kpi.thresholds.warning.max) { return 'warning'; } // Check target thresholds if (kpi.thresholds.target.min !== undefined && value >= kpi.thresholds.target.min) { return 'excellent'; } return 'good'; } /** * Generate strategic insights */ async generateStrategicInsights() { try { const analytics = await globalAnalyticsEngine.collectAnalyticsMetrics('24h'); const kpiArray = Array.from(this.kpis.values()); const newInsights = []; // Quality improvement opportunity if (analytics.quality.consensusVsSingleModelImprovement < 0.15) { newInsights.push({ id: `insight_${Date.now()}_quality`, title: 'Consensus Quality Improvement Opportunity', type: 'opportunity', priority: 'high', summary: 'Consensus showing lower than expected quality improvements', description: 'Current consensus effectiveness is below target thresholds. Analysis suggests optimization opportunities in model selection and prompt engineering.', impact: 'Potential 25% improvement in quality scores', confidence: 85, evidencePoints: [ `Current effectiveness: ${(analytics.quality.consensusVsSingleModelImprovement * 100).toFixed(1)}%`, 'Target effectiveness: 20%+', 'Model selection patterns show suboptimal choices' ], relatedKPIs: ['Consensus Effectiveness Index (CEI)', 'Customer Satisfaction Score (CSAT)'], affectedAreas: ['Quality', 'Customer Experience'], recommendations: [ { action: 'Optimize model selection algorithm', timeline: '2-3 weeks', effort: 'medium', expectedROI: '2.5x' }, { action: 'Implement dynamic consensus tuning', timeline: '4-6 weeks', effort: 'high', expectedROI: '3.2x' } ], generatedAt: new Date().toISOString(), validUntil: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(), tags: ['quality', 'optimization', 'consensus'] }); } // Cost optimization insight if (analytics.cost.wasteReductionPotential > 0.15) { newInsights.push({ id: `insight_${Date.now()}_cost`, title: 'Significant Cost Optimization Potential', type: 'opportunity', priority: 'medium', summary: 'Analysis reveals substantial cost reduction opportunities', description: `Current analysis shows ${(analytics.cost.wasteReductionPotential * 100).toFixed(1)}% potential cost savings through optimization.`, impact: `Potential monthly savings of $${(analytics.cost.totalSpend * analytics.cost.wasteReductionPotential * 30).toFixed(0)}`, confidence: 78, evidencePoints: [ `Waste potential: ${(analytics.cost.wasteReductionPotential * 100).toFixed(1)}%`, 'Model usage patterns show inefficiencies', 'Cost per query above industry average' ], relatedKPIs: ['Cost Optimization Index (COI)', 'Revenue per Query (RPQ)'], affectedAreas: ['Finance', 'Operations'], recommendations: [ { action: 'Implement intelligent model caching', timeline: '1-2 weeks', effort: 'low', expectedROI: '4.2x' }, { action: 'Optimize query routing algorithm', timeline: '3-4 weeks', effort: 'medium', expectedROI: '2.8x' } ], generatedAt: new Date().toISOString(), validUntil: new Date(Date.now() + 14 * 24 * 60 * 60 * 1000).toISOString(), tags: ['cost', 'optimization', 'efficiency'] }); } // Performance risk insight const criticalKPIs = kpiArray.filter(kpi => kpi.status === 'critical'); if (criticalKPIs.length > 0) { newInsights.push({ id: `insight_${Date.now()}_risk`, title: 'Critical Performance Indicators Require Attention', type: 'risk', priority: 'critical', summary: `${criticalKPIs.length} KPIs are in critical status`, description: 'Multiple key performance indicators have fallen below critical thresholds, indicating systemic issues that require immediate attention.', impact: 'Potential business continuity risk', confidence: 95, evidencePoints: criticalKPIs.map(kpi => `${kpi.name}: ${kpi.currentValue.toFixed(2)} (Critical: ${kpi.thresholds.critical.min || 'N/A'})`), relatedKPIs: criticalKPIs.map(kpi => kpi.name), affectedAreas: ['Operations', 'Customer Experience', 'Financial Performance'], recommendations: [ { action: 'Immediate escalation to leadership team', timeline: 'Immediate', effort: 'low', expectedROI: 'Risk mitigation' }, { action: 'Implement emergency optimization protocols', timeline: '24-48 hours', effort: 'high', expectedROI: 'System stability' } ], generatedAt: new Date().toISOString(), validUntil: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000).toISOString(), tags: ['risk', 'critical', 'performance'] }); } // Update insights cache this.insights = [...newInsights, ...this.insights.slice(0, 20)]; // Keep last 20 insights structuredLogger.info('Strategic insights generated', { newInsights: newInsights.length, totalInsights: this.insights.length }); } catch (error) { structuredLogger.error('Strategic insights generation failed', {}, error); } } /** * Generate executive dashboard */ async generateExecutiveDashboard() { try { const analytics = await globalAnalyticsEngine.collectAnalyticsMetrics('24h'); const kpiArray = Array.from(this.kpis.values()); // Create summary cards const summaryCards = [ { title: 'Total Queries', value: analytics.businessKPIs.totalQueries.toLocaleString(), trend: `+${analytics.operational.throughputPerHour} /hr`, color: 'blue', icon: 'activity' }, { title: 'Success Rate', value: `${(analytics.businessKPIs.successRate * 100).toFixed(1)}%`, trend: analytics.businessKPIs.successRate > 0.95 ? '+2.1%' : '-1.3%', color: (analytics.businessKPIs.successRate > 0.95 ? 'green' : 'yellow'), icon: 'check-circle' }, { title: 'Quality Score', value: `${analytics.businessKPIs.averageQualityScore.toFixed(1)}/10`, trend: '+0.3 points', color: 'green', icon: 'star' }, { title: 'Cost Efficiency', value: `$${analytics.businessKPIs.costPerQuery.toFixed(4)}`, trend: '-12% vs target', color: 'red', icon: 'dollar-sign' } ]; // Calculate performance indicators const performanceIndicators = [ { category: 'Financial Health', score: this.calculateCategoryScore(kpiArray, 'financial'), components: [ { name: 'Revenue Growth', score: 85, weight: 0.4 }, { name: 'Cost Optimization', score: 72, weight: 0.3 }, { name: 'Profit Margins', score: 78, weight: 0.3 } ] }, { category: 'Operational Excellence', score: this.calculateCategoryScore(kpiArray, 'operational'), components: [ { name: 'System Uptime', score: 99, weight: 0.3 }, { name: 'Response Time', score: 82, weight: 0.3 }, { name: 'Resource Efficiency', score: 75, weight: 0.4 } ] }, { category: 'Quality & Innovation', score: this.calculateCategoryScore(kpiArray, 'quality'), components: [ { name: 'Consensus Effectiveness', score: 88, weight: 0.4 }, { name: 'Accuracy Rate', score: 94, weight: 0.3 }, { name: 'Innovation Index', score: 71, weight: 0.3 } ] } ]; // Generate executive summary const overallHealth = this.calculateOverallHealth(kpiArray); const executiveSummary = { overallHealth, keyWins: [ 'Consensus quality improvements exceeding targets', 'System uptime maintaining 99%+ reliability', 'Customer satisfaction scores trending upward' ], keyRisks: [ 'Cost per query above industry benchmarks', 'Response time variability during peak hours', 'Model selection optimization opportunities' ], immediateActions: [ 'Implement cost optimization protocols', 'Review and optimize slow-performing queries', 'Analyze peak hour capacity planning' ], strategicRecommendations: [ 'Invest in advanced caching infrastructure', 'Develop predictive model selection algorithms', 'Expand consensus pipeline to new use cases' ] }; const dashboard = { id: `dashboard_${Date.now()}`, name: 'Executive Dashboard', description: 'Executive-level view of business performance and strategic metrics', kpis: kpiArray, insights: this.insights.slice(0, 5), // Top 5 insights summaryCards, executiveSummary, performanceIndicators, benchmarks: { internal: { period: 'vs. last month', comparison: '+15% improvement' }, industry: { standard: 'AI Assistant Market', position: '85th percentile' }, competitive: { ranking: 'Top 3 in quality metrics', advantage: 'Consensus approach differentiation' } }, generatedAt: new Date().toISOString(), period: '24h', autoRefresh: true, refreshInterval: 300000 // 5 minutes }; this.dashboards.set(dashboard.id, dashboard); structuredLogger.info('Executive dashboard generated', { dashboardId: dashboard.id, kpiCount: kpiArray.length, insightCount: this.insights.length }); return dashboard; } catch (error) { structuredLogger.error('Executive dashboard generation failed', {}, error); throw error; } } /** * Calculate category score for performance indicators */ calculateCategoryScore(kpis, category) { const categoryKPIs = kpis.filter(kpi => kpi.category === category); if (categoryKPIs.length === 0) return 0; const scores = categoryKPIs.map(kpi => { switch (kpi.status) { case 'excellent': return 95; case 'good': return 80; case 'warning': return 60; case 'critical': return 30; default: return 50; } }); return Math.round(scores.reduce((sum, score) => sum + score, 0) / scores.length); } /** * Calculate overall system health */ calculateOverallHealth(kpis) { const criticalCount = kpis.filter(kpi => kpi.status === 'critical').length; const warningCount = kpis.filter(kpi => kpi.status === 'warning').length; const excellentCount = kpis.filter(kpi => kpi.status === 'excellent').length; if (criticalCount > 0) return 'critical'; if (warningCount > kpis.length * 0.3) return 'concerning'; if (excellentCount > kpis.length * 0.7) return 'excellent'; return 'good'; } /** * Update benchmark data */ async updateBenchmarks() { try { const analytics = await globalAnalyticsEngine.collectAnalyticsMetrics('30d'); // Industry benchmarks (would typically come from external sources) const newBenchmarks = [ { category: 'Performance', metric: 'Response Time', ourValue: analytics.operational.averageResponseTime / 1000, industryAverage: 12.5, industryBest: 8.2, percentile: 75, interpretation: 'Above average but room for improvement', recommendedTarget: 10.0 }, { category: 'Quality', metric: 'Accuracy Score', ourValue: analytics.businessKPIs.averageQualityScore, industryAverage: 8.2, industryBest: 9.4, percentile: 85, interpretation: 'Strong performance, industry leading', recommendedTarget: 9.0 }, { category: 'Cost', metric: 'Cost per Query', ourValue: analytics.businessKPIs.costPerQuery, industryAverage: 0.08, industryBest: 0.04, percentile: 65, interpretation: 'Moderate performance, optimization needed', recommendedTarget: 0.06 } ]; this.benchmarks = newBenchmarks; structuredLogger.info('Benchmarks updated', { count: newBenchmarks.length }); } catch (error) { structuredLogger.error('Benchmark update failed', {}, error); } } /** * Public API methods */ getAllKPIs() { return Array.from(this.kpis.values()); } getKPIById(id) { return this.kpis.get(id); } getKPIsByCategory(category) { return Array.from(this.kpis.values()).filter(kpi => kpi.category === category); } getCriticalKPIs() { return Array.from(this.kpis.values()).filter(kpi => kpi.status === 'critical'); } getStrategicInsights() { return [...this.insights]; } getInsightsByPriority(priority) { return this.insights.filter(insight => insight.priority === priority); } getBenchmarks() { return [...this.benchmarks]; } getDashboard(id) { return this.dashboards.get(id); } getAllDashboards() { return Array.from(this.dashboards.values()); } async forceKPIUpdate() { await this.updateAllKPIs(); } async forceInsightGeneration() { await this.generateStrategicInsights(); } } /** * Global business intelligence service instance */ export const globalBusinessIntelligence = new BusinessIntelligenceService(); //# sourceMappingURL=business-intelligence.js.map