Mercurial > hg > soundsoftware-site
annotate app/helpers/activities_helper.rb @ 1467:5bd4d3da7eb3 feature_564
Close obsolete branch feature_564
author | Chris Cannam |
---|---|
date | Wed, 23 Jan 2013 17:11:01 +0000 |
parents | e19f375f9afa |
children | adb5f38f6ab7 |
rev | line source |
---|---|
chris@1011 | 1 |
chris@1011 | 2 module ActivitiesHelper |
chris@1011 | 3 |
chris@1011 | 4 def busy_projects(events, count) |
chris@1186 | 5 |
chris@1186 | 6 # Score each project for which there are any events, by giving |
chris@1186 | 7 # each event a score based on how long ago it was (the more recent |
chris@1186 | 8 # the better). |
chris@1186 | 9 |
chris@1186 | 10 projhash = Hash.new |
chris@1186 | 11 |
chris@1186 | 12 events.each do |e| |
chris@1186 | 13 if e.respond_to?(:project) |
chris@1186 | 14 p = e.project |
chris@1186 | 15 d = if e.respond_to? :updated_at then e.updated_at else e.updated_on end |
chris@1186 | 16 dd = Date.parse d.to_s |
chris@1186 | 17 age = Date.today - dd |
chris@1186 | 18 score = (age < 14 ? 15-age : 1) |
chris@1186 | 19 if projhash.key? p |
chris@1186 | 20 projhash[p] += score |
chris@1186 | 21 else |
chris@1186 | 22 projhash[p] = score |
chris@1186 | 23 end |
chris@1186 | 24 end |
chris@1186 | 25 end |
chris@1186 | 26 |
chris@1186 | 27 # pick N highest values and use cutoff value as selection threshold |
chris@1186 | 28 threshold = projhash.values.sort.last(count).first |
chris@1186 | 29 |
chris@1186 | 30 # select projects above threshold and pick N from them randomly |
chris@1186 | 31 busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count) |
chris@1186 | 32 |
chris@1186 | 33 # return projects rather than just ids |
chris@1011 | 34 busy.map { |pid| Project.find(pid) } |
chris@1011 | 35 end |
chris@1011 | 36 |
chris@1011 | 37 def busy_institutions(events, count) |
Chris@1013 | 38 authors = events.map do |e| |
Chris@1013 | 39 e.event_author unless !e.respond_to?(:event_author) |
Chris@1013 | 40 end.compact |
Chris@1013 | 41 institutions = authors.map do |a| |
Chris@1013 | 42 if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil? |
Chris@1013 | 43 a.ssamr_user_detail.institution_name |
Chris@1013 | 44 end |
Chris@1013 | 45 end |
chris@1011 | 46 insthash = institutions.compact.sort.group_by { |i| i } |
chris@1011 | 47 insthash = insthash.merge(insthash) { |k,v| v.length } |
chris@1011 | 48 threshold = insthash.values.sort.last(count).first |
chris@1011 | 49 insthash.keys.select { |k| insthash[k] >= threshold }.sample(count) |
chris@1011 | 50 end |
chris@1011 | 51 |
chris@1011 | 52 end |