Skip to content

Instantly share code, notes, and snippets.

@cmorss
Created April 4, 2014 16:46
Show Gist options
  • Save cmorss/9978519 to your computer and use it in GitHub Desktop.
Save cmorss/9978519 to your computer and use it in GitHub Desktop.
Top Genres and Moods for an Artist
def artist_stats ids
Artist.find(ids).each do |artist|
puts artist.name
tracks = artist.tracks.active
puts "tracks : #{tracks.count}"
puts "streams : #{tracks.map {|t| Track.hits[t.id]}.sum}"
downloads = Filing.joins(:folder).where("filings.track_id in (?) and folders.name = 'Downloads'", tracks.map(&:id)).count
puts "downloads : #{downloads}"
licensings = WorkTrack.where(track_id: tracks.map(&:id)).count
puts "licensings : #{licensings}"
puts "total : #{licensings + downloads}"
genres = artist.tracks.active.map{|t| t.genres.map(&:full_name)}.flatten
b = Hash.new(0)
genres.each {|g| b[g] += 1 }
b.map do |g,c|
("%02d" % c) + " -> #{g}"
end.sort.reverse.each {|l| puts l}
puts "---------------------------------"
moods = artist.tracks.active.map{|t| t.moods.map(&:name)}.flatten
b = Hash.new(0)
moods.each {|g| b[g] += 1 }
b.map do |g,c|
("%02d" % c) + " -> #{g}"
end.sort.reverse.each {|l| puts l}
puts "---------------------------------"
end
nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment