add nice statistics
This commit is contained in:
3
app/models/activity.rb
Normal file
3
app/models/activity.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class Activity < ApplicationRecord
|
||||
belongs_to :user
|
||||
end
|
||||
3
app/models/login.rb
Normal file
3
app/models/login.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class Login < ApplicationRecord
|
||||
belongs_to :user
|
||||
end
|
||||
2
app/models/shakti.rb
Normal file
2
app/models/shakti.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class Shakti < ApplicationRecord
|
||||
end
|
||||
40
app/models/statistics.rb
Normal file
40
app/models/statistics.rb
Normal file
@@ -0,0 +1,40 @@
|
||||
class Statistics
|
||||
attr_reader :user
|
||||
|
||||
def initialize(user)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def total_plays
|
||||
Activity.where(user: user).count
|
||||
end
|
||||
|
||||
def activity_by_day
|
||||
start_date = 1.year.ago.to_date
|
||||
end_date = Date.today
|
||||
activities = Activity.where(user: user, created_at: start_date.beginning_of_day..end_date.end_of_day)
|
||||
activities.group("DATE(created_at)").count.transform_keys { |d| d.to_s }
|
||||
end
|
||||
|
||||
def top_artists_all_time
|
||||
Activity.where(user: user)
|
||||
.group(:item_title).order("count_id DESC").limit(5).count(:id)
|
||||
end
|
||||
|
||||
def top_artists_year
|
||||
year_start = Date.today.beginning_of_year
|
||||
Activity.where(user: user, created_at: year_start..Date.today.end_of_day)
|
||||
.group(:item_title).order("count_id DESC").limit(5).count(:id)
|
||||
end
|
||||
|
||||
def top_artists_upcoming
|
||||
upcoming_start = 30.days.ago.to_date
|
||||
upcoming_end = Date.today
|
||||
Activity.where(user: user, started_at: upcoming_start.beginning_of_day..upcoming_end.end_of_day)
|
||||
.group(:item_title).order("count_id DESC").limit(5).count(:id)
|
||||
end
|
||||
|
||||
def since_date
|
||||
Activity.where(user: user).order(:created_at).limit(1).pick(:created_at)
|
||||
end
|
||||
end
|
||||
6
app/models/user.rb
Normal file
6
app/models/user.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
class User < ApplicationRecord
|
||||
# Include default devise modules. Others available are:
|
||||
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :validatable
|
||||
end
|
||||
Reference in New Issue
Block a user