-
Notifications
You must be signed in to change notification settings - Fork 49
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
Log downloads #1533
Log downloads #1533
Conversation
change db table to prep for report filtering remove admin_stats_presenter_decorator correct title field
@@ -0,0 +1,30 @@ | |||
module Hyrax | |||
DownloadsController.class_eval do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unusual way to do this. I like to do this same sort of thing by making a module and include it in the DownloadsController
in config/initializers/hyku.rb
(or something) add:
Hyrax::DownloadsController.include DownloadsControllerDecorator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I'll try this 👍
update_download_stats(asset) | ||
end | ||
|
||
def download_stats(current_file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because these methods have nothing to do with HTTP interaction they could move out of the controller to a service class, so that the only addition on line 6 needs to be:
DownloadStats.update(asset)
|
||
def download_stats(current_file) | ||
if FileDownloadStat.find_by(file_id: current_file.id) | ||
FileDownloadStat.find_by(file_id: current_file.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One common practice to avoid long if/else
blocks is do to:
stat = FileDownloadStat.find_by(file_id: current_file.id)
return stat if stat
... (the stuff that was in the else block)...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
Admin::StatsController.class_eval do | ||
def show | ||
super | ||
@works_to_display ||= ::FileDownloadStat.all.select do |e| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will load all database records into memory and iterate over them trying to find records that match this condition. But if you do ::FileDownloadStat.where(user_id: current_user.id)
then the database will do all the work and you'll only load into memory the records you care about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 thanks again!!
@works_to_display ||= ::FileDownloadStat.all.select do |e| | ||
e[:user_id] == current_user.id | ||
end | ||
@total_downloads ||= @works_to_display.map(&:downloads).reduce(:+) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you using ||=
in this method? Is there somewhere else that could have already set @total_downloads
? If so, can you add a comment about where that might occur? Otherwise just use simple assignment (=
)
<h2>Downloads per Work</h2> | ||
<ul> | ||
<% @works_to_display.each do |dwnld| %> | ||
<li><%= dwnld.title %></li><span class="count">(<%= dwnld.downloads %>)</span> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This markup seems odd. I'm not sure ul
can have any children besides li
. Did you want to tuck the span
within the li
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes span
within li
is nicer thanks. I'll see how else we may want this styled.
class ChangeFileDownloadStatsDateToArray < ActiveRecord::Migration[5.1] | ||
def change | ||
remove_column :file_download_stats, :date, :datetime | ||
add_column :file_download_stats, :date, :datetime, array: true, default: [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change may end up breaking this code: https://github.com/samvera/hyrax/blob/be223ff41debc86bac9a5e74ac4151102513e9ac/app/models/hyrax/statistic.rb#L53
Are you sure you don't want to just make a new database table rather than trying to reuse one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch thank you! We think we won't use GA for our fork but better make a change in line with this version. I'll follow your suggestion.
I'll work on it again tomorrow. @jcoyne thanks a ton for helping me improve this 👍 |
568dbcb
to
d93ff5a
Compare
Update `title` of WorkDownloadStat if Work is edited Minor change in display
d93ff5a
to
b28522e
Compare
@cldambrosio looks like this needs to have master merged in to it and that will likely help with these test failures. |
Thank you @orangewolf I will do that soon! |
Logging and displaying download statistics independently from Google Analytics
Refs Hyrax#2539
Changes proposed in this pull request:
title
and change thedate
field to an array; to enable filtering the data by Work and by Date.@samvera/hyrax-code-reviewers