chris@1011
|
1
|
chris@1011
|
2 module ActivitiesHelper
|
chris@1011
|
3
|
chris@1011
|
4 def busy_projects(events, count)
|
chris@1011
|
5 # Transform events list into hash from project id to number of
|
chris@1011
|
6 # occurrences of project in list (there is surely a tidier way
|
chris@1011
|
7 # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8)
|
chris@1011
|
8 phash = events.map { |e| e.project unless !e.respond_to?(:project) }.sort.group_by { |p| p.id }
|
chris@1011
|
9 phash = phash.merge(phash) { |k,v| v.length }
|
chris@1011
|
10 threshold = phash.values.sort.last(count).first
|
chris@1011
|
11 busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count)
|
chris@1011
|
12 busy.map { |pid| Project.find(pid) }
|
chris@1011
|
13 end
|
chris@1011
|
14
|
chris@1011
|
15 def busy_institutions(events, count)
|
Chris@1013
|
16 authors = events.map do |e|
|
Chris@1013
|
17 e.event_author unless !e.respond_to?(:event_author)
|
Chris@1013
|
18 end.compact
|
Chris@1013
|
19 institutions = authors.map do |a|
|
Chris@1013
|
20 if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil?
|
Chris@1013
|
21 a.ssamr_user_detail.institution_name
|
Chris@1013
|
22 end
|
Chris@1013
|
23 end
|
chris@1011
|
24 insthash = institutions.compact.sort.group_by { |i| i }
|
chris@1011
|
25 insthash = insthash.merge(insthash) { |k,v| v.length }
|
chris@1011
|
26 threshold = insthash.values.sort.last(count).first
|
chris@1011
|
27 insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
|
chris@1011
|
28 end
|
chris@1011
|
29
|
chris@1011
|
30 end
|