finish activity ui
This commit is contained in:
@@ -9,6 +9,11 @@ class Statistics
|
||||
Activity.where(user: user).count
|
||||
end
|
||||
|
||||
def total_plays_year
|
||||
year_start = Date.today.beginning_of_year
|
||||
Activity.where(user: user, created_at: year_start..Date.today.end_of_day).count
|
||||
end
|
||||
|
||||
def activity_by_day
|
||||
start_date = 1.year.ago.to_date
|
||||
end_date = Date.today
|
||||
@@ -37,4 +42,37 @@ class Statistics
|
||||
def since_date
|
||||
Activity.where(user: user).order(:created_at).limit(1).pick(:created_at)
|
||||
end
|
||||
|
||||
# Returns a hash with streak info: :start_date, :end_date, :length, :total_plays, :most_played_song, :most_played_song_count
|
||||
def longest_streak
|
||||
days = activity_by_day.keys.map { |d| Date.parse(d) }.sort
|
||||
return nil if days.empty?
|
||||
streaks = []
|
||||
current_streak = []
|
||||
days.each_with_index do |day, i|
|
||||
if i == 0 || day == days[i-1] + 1
|
||||
current_streak << day
|
||||
else
|
||||
streaks << current_streak
|
||||
current_streak = [day]
|
||||
end
|
||||
end
|
||||
streaks << current_streak unless current_streak.empty?
|
||||
best = streaks.max_by(&:length)
|
||||
return nil unless best && best.length > 0
|
||||
streak_start = best.first
|
||||
streak_end = best.last
|
||||
plays = Activity.where(user: user, created_at: streak_start.beginning_of_day..streak_end.end_of_day)
|
||||
total_plays = plays.count
|
||||
song_counts = plays.group(:item_title).order('count_id DESC').limit(1).count(:id)
|
||||
most_played_song, most_played_song_count = song_counts.first || [nil, 0]
|
||||
{
|
||||
start_date: streak_start,
|
||||
end_date: streak_end,
|
||||
length: best.length,
|
||||
total_plays: total_plays,
|
||||
most_played_song: most_played_song,
|
||||
most_played_song_count: most_played_song_count
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,4 +3,8 @@ class User < ApplicationRecord
|
||||
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :validatable
|
||||
|
||||
def is_admin?
|
||||
id == 1
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user