Mercurial > hg > soundsoftware-site
comparison app/helpers/activities_helper.rb @ 1209:1b1138f6f55e
Merge from branch live
author | Chris Cannam |
---|---|
date | Wed, 23 Jan 2013 13:11:25 +0000 |
parents | adb5f38f6ab7 |
children | 875b5b4c574d |
comparison
equal
deleted
inserted
replaced
1078:b9e34a051f82 | 1209:1b1138f6f55e |
---|---|
1 | 1 |
2 module ActivitiesHelper | 2 module ActivitiesHelper |
3 | 3 |
4 def date_of_event(e) | |
5 if e.respond_to? :updated_at | |
6 e.updated_at | |
7 elsif e.respond_to? :updated_on | |
8 e.updated_on | |
9 elsif e.respond_to? :created_on | |
10 e.created_on | |
11 elsif e.respond_to? :committed_on | |
12 e.committed_on | |
13 else | |
14 nil | |
15 end | |
16 end | |
17 | |
4 def busy_projects(events, count) | 18 def busy_projects(events, count) |
5 # Transform events list into hash from project id to number of | 19 |
6 # occurrences of project in list (there is surely a tidier way | 20 # 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) | 21 # each event a score based on how long ago it was (the more recent |
8 phash = events.map { |e| e.project unless !e.respond_to?(:project) }.sort.group_by { |p| p.id } | 22 # the better). |
9 phash = phash.merge(phash) { |k,v| v.length } | 23 |
10 threshold = phash.values.sort.last(count).first | 24 projhash = Hash.new |
11 busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count) | 25 |
26 events.each do |e| | |
27 if e.respond_to?(:project) | |
28 p = e.project | |
29 d = date_of_event e | |
30 if !d.nil? | |
31 dd = Date.parse d.to_s | |
32 age = Date.today - dd | |
33 score = (age < 14 ? 15-age : 1) | |
34 if projhash.key? p | |
35 projhash[p] += score | |
36 else | |
37 projhash[p] = score | |
38 end | |
39 end | |
40 end | |
41 end | |
42 | |
43 # pick N highest values and use cutoff value as selection threshold | |
44 threshold = projhash.values.sort.last(count).first | |
45 | |
46 # select projects above threshold and pick N from them randomly | |
47 busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count) | |
48 | |
49 # return projects rather than just ids | |
12 busy.map { |pid| Project.find(pid) } | 50 busy.map { |pid| Project.find(pid) } |
13 end | 51 end |
14 | 52 |
15 def busy_institutions(events, count) | 53 def busy_institutions(events, count) |
16 authors = events.map do |e| | 54 authors = events.map do |e| |