mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-12-01 08:18:47 +00:00
feat: Refactor pricing optimization service by extracting helper functions to separate file
- Move calculateElasticity, calculateCompetitiveIndex, and calculateOptimizationROI to optimization_opt_service.go - Move generateAnalysis, calculateConfidence, and calculateChurnRate to optimization_opt_service.go - Simplify calculatePriceElasticity by removing comments and consolidating logic - Simplify optimizeForRevenue, optimizeForMarketShare, optimizeForProfitMargin, optimizeForCompetitive, and optimizeForChurn
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
package pricing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
// calculateElasticity calculates price elasticity using advanced regression analysis
|
||||
func (s *PricingOptimizationService) calculateElasticity(_ context.Context, ratePlan *RatePlan) float64 {
|
||||
// For demonstration, use dynamic elasticity based on rate plan characteristics
|
||||
// In production, this would use historical data and market analysis
|
||||
|
||||
baseElasticity := -1.2 // Base telecom elasticity
|
||||
|
||||
// Adjust elasticity based on price point
|
||||
if ratePlan.BasePrice < 20 {
|
||||
// Lower price plans tend to be more elastic
|
||||
baseElasticity = -1.5
|
||||
} else if ratePlan.BasePrice > 50 {
|
||||
// Higher price plans tend to be less elastic
|
||||
baseElasticity = -0.8
|
||||
}
|
||||
|
||||
// Add some randomness to simulate market variability
|
||||
variation := (float64(time.Now().UnixNano()%1000)/1000.0)*0.4 - 0.2
|
||||
|
||||
finalElasticity := baseElasticity + variation
|
||||
|
||||
// Bounds checking for realistic telecom elasticity
|
||||
if finalElasticity < -2.0 {
|
||||
finalElasticity = -2.0
|
||||
} else if finalElasticity > -0.3 {
|
||||
finalElasticity = -0.3
|
||||
}
|
||||
|
||||
return finalElasticity
|
||||
}
|
||||
|
||||
// calculateCompetitiveIndex calculates competitive positioning index using market analysis
|
||||
func (s *PricingOptimizationService) calculateCompetitiveIndex(ctx context.Context, period string) float64 {
|
||||
// Advanced competitive index calculation based on multiple factors
|
||||
// In production, this would analyze real competitor data
|
||||
|
||||
baseIndex := 75.0 // Base competitive position
|
||||
|
||||
// Factor in market conditions (seasonal variations)
|
||||
month := time.Now().Month()
|
||||
if month >= time.November || month <= time.January {
|
||||
// Holiday season - more competitive
|
||||
baseIndex += 5.0
|
||||
} else if month >= time.June && month <= time.August {
|
||||
// Summer - less competitive
|
||||
baseIndex -= 3.0
|
||||
}
|
||||
|
||||
// Add some market variability
|
||||
variation := (float64(time.Now().UnixNano()%2000)/2000.0)*10.0 - 5.0
|
||||
|
||||
finalIndex := baseIndex + variation
|
||||
|
||||
// Bounds: 0-100 scale
|
||||
if finalIndex < 0 {
|
||||
finalIndex = 0
|
||||
} else if finalIndex > 100 {
|
||||
finalIndex = 100
|
||||
}
|
||||
|
||||
return finalIndex
|
||||
}
|
||||
|
||||
// calculateOptimizationROI calculates ROI from optimizations using financial modeling
|
||||
func (s *PricingOptimizationService) calculateOptimizationROI(ctx context.Context, period string) float64 {
|
||||
// Advanced ROI calculation based on optimization effectiveness
|
||||
// In production, this would track actual optimization results
|
||||
|
||||
// Base ROI varies by optimization type and market conditions
|
||||
baseROI := 15.5 // Base optimization ROI
|
||||
|
||||
// Adjust based on period type
|
||||
switch period {
|
||||
case "daily":
|
||||
baseROI *= 0.8 // Short-term optimizations have lower ROI
|
||||
case "weekly":
|
||||
baseROI *= 0.9 // Medium-term
|
||||
case "monthly":
|
||||
baseROI *= 1.0 // Standard
|
||||
case "quarterly":
|
||||
baseROI *= 1.2 // Long-term optimizations have higher ROI
|
||||
default:
|
||||
baseROI *= 1.0
|
||||
}
|
||||
|
||||
// Factor in market maturity (simulated by time)
|
||||
hour := time.Now().Hour()
|
||||
if hour >= 9 && hour <= 17 {
|
||||
// Business hours - better optimization results
|
||||
baseROI += 2.0
|
||||
}
|
||||
|
||||
// Add variability based on optimization success rate
|
||||
variability := (float64(time.Now().UnixNano()%1500)/1500.0)*8.0 - 4.0
|
||||
|
||||
finalROI := baseROI + variability
|
||||
|
||||
// Realistic bounds for telecom optimization ROI
|
||||
if finalROI < 5.0 {
|
||||
finalROI = 5.0
|
||||
} else if finalROI > 35.0 {
|
||||
finalROI = 35.0
|
||||
}
|
||||
|
||||
return finalROI
|
||||
}
|
||||
|
||||
// generateAnalysis generates reasoning, risks, and recommendations
|
||||
func (s *PricingOptimizationService) generateAnalysis(ratePlan *RatePlan, optimalPrice float64, strategy OptimizationStrategy, data []HistoricalDataPoint) ([]string, []string, []string) {
|
||||
reasoning := make([]string, 0)
|
||||
risks := make([]string, 0)
|
||||
recommendations := make([]string, 0)
|
||||
|
||||
priceChange := ((optimalPrice - ratePlan.BasePrice) / ratePlan.BasePrice) * 100
|
||||
|
||||
// Generate reasoning based on strategy
|
||||
switch strategy {
|
||||
case StrategyRevenueMax:
|
||||
reasoning = append(reasoning, "Optimized for maximum revenue generation")
|
||||
reasoning = append(reasoning, fmt.Sprintf("Price change of %.1f%% expected to maximize revenue", priceChange))
|
||||
|
||||
if priceChange > 10 {
|
||||
risks = append(risks, "Significant price increase may impact demand")
|
||||
risks = append(risks, "Competitive pressure may increase")
|
||||
}
|
||||
|
||||
case StrategyMarketShare:
|
||||
reasoning = append(reasoning, "Optimized for market share growth")
|
||||
reasoning = append(reasoning, "Lower pricing strategy to attract more customers")
|
||||
|
||||
risks = append(risks, "Lower margins may impact profitability")
|
||||
risks = append(risks, "May attract price-sensitive customers with higher churn")
|
||||
|
||||
case StrategyCompetitive:
|
||||
reasoning = append(reasoning, "Priced competitively relative to market")
|
||||
reasoning = append(reasoning, "Positioned below median competitor pricing")
|
||||
|
||||
risks = append(risks, "Competitors may respond with price cuts")
|
||||
risks = append(risks, "Margin pressure in competitive market")
|
||||
}
|
||||
|
||||
// General recommendations
|
||||
recommendations = append(recommendations, "Monitor demand closely after price change")
|
||||
recommendations = append(recommendations, "Track competitor pricing responses")
|
||||
recommendations = append(recommendations, "Review customer feedback and churn rates")
|
||||
|
||||
if math.Abs(priceChange) > 15 {
|
||||
recommendations = append(recommendations, "Consider gradual price adjustment")
|
||||
recommendations = append(recommendations, "Implement promotional offers for existing customers")
|
||||
}
|
||||
|
||||
return reasoning, risks, recommendations
|
||||
}
|
||||
|
||||
// calculateConfidence calculates confidence level for predictions
|
||||
func (s *PricingOptimizationService) calculateConfidence(data []HistoricalDataPoint) float64 {
|
||||
// More data points = higher confidence
|
||||
dataPoints := len(data)
|
||||
if dataPoints >= 12 {
|
||||
return 85.0
|
||||
} else if dataPoints >= 6 {
|
||||
return 70.0
|
||||
} else if dataPoints >= 3 {
|
||||
return 50.0
|
||||
} else {
|
||||
return 25.0
|
||||
}
|
||||
}
|
||||
|
||||
// calculateChurnRate calculates churn rate for period
|
||||
func (s *PricingOptimizationService) calculateChurnRate(ctx context.Context, period string) float64 {
|
||||
var totalSubs, churnedSubs int64
|
||||
|
||||
startDate := s.getPeriodStart(period)
|
||||
endDate := s.getPeriodEnd(period)
|
||||
|
||||
s.db.WithContext(ctx).Table("profiles").
|
||||
Where("created_at < ?", startDate).
|
||||
Count(&totalSubs)
|
||||
|
||||
s.db.WithContext(ctx).Table("rate_plan_subscriptions").
|
||||
Where("ended_at BETWEEN ? AND ?", startDate, endDate).
|
||||
Count(&churnedSubs)
|
||||
|
||||
if totalSubs == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return float64(churnedSubs) / float64(totalSubs) * 100
|
||||
}
|
||||
Reference in New Issue
Block a user