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

Log downloads #1533

Closed
wants to merge 8 commits into from
Closed

Conversation

cldambrosio
Copy link
Contributor

@cldambrosio cldambrosio commented Jun 11, 2018

Logging and displaying download statistics independently from Google Analytics

Refs Hyrax#2539

Changes proposed in this pull request:

  • In the FileDownloadStats database table, add a title and change the date field to an array; to enable filtering the data by Work and by Date.
  • Create and update records when a file is downloaded
  • Render these stats in the dashboard
    stats

@samvera/hyrax-code-reviewers

change db table to prep for report filtering

remove admin_stats_presenter_decorator

correct title field
jcoyne
jcoyne previously requested changes Jun 11, 2018
@@ -0,0 +1,30 @@
module Hyrax
DownloadsController.class_eval do
Copy link
Member

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

Copy link
Contributor Author

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)
Copy link
Member

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)
Copy link
Member

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

Copy link
Contributor Author

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|
Copy link
Member

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.

Copy link
Contributor Author

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(:+)
Copy link
Member

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>
Copy link
Member

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?

Copy link
Contributor Author

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: []
Copy link
Member

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?

Copy link
Contributor Author

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.

@cldambrosio
Copy link
Contributor Author

I'll work on it again tomorrow. @jcoyne thanks a ton for helping me improve this 👍

@cldambrosio cldambrosio force-pushed the log-downloads branch 3 times, most recently from 568dbcb to d93ff5a Compare June 18, 2018 15:01
Update `title` of WorkDownloadStat if Work is edited

Minor change in display
@cldambrosio
Copy link
Contributor Author

Style changed to:

downloads_report

@cldambrosio cldambrosio changed the title [WIP] log downloads Log downloads Jun 25, 2018
@orangewolf
Copy link
Member

Note the 4 failures here are the same 4 from @cjcolvar experiments in #1523 and #1522. They appear to be a problem with forks and not a real code change issue.

@orangewolf
Copy link
Member

@cldambrosio looks like this needs to have master merged in to it and that will likely help with these test failures.

@jcoyne jcoyne dismissed their stale review October 10, 2018 20:25

this review is stale

@cldambrosio
Copy link
Contributor Author

Thank you @orangewolf I will do that soon!

@mankind mankind deleted the log-downloads branch January 18, 2019 09:28
@orangewolf orangewolf closed this Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants