chris@1011: chris@1011: module ActivitiesHelper chris@1011: chris@1011: def busy_projects(events, count) chris@1186: chris@1186: # Score each project for which there are any events, by giving chris@1186: # each event a score based on how long ago it was (the more recent chris@1186: # the better). chris@1186: chris@1186: projhash = Hash.new chris@1186: chris@1186: events.each do |e| chris@1186: if e.respond_to?(:project) chris@1186: p = e.project chris@1186: d = if e.respond_to? :updated_at then e.updated_at else e.updated_on end chris@1186: dd = Date.parse d.to_s chris@1186: age = Date.today - dd chris@1186: score = (age < 14 ? 15-age : 1) chris@1186: if projhash.key? p chris@1186: projhash[p] += score chris@1186: else chris@1186: projhash[p] = score chris@1186: end chris@1186: end chris@1186: end chris@1186: chris@1186: # pick N highest values and use cutoff value as selection threshold chris@1186: threshold = projhash.values.sort.last(count).first chris@1186: chris@1186: # select projects above threshold and pick N from them randomly chris@1186: busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count) chris@1186: chris@1186: # return projects rather than just ids chris@1011: busy.map { |pid| Project.find(pid) } chris@1011: end chris@1011: chris@1011: def busy_institutions(events, count) Chris@1013: authors = events.map do |e| Chris@1013: e.event_author unless !e.respond_to?(:event_author) Chris@1013: end.compact Chris@1013: institutions = authors.map do |a| Chris@1013: if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil? Chris@1013: a.ssamr_user_detail.institution_name Chris@1013: end Chris@1013: end chris@1011: insthash = institutions.compact.sort.group_by { |i| i } chris@1011: insthash = insthash.merge(insthash) { |k,v| v.length } chris@1011: threshold = insthash.values.sort.last(count).first chris@1011: insthash.keys.select { |k| insthash[k] >= threshold }.sample(count) chris@1011: end chris@1011: chris@1011: end