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.



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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(Sufia::Noid.noidify(id))
  earliest = Sufia.config.analytic_start_date
  self.created = DateTime.parse(file.create_date)
  self.created = earliest > created ? earliest : created unless earliest.blank?
  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.



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

def created
  @created
end

#downloadsObject

Returns the value of attribute downloads.



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

def downloads
  @downloads
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#pageviewsObject

Returns the value of attribute pageviews.



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

def pageviews
  @pageviews
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#to_flotObject

Package data for visualization using JQuery Flot



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

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

#total_downloadsObject



19
20
21
# File 'app/models/file_usage.rb', line 19

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

#total_pageviewsObject



23
24
25
# File 'app/models/file_usage.rb', line 23

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