Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HYC-1934 - Send download events to database #1108

Merged
merged 12 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions app/controllers/concerns/hyc/download_analytics_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def track_download
return
end
if Hyrax::Analytics.config.auth_token.present? && !request.url.match('thumbnail')
Rails.logger.debug("Recording download event for #{params[:id]}")
Rails.logger.debug("Recording download event for #{fileset_id}")
medium = request.referrer.present? ? 'referral' : 'direct'

client_ip = request.remote_ip
Expand All @@ -34,7 +34,7 @@ def track_download
apiv: '1',
e_a: 'DownloadIR',
e_c: @admin_set_name,
e_n: params[:id] || 'Unknown',
e_n: fileset_id,
e_v: medium,
_id: client_id,
cip: client_ip,
Expand All @@ -50,18 +50,53 @@ def track_download
if response.code >= 300
Rails.logger.error("DownloadAnalyticsBehavior received an error response #{response.code} for matomo query: #{uri}")
end
# Send download events to db
create_download_stat
Rails.logger.debug("DownloadAnalyticsBehavior request completed #{response.code}")
response.code
end
end

def create_download_stat
record_id_value = record_id
admin_set_id_value = admin_set_id
work_type = fetch_record.dig(0, 'has_model_ssim', 0)
date = Date.today

Rails.logger.debug('Creating or updating hyc-download-stat database entry with the following attributes:')
Rails.logger.debug("fileset_id: #{fileset_id}, work_id: #{record_id_value}, admin_set_id: #{admin_set_id_value}, work_type: #{work_type}, date: #{date.beginning_of_month}")

stat = HycDownloadStat.find_or_initialize_by(
fileset_id: fileset_id,
work_id: record_id_value,
admin_set_id: admin_set_id_value,
work_type: work_type,
date: date.beginning_of_month
)
stat.download_count += 1
if stat.save
Rails.logger.debug("Database entry for fileset_id: #{fileset_id} successfully saved with download count: #{stat.download_count}.")
else
Rails.logger.error("Failed to update database entry for fileset_id: #{fileset_id}." \
"Errors: #{stat.errors.full_messages}")
end
end

def bot_request?(user_agent)
browser = Browser.new(user_agent)
browser.bot?
end

def fetch_record
@record ||= ActiveFedora::SolrService.get("file_set_ids_ssim:#{params[:id]}", rows: 1)['response']['docs']
@record ||= ActiveFedora::SolrService.get("file_set_ids_ssim:#{fileset_id}", rows: 1)['response']['docs']
end

def fetch_admin_set
@admin_set ||= ActiveFedora::SolrService.get("title_tesim:#{@admin_set_name}", rows: 1)['response']['docs']
end

def admin_set_id
@admin_set_id ||= fetch_admin_set.dig(0, 'id')
end

def record_id
Expand All @@ -72,6 +107,10 @@ def record_id
end
end

def fileset_id
@fileset_id ||= params[:id] || 'Unknown'
end

def record_title
@record_title ||= if !fetch_record.blank? && fetch_record[0]['title_tesim']
fetch_record[0]['title_tesim'].first
Expand Down
12 changes: 12 additions & 0 deletions app/models/hyc_download_stat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
class HycDownloadStat < ApplicationRecord
scope :with_fileset_id_and_date, ->(fileset_id, start_date, end_date) { where(fileset_id: fileset_id, date: start_date..end_date) }
scope :with_work_id_and_date, ->(work_id, start_date, end_date) { where(work_id: work_id, date: start_date..end_date) }
scope :with_admin_set_id, ->(admin_set_id) { where(admin_set_id: admin_set_id) }
scope :with_work_type, ->(work_type) { where(work_type: work_type) }

# Additional scopes for flexibility
scope :with_fileset_id, ->(fileset_id) { where(fileset_id: fileset_id) }
scope :with_work_id, ->(work_id) { where(work_id: work_id) }
scope :within_date_range, ->(start_date, end_date) { where(date: start_date..end_date) }
end
20 changes: 20 additions & 0 deletions db/migrate/20240714234200_create_hyc_download_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true
class CreateHycDownloadStats < ActiveRecord::Migration[6.1]
def change
create_table :hyc_download_stats do |t|
t.string :fileset_id, null: false
t.string :work_id, null: false
t.string :admin_set_id, null: false
t.string :work_type, null: false
t.date :date, null: false
t.integer :download_count, default: 0, null: false

t.timestamps
end

add_index :hyc_download_stats, [:fileset_id, :date]
add_index :hyc_download_stats, [:work_id, :date]
add_index :hyc_download_stats, :admin_set_id
add_index :hyc_download_stats, :work_type
end
end
17 changes: 16 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: 2023_06_08_153601) do
ActiveRecord::Schema.define(version: 2024_07_14_234200) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -249,6 +249,21 @@
t.index ["user_id"], name: "index_file_view_stats_on_user_id"
end

create_table "hyc_download_stats", force: :cascade do |t|
t.string "fileset_id", null: false
t.string "work_id", null: false
t.string "admin_set_id", null: false
t.string "work_type", null: false
t.date "date", null: false
t.integer "download_count", default: 0, null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["admin_set_id"], name: "index_hyc_download_stats_on_admin_set_id"
t.index ["fileset_id", "date"], name: "index_hyc_download_stats_on_fileset_id_and_date"
t.index ["work_id", "date"], name: "index_hyc_download_stats_on_work_id_and_date"
t.index ["work_type"], name: "index_hyc_download_stats_on_work_type"
end

create_table "hyrax_collection_types", id: :serial, force: :cascade do |t|
t.string "title"
t.text "description"
Expand Down
Loading
Loading