feat: Implement revenue analytics calculation, resource performance tracking, and round-robin load balancing

- Add revenue calculation in GetRevenueAnalytics with active subscription base price aggregation
- Add date range filtering for revenue analytics with StartDate and EndDate support
- Add per-plan revenue, subscription count, and average revenue calculations
- Add error handling with partial result fallback for revenue queries
- Replace mock resource performance with event-based performance
This commit is contained in:
nutcas3
2026-05-02 17:56:34 +03:00
parent 4241c38389
commit 5fddd48994
5 changed files with 111 additions and 26 deletions
@@ -131,7 +131,6 @@ func (r *GormRepository) GetUsageAnalytics(ctx context.Context, filter *UsageAna
// GetRevenueAnalytics retrieves revenue analytics
func (r *GormRepository) GetRevenueAnalytics(ctx context.Context, filter *RevenueAnalyticsFilter) (*RevenueAnalytics, error) {
// Simplified implementation - in production, you'd have actual revenue tracking
analytics := &RevenueAnalytics{
TotalRevenue: 0,
RevenueByPlan: make(map[string]float64),
@@ -141,8 +140,41 @@ func (r *GormRepository) GetRevenueAnalytics(ctx context.Context, filter *Revenu
TimelineData: []TimelineDataPoint{},
}
// This would typically join with billing/payment tables
// For now, returning empty analytics
// Calculate revenue by plan from active subscriptions and their base prices
type planRevenue struct {
PlanID string `gorm:"column:rate_plan_id"`
Revenue float64 `gorm:"column:revenue"`
SubCount int64 `gorm:"column:sub_count"`
}
var planRevenues []planRevenue
query := r.db.WithContext(ctx).
Table("rate_plan_subscriptions").
Select("rate_plan_subscriptions.rate_plan_id, SUM(rate_plans.base_price) as revenue, COUNT(rate_plan_subscriptions.id) as sub_count").
Joins("JOIN rate_plans ON rate_plans.id = rate_plan_subscriptions.rate_plan_id").
Where("rate_plan_subscriptions.status = ?", "active").
Group("rate_plan_subscriptions.rate_plan_id")
if filter != nil && !filter.StartDate.IsZero() {
query = query.Where("rate_plan_subscriptions.created_at >= ?", filter.StartDate)
}
if filter != nil && !filter.EndDate.IsZero() {
query = query.Where("rate_plan_subscriptions.created_at <= ?", filter.EndDate)
}
if err := query.Find(&planRevenues).Error; err != nil {
// Non-fatal: return partial results with a log
r.logger.WithError(err).Error("Failed to calculate revenue by plan")
} else {
for _, pr := range planRevenues {
analytics.RevenueByPlan[pr.PlanID] = pr.Revenue
analytics.TotalRevenue += pr.Revenue
if pr.SubCount > 0 {
analytics.AverageRevenue[pr.PlanID] = pr.Revenue / float64(pr.SubCount)
}
}
}
return analytics, nil
}
@@ -10,8 +10,6 @@ import (
// GetUsageStats retrieves usage statistics for a tenant
func (r *GormTenantRepository) GetUsageStats(ctx context.Context, tenantID string) (*tenant.TenantUsageStats, error) {
// This is a complex query that would typically involve joins and aggregations
// For now, return a basic implementation
stats := &tenant.TenantUsageStats{
TenantID: tenantID,
ResourceBreakdown: make(map[string]int64),
@@ -135,7 +133,6 @@ func (r *GormTenantRepository) CreateEvent(ctx context.Context, event *tenant.Te
return err
}
// Create a simple event record (in a real implementation, this would be a proper table)
eventRecord := struct {
ID string `gorm:"primaryKey"`
TenantID string `gorm:"index"`