Class: FileUsage
- Inherits:
-
Object
- Object
- FileUsage
- Defined in:
- app/models/file_usage.rb
Instance Attribute Summary collapse
-
#created ⇒ Object
Returns the value of attribute created.
-
#downloads ⇒ Object
Returns the value of attribute downloads.
-
#id ⇒ Object
Returns the value of attribute id.
-
#pageviews ⇒ Object
Returns the value of attribute pageviews.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#date_for_analytics(file) ⇒ Object
file.date_uploaded reflects the date the file was uploaded by the user and therefore (if available) the date that we want to use for the stats file.create_date reflects the date the file was added to Fedora.
-
#initialize(id) ⇒ FileUsage
constructor
A new instance of FileUsage.
- #string_to_date(date_str) ⇒ Object
-
#to_flot ⇒ Object
Package data for visualization using JQuery Flot.
- #total_downloads ⇒ Object
- #total_pageviews ⇒ Object
Constructor Details
#initialize(id) ⇒ FileUsage
Returns a new instance of FileUsage.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'app/models/file_usage.rb', line 5 def initialize id file = ::GenericFile.find(id) user = User.where(email: file.depositor).first user_id = user ? user.id : nil self.id = id self.path = Sufia::Engine.routes.url_helpers.generic_file_path(id) self.created = date_for_analytics(file) self.downloads = FileDownloadStat.to_flots FileDownloadStat.statistics(id, created, user_id) self.pageviews = FileViewStat.to_flots FileViewStat.statistics(id, created, user_id) end |
Instance Attribute Details
#created ⇒ Object
Returns the value of attribute created.
3 4 5 |
# File 'app/models/file_usage.rb', line 3 def created @created end |
#downloads ⇒ Object
Returns the value of attribute downloads.
3 4 5 |
# File 'app/models/file_usage.rb', line 3 def downloads @downloads end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'app/models/file_usage.rb', line 3 def id @id end |
#pageviews ⇒ Object
Returns the value of attribute pageviews.
3 4 5 |
# File 'app/models/file_usage.rb', line 3 def pageviews @pageviews end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'app/models/file_usage.rb', line 3 def path @path end |
Instance Method Details
#date_for_analytics(file) ⇒ Object
file.date_uploaded reflects the date the file was uploaded by the user and therefore (if available) the date that we want to use for the stats file.create_date reflects the date the file was added to Fedora. On data migrated from one repository to another the created_date can be later than the date the file was uploaded.
22 23 24 25 26 27 28 |
# File 'app/models/file_usage.rb', line 22 def date_for_analytics(file) earliest = Sufia.config.analytic_start_date date_uploaded = string_to_date file.date_uploaded date_analytics = date_uploaded ? date_uploaded : file.create_date return date_analytics if earliest.blank? earliest > date_analytics ? earliest : date_analytics end |
#string_to_date(date_str) ⇒ Object
30 31 32 33 34 |
# File 'app/models/file_usage.rb', line 30 def string_to_date(date_str) return DateTime.parse(date_str) rescue ArgumentError, TypeError return nil end |
#to_flot ⇒ Object
Package data for visualization using JQuery Flot
45 46 47 48 49 50 |
# File 'app/models/file_usage.rb', line 45 def to_flot [ { label: "Pageviews", data: pageviews }, { label: "Downloads", data: downloads } ] end |
#total_downloads ⇒ Object
36 37 38 |
# File 'app/models/file_usage.rb', line 36 def total_downloads self.downloads.reduce(0) { |total, result| total + result[1].to_i } end |
#total_pageviews ⇒ Object
40 41 42 |
# File 'app/models/file_usage.rb', line 40 def total_pageviews self.pageviews.reduce(0) { |total, result| total + result[1].to_i } end |