comparison app/helpers/activities_helper.rb @ 1168:4d1a31b30987 cannam

Remove nil projects before sorting
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Mon, 21 Jan 2013 15:48:48 +0000
parents b98f60a6d231
children e19f375f9afa
comparison
equal deleted inserted replaced
1167:54a92921cf09 1168:4d1a31b30987
3 3
4 def busy_projects(events, count) 4 def busy_projects(events, count)
5 # Transform events list into hash from project id to number of 5 # Transform events list into hash from project id to number of
6 # occurrences of project in list (there is surely a tidier way 6 # occurrences of project in list (there is surely a tidier way
7 # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8) 7 # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8)
8 phash = events.map { |e| e.project unless !e.respond_to?(:project) }.sort.group_by { |p| p.id } 8 phash = events.map do |e|
9 e.project unless !e.respond_to?(:project)
10 end.select { |p| !p.nil? }.sort.group_by { |p| p.id }
9 phash = phash.merge(phash) { |k,v| v.length } 11 phash = phash.merge(phash) { |k,v| v.length }
10 threshold = phash.values.sort.last(count).first 12 threshold = phash.values.sort.last(count).first
11 busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count) 13 busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count)
12 busy.map { |pid| Project.find(pid) } 14 busy.map { |pid| Project.find(pid) }
13 end 15 end