Skip to content

Commit 8a95428

Browse files
authored
chore: reduce complexity and logic split helpers (#16)
* chore: reduce complexity and logic split helpers up * Update elasticsearch gem version to 9.2 * Update elasticsearch gem version constraint * Update Gemfile.lock with elasticsearch dependencies
1 parent aa3435c commit 8a95428

20 files changed

+371
-189
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ TEST_PASSWORD=Test123!@#
7979

8080
PANDASCORE_API_KEY=your_pandascore_api_key_here
8181
PANDASCORE_BASE_URL=https://api.pandascore.co
82-
PANDASCORE_CACHE_TTL=3600
82+
PANDASCORE_CACHE_TTL=3600

.env.production.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ SECRET_KEY_BASE=CHANGE_ME_GENERATE_WITH_rails_secret
2727
DEVISE_JWT_SECRET_KEY=CHANGE_ME_GENERATE_WITH_rails_secret
2828

2929
# CORS
30-
CORS_ORIGINS=https://prostaff.gg,https://api.prostaff.gg,https://www.prostaff.gg
30+
CORS_ORIGINS=https://prostaff.gg,https://www.prostaff.gg,https://prostaffgg.netlify.app
3131

3232
# External APIs
3333
RIOT_API_KEY=RGAPI-YOUR-PRODUCTION-KEY-HERE

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,9 @@ TEST_ANALYSIS_REPORT.md
252252
MODULAR_MIGRATION_PHASE1_SUMMARY.md
253253
MODULAR_MONOLITH_MIGRATION_PLAN.md
254254
app/modules/players/README.md
255+
/League-Data-Scraping-And-Analytics-master/jsons
256+
/League-Data-Scraping-And-Analytics-master/Pro/game
257+
/League-Data-Scraping-And-Analytics-master/Pro/timeline
258+
League-Data-Scraping-And-Analytics-master/ProStaff-Scraper/
259+
DOCS/ELASTICSEARCH_SETUP.md
260+
DOCS/deployment/QUICK_DEPLOY_VPS.md

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ gem 'rswag'
7575
gem 'rswag-api'
7676
gem 'rswag-ui'
7777

78+
# Elasticsearch client (for analytics queries)
79+
gem 'elasticsearch', '~> 9.1', '>= 9.1.3'
80+
7881
group :development, :test do
7982
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
8083
gem 'debug', platforms: %i[mri mingw x64_mingw]

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ GEM
107107
dotenv (= 3.1.8)
108108
railties (>= 6.1)
109109
drb (2.2.3)
110+
elastic-transport (8.4.1)
111+
faraday (< 3)
112+
multi_json
113+
elasticsearch (9.2.0)
114+
elastic-transport (~> 8.3)
115+
elasticsearch-api (= 9.2.0)
116+
elasticsearch-api (9.2.0)
117+
multi_json
110118
erb (5.0.3)
111119
erubi (1.13.1)
112120
et-orbi (1.4.0)
@@ -172,6 +180,7 @@ GEM
172180
mini_mime (1.1.5)
173181
minitest (5.26.0)
174182
msgpack (1.8.0)
183+
multi_json (1.18.0)
175184
net-http (0.6.0)
176185
uri
177186
net-imap (0.5.12)
@@ -371,6 +380,7 @@ DEPENDENCIES
371380
database_cleaner-active_record
372381
debug
373382
dotenv-rails
383+
elasticsearch (~> 9.1, >= 9.1.3)
374384
factory_bot_rails
375385
faker
376386
faraday

app/controllers/api/v1/analytics/performance_controller.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,9 @@ def apply_date_filters(matches)
7575
# @param period [String] Time period (week, month, season)
7676
# @return [Integer] Number of days
7777
def time_period_to_days(period)
78-
case period
79-
when 'week' then 7
80-
when 'month' then 30
81-
when 'season' then 90
82-
else 30
83-
end
78+
return 7 if period == 'week'
79+
return 90 if period == 'season'
80+
30
8481
end
8582

8683
# Legacy method - kept for backwards compatibility

app/controllers/api/v1/dashboard_controller_optimized.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def calculate_win_rate_fast(wins, total)
5656
((wins.to_f / total) * 100).round(1)
5757
end
5858

59-
def calculate_recent_form(matches)
60-
matches.map { |m| m.victory? ? 'W' : 'L' }.join('')
59+
def calculate_recent_form(matches)
60+
matches.map { |m| m.victory? ? 'W' : 'L' }.join
6161
end
6262

6363
def calculate_average_kda_fast(kda_result)

app/controllers/api/v1/scrims/opponent_teams_controller.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,13 @@ def destroy
113113
# only modify teams they have scrims with.
114114
# Read operations (index/show) are allowed for all teams to enable discovery.
115115
#
116-
# SECURITY: Unscoped find is intentional here. OpponentTeam is a global
117-
# resource visible to all organizations for discovery. Authorization is
118-
# handled by verify_team_usage! for modifications.
119-
# rubocop:disable Rails/FindById
120116
def set_opponent_team
121-
@opponent_team = OpponentTeam.find(params[:id])
122-
rescue ActiveRecord::RecordNotFound
123-
render json: { error: 'Opponent team not found' }, status: :not_found
117+
id = Integer(params[:id]) rescue nil
118+
return render json: { error: 'Opponent team not found' }, status: :not_found unless id
119+
120+
@opponent_team = OpponentTeam.find_by(id: id)
121+
return render json: { error: 'Opponent team not found' }, status: :not_found unless @opponent_team
124122
end
125-
# rubocop:enable Rails/FindById
126123

127124
# Verifies that current organization has used this opponent team
128125
# Prevents organizations from modifying/deleting teams they haven't interacted with

app/jobs/sync_player_from_riot_job.rb

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,26 @@ class SyncPlayerFromRiotJob < ApplicationJob
66
def perform(player_id)
77
player = Player.find(player_id)
88

9-
unless player.riot_puuid.present? || player.summoner_name.present?
10-
player.update(sync_status: 'error', last_sync_at: Time.current)
11-
Rails.logger.error "Player #{player_id} missing Riot info"
12-
return
13-
end
9+
return mark_error(player, "Player #{player_id} missing Riot info") unless player.riot_puuid.present? || player.summoner_name.present?
1410

1511
riot_api_key = ENV['RIOT_API_KEY']
16-
unless riot_api_key.present?
17-
player.update(sync_status: 'error', last_sync_at: Time.current)
18-
Rails.logger.error 'Riot API key not configured'
19-
return
20-
end
12+
return mark_error(player, 'Riot API key not configured') unless riot_api_key.present?
13+
14+
region = player.region.presence&.downcase || 'br1'
2115

2216
begin
23-
region = player.region.presence&.downcase || 'br1'
24-
25-
summoner_data = if player.riot_puuid.present?
26-
fetch_summoner_by_puuid(player.riot_puuid, region, riot_api_key)
27-
else
28-
fetch_summoner_by_name(player.summoner_name, region, riot_api_key)
29-
end
30-
31-
# Use PUUID for league endpoint (workaround for Riot API bug where summoner_data['id'] is nil)
32-
# See: https://github.com/RiotGames/developer-relations/issues/1092
33-
ranked_data = fetch_ranked_stats_by_puuid(player.riot_puuid, region, riot_api_key)
34-
35-
update_data = {
36-
riot_puuid: summoner_data['puuid'],
37-
riot_summoner_id: summoner_data['id'],
38-
summoner_level: summoner_data['summonerLevel'],
39-
profile_icon_id: summoner_data['profileIconId'],
40-
sync_status: 'success',
41-
last_sync_at: Time.current
42-
}
43-
44-
solo_queue = ranked_data.find { |q| q['queueType'] == 'RANKED_SOLO_5x5' }
45-
if solo_queue
46-
update_data.merge!({
47-
solo_queue_tier: solo_queue['tier'],
48-
solo_queue_rank: solo_queue['rank'],
49-
solo_queue_lp: solo_queue['leaguePoints'],
50-
solo_queue_wins: solo_queue['wins'],
51-
solo_queue_losses: solo_queue['losses']
52-
})
53-
end
54-
55-
flex_queue = ranked_data.find { |q| q['queueType'] == 'RANKED_FLEX_SR' }
56-
if flex_queue
57-
update_data.merge!({
58-
flex_queue_tier: flex_queue['tier'],
59-
flex_queue_rank: flex_queue['rank'],
60-
flex_queue_lp: flex_queue['leaguePoints']
61-
})
62-
end
17+
summoner_data = fetch_summoner(player, region, riot_api_key)
18+
ranked_data = fetch_ranked_stats_by_puuid(summoner_data['puuid'], region, riot_api_key)
6319

64-
player.update!(update_data)
20+
update_data = build_update_data(summoner_data)
21+
update_data.merge!(extract_queue_updates(ranked_data))
6522

23+
player.update!(update_data)
6624
Rails.logger.info "Successfully synced player #{player_id} from Riot API"
6725
rescue StandardError => e
6826
Rails.logger.error "Failed to sync player #{player_id}: #{e.message}"
6927
Rails.logger.error e.backtrace.join("\n")
70-
71-
player.update(sync_status: 'error', last_sync_at: Time.current)
28+
mark_error(player)
7229
end
7330
end
7431

@@ -154,3 +111,49 @@ def fetch_ranked_stats_by_puuid(puuid, region, api_key)
154111
JSON.parse(response.body)
155112
end
156113
end
114+
def fetch_summoner(player, region, api_key)
115+
return fetch_summoner_by_puuid(player.riot_puuid, region, api_key) if player.riot_puuid.present?
116+
fetch_summoner_by_name(player.summoner_name, region, api_key)
117+
end
118+
119+
def build_update_data(summoner_data)
120+
{
121+
riot_puuid: summoner_data['puuid'],
122+
riot_summoner_id: summoner_data['id'],
123+
summoner_level: summoner_data['summonerLevel'],
124+
profile_icon_id: summoner_data['profileIconId'],
125+
sync_status: 'success',
126+
last_sync_at: Time.current
127+
}
128+
end
129+
130+
def extract_queue_updates(ranked_data)
131+
updates = {}
132+
133+
solo = ranked_data.find { |q| q['queueType'] == 'RANKED_SOLO_5x5' }
134+
if solo
135+
updates.merge!({
136+
solo_queue_tier: solo['tier'],
137+
solo_queue_rank: solo['rank'],
138+
solo_queue_lp: solo['leaguePoints'],
139+
solo_queue_wins: solo['wins'],
140+
solo_queue_losses: solo['losses']
141+
})
142+
end
143+
144+
flex = ranked_data.find { |q| q['queueType'] == 'RANKED_FLEX_SR' }
145+
if flex
146+
updates.merge!({
147+
flex_queue_tier: flex['tier'],
148+
flex_queue_rank: flex['rank'],
149+
flex_queue_lp: flex['leaguePoints']
150+
})
151+
end
152+
153+
updates
154+
end
155+
156+
def mark_error(player, message = nil)
157+
Rails.logger.error(message) if message
158+
player.update(sync_status: 'error', last_sync_at: Time.current)
159+
end

app/models/audit_log.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,10 @@ def time_ago
122122
end
123123

124124
def risk_level
125-
case action
126-
when 'delete' then 'high'
127-
when 'update' then 'medium'
128-
when 'create' then 'low'
129-
when 'login', 'logout' then 'info'
130-
else 'medium'
131-
end
125+
return 'high' if action == 'delete'
126+
return 'low' if action == 'create'
127+
return 'info' if %w[login logout].include?(action)
128+
'medium'
132129
end
133130

134131
def risk_color

0 commit comments

Comments
 (0)