comparison app/helpers/activities_helper.rb @ 1186:e19f375f9afa feature_566

Score activity to favour more recent events
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 22 Jan 2013 14:49:24 +0000
parents 4d1a31b30987
children adb5f38f6ab7
comparison
equal deleted inserted replaced
1185:f37237021a46 1186:e19f375f9afa
1 1
2 module ActivitiesHelper 2 module ActivitiesHelper
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
6 # occurrences of project in list (there is surely a tidier way 6 # Score each project for which there are any events, by giving
7 # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8) 7 # each event a score based on how long ago it was (the more recent
8 phash = events.map do |e| 8 # the better).
9 e.project unless !e.respond_to?(:project) 9
10 end.select { |p| !p.nil? }.sort.group_by { |p| p.id } 10 projhash = Hash.new
11 phash = phash.merge(phash) { |k,v| v.length } 11
12 threshold = phash.values.sort.last(count).first 12 events.each do |e|
13 busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count) 13 if e.respond_to?(:project)
14 p = e.project
15 d = if e.respond_to? :updated_at then e.updated_at else e.updated_on end
16 dd = Date.parse d.to_s
17 age = Date.today - dd
18 score = (age < 14 ? 15-age : 1)
19 if projhash.key? p
20 projhash[p] += score
21 else
22 projhash[p] = score
23 end
24 end
25 end
26
27 # pick N highest values and use cutoff value as selection threshold
28 threshold = projhash.values.sort.last(count).first
29
30 # select projects above threshold and pick N from them randomly
31 busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count)
32
33 # return projects rather than just ids
14 busy.map { |pid| Project.find(pid) } 34 busy.map { |pid| Project.find(pid) }
15 end 35 end
16 36
17 def busy_institutions(events, count) 37 def busy_institutions(events, count)
18 authors = events.map do |e| 38 authors = events.map do |e|