about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2017-10-03 22:49:19 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2017-10-03 22:49:19 -0400
commit353b170392892a1f9bdde74550112b5131c99fd2 (patch)
tree46305a7d5fc40c4a70a7fd7615a0e549730605ec
parent4bc96847f621c4cb3940fe99b3b4b67b20596189 (diff)
downloadpokeviewer-353b170392892a1f9bdde74550112b5131c99fd2.tar.gz
pokeviewer-353b170392892a1f9bdde74550112b5131c99fd2.tar.bz2
pokeviewer-353b170392892a1f9bdde74550112b5131c99fd2.zip
Optimized front page queries
Reduced the number of queries that the front page runs to 2 queries
total: one to get trainer data, and one to get the Pokémon. The latter
query selects only the necessary columns, notably ignoring basically
everything from the revisions join other than the nickname of the most
recent revision. The controller then does the work of chunking the
Pokémon into party/boxes, and then into trainers/unaccounted.

refs #2
-rw-r--r--app/controllers/pokeviewer/pokemon_controller.rb67
-rw-r--r--app/views/pokeviewer/pokemon/index.html.haml12
2 files changed, 70 insertions, 9 deletions
diff --git a/app/controllers/pokeviewer/pokemon_controller.rb b/app/controllers/pokeviewer/pokemon_controller.rb index 17bd83d..f9f6c15 100644 --- a/app/controllers/pokeviewer/pokemon_controller.rb +++ b/app/controllers/pokeviewer/pokemon_controller.rb
@@ -3,12 +3,73 @@ require_dependency "pokeviewer/application_controller"
3module Pokeviewer 3module Pokeviewer
4 class PokemonController < ApplicationController 4 class PokemonController < ApplicationController
5 def index 5 def index
6 @trainers = Trainer.all 6 pokemon = Pokemon.joins(:revisions).
7 @unaccounted = Pokemon.where(trainer: nil) 7 order("trainer_id IS NULL DESC").
8 order(trainer_id: :asc).
9 order(box: :asc).
10 order(slot: :asc).
11 order("pokeviewer_revisions.sequential_id DESC").
12 group("pokeviewer_pokemon.uuid").
13 select(:box, :slot, :uuid, :trainer_id, :species_id).
14 select(:ot_gender, :ot_name, :unown_letter).
15 select("pokeviewer_revisions.nickname AS nickname").
16 chunk do |p|
17 if p.trainer_id.nil?
18 -1
19 else
20 p.trainer_id
21 end
22 end
23
24 if pokemon.peek.first == -1
25 @unaccounted = pokemon.next.second
26 else
27 @unaccounted = []
28 end
29
30 @trainers = Trainer.order(id: :asc).all.map do |trainer|
31 if trainer.id == pokemon.peek.first
32 party = []
33
34 boxes = (1..14).map do |i|
35 {
36 name: trainer.box_name(i),
37 pokemon: [nil] * 30
38 }
39 end
40
41 pokemon.next.second.chunk do |p|
42 if p.box.nil?
43 -1
44 else
45 p.box
46 end
47 end.each do |box, pokes|
48 if box == -1
49 party = pokes
50 else
51 boxes[box-1][:pokemon] = (0..29).map do |i|
52 if not pokes.empty? and (pokes.first.slot == i)
53 pokes.shift
54 else
55 nil
56 end
57 end
58 end
59 end
60
61 [trainer, party, boxes]
62 else
63 nil
64 end
65 end.compact
8 end 66 end
9 67
10 def show 68 def show
11 @pokemon = Pokemon.find_by_uuid! params[:id] 69 @pokemon = Pokemon.includes(
70 :trainer, :species, :location,
71 revisions: [:item, :move_1, :move_2, :move_3, :move_4]
72 ).find_by_uuid! params[:id]
12 end 73 end
13 end 74 end
14end 75end
diff --git a/app/views/pokeviewer/pokemon/index.html.haml b/app/views/pokeviewer/pokemon/index.html.haml index 0be3edd..e1793dc 100644 --- a/app/views/pokeviewer/pokemon/index.html.haml +++ b/app/views/pokeviewer/pokemon/index.html.haml
@@ -1,4 +1,4 @@
1- @trainers.each do |trainer| 1- @trainers.each do |trainer, party, boxes|
2 .trainer 2 .trainer
3 .trainer-info{ class: trainer.game } 3 .trainer-info{ class: trainer.game }
4 %h2= trainer.name 4 %h2= trainer.name
@@ -6,11 +6,11 @@
6 .pc-boxes 6 .pc-boxes
7 %ul.party.pc-box 7 %ul.party.pc-box
8 %h3 Party 8 %h3 Party
9 - trainer.party.each do |p| 9 - party.each do |p|
10 %li 10 %li
11 %span.party-icon= image_tag p.icon_path 11 %span.party-icon= image_tag p.icon_path
12 %span.party-name= link_to p.revisions.last.nickname, p 12 %span.party-name= link_to p.nickname, p
13 - trainer.boxes.each do |box| 13 - boxes.each do |box|
14 .pc-box 14 .pc-box
15 %h3= box[:name] 15 %h3= box[:name]
16 %table.pc-contents 16 %table.pc-contents
@@ -23,7 +23,7 @@
23 - else 23 - else
24 = link_to image_tag(p.icon_path), p 24 = link_to image_tag(p.icon_path), p
25 .pc-data.pkv-hover 25 .pc-data.pkv-hover
26 .pc-data-name= p.revisions.last.nickname 26 .pc-data-name= p.nickname
27 .pc-data-ot 27 .pc-data-ot
28 OT/ 28 OT/
29 %span{ class: p.ot_gender }>= p.ot_name 29 %span{ class: p.ot_gender }>= p.ot_name
@@ -36,7 +36,7 @@
36 %li.pc-pokemon.pkv-has-hover 36 %li.pc-pokemon.pkv-has-hover
37 = link_to image_tag(p.icon_path), p 37 = link_to image_tag(p.icon_path), p
38 .pc-data.pkv-hover 38 .pc-data.pkv-hover
39 .pc-data-name= p.revisions.last.nickname 39 .pc-data-name= p.nickname
40 .pc-data-ot 40 .pc-data-ot
41 OT/ 41 OT/
42 %span{ class: p.ot_gender }>= p.ot_name 42 %span{ class: p.ot_gender }>= p.ot_name