Skip to content

Commit

Permalink
Add yearly leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
pjurewicz committed Oct 12, 2023
1 parent c24aea8 commit 389decd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/models/daily_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class DailyStatistic < ActiveRecord::Base
scope :by_period, -> (starting_date, duration) { where('date >= ? and date < ?', starting_date, starting_date + duration) }
scope :for_this_week, -> (time_zone) { by_period(Time.now.in_time_zone(time_zone).beginning_of_week.to_date, 7.days) }
scope :for_this_month, -> (time_zone) { by_period(Time.now.in_time_zone(time_zone).beginning_of_month.to_date, 1.month) }
scope :for_this_year, -> (time_zone) { by_period(Time.now.in_time_zone(time_zone).beginning_of_year.to_date, 1.year) }
scope :of_team, -> (team_id) { where(team_id: team_id) }

def self.rebuild_read_model!(event_store = Rails.configuration.event_store)
Expand Down
28 changes: 13 additions & 15 deletions app/models/leaderboards.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
class Leaderboards
def initialize(team)
@team_id = Integer(team.id)
@team_time_zone = team.time_zone
@team_scope = DailyStatistic.of_team(team.id)
end

def top_for_this_week
DailyStatistic
.for_this_week(@team_time_zone)
.of_team(@team_id)
.group('user_name')
.order('sum_points desc')
.limit(10)
.sum('points')
.group_by(&:second)
.then(&method(:format))
aggreated_data(@team_scope.for_this_week(@team_time_zone))
end

def top_for_this_month
DailyStatistic
.for_this_month(@team_time_zone)
.of_team(@team_id)
aggreated_data(@team_scope.for_this_month(@team_time_zone))
end

def top_for_this_year
aggreated_data(@team_scope.for_this_year(@team_time_zone))
end

private

def aggreated_data(scope)
scope
.group('user_name')
.order('sum_points desc')
.limit(10)
Expand All @@ -28,8 +28,6 @@ def top_for_this_month
.then(&method(:format))
end

private

def format(hash)
hash
.map { |count, members| "#{count}: #{members.map(&:first).join(", ")}" }
Expand Down

0 comments on commit 389decd

Please sign in to comment.