diff --git a/app/controllers/concerns/facetable.rb b/app/controllers/concerns/facetable.rb index 99a7d8942..32aba70e0 100644 --- a/app/controllers/concerns/facetable.rb +++ b/app/controllers/concerns/facetable.rb @@ -134,14 +134,6 @@ def facet_by_source(arr) end end - # def facet_citations_by_year(hash) - # hash.map do |hsh| - # { "id" => hsh["key"].to_i, - # "title" => hsh["key"], - # "count" => hsh["doc_count"] } - # end - # end - def facet_citations_by_year(hash) arr = hash.dig('years', 'buckets').map do |h| year = h['key_as_string'][0..3].to_i @@ -156,6 +148,22 @@ def facet_citations_by_year(hash) "years" => arr } end + def facet_counts_by_year_month(hash) + arr = hash.dig('year_months', 'buckets').map do |h| + month = h["key_as_string"][5..6].to_i + title = I18n.t("date.month_names")[month] + " " + h["key_as_string"][0..3] + + { + "id" => h["key_as_string"][0..6], + 'title' => title, + 'sum' => h.dig('total_by_year_month', 'value') } + end + { "count" => hash.dig("sum_distribution", "value"), + "yearMonths" => arr } + end + + + def facet_by_relation_type(arr) arr.map do |hsh| arr = hsh.dig("year_months", "buckets").map do |h| diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 387709cd4..7c20b0f4f 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -115,21 +115,21 @@ def index total_pages = page[:size] > 0 ? (total_for_pages / page[:size]).ceil : 0 - aggregations = params.fetch(:aggregations,"") || "" + aggregations = params.fetch(:aggregations, "") || "" sources = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.sources.buckets) : nil - prefixes = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil - citation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil - relation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil + prefixes = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil + citation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil + relation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil registrants = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil pairings = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil dois = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_dois(response.response.aggregations.dois.buckets) : nil dois_usage = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_dois(response.response.aggregations.dois_usage.dois.buckets) : nil dois_citations = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_citations_by_year(response.response.aggregations.dois_citations) : nil - citations_histogram = total.positive? && aggregations.include?("metrics_aggregations") ? facet_citations_by_year(response.response.aggregations.citations_histogram) : nil - citations = total.positive? && aggregations.include?("metrics_aggregations") ? facet_citations_by_dois(response.response.aggregations.citations.dois.buckets) : nil - views_histogram = total.positive? && aggregations.include?("metrics_aggregations") ? facet_citations_by_year(response.response.aggregations.views) : nil - downloads_histogram = total.positive? && aggregations.include?("metrics_aggregations") ? facet_citations_by_year(response.response.aggregations.downloads) : nil + citations_histogram = total.positive? && aggregations.include?("metrics_aggregations") ? facet_citations_by_year(response.response.aggregations.citations_histogram) : nil + citations = total.positive? && aggregations.include?("metrics_aggregations") ? facet_citations_by_dois(response.response.aggregations.citations.dois.buckets) : nil + views_histogram = total.positive? && aggregations.include?("metrics_aggregations") ? facet_counts_by_year_month(response.response.aggregations.views) : nil + downloads_histogram = total.positive? && aggregations.include?("metrics_aggregations") ? facet_counts_by_year_month(response.response.aggregations.downloads) : nil results = response.results diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 7b07808e2..3280c25af 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -95,7 +95,7 @@ def find_by_id_list(ids, options={}) }) end - def get_aggregations_hash(aggregations="") + def get_aggregations_hash(aggregations = "") return send(:query_aggregations) if aggregations.blank? aggs = {} aggregations.split(",").each do |agg| diff --git a/app/models/event.rb b/app/models/event.rb index c33cf1fbb..2d32fb2c3 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -74,14 +74,11 @@ class Event < ActiveRecord::Base ] VIEWS_RELATION_TYPES = [ - "unique-dataset-investigations-regular", - "total-dataset-investigations-regular" + "unique-dataset-investigations-regular" ] - DOWNLOADS_RELATION_TYPES = [ - "unique-dataset-requests-regular", - "total-dataset-requests-regular" + "unique-dataset-requests-regular" ] validates :subj_id, :source_id, :source_token, presence: true @@ -224,7 +221,11 @@ def self.query_aggregations end def self.metrics_aggregations - + sum_distribution = { + sum_bucket: { + buckets_path: "year_months>total_by_year_month" + } + } sum_year_distribution = { sum_bucket: { buckets_path: "years>total_by_year" @@ -233,69 +234,29 @@ def self.metrics_aggregations { citations_histogram: { - filter: { - script: { - script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)" - } + filter: {script: {script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)"} }, aggs: { years: { date_histogram: { field: 'citation_year', interval: 'year', min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: 'total' }}}},"sum_distribution"=>sum_year_distribution} }, citations: { - filter: { - script: { - script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)" - } + filter: {script: {script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)"} }, aggs: { dois: { terms: { field: 'obj_id', size: 50, min_doc_count: 1 }, aggs: { unique_citations: { cardinality: { field: 'citation_id' }}} }} }, views: { - filter: { - script: { - script: "#{VIEWS_RELATION_TYPES}.contains(doc['relation_type_id'].value) && doc['source_id'].value == 'datacite-usage' && doc['occurred_at'].value.getMillis() >= doc['obj.datePublished'].value.getMillis() && doc['occurred_at'].value.getMillis() < new Date().getTime()" , - } + filter: {script: {script: "#{VIEWS_RELATION_TYPES}.contains(doc['relation_type_id'].value) && doc['source_id'].value == 'datacite-usage' && doc['occurred_at'].value.getMillis() >= doc['obj.datePublished'].value.getMillis() && doc['occurred_at'].value.getMillis() < new Date().getTime()"} }, aggs: { - dois: { - terms: { - field: 'obj_id', - size: 50, - min_doc_count: 1 - }, - aggs: { - "total_by_type" => { - sum: { - field: 'total' - } - } - } - }, - years: { date_histogram: { field: 'occurred_at', interval: 'year', min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: 'total' } } } }, "sum_distribution" => sum_year_distribution + year_months: { date_histogram: { field: 'occurred_at', interval: 'month', min_doc_count: 1 }, aggs: { "total_by_year_month" => { sum: { field: 'total' } } } }, "sum_distribution" => sum_distribution } }, downloads: { - filter: { - script: { - script: "#{DOWNLOADS_RELATION_TYPES}.contains(doc['relation_type_id'].value) && doc['source_id'].value == 'datacite-usage' && doc['occurred_at'].value.getMillis() >= doc['obj.datePublished'].value.getMillis() && doc['occurred_at'].value.getMillis() < new Date().getTime()" , - } + filter: {script: {script: "#{DOWNLOADS_RELATION_TYPES}.contains(doc['relation_type_id'].value) && doc['source_id'].value == 'datacite-usage' && doc['occurred_at'].value.getMillis() >= doc['obj.datePublished'].value.getMillis() && doc['occurred_at'].value.getMillis() < new Date().getTime()"} }, aggs: { - dois: { - terms: { - field: 'obj_id', - size: 50, - min_doc_count: 1 - }, - aggs: { - "total_by_type" => { - sum: { - field: 'total' - } - } - } - }, - years: { date_histogram: { field: 'occurred_at', interval: 'year', min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: 'total' } } } }, "sum_distribution" => sum_year_distribution + year_months: { date_histogram: { field: 'occurred_at', interval: 'month', min_doc_count: 1 }, aggs: { "total_by_year_month" => { sum: { field: 'total' } } } }, "sum_distribution" => sum_distribution } } }