Skip to content

Commit

Permalink
include aggregate metrics. #390
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 2, 2020
1 parent 63add18 commit fd66e25
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
7 changes: 6 additions & 1 deletion app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ def index

def show
options = {}
options[:meta] = { dois: doi_count(client_id: params[:id]) }
options[:meta] = {
dois: doi_count(client_id: params[:id]),
citations: citation_count(client_id: params[:id]),
views: view_count(client_id: params[:id]),
downloads: download_count(client_id: params[:id]),
}.compact
options[:include] = @include
options[:is_collection] = false
options[:params] = { current_ability: current_ability }
Expand Down
48 changes: 48 additions & 0 deletions app/controllers/concerns/countable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,54 @@ def doi_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id: nil
response.results.total.positive? ? facet_by_year(response.response.aggregations.created.buckets) : []
end

def view_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id: nil, state: nil)
if client_id
response = Doi.query(nil, client_id: client_id, page: { number: 1, size: 0 })
elsif provider_id
response = Doi.query(nil, provider_id: provider_id, page: { number: 1, size: 0 })
elsif consortium_id
response = Doi.query(nil, consortium_id: consortium_id, page: { number: 1, size: 0 })
elsif user_id
response = Doi.query(nil, user_id: user_id, state: state, page: { number: 1, size: 0 })
else
response = Doi.query(nil, page: { number: 1, size: 0 })
end

response.results.total.positive? ? facet_by_year(response.response.aggregations.views.buckets) : 0
end

def download_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id: nil, state: nil)
if client_id
response = Doi.query(nil, client_id: client_id, page: { number: 1, size: 0 })
elsif provider_id
response = Doi.query(nil, provider_id: provider_id, page: { number: 1, size: 0 })
elsif consortium_id
response = Doi.query(nil, consortium_id: consortium_id, page: { number: 1, size: 0 })
elsif user_id
response = Doi.query(nil, user_id: user_id, state: state, page: { number: 1, size: 0 })
else
response = Doi.query(nil, page: { number: 1, size: 0 })
end

response.results.total.positive? ? facet_by_year(response.response.aggregations.downloads.buckets) : 0
end

def citation_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id: nil, state: nil)
if client_id
response = Doi.query(nil, client_id: client_id, page: { number: 1, size: 0 })
elsif provider_id
response = Doi.query(nil, provider_id: provider_id, page: { number: 1, size: 0 })
elsif consortium_id
response = Doi.query(nil, consortium_id: consortium_id, page: { number: 1, size: 0 })
elsif user_id
response = Doi.query(nil, user_id: user_id, state: state, page: { number: 1, size: 0 })
else
response = Doi.query(nil, page: { number: 1, size: 0 })
end

response.results.total.positive? ? facet_by_year(response.response.aggregations.citations.buckets) : 0
end

# cumulative count clients by year
# count until the previous year if client has been deleted
# show all clients for admin
Expand Down
15 changes: 14 additions & 1 deletion app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,37 @@ def show
clients = client_count(provider_id: nil)
dois = doi_count(provider_id: nil)
resource_types = resource_type_count(provider_id: nil)
citations = citation_count(provider_id: nil)
views = view_count(provider_id: nil)
downloads = download_count(provider_id: nil)
elsif @provider.member_type == "consortium"
providers = provider_count(consortium_id: params[:id])
clients = client_count(consortium_id: params[:id])
dois = doi_count(consortium_id: params[:id])
resource_types = resource_type_count(consortium_id: params[:id])
citations = citation_count(consortium_id: params[:id])
views = view_count(consortium_id: params[:id])
downloads = download_count(consortium_id: params[:id])
else
providers = nil
clients = client_count(provider_id: params[:id])
dois = doi_count(provider_id: params[:id])
resource_types = resource_type_count(provider_id: params[:id])
citations = citation_count(provider_id: params[:id])
views = view_count(provider_id: params[:id])
downloads = download_count(provider_id: params[:id])
end

options = {}
options[:meta] = {
providers: providers,
clients: clients,
dois: dois,
"resourceTypes" => resource_types }.compact
"resourceTypes" => resource_types,
citations: citations,
views: views,
downloads: downloads,
}.compact
options[:include] = @include
options[:is_collection] = false
options[:params] = { current_ability: current_ability }
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ def show
options = {}
options[:meta] = {
dois: doi_count(client_id: params[:id]),
"resourceTypes" => resource_type_count(client_id: params[:id]) }.compact
"resourceTypes" => resource_type_count(client_id: params[:id]),
citations: citation_count(client_id: params[:id]),
views: view_count(client_id: params[:id]),
downloads: download_count(client_id: params[:id]),
}.compact
options[:include] = @include
options[:is_collection] = false
options[:params] = { current_ability: current_ability }
Expand Down
3 changes: 3 additions & 0 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,9 @@ def self.query_aggregations
sources: { terms: { field: 'source', size: 15, min_doc_count: 1 } },
subjects: { terms: { field: 'subjects.subject', size: 15, min_doc_count: 1 } },
certificates: { terms: { field: 'client.certificate', size: 15, min_doc_count: 1 } },
views: { value_count: { field: "views_count" } },
downloads: { value_count: { field: "download_count" } },
citations: { value_count: { field: "citation_count" } },
}
end

Expand Down

0 comments on commit fd66e25

Please sign in to comment.