Skip to content

Commit

Permalink
aggregations for views, downloads and citations. #390
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 2, 2020
1 parent fd66e25 commit ee4a10f
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 36 deletions.
14 changes: 7 additions & 7 deletions app/controllers/concerns/countable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def doi_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id: nil
else
response = Doi.query(nil, page: { number: 1, size: 0 })
end

response.results.total.positive? ? facet_by_year(response.response.aggregations.created.buckets) : []
end

Expand All @@ -30,8 +30,8 @@ def view_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id: ni
else
response = Doi.query(nil, page: { number: 1, size: 0 })
end
response.results.total.positive? ? facet_by_year(response.response.aggregations.views.buckets) : 0

response.results.total.positive? ? metric_facet_by_year(response.response.aggregations.views.buckets) : []
end

def download_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id: nil, state: nil)
Expand All @@ -46,8 +46,8 @@ def download_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id
else
response = Doi.query(nil, page: { number: 1, size: 0 })
end
response.results.total.positive? ? facet_by_year(response.response.aggregations.downloads.buckets) : 0

response.results.total.positive? ? metric_facet_by_year(response.response.aggregations.downloads.buckets) : []
end

def citation_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id: nil, state: nil)
Expand All @@ -62,8 +62,8 @@ def citation_count(client_id: nil, provider_id: nil, consortium_id: nil, user_id
else
response = Doi.query(nil, page: { number: 1, size: 0 })
end
response.results.total.positive? ? facet_by_year(response.response.aggregations.citations.buckets) : 0

response.results.total.positive? ? metric_facet_by_year(response.response.aggregations.citations.buckets) : []
end

# cumulative count clients by year
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def facet_by_year(arr)
end
end

def metric_facet_by_year(arr)
arr.map do |hsh|
{ "id" => hsh["key_as_string"][0..3],
"title" => hsh["key_as_string"][0..3],
"count" => hsh.dig("metric_count", "value").to_i }
end
end

def facet_annual(arr)
arr.map do |hsh|
{ "id" => hsh["key"][0..3],
Expand Down
57 changes: 32 additions & 25 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,43 +170,50 @@ def index
certificates = nil

bm = Benchmark.ms {
states = total > 0 ? facet_by_key(response.aggregations.states.buckets) : nil
resource_types = total > 0 ? facet_by_resource_type(response.aggregations.resource_types.buckets) : nil
years = total > 0 ? facet_by_year(response.aggregations.years.buckets) : nil
created = total > 0 ? facet_by_year(response.aggregations.created.buckets) : nil
registered = total > 0 ? facet_by_year(response.aggregations.registered.buckets) : nil
providers = total > 0 ? facet_by_provider(response.aggregations.providers.buckets) : nil
clients = total > 0 ? facet_by_client(response.aggregations.clients.buckets) : nil
prefixes = total > 0 ? facet_by_key(response.aggregations.prefixes.buckets) : nil
schema_versions = total > 0 ? facet_by_schema(response.aggregations.schema_versions.buckets) : nil
states = total.positive? ? facet_by_key(response.aggregations.states.buckets) : nil
resource_types = total.positive? ? facet_by_resource_type(response.aggregations.resource_types.buckets) : nil
years = total.positive? ? facet_by_year(response.aggregations.years.buckets) : nil
created = total.positive? ? facet_by_year(response.aggregations.created.buckets) : nil
registered = total.positive? ? facet_by_year(response.aggregations.registered.buckets) : nil
providers = total.positive? ? facet_by_provider(response.aggregations.providers.buckets) : nil
clients = total.positive? ? facet_by_client(response.aggregations.clients.buckets) : nil
prefixes = total.positive? ? facet_by_key(response.aggregations.prefixes.buckets) : nil
schema_versions = total.positive? ? facet_by_schema(response.aggregations.schema_versions.buckets) : nil
}
logger.warn method: "GET", path: "/dois", message: "AggregationsBasic /dois", duration: bm

bm = Benchmark.ms {
affiliations = total > 0 ? facet_by_affiliation(response.aggregations.affiliations.buckets) : nil
sources = total > 0 ? facet_by_key(response.aggregations.sources.buckets) : nil
subjects = total > 0 ? facet_by_key(response.aggregations.subjects.buckets) : nil
certificates = total > 0 ? facet_by_key(response.aggregations.certificates.buckets) : nil
affiliations = total.positive? ? facet_by_affiliation(response.aggregations.affiliations.buckets) : nil
sources = total.positive? ? facet_by_key(response.aggregations.sources.buckets) : nil
subjects = total.positive? ? facet_by_key(response.aggregations.subjects.buckets) : nil
certificates = total.positive? ? facet_by_key(response.aggregations.certificates.buckets) : nil
}
logger.warn method: "GET", path: "/dois", message: "AggregationsExtended /dois", duration: bm

bm = Benchmark.ms {
link_checks_status = total > 0 ? facet_by_cumulative_year(response.aggregations.link_checks_status.buckets) : nil
links_with_schema_org = total > 0 ? facet_by_cumulative_year(response.aggregations.link_checks_has_schema_org.buckets) : nil
link_checks_schema_org_id = total > 0 ? response.aggregations.link_checks_schema_org_id.value : nil
link_checks_dc_identifier = total > 0 ? response.aggregations.link_checks_dc_identifier.value : nil
link_checks_citation_doi = total > 0 ? response.aggregations.link_checks_citation_doi.value : nil
links_checked = total > 0 ? response.aggregations.links_checked.value : nil
link_checks_status = total.positive? ? facet_by_cumulative_year(response.aggregations.link_checks_status.buckets) : nil
links_with_schema_org = total.positive? ? facet_by_cumulative_year(response.aggregations.link_checks_has_schema_org.buckets) : nil
link_checks_schema_org_id = total.positive? ? response.aggregations.link_checks_schema_org_id.value : nil
link_checks_dc_identifier = total.positive? ? response.aggregations.link_checks_dc_identifier.value : nil
link_checks_citation_doi = total.positive? ? response.aggregations.link_checks_citation_doi.value : nil
links_checked = total.positive? ? response.aggregations.links_checked.value : nil
}
logger.warn method: "GET", path: "/dois", message: "AggregationsLinkChecks /dois", duration: bm

if params[:mix_in] == "metrics"
dois_names = results.map { |result| result.dig(:_source, :doi) }.join(',')
metrics_array = get_metrics_array(dois_names)
results = mix_in_metrics_array(results, metrics_array)
end

person_metrics = params[:mix_in] == "metrics" ? get_person_metrics(params[:user_id]) : {}
person_metrics = get_person_metrics(params[:user_id])
citations = person_metrics[:citations]
views = person_metrics[:views]
downloads = person_metrics[:downloads]
else
citations = total.positive? ? metric_facet_by_year(response.aggregations.citations.buckets) : nil
views = total.positive? ? metric_facet_by_year(response.aggregations.views.buckets) : nil
downloads = total.positive? ? metric_facet_by_year(response.aggregations.downloads.buckets) : nil
end

respond_to do |format|
format.json do
Expand Down Expand Up @@ -234,9 +241,9 @@ def index
"linkChecksDcIdentifier" => link_checks_dc_identifier,
"linkChecksCitationDoi" => link_checks_citation_doi,
subjects: subjects,
citations: person_metrics[:citations],
views: person_metrics[:views],
downloads: person_metrics[:downloads],
citations: citations,
views: views,
downloads: downloads,
}.compact

options[:links] = {
Expand All @@ -261,7 +268,7 @@ def index
mix_in: params[:mix_in],
metrics: metrics_array,
affiliation: params[:affiliation],
is_collection: options[:is_collection]
is_collection: options[:is_collection],
}

# sparse fieldsets
Expand Down
21 changes: 18 additions & 3 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,24 @@ 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" } },
views: {
date_histogram: { field: "publication_year", interval: "year", min_doc_count: 1 },
aggs: {
metric_count: { sum: { field: "view_count" } },
}
},
downloads: {
date_histogram: { field: "publication_year", interval: "year", min_doc_count: 1 },
aggs: {
metric_count: { sum: { field: "download_count" } },
}
},
citations: {
date_histogram: { field: "publication_year", interval: "year", min_doc_count: 1 },
aggs: {
metric_count: { sum: { field: "citation_count" } },
}
},
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def as_indexed_json(options = {})
"occurred_at" => occurred_at,
"citation_id" => citation_id,
"citation_year" => citation_year,
"cache_key" => cache_key
"cache_key" => cache_key,
}
end

Expand Down
55 changes: 55 additions & 0 deletions spec/fixtures/vcr_cassettes/dois/citations/has_downloads_meta.yml

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions spec/fixtures/vcr_cassettes/dois/downloads/has_downloads_meta.yml

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions spec/fixtures/vcr_cassettes/dois/views/has_views_meta.yml

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@
expect(json.dig('data', 'attributes', 'viewCount')).to eq(75)
expect(json.dig('data', 'attributes', 'viewsOverTime')).to eq([{"total"=>25, "year_month"=>"2015-06"}, {"total"=>25, "year_month"=>"2015-06"}, {"total"=>25, "year_month"=>"2015-06"}])
end

it "has views meta" do
get "/dois", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'views')).to eq([{"count"=>75, "id"=>"2011", "title"=>"2011"}])
end

it "repository shows summary count" do
get "/repositories/#{client.uid}", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'name')).to eq(client.name)
expect(json.dig('meta', 'views')).to eq([{"count"=>75, "id"=>"2011", "title"=>"2011"}])
end
end

describe "downloads", elasticsearch: true, vcr: true do
Expand All @@ -218,6 +233,21 @@
expect(json.dig('data', 'attributes', 'downloadCount')).to eq(30)
expect(json.dig('data', 'attributes', 'downloadsOverTime')).to eq([{"total"=>10, "year_month"=>"2015-06"}, {"total"=>10, "year_month"=>"2015-06"}, {"total"=>10, "year_month"=>"2015-06"}])
end

it "has downloads meta" do
get "/dois", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'downloads')).to eq([{"count"=>30, "id"=>"2011", "title"=>"2011"}])
end

it "repository shows summary count" do
get "/repositories/#{client.uid}", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'name')).to eq(client.name)
expect(json.dig('meta', 'downloads')).to eq([{"count"=>30, "id"=>"2011", "title"=>"2011"}])
end
end

describe "references", elasticsearch: true, vcr: true do
Expand Down Expand Up @@ -266,6 +296,22 @@
expect(json.dig('included').length).to eq(2)
expect(json.dig('included', 0, 'attributes', 'relationTypeId')).to eq("is-referenced-by")
end

it "has downloads meta" do
get "/dois", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'citations')).to eq([{"count"=>1, "id"=>"2011", "title"=>"2011"}])
end

it "repository shows summary count" do
get "/repositories/#{client.uid}", nil, headers

puts last_response.body
expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'name')).to eq(client.name)
expect(json.dig('meta', 'citations')).to eq([{"count"=>1, "id"=>"2011", "title"=>"2011"}])
end
end

describe "parts", elasticsearch: true, vcr: true do
Expand Down

0 comments on commit ee4a10f

Please sign in to comment.