chris@1011
|
1
|
chris@1011
|
2 module ActivitiesHelper
|
chris@1011
|
3
|
chris@1205
|
4 def date_of_event(e)
|
chris@1205
|
5 if e.respond_to? :updated_at
|
chris@1205
|
6 e.updated_at
|
chris@1205
|
7 elsif e.respond_to? :updated_on
|
chris@1205
|
8 e.updated_on
|
chris@1205
|
9 elsif e.respond_to? :created_on
|
chris@1205
|
10 e.created_on
|
chris@1205
|
11 elsif e.respond_to? :committed_on
|
chris@1205
|
12 e.committed_on
|
chris@1205
|
13 else
|
chris@1205
|
14 nil
|
chris@1205
|
15 end
|
chris@1205
|
16 end
|
chris@1205
|
17
|
chris@1011
|
18 def busy_projects(events, count)
|
chris@1186
|
19
|
chris@1186
|
20 # Score each project for which there are any events, by giving
|
chris@1186
|
21 # each event a score based on how long ago it was (the more recent
|
chris@1186
|
22 # the better).
|
chris@1186
|
23
|
chris@1186
|
24 projhash = Hash.new
|
chris@1186
|
25
|
chris@1186
|
26 events.each do |e|
|
chris@1186
|
27 if e.respond_to?(:project)
|
chris@1186
|
28 p = e.project
|
chris@1205
|
29 d = date_of_event e
|
chris@1205
|
30 if !d.nil?
|
chris@1205
|
31 dd = Date.parse d.to_s
|
chris@1205
|
32 age = Date.today - dd
|
chris@1205
|
33 score = (age < 14 ? 15-age : 1)
|
chris@1205
|
34 if projhash.key? p
|
chris@1205
|
35 projhash[p] += score
|
chris@1205
|
36 else
|
chris@1205
|
37 projhash[p] = score
|
chris@1205
|
38 end
|
chris@1186
|
39 end
|
chris@1186
|
40 end
|
chris@1186
|
41 end
|
chris@1186
|
42
|
chris@1186
|
43 # pick N highest values and use cutoff value as selection threshold
|
chris@1186
|
44 threshold = projhash.values.sort.last(count).first
|
chris@1186
|
45
|
chris@1186
|
46 # select projects above threshold and pick N from them randomly
|
chris@1186
|
47 busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count)
|
chris@1186
|
48
|
chris@1186
|
49 # return projects rather than just ids
|
chris@1011
|
50 busy.map { |pid| Project.find(pid) }
|
chris@1011
|
51 end
|
chris@1011
|
52
|
chris@1011
|
53 def busy_institutions(events, count)
|
Chris@1013
|
54 authors = events.map do |e|
|
Chris@1013
|
55 e.event_author unless !e.respond_to?(:event_author)
|
Chris@1013
|
56 end.compact
|
Chris@1013
|
57 institutions = authors.map do |a|
|
Chris@1013
|
58 if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil?
|
Chris@1013
|
59 a.ssamr_user_detail.institution_name
|
Chris@1013
|
60 end
|
Chris@1013
|
61 end
|
chris@1011
|
62 insthash = institutions.compact.sort.group_by { |i| i }
|
chris@1011
|
63 insthash = insthash.merge(insthash) { |k,v| v.length }
|
chris@1011
|
64 threshold = insthash.values.sort.last(count).first
|
chris@1011
|
65 insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
|
chris@1011
|
66 end
|
chris@1011
|
67
|
chris@1011
|
68 end
|