-
Notifications
You must be signed in to change notification settings - Fork 78
/
work_usage.rb
50 lines (42 loc) · 1.69 KB
/
work_usage.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# class WorkUsage follows the model established by FileUsage
# Called by the stats controller, it finds cached work pageview data,
# and prepares it for visualization in /app/views/stats/work.html.erb
class WorkUsage
attr_accessor :id, :created, :path, :pageviews
def initialize(id)
work = CurationConcerns::WorkRelation.new.find(id)
user = User.find_by(email: work.depositor)
user_id = user ? user.id : nil
self.id = id
self.path = Rails.application.routes.url_helpers.curation_concerns_generic_work_path(id)
self.created = date_for_analytics(work)
self.pageviews = WorkViewStat.to_flots WorkViewStat.statistics(id, created, user_id)
end
def total_pageviews
pageviews.reduce(0) { |total, result| total + result[1].to_i }
end
# Package data for visualization using JQuery Flot
def to_flot
[
{ label: "Pageviews", data: pageviews }
]
end
private
# work.date_uploaded reflects the date the work was uploaded by the user
# and therefore (if available) the date that we want to use for the stats
# work.create_date reflects the date the work was added to Fedora. On data
# migrated from one repository to another the created_date can be later
# than the date the work was uploaded.
def date_for_analytics(work)
earliest = Sufia.config.analytic_start_date
date_uploaded = string_to_date work.date_uploaded
date_analytics = date_uploaded ? date_uploaded : work.create_date
return date_analytics if earliest.blank?
earliest > date_analytics ? earliest : date_analytics
end
def string_to_date(date_str)
return Time.zone.parse(date_str)
rescue ArgumentError, TypeError
return nil
end
end