Class: FileUsage

Inherits:
Object
  • Object
show all
Defined in:
app/models/file_usage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ FileUsage

Returns a new instance of FileUsage.



4
5
6
7
8
9
10
11
12
13
14
# File 'app/models/file_usage.rb', line 4

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

#createdObject

Returns the value of attribute created.



2
3
4
# File 'app/models/file_usage.rb', line 2

def created
  @created
end

#downloadsObject

Returns the value of attribute downloads.



2
3
4
# File 'app/models/file_usage.rb', line 2

def downloads
  @downloads
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'app/models/file_usage.rb', line 2

def id
  @id
end

#pageviewsObject

Returns the value of attribute pageviews.



2
3
4
# File 'app/models/file_usage.rb', line 2

def pageviews
  @pageviews
end

#pathObject

Returns the value of attribute path.



2
3
4
# File 'app/models/file_usage.rb', line 2

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.



21
22
23
24
25
26
27
# File 'app/models/file_usage.rb', line 21

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



29
30
31
32
33
# File 'app/models/file_usage.rb', line 29

def string_to_date(date_str)
  return DateTime.parse(date_str)
rescue ArgumentError, TypeError
  return nil
end

#to_flotObject

Package data for visualization using JQuery Flot



44
45
46
47
48
49
# File 'app/models/file_usage.rb', line 44

def to_flot
  [
    { label: "Pageviews",  data: pageviews },
    { label: "Downloads",  data: downloads }
  ]
end

#total_downloadsObject



35
36
37
# File 'app/models/file_usage.rb', line 35

def total_downloads
  downloads.reduce(0) { |total, result| total + result[1].to_i }
end

#total_pageviewsObject



39
40
41
# File 'app/models/file_usage.rb', line 39

def total_pageviews
  pageviews.reduce(0) { |total, result| total + result[1].to_i }
end