Sha256: 861ea23854fc8cfff082c87ca4a92577c6d98d850b7d7570465ada4de9bb6a57

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

class FileUsage
  attr_accessor :id, :created, :downloads, :pageviews

  def initialize(id)
    file = ::FileSet.find(id)
    user = User.find_by(email: file.depositor)
    user_id = user ? user.id : nil

    self.id = id
    self.created = date_for_analytics(file)
    self.downloads = FileDownloadStat.to_flots FileDownloadStat.statistics(file, created, user_id)
    self.pageviews = FileViewStat.to_flots FileViewStat.statistics(file, created, user_id)
  end

  # 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.
  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

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

  def total_downloads
    downloads.reduce(0) { |total, result| total + result[1].to_i }
  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 },
      { label: "Downloads",  data: downloads }
    ]
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sufia-7.1.0 app/presenters/file_usage.rb
sufia-7.0.0 app/presenters/file_usage.rb
sufia-7.0.0.rc2 app/presenters/file_usage.rb
sufia-7.0.0.rc1 app/presenters/file_usage.rb