Skip to content

Commit

Permalink
Merge eb1350e into 8ca27b8
Browse files Browse the repository at this point in the history
  • Loading branch information
cldambrosio committed Oct 4, 2018
2 parents 8ca27b8 + eb1350e commit c3040c7
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 15 deletions.
6 changes: 1 addition & 5 deletions app/controllers/hyrax/generic_works_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module Hyrax
class GenericWorksController < ApplicationController
# Adds Hyrax behaviors to the controller.
include Hyrax::WorksControllerBehavior
include Hyrax::BreadcrumbsForWorks

class GenericWorksController < SharedBehaviorsController
self.curation_concern_type = GenericWork

include Hyku::IIIFManifest
Expand Down
9 changes: 1 addition & 8 deletions app/controllers/hyrax/images_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# Generated via
# `rails generate hyrax:work Image`

module Hyrax
class ImagesController < ApplicationController
# Adds Hyrax behaviors to the controller.
include Hyrax::WorksControllerBehavior
include Hyrax::BreadcrumbsForWorks

class ImagesController < SharedBehaviorsController
self.curation_concern_type = ::Image

# Use this line if you want to use a custom presenter
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/hyrax/shared_behaviors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Hyrax
class SharedBehaviorsController < ApplicationController
# Adds Hyrax behaviors to the controller.
include Hyrax::WorksControllerBehavior
include Hyrax::BreadcrumbsForWorks

private

def after_update_response
download_stats = WorkDownloadStat.find_by(work_uid: presenter.solr_document.id)
download_stats.update_attributes(title: presenter.solr_document.title.first) if download_stats
super
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Hyrax
Admin::StatsController.class_eval do
def show
super
@works_to_display ||= ::WorkDownloadStat.where(owner_id: current_user.id)
@total_downloads = @works_to_display.map(&:downloads).reduce(:+)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Hyrax
module DownloadsControllerDecorator
# `asset` is inherited from hydra-head/hydra-core/app/controllers/concerns/hydra/controller/download_behavior.rb
def send_content
super
WorkDownloadStat.new.log_download(asset)
end
end
end
18 changes: 18 additions & 0 deletions app/models/work_download_stat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class WorkDownloadStat < ApplicationRecord
def current_work_stats(work)
WorkDownloadStat.find_or_create_by(work_uid: work.parent.id) do |wds|
wds.title = work.parent.title.first
wds.owner_id = ::User.find_by(email: work.depositor).id
end
end

## method below works with a FileSet as argument
def log_download(record)
raise TypeError, 'argument is not a file_set' unless record.file_set?
stats = current_work_stats(record)
total = stats.downloads + 1
dates_arr = stats.date << Time.now.utc
stats.update_attributes(downloads: total,
date: dates_arr) ## rewrites the array in db, could be made more efficient?
end
end
8 changes: 8 additions & 0 deletions app/views/hyrax/admin/stats/_downloads.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h3>Total Downloads: <%= @total_downloads %></h3>
<br/>
<h3>Downloads per Work</h3>
<ul>
<% @works_to_display.each do |dwnld| %>
<li><%= dwnld.title %><span class="count">: <%= dwnld.downloads %></span></li>
<% end %>
</ul>
2 changes: 1 addition & 1 deletion app/views/hyrax/admin/stats/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<div role="tabpanel" class="tab-pane" id="downloads">
<div class="panel panel-default labels">
<div class="panel-body">
...
<%= render "hyrax/admin/stats/downloads" %>
</div>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class Application < Rails::Application
# authenticity token errors.
Hyrax::Admin::AppearancesController.form_class = AppearanceForm
Hyrax::FileSetsController.show_presenter = Hyku::FileSetPresenter
Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
require_dependency(c)
end
Hyrax::DownloadsController.include ::Hyrax::DownloadsControllerDecorator
end

config.before_initialize do
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20180612084347_create_work_download_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateWorkDownloadStats < ActiveRecord::Migration[5.1]
def change
create_table :work_download_stats do |t|
t.string :work_uid
t.string :title
t.integer :downloads, default: 0
t.integer :owner_id
t.datetime :date, array: true, default: []

t.timestamps null: false
end

add_index :work_download_stats, :owner_id
add_index :work_download_stats, :work_uid
end
end
14 changes: 13 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180524010247) do
ActiveRecord::Schema.define(version: 20180612084347) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -640,6 +640,18 @@
t.datetime "updated_at"
end

create_table "work_download_stats", force: :cascade do |t|
t.string "work_uid"
t.string "title"
t.integer "downloads", default: 0
t.integer "owner_id"
t.datetime "date", default: [], array: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["owner_id"], name: "index_work_download_stats_on_owner_id"
t.index ["work_uid"], name: "index_work_download_stats_on_work_uid"
end

create_table "work_view_stats", id: :serial, force: :cascade do |t|
t.datetime "date"
t.integer "work_views"
Expand Down
54 changes: 54 additions & 0 deletions spec/models/work_download_stat_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'rails_helper'

RSpec.describe WorkDownloadStat, type: :model do
let(:work) { create(:work_with_one_file) }
let(:fs) { FileSet.find(work.file_sets[0].id) }
let(:existing_record) do
WorkDownloadStat.create(work_uid: fs.parent.id,
title: fs.parent.title.first,
owner_id: ::User.find_by(email: fs.depositor).id)
end

describe '#log_download' do
it 'adds 1 to the downloads field when the Work is downloaded' do
r = existing_record
WorkDownloadStat.new.log_download(fs)
expect(r.reload.downloads).to eq 1
end
it 'raises an error if the argument is not a file_set' do
expect { WorkDownloadStat.new.log_download(work) }.to raise_error(TypeError)
end
it 'records the datetime when the Work was downloaded' do
r = existing_record
WorkDownloadStat.new.log_download(fs)
expect(r.reload.date[0]).to be_within(0.1).of(Time.now.utc)
end
end

describe '#current_work_stats' do
it 'finds a record when it exists' do
r = existing_record
expect(WorkDownloadStat.new.current_work_stats(fs)).to eq(r)
end
it 'creates a record when there is none yet for the specified Work' do
expect(WorkDownloadStat.new.current_work_stats(fs).id).to eq(WorkDownloadStat.last.id)
end

context "when creating a new record" do
it 'assigns a work_uid which is the same as the corresponding Work id' do
expect(WorkDownloadStat.new.current_work_stats(fs).work_uid).to eq(work.id)
end
it 'assigns an owner_id' do ## TO DO: who should be the owner? currently is the `depositor`
expect(WorkDownloadStat.new.current_work_stats(fs).owner_id).not_to be_nil
end
it 'has 0 downloads' do
expect(WorkDownloadStat.new.current_work_stats(fs).downloads).to eq 0
end
it 'does not create duplicates' do
r = existing_record
stats = WorkDownloadStat.new.current_work_stats(fs)
expect(stats.id).to eq(r.id)
end
end
end
end

0 comments on commit c3040c7

Please sign in to comment.