|
| 1 | +# OPTIMIZED VERSION - in validation, dont send to production |
| 2 | +class Api::V1::DashboardController < Api::V1::BaseController |
| 3 | + def stats |
| 4 | + cache_key = "dashboard_stats_#{current_organization.id}_#{current_organization.updated_at.to_i}" |
| 5 | + |
| 6 | + Rails.cache.fetch(cache_key, expires_in: 5.minutes) do |
| 7 | + calculate_stats |
| 8 | + end |
| 9 | + end |
| 10 | + |
| 11 | + private |
| 12 | + |
| 13 | + def calculate_stats |
| 14 | + matches = organization_scoped(Match) |
| 15 | + .recent(30) |
| 16 | + .includes(:player_match_stats) |
| 17 | + |
| 18 | + matches_array = matches.to_a |
| 19 | + players = organization_scoped(Player).active |
| 20 | + |
| 21 | + match_stats = matches_array.group_by(&:victory?) |
| 22 | + wins = match_stats[true]&.size || 0 |
| 23 | + losses = match_stats[false]&.size || 0 |
| 24 | + |
| 25 | + kda_result = PlayerMatchStat |
| 26 | + .where(match_id: matches_array.map(&:id)) |
| 27 | + .select('SUM(kills) as total_kills, SUM(deaths) as total_deaths, SUM(assists) as total_assists') |
| 28 | + .first |
| 29 | + |
| 30 | + goal_counts = organization_scoped(TeamGoal).group(:status).count |
| 31 | + |
| 32 | + { |
| 33 | + total_players: players.count, |
| 34 | + active_players: players.where(status: 'active').count, |
| 35 | + total_matches: matches_array.size, |
| 36 | + wins: wins, |
| 37 | + losses: losses, |
| 38 | + win_rate: calculate_win_rate_fast(wins, matches_array.size), |
| 39 | + recent_form: calculate_recent_form(matches_array.first(5)), |
| 40 | + avg_kda: calculate_average_kda_fast(kda_result), |
| 41 | + active_goals: goal_counts['active'] || 0, |
| 42 | + completed_goals: goal_counts['completed'] || 0, |
| 43 | + upcoming_matches: organization_scoped(Schedule) |
| 44 | + .where('start_time >= ? AND event_type = ?', Time.current, 'match') |
| 45 | + .count |
| 46 | + } |
| 47 | + end |
| 48 | + |
| 49 | + def calculate_win_rate_fast(wins, total) |
| 50 | + return 0 if total.zero? |
| 51 | + ((wins.to_f / total) * 100).round(1) |
| 52 | + end |
| 53 | + |
| 54 | + def calculate_recent_form(matches) |
| 55 | + matches.map { |m| m.victory? ? 'W' : 'L' }.join('') |
| 56 | + end |
| 57 | + |
| 58 | + def calculate_average_kda_fast(kda_result) |
| 59 | + return 0 unless kda_result |
| 60 | + |
| 61 | + total_kills = kda_result.total_kills || 0 |
| 62 | + total_deaths = kda_result.total_deaths || 0 |
| 63 | + total_assists = kda_result.total_assists || 0 |
| 64 | + |
| 65 | + deaths = total_deaths.zero? ? 1 : total_deaths |
| 66 | + ((total_kills + total_assists).to_f / deaths).round(2) |
| 67 | + end |
| 68 | + |
| 69 | + def roster_status_data |
| 70 | + players = organization_scoped(Player).includes(:champion_pools) |
| 71 | + |
| 72 | + players_array = players.to_a |
| 73 | + |
| 74 | + { |
| 75 | + by_role: players_array.group_by(&:role).transform_values(&:count), |
| 76 | + by_status: players_array.group_by(&:status).transform_values(&:count), |
| 77 | + contracts_expiring: players.contracts_expiring_soon.count |
| 78 | + } |
| 79 | + end |
| 80 | +end |
0 commit comments