Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrote sorting code in order to clean up the number of queries being run. #29

Merged
merged 2 commits into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/achievements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class AchievementsController < ApplicationController
def index
@achievements = @game.achievements.order(:text)
@title = 'Achievements'
@subtitle = pluralize(@achievements.count, 'achievement')
@subtitle = pluralize(@achievements.size, 'achievement')
end
end
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def load_messages_count
unless current_user.nil?
time = current_user.messages_stamp
@messages_count = if time.nil?
@game.messages.count
@game.messages.size
else
@game.messages.where('updated_at >= :time', time: time.utc).count
@game.messages.where('updated_at >= :time', time: time.utc).size
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/challenges_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def index
# Only exists for the purpose of providing an active tab for admins.
@active_division = @divisions.first
@title = 'Challenges'
@subtitle = %(#{pluralize(@challenges.count, 'challenge')} in
#{pluralize(@categories.count, 'category')})
@subtitle = %(#{pluralize(@challenges.size, 'challenge')} in
#{pluralize(@categories.size, 'category')})
end

# rubocop:disable Metrics/AbcSize
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/games_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def show
@events = @game.feed_items.order(:created_at).reverse_order.page(params[:page]).per(25)
@title = @game.name
@html_title = @title
@subtitle = %(#{pluralize(@game.players.count, 'team')} and
#{pluralize(@game.challenges.count, 'challenge')})
@subtitle = %(#{pluralize(@game.players.size, 'team')} and
#{pluralize(@game.challenges.size, 'challenge')})
@submitted_flags = to_timeline(SubmittedFlag.all.group_by do |sf|
sf.updated_at.change(sec: 0)
end)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ def index
@messages_count = 0
current_user.update_attribute(:messages_stamp, Time.now.utc)
@title = 'Messages'
@subtitle = pluralize(@messages.count, 'message')
@subtitle = pluralize(@messages.size, 'message')
end
end
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
@divisions = @game.divisions
@active_division = current_user && !current_user.admin? ? current_user.division : @divisions.first
@title = 'Teams'
@subtitle = pluralize(@game.players.count, 'team')
@subtitle = pluralize(@game.players.size, 'team')
end

# rubocop:disable Metrics/AbcSize
Expand All @@ -23,7 +23,7 @@ def show
@score = @player.score
@title = @player.display_name
# This line is long because we need to NOT create these types of things in the controller.
@subtitle = %(#{pluralize(@score, 'point')} and #{pluralize(@achievements.count, 'achievement')}
@subtitle = %(#{pluralize(@score, 'point')} and #{pluralize(@achievements.size, 'achievement')}
in #{@player.division.name} division)
render :show
end
Expand Down
59 changes: 18 additions & 41 deletions app/models/division.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,26 @@ def add_states_to_challenges
end
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable MethodLength
# Sorts the provided list of players.
# Sorts the provided list of players. This sorts directly in the database instead of getting the
# data out of the database and sorting in rails. It gets all feed items of type ScoreAdjustment
# and SolvedChallenge and sums up their values or the value of the challenge in the case of a
# SolvedChallenge.
def filter_and_sort_players(filters)
players.where(filters).sort do |a, b|
#
# get scores
#
a_score = a.score
b_score = b.score

#
# if the scores are the same sort based on the first
# team to get to the current score
#
if a_score == b_score

#
# get solved challenges
#
future_date = Time.zone.now + 100.years
a_most_recent = a.solved_challenges.order(:created_at).last
a_date = a_most_recent ? a_most_recent.created_at : future_date
b_most_recent = b.solved_challenges.order(:created_at).last
b_date = b_most_recent ? b_most_recent.created_at : future_date

#
# if both teams have solved challenges
#
if a_date == b_date
a.display_name <=> b.display_name
else
a_date <=> b_date
end

#
# sort based on score
#
else
b_score <=> a_score
end
end
players.includes(:achievements).where(filters)
.joins(
"LEFT JOIN feed_items
ON feed_items.user_id = users.id
AND feed_items.type IN ('SolvedChallenge', 'ScoreAdjustment')
LEFT JOIN challenges ON challenges.id = feed_items.challenge_id"
)
.group('users.id')
.select(
'COALESCE(sum(challenges.point_value), 0) + COALESCE(sum(feed_items.point_value), 0)
as current_score,
MAX(feed_items.created_at) as last_solved_date, users.*'
)
.order('current_score desc', 'last_solved_date asc', 'display_name asc')
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable MethodLength
end
13 changes: 5 additions & 8 deletions app/models/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ class Player < User
after_create :touch_file

def score
points = 0
solved_challenges.includes(:challenge).each do |s|
points += s.challenge.point_value
end
score_adjustments.each do |a|
points += a.point_value
end
points
feed_items.where(type: [SolvedChallenge, ScoreAdjustment])
.joins(
'LEFT JOIN challenges ON challenges.id = feed_items.challenge_id'
)
.pluck(:point_value, :'challenges.point_value').flatten.compact.sum
end

def display_name
Expand Down
2 changes: 1 addition & 1 deletion app/views/achievements/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- if @achievements.count == 0
- if @achievements.size == 0
.zero-items-text No Achievements
- else
%table.table.table-bordered.table-striped
Expand Down
10 changes: 5 additions & 5 deletions app/views/challenges/_gameboard.html.haml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
- if challenges.count == 0
- if challenges.size == 0
.zero-items-text No Challenges
- else
- max = 0
%table.table.table-bordered
%thead
%tr
- categories.each do |c|
%th{ :style => "text-align:center;width:#{1.0 / @categories.count.to_f * 100.0}%;" }= c.name
- max = [max, c.challenges.count].max
%th{ :style => "text-align:center;width:#{1.0 / @categories.size.to_f * 100.0}%;" }= c.name
- max = [max, c.challenges.size].max
%tbody
- is_admin = current_user.admin?
- max.times do |row|
%tr
- categories.count.times do |column|
- categories.size.times do |column|
- challenges = categories[column].challenges
- if row < challenges.count
- if row < challenges.size
- challenge = challenges[row]
- is_solved = false
- style = "text-align:center;"
Expand Down
2 changes: 1 addition & 1 deletion app/views/challenges/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
.control-group
= button_tag "Submit", :class => "btn btn-primary"

- if @solved_by.count > 0
- if @solved_by.size > 0
%table.table.table-bordered.table-striped
%thead
%tr
Expand Down
8 changes: 4 additions & 4 deletions app/views/games/summary.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
window.highlightOnMap = (mapSelector, elementSelector) ->
d3.selectAll(elementSelector)
.transition()
.attr("class", "highlighted")
.attr("class", "highlighted")

window.unhighlightOnMap = (mapSelector, elementSelector) ->
d3.selectAll(elementSelector)
.transition()
.delay(250)
.attr("class", "team_location " + d3.selectAll(elementSelector).attr("tags"))
.attr("class", "team_location " + d3.selectAll(elementSelector).attr("tags"))
%h3{:class=>"muted"}
Top 5 Teams
%br/
Expand Down Expand Up @@ -84,12 +84,12 @@
%h3{:class=>"h4 muted"}
Currently Logged In Users:
%span{:class=>"lead"}
= @signed_in_players.count
= @signed_in_players.size
- @players.each do |p|
%div{style:"padding-left:15px;padding-top:10px;"}
%span{onmouseover:"Javascript:highlightOnMap('#map','##{p.email}')",onmouseout:"Javascript:unhighlightOnMap('#map','##{p.email}')"}
%strong{class: "text-info"}

= p.email
- if p.current_sign_in_ip.nil?
are not currently playing.
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/_divisions.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- if divisions.count > 1
- if divisions.size > 1
.panel.panel-default
-# Generate tabs for each division with a name
%ul.nav.nav-tabs
Expand Down
9 changes: 6 additions & 3 deletions app/views/users/_list.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- if players.count == 0
- if players.size.eql? 0
.zero-items-text No Teams
- else
- max = 0
Expand All @@ -12,15 +12,18 @@
%th{ :style => "width:50%;" }
%tbody
- players.each_with_index do |p, i|
- score = p.score
/ Current score only exists due to the division.rb model creating it inside of
/ filter_and_sort_players, it is used here since it saves us from doing a costly
/ score recalculation for each user.
- score = p.current_score
- if score > max
- max = score
- percent = score.to_f / max.to_f * 100.0
%tr
%td= i + 1
%td{:class => "#{p.tags}"}
= link_to p.display_name, p
%td= p.achievements.count
%td= p.achievements.size
%td= score
%td
.progress{ :style => "margin:0;" }
Expand Down
12 changes: 6 additions & 6 deletions app/views/users/public.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
%h3.muted Flags/Min:
= render partial: "events/timeline", locals: {id: "timeline", data: @submitted_flags}
%div.span5.offset1
%h3.muted
%h3.muted
Affiliation:
%small
%small
-if not @player.affiliation == ''
= @player.affiliation
= @player.affiliation
-else
Unknown

%div.row
%div.span5
- if @solved_challenges.count == 0
- if @solved_challenges.size == 0
.zero-items-text No Solved Challenges
- else
%table.table.table-bordered.table-striped
Expand All @@ -31,6 +31,6 @@
%td= s.challenge.point_value
%td= s.created_at.strftime("%b %e %y, %R %Z")


%div.span5.offset1
= render partial: "users/map", locals: {data: @players}
18 changes: 9 additions & 9 deletions app/views/users/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
%h3.muted Flags/Min:
= render partial: "events/timeline", locals: {id: "timeline", data: @submitted_flags}
%div.span5.offset1
%h3.muted
%h3.muted
-if current_user == @player and not @game.disable_vpn
Connection Information
-else
-else
Affiliation:
%small
%small
=@player.affiliation
%div.row
%div.span5
- if @solved_challenges.count == 0
- if @solved_challenges.size == 0
.zero-items-text No Solved Challenges
- else
%table.table.table-bordered.table-striped
Expand All @@ -30,10 +30,10 @@
%td= s.challenge.point_value
%td= s.created_at.strftime("%b %e %y, %R %Z")


-if(current_user == @player and not @game.disable_vpn)
%div.span5.offset1
:markdown
:markdown
To connect to the jumpbox from Kali download your key using the link below. Unzip the folder, open a terminal in that folder and run

`openvpn (ovpn file)`
Expand All @@ -43,7 +43,7 @@
-else
%div.span5.offset1
= render partial: "users/map", locals: {data: @players}


- if (not current_user.nil? and current_user.admin?) || current_user == @player and not @game.disable_vpn
%h3 VPN Cert
Expand All @@ -56,7 +56,7 @@

%h3 Achievements

- if @achievements.count == 0
- if @achievements.size == 0
.zero-items-text No Achievements
- else
%table.table.table-bordered.table-striped
Expand All @@ -72,7 +72,7 @@

- unless current_user.nil?
%h3 Score Adjustments
- if @adjustments.count == 0
- if @adjustments.size == 0
.zero-items-text No Adjustments
- else
%table.table.table-bordered.table-striped
Expand Down