-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module Stats | ||
class TracksStat | ||
|
||
# X Wortanteil / period / show | ||
# * Uniq Artists / period / show (via track counts by artist) | ||
# * Uniq Song / period / show (via song counts) | ||
# * Track counts by Artist / period /show | ||
# * Song counts / period / show | ||
# | ||
# X Broadcast hours / period / show | ||
|
||
attr_reader :date_range | ||
|
||
def initialize(date_range) | ||
@date_range = date_range | ||
end | ||
|
||
def tracks | ||
Track.within(date_range.first, date_range.last).joins(:broadcast) | ||
end | ||
|
||
def broadcast | ||
Broadcast.within(date_range.first, date_range.last) | ||
end | ||
|
||
def track_durations | ||
@track_durations ||= | ||
tracks.group(:show_id).sum(duration_in_seconds('tracks')) | ||
end | ||
|
||
def broadcast_durations | ||
@broadcast_durations ||= | ||
broadcasts.group(:show_id).sum(duration_in_seconds('broadcasts')) | ||
end | ||
|
||
def track_ratios | ||
broadcast_durations.each_with_object do |(show_id, value), hash| | ||
hash[show_id] = track_durations[show_id] / value.to_f | ||
end | ||
end | ||
|
||
def duration_in_seconds(table) | ||
case db_adapter | ||
when /postgres/ | ||
"extract(epoch from (#{table}.finished_at - #{table}.started_at))" | ||
when /sqlite/ | ||
"strftime('%s', #{table}.finished_at) - strftime('%s', #{table}.started_at)" | ||
else | ||
raise "Unsupported DB adapter #{db_adapter}" | ||
end | ||
end | ||
|
||
def db_adapter | ||
Track.connection.adapter_name.downcase | ||
end | ||
|
||
end | ||
end |