From 44ec0b55f0608ee4d0734936831df5a060da8cae Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sat, 23 May 2015 05:08:16 -0400 Subject: [PATCH 1/8] introduced constructor on TournamentMatchGen --- app/models/tournament.rb | 5 +--- app/models/tournamentmatchgen.rb | 44 ++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 886518e..c3e094d 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -36,7 +36,7 @@ class Tournament < ActiveRecord::Base end def generateMatchups - @matches = Tournamentmatchgen.new.genMatches(self) + @matches = Tournamentmatchgen.new(self).genMatches() end def destroyAllMatches @@ -44,6 +44,3 @@ class Tournament < ActiveRecord::Base end end - - - diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index e518363..4f8428d 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -1,35 +1,45 @@ class Tournamentmatchgen - def genMatches(tournament) - if tournament.tournament_type == "Pool to bracket" - @matches = poolToBracket(tournament) + + def initialize(tournament) + @tournament = tournament + @matches = @tournament.matches + end + + def genMatches + if @tournament.tournament_type == "Pool to bracket" + @matches = poolToBracket() end return @matches end - def poolToBracket(tournament) - tournament.destroyAllMatches + def poolToBracket + destroyMatches() + @tournament.weights.sort_by{|x|[x.max]}.each do |w| + buildTournamentWeights(w) + end + @matches = Boutgen.new.assignBouts(@matches,@tournament.weights) + @matches = Losernamegen.new.assignLoserNames(@matches,@tournament.weights) + saveMatches + return @matches + end + + def destroyMatches + @tournament.destroyAllMatches @matches = [] - tournament.weights.sort_by{|x|[x.max]}.each do |w| - buildTournamentWeights(tournament.id, w) - end - @matches = Boutgen.new.assignBouts(@matches,tournament.weights) - @matches = Losernamegen.new.assignLoserNames(@matches,tournament.weights) - saveMatches(tournament,@matches) - return @matches end - def buildTournamentWeights(tournament_id, weight) + def buildTournamentWeights(weight) @wrestlers = weight.wrestlers - @matches = Pool.new.generatePools(weight, tournament_id, @matches) + @matches = Pool.new.generatePools(weight, @tournament.id, @matches) @weight_matches = @matches.select{|m| m.weight_id == weight.id } @last_match = @weight_matches.sort_by{|m| m.round}.last @highest_round = @last_match.round @matches = Poolbracket.new.generateBracketMatches(@matches, weight, @highest_round) end - def saveMatches(tournament,matches) - matches.each do |m| - m.tournament_id = tournament.id + def saveMatches + @matches.each do |m| + m.tournament_id = @tournament.id m.save end end From 9d78cd52d250f5a8ff839fdf9ef7bd640a0217fe Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sat, 23 May 2015 05:24:18 -0400 Subject: [PATCH 2/8] Composed the poolToBracket method. It should simply read as an outline of how to do its job. --- app/models/tournamentmatchgen.rb | 33 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index 4f8428d..a44b1a2 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -13,12 +13,9 @@ class Tournamentmatchgen end def poolToBracket - destroyMatches() - @tournament.weights.sort_by{|x|[x.max]}.each do |w| - buildTournamentWeights(w) - end - @matches = Boutgen.new.assignBouts(@matches,@tournament.weights) - @matches = Losernamegen.new.assignLoserNames(@matches,@tournament.weights) + destroyMatches + buildTournamentWeights + generateMatches saveMatches return @matches end @@ -28,13 +25,22 @@ class Tournamentmatchgen @matches = [] end - def buildTournamentWeights(weight) - @wrestlers = weight.wrestlers - @matches = Pool.new.generatePools(weight, @tournament.id, @matches) - @weight_matches = @matches.select{|m| m.weight_id == weight.id } - @last_match = @weight_matches.sort_by{|m| m.round}.last - @highest_round = @last_match.round - @matches = Poolbracket.new.generateBracketMatches(@matches, weight, @highest_round) + def buildTournamentWeights + @tournament.weights.sort_by{|x|[x.max]}.each do |weight| + @wrestlers = weight.wrestlers + @matches = Pool.new.generatePools(weight, @tournament.id, @matches) + @weight_matches = @matches.select{|m| m.weight_id == weight.id } + @last_match = @weight_matches.sort_by{|m| m.round}.last + @highest_round = @last_match.round + @matches = Poolbracket.new.generateBracketMatches(@matches, weight, @highest_round) + end + end + + def generateMatches + @matches = + Losernamegen.new.assignLoserNames( + Boutgen.new.assignBouts(@matches, @tournament.weights), + @tournament.weights) end def saveMatches @@ -43,4 +49,5 @@ class Tournamentmatchgen m.save end end + end From 81779246359bfedb690fe924199f6ecb5d746354 Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sat, 23 May 2015 05:46:28 -0400 Subject: [PATCH 3/8] move some variables to method scope --- app/models/tournamentmatchgen.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index a44b1a2..a26810e 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -9,7 +9,7 @@ class Tournamentmatchgen if @tournament.tournament_type == "Pool to bracket" @matches = poolToBracket() end - return @matches + @matches end def poolToBracket @@ -17,7 +17,7 @@ class Tournamentmatchgen buildTournamentWeights generateMatches saveMatches - return @matches + @matches end def destroyMatches @@ -27,12 +27,11 @@ class Tournamentmatchgen def buildTournamentWeights @tournament.weights.sort_by{|x|[x.max]}.each do |weight| - @wrestlers = weight.wrestlers @matches = Pool.new.generatePools(weight, @tournament.id, @matches) - @weight_matches = @matches.select{|m| m.weight_id == weight.id } - @last_match = @weight_matches.sort_by{|m| m.round}.last - @highest_round = @last_match.round - @matches = Poolbracket.new.generateBracketMatches(@matches, weight, @highest_round) + weight_matches = @matches.select{|m| m.weight_id == weight.id } + last_match = weight_matches.sort_by{|m| m.round}.last + highest_round = last_match.round + @matches = Poolbracket.new.generateBracketMatches(@matches, weight, highest_round) end end From 399923b94979d97b2842c98ab8c380a75155019e Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sat, 23 May 2015 06:01:15 -0400 Subject: [PATCH 4/8] removed the matches argument to generatePools and roundRobin --- app/models/pool.rb | 12 +++++++----- app/models/tournamentmatchgen.rb | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/models/pool.rb b/app/models/pool.rb index cc6cb98..6bf15f4 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -1,15 +1,17 @@ class Pool - def generatePools(weight, tournament, matches) + def generatePools(weight, tournament) + matches = [] @pools = weight.pools @pool = 1 while @pool <= @pools - matches = roundRobin(weight.wrestlers, weight, tournament, matches) + matches += roundRobin(weight.wrestlers, weight, tournament) @pool += 1 end return matches end - def roundRobin(wrestlers,weight,tournament,matches) + def roundRobin(wrestlers,weight,tournament) + matches = [] @wrestlers = wrestlers.select{|w| w.generatePoolNumber == @pool}.to_a @poolMatches = RoundRobinTournament.schedule(@wrestlers).reverse @poolMatches.each_with_index do |b, index| @@ -17,8 +19,8 @@ class Pool @bout = b.map @bout.each do |bout| if bout[0] != nil and bout[1] != nil - @match = Match.new(w1: bout[0].id, w2: bout[1].id, weight_id: weight.id, round: round) - matches << @match + match = Match.new(w1: bout[0].id, w2: bout[1].id, weight_id: weight.id, round: round) + matches << match end end end diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index a26810e..c418e32 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -27,11 +27,11 @@ class Tournamentmatchgen def buildTournamentWeights @tournament.weights.sort_by{|x|[x.max]}.each do |weight| - @matches = Pool.new.generatePools(weight, @tournament.id, @matches) - weight_matches = @matches.select{|m| m.weight_id == weight.id } + matches = Pool.new.generatePools(weight, @tournament.id) + weight_matches = matches.select{|m| m.weight_id == weight.id } last_match = weight_matches.sort_by{|m| m.round}.last highest_round = last_match.round - @matches = Poolbracket.new.generateBracketMatches(@matches, weight, highest_round) + @matches += Poolbracket.new.generateBracketMatches(matches, weight, highest_round) end end From aec47ddf1316336628b9eb33d693249763f751c9 Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sat, 23 May 2015 06:15:44 -0400 Subject: [PATCH 5/8] introduced a constructor for Pool and reduced arguments on roundRobin and generatePools --- app/models/pool.rb | 18 +++++++++++------- app/models/tournamentmatchgen.rb | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/models/pool.rb b/app/models/pool.rb index 6bf15f4..168f59b 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -1,18 +1,22 @@ class Pool - def generatePools(weight, tournament) - matches = [] - @pools = weight.pools + def initialize(weight) + @weight = weight @pool = 1 - while @pool <= @pools - matches += roundRobin(weight.wrestlers, weight, tournament) + end + + def generatePools + matches = [] + pools = @weight.pools + while @pool <= pools + matches += roundRobin(@weight) @pool += 1 end return matches end - def roundRobin(wrestlers,weight,tournament) + def roundRobin(weight) matches = [] - @wrestlers = wrestlers.select{|w| w.generatePoolNumber == @pool}.to_a + @wrestlers = weight.wrestlers.select{|w| w.generatePoolNumber == @pool}.to_a @poolMatches = RoundRobinTournament.schedule(@wrestlers).reverse @poolMatches.each_with_index do |b, index| round = index + 1 diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index c418e32..a88f36a 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -27,7 +27,7 @@ class Tournamentmatchgen def buildTournamentWeights @tournament.weights.sort_by{|x|[x.max]}.each do |weight| - matches = Pool.new.generatePools(weight, @tournament.id) + matches = Pool.new(weight).generatePools() weight_matches = matches.select{|m| m.weight_id == weight.id } last_match = weight_matches.sort_by{|m| m.round}.last highest_round = last_match.round From 23d8c4e1592b5719c1fb18d9d5d8d6bb2033286b Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sat, 23 May 2015 06:38:55 -0400 Subject: [PATCH 6/8] remove another argument from roundRobin --- app/models/pool.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/models/pool.rb b/app/models/pool.rb index 168f59b..37c928d 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -8,22 +8,26 @@ class Pool matches = [] pools = @weight.pools while @pool <= pools - matches += roundRobin(@weight) + matches += roundRobin() @pool += 1 end return matches end - def roundRobin(weight) + def roundRobin matches = [] - @wrestlers = weight.wrestlers.select{|w| w.generatePoolNumber == @pool}.to_a - @poolMatches = RoundRobinTournament.schedule(@wrestlers).reverse - @poolMatches.each_with_index do |b, index| + wrestlers = @weight.wrestlers.select{|w| w.generatePoolNumber == @pool}.to_a + poolMatches = RoundRobinTournament.schedule(wrestlers).reverse + poolMatches.each_with_index do |b, index| round = index + 1 - @bout = b.map - @bout.each do |bout| + bouts = b.map + bouts.each do |bout| if bout[0] != nil and bout[1] != nil - match = Match.new(w1: bout[0].id, w2: bout[1].id, weight_id: weight.id, round: round) + match = Match.new( + w1: bout[0].id, + w2: bout[1].id, + weight_id: @weight.id, + round: round) matches << match end end From 2a57cafd6db1b9b0e39700a836cc9e02f0ae1f17 Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sat, 23 May 2015 08:07:11 -0400 Subject: [PATCH 7/8] extracted wrestlers_for_pool method removed unnecessary select --- app/models/pool.rb | 2 +- app/models/tournament.rb | 4 ++-- app/models/tournamentmatchgen.rb | 3 +-- app/models/weight.rb | 4 ++++ 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/models/pool.rb b/app/models/pool.rb index 37c928d..cb823c8 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -16,7 +16,7 @@ class Pool def roundRobin matches = [] - wrestlers = @weight.wrestlers.select{|w| w.generatePoolNumber == @pool}.to_a + wrestlers = @weight.wrestlers_for_pool(@pool) poolMatches = RoundRobinTournament.schedule(wrestlers).reverse poolMatches.each_with_index do |b, index| round = index + 1 diff --git a/app/models/tournament.rb b/app/models/tournament.rb index c3e094d..b41c740 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -28,10 +28,10 @@ class Tournament < ActiveRecord::Base def upcomingMatches if matches.nil? - return matches + return nil else generateMatchups - return matches + matches end end diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index a88f36a..5285559 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -28,8 +28,7 @@ class Tournamentmatchgen def buildTournamentWeights @tournament.weights.sort_by{|x|[x.max]}.each do |weight| matches = Pool.new(weight).generatePools() - weight_matches = matches.select{|m| m.weight_id == weight.id } - last_match = weight_matches.sort_by{|m| m.round}.last + last_match = matches.sort_by{|m| m.round}.last highest_round = last_match.round @matches += Poolbracket.new.generateBracketMatches(matches, weight, highest_round) end diff --git a/app/models/weight.rb b/app/models/weight.rb index 0298e8b..3ae2cf8 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -8,6 +8,10 @@ class Weight < ActiveRecord::Base self.tournament.destroyAllMatches end + def wrestlers_for_pool(pool) + wrestlers.select{|w| w.generatePoolNumber == pool}.to_a + end + def pools @wrestlers = self.wrestlers if @wrestlers.size <= 6 From bec751c4fd066494f21c689312f3330724b27087 Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sat, 23 May 2015 08:48:13 -0400 Subject: [PATCH 8/8] Added indexes on the frequent queries. The problem is still too many queries. --- db/migrate/20150523121319_introduce_indexes.rb | 10 ++++++++++ db/schema.rb | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20150523121319_introduce_indexes.rb diff --git a/db/migrate/20150523121319_introduce_indexes.rb b/db/migrate/20150523121319_introduce_indexes.rb new file mode 100644 index 0000000..ff24411 --- /dev/null +++ b/db/migrate/20150523121319_introduce_indexes.rb @@ -0,0 +1,10 @@ +class IntroduceIndexes < ActiveRecord::Migration + def change + add_index :weights, :tournament_id + add_index :schools, :tournament_id + add_index :mats, :tournament_id + add_index :matches, :tournament_id + add_index :matches, [:w1, :w2], :unique => true + add_index :wrestlers, :weight_id + end +end diff --git a/db/schema.rb b/db/schema.rb index d9d5f13..ffedbe8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150517075923) do +ActiveRecord::Schema.define(version: 20150523121319) do create_table "matches", force: :cascade do |t| t.integer "w1" @@ -34,6 +34,9 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.string "loser2_name" end + add_index "matches", ["tournament_id"], name: "index_matches_on_tournament_id" + add_index "matches", ["w1", "w2"], name: "index_matches_on_w1_and_w2", unique: true + create_table "mats", force: :cascade do |t| t.string "name" t.integer "tournament_id" @@ -41,6 +44,8 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.datetime "updated_at" end + add_index "mats", ["tournament_id"], name: "index_mats_on_tournament_id" + create_table "schools", force: :cascade do |t| t.string "name" t.datetime "created_at" @@ -48,6 +53,8 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.integer "tournament_id" end + add_index "schools", ["tournament_id"], name: "index_schools_on_tournament_id" + create_table "tournaments", force: :cascade do |t| t.string "name" t.string "address" @@ -83,6 +90,8 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.integer "tournament_id" end + add_index "weights", ["tournament_id"], name: "index_weights_on_tournament_id" + create_table "wrestlers", force: :cascade do |t| t.string "name" t.integer "school_id" @@ -97,4 +106,6 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.boolean "extra" end + add_index "wrestlers", ["weight_id"], name: "index_wrestlers_on_weight_id" + end