Sha256: ec81d0d65e91476f3510868c39a66d4fd81d0985add14089d487b7408bfb223d

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

# 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 = ::GenericWork.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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sufia-7.0.0.beta4 app/presenters/work_usage.rb
sufia-7.0.0.beta3 app/presenters/work_usage.rb
sufia-7.0.0.beta2 app/presenters/work_usage.rb
sufia-7.0.0.beta1 app/presenters/work_usage.rb