Skip to content

Commit

Permalink
add track stats
Browse files Browse the repository at this point in the history
  • Loading branch information
codez committed Aug 14, 2019
1 parent e263eef commit 3481b46
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions app/services/stats/tracks_stat.rb
@@ -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

0 comments on commit 3481b46

Please sign in to comment.