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

Add case stats #691

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions app/assets/stylesheets/case_states.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.stat-label
font-style: italic
42 changes: 42 additions & 0 deletions app/controllers/cases/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Cases
# The stats for a {Case} include its slug, what library it is in, etc.
class StatsController < ApplicationController
before_action :authenticate_reader!

layout 'admin'

# @param [GET] /cases/case-slug/stats
def show
set_case
set_libraries
set_editorships
set_group_and_deployment
end

private

def set_case
@case = Case.friendly.find(params[:case_slug]).decorate
@case.licensor current_reader
authorize @case
end

def set_libraries
@libraries = LibraryPolicy::AdminScope.new(current_reader, Library)
.resolve
end

def set_editorships
@editorships = @case.editorships
end

def set_group_and_deployment
@enrollment = current_user.enrollment_for_case @case
@group = @enrollment.try(:active_group) || GlobalGroup.new
@deployment = @group.deployment_for_case @case
end

end
end
16 changes: 16 additions & 0 deletions app/helpers/cases_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,20 @@ def translators_string(c)
def cases_as_json(cases)
array = cases.map { |c| raw render partial: 'cases/case', formats: [:json], locals: { c: c } }
end

def locales_to_sentence(locales = [])
if locales.present?
locales.map{|locale|
tr = Translation.language_name(locale)
if tr
"#{tr} (#{locale})"
else
""
end
}&.compact&.to_sentence
else
""
end

end
end
8 changes: 8 additions & 0 deletions app/javascript/catalog/CatalogToolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,20 @@ class SearchField extends React.Component<
onSubmit={this.handleSubmit}
ref={ref}
>

<InputGroup
inputRef={el => (this.input = el)}
className="pt-round"
leftIcon="search"
aria-label={this.props.intl.formatMessage({
id: 'search.searchCases',
})}
rightElement={
<button
type="button"
aria-label={this.props.intl.formatMessage({
id: 'search.submitSearch',
})}
className="pt-button pt-minimal pt-icon-arrow-right"
onClick={this.handleSubmit}
/>
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/catalog/search_results/SearchForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ function SearchForm ({ history, intl, params }: Props) {
<FormGroup>
<InputGroup
className="pt-fill"
aria-label={intl.formatMessage({
id: 'search.fullTextSearch',
})}
role="search"
leftIcon="search"
placeholder={intl.formatMessage({
id: 'search.fullTextSearch',
Expand Down
7 changes: 7 additions & 0 deletions app/javascript/reading_list/CaseChooser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ function CaseChooser ({ cases, intl, onSelect }: Props) {
className="pt-round"
leftIcon="search"
name="q"
role="search"
aria-label={intl.formatMessage({
id: 'readingListItems.new.searchForACaseToAdd',
})}
rightElement={
<button
className="pt-button pt-minimal pt-icon-arrow-right"
type="submit"
aria-label={intl.formatMessage({
id: 'search.submitSearch',
})}
/>
}
placeholder={intl.formatMessage({
Expand Down
66 changes: 66 additions & 0 deletions app/views/cases/stats/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
- content_for :title do
= I18n.t('cases.stats.show.case_stats')
= @case.short_title


.window.admin
.admin-card.pt-card.pt-elevation-2
= link_to case_path @case do
.case-cover-card-top{ style: "background-image: url(#{@case.cover_url})" }
%span.admin__table__slug= @case.short_title

%div
%h1.admin__title= t '.case_stats'
%div
%ul
%li
%span.stat-label= t '.created'
= @case.created_at.strftime("%B %d, %Y")
%li
%span.stat-label= t '.published'
= @case.published_at.strftime("%B %d, %Y")
%li
%span.stat-label= t '.last_updated'
= @case.updated_at.strftime("%B %d, %Y")

%table.admin__table.pt-html-table
%thead
%tr
%th(scope="col")='Activity'
%th(scope="col")='Last Seven Days'
%th(scope="col")='Last Six Months'
%th(scope="col")='Last 12 Months'
%th(scope="col")='All Time'
%tbody
%tr
%td= t '.deployments'
%td= @case.deployments.where(created_at: 7.days.ago..).size
%td= @case.deployments.where(created_at: 6.months.ago..).size
%td= @case.deployments.where(created_at: 12.months.ago..).size
%td= @case.deployments.size
%tr
%td= t '.visits'
%td= @case.events.interesting.where_properties(name: 'visit_element').where(time: 7.days.ago..).group(:user_id).count.keys.count
%td= @case.events.interesting.where_properties(name: 'visit_element').where(time: 6.months.ago..).group(:user_id).count.keys.count
%td= @case.events.interesting.where_properties(name: 'visit_element').where(time: 12.months.ago..).group(:user_id).count.keys.count
%td= @case.events.interesting.where_properties(name: 'visit_element').group(:user_id).count.keys.count
%tr
%td= t '.locales'
%td= locales_to_sentence(Reader.where(id: @case.events.interesting.where_properties(name: 'visit_element').where(time: 7.days.ago..).group(:user_id).count.keys).pluck(:locale))
%td= locales_to_sentence(Reader.where(id: @case.events.interesting.where_properties(name: 'visit_element').where(time: 6.months.ago..).group(:user_id).count.keys).pluck(:locale))
%td= locales_to_sentence(Reader.where(id: @case.events.interesting.where_properties(name: 'visit_element').where(time: 12.months.ago..).group(:user_id).count.keys).pluck(:locale))
%td= locales_to_sentence(Reader.where(id: @case.events.interesting.where_properties(name: 'visit_element').group(:user_id).count.keys).pluck(:locale))
- @case.podcasts.each do |podcast|
- podcast_listen_events = Ahoy::Event.interesting.where_properties(podcast_id: podcast.id)
%tr
%td= t ('.podcast_listens'), podcast_title: podcast.title
%td= podcast_listen_events.where(time: 7.days.ago..).size
%td= podcast_listen_events.where(time: 6.months.ago..).size
%td= podcast_listen_events.where(time: 12.months.ago..).size
%td= podcast_listen_events.size

.pt-callout.pt-icon-info-sign.form__callout= t '.disclaimer'

%table

6 changes: 4 additions & 2 deletions app/views/deployments/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
.pt-input-group.pt-round
%span.pt-icon.pt-icon-search
%input.pt-input{ type: 'text', name: 'q',
placeholder: I18n.t('search.search_cases') }
role: 'search',
placeholder: I18n.t('search.search_cases'),
'aria-label': I18n.t('search.search_cases')}
%span.pt-input-action
%button.pt-button.pt-minimal.pt-icon-arrow-right(type="submit")
%button.pt-button.pt-minimal.pt-icon-arrow-right{type:"submit",'aria-label': I18n.t('search.submit_search')}

%main.md-detail
- if deployments.empty?
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<%= form_with url: '/catalog/search', local: true, method: :get do %>
<div class="pt-input-group pt-round">
<span class="pt-icon pt-icon-search"></span>
<input class="pt-input" name="q" placeholder="<%= I18n.t('search.search_cases') %>" type="text">
<input class="pt-input" name="q" role="search" aria-label="Search Cases" placeholder="<%= I18n.t('search.search_cases') %>" type="text">
<span class="pt-input-action">
<button type="submit" class="pt-button pt-minimal pt-icon-arrow-right"></button>
<button type="submit" aria-label="Submit search" class="pt-button pt-minimal pt-icon-arrow-right"></button>
</span>
</input>
</div>
Expand Down
9 changes: 8 additions & 1 deletion app/views/my_cases/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
aria: { label: I18n.t('helpers.help') },
target: '_blank', rel: 'noopener noreferrer' do
-# Empty

:markdown
#{t '.put_your_own_cases_on_gala'}
.pt-callout.pt-icon-hand-right
Expand Down Expand Up @@ -51,6 +50,7 @@
%td= t 'activerecord.attributes.case.locale'
%td= t 'activerecord.attributes.case.library'
%td= t 'activerecord.attributes.case.published'
%td= t 'my_cases.index.case_stats'
%td
%td
%td
Expand Down Expand Up @@ -90,6 +90,11 @@
- else

%td
= link_to '', case_stats_path(kase),
aria: { label: t('.case_stats') },
class: 'pt-button pt-minimal pt-icon-series-search'

%td
= link_to '', edit_case_path(kase),
aria: { label: t('.edit_case') },
Expand All @@ -104,6 +109,8 @@
= link_to '', case_confirm_deletion_path(kase),
class: 'pt-button pt-minimal pt-intent-danger pt-icon-trash',
aria: { label: t('.delete_case') }



- else
.pt-non-ideal-state
Expand Down
18 changes: 17 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ en:
locations: Site locations
new:
create_a_case: Create a Case
stats:
show:
case_stats: Case Stats
created: Created
published: "Published:"
last_updated: "Last Updated:"
deployments: "Deployments"
visits: Unique Visits
locales: Locales
podcast_listens: Podcast Listens - %{podcast_title}
disclaimer: >
Totals count only users that were signed-in to Gala when viewing the case.
'Unique Visits' counts readers who viewed a page beyond the overview page of your case.
Readers' browser settings may impact the accuracy of these counts.
settings:
edit:
change_library: Change library
Expand Down Expand Up @@ -830,7 +844,8 @@ en:
Invite Someone to Manage this Library
my_cases:
index:
confirm_delete: >
case_stats: Case Stats
confirm_delete:
Are you sure you wish to delete this case? This action cannot be undone.
create_a_case: Create a Case
delete_case: Delete case.
Expand Down Expand Up @@ -1023,6 +1038,7 @@ en:
results: Search results
search: Search
search_cases: Search Cases...
submit_search: Submit search
try_again: Try searching for something else.
spotlights:
add_citation: >
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@

resource :settings, module: 'cases', only: %i[edit update]

resource :stats, module: 'cases', only: %i[show]

resources :taggings, only: %i[create destroy], param: :tag_name

resources :translations, only: %i[new create show], param: :case_locale
Expand Down