Skip to content

Commit

Permalink
include changes to display citations
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 8, 2019
1 parent b02d200 commit f9d1ded
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
15 changes: 15 additions & 0 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Facetable

SOURCES = {
"datacite-usage" => "DataCite Usage Stats",
"datacite-resolution" => "DataCite Resolution Stats",
"datacite-related" => "DataCite Related Identifiers",
"datacite-crossref" => "DataCite to Crossref",
"datacite-kisti" => "DataCite to KISTI",
Expand Down Expand Up @@ -133,6 +134,20 @@ def facet_by_source(arr)
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
title = h['key_as_string'][0..3]

{
'id' => year,
'title' => title,
'sum' => h.dig('total_by_year', 'value') }
end
{ "count" => hash.dig("sum_distribution", "value"),
"years" => arr }
end

def facet_by_relation_type(arr)
arr.map do |hsh|
arr = hsh.dig("year_months", "buckets").map do |h|
Expand Down
33 changes: 14 additions & 19 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,11 @@ def index
when "-created" then { created_at: { order: 'desc' }}
when "updated" then { updated_at: { order: 'asc' }}
when "-updated" then { updated_at: { order: 'desc' }}
when "relation_type_id" then { relation_type_id: { order: 'asc' }}
else { updated_at: { order: 'asc' }}
end

page = params[:page] || {}
if page[:size].present?
page[:size] = [page[:size].to_i, 1000].min
max_number = 1
else
page[:size] = 1000
max_number = 10000/page[:size]
end
page[:number] = page[:number].to_i > 0 ? [page[:number].to_i, max_number].min : 1
page = page_from_params(params)

if params[:id].present?
response = Event.find_by_id(params[:id])
Expand Down Expand Up @@ -112,14 +105,15 @@ def index
total_for_pages = page[:cursor].present? ? total.to_f : [total.to_f, 10000].min
total_pages = page[:size] > 0 ? (total_for_pages / page[:size]).ceil : 0

sources = total > 0 ? facet_by_source(response.response.aggregations.sources.buckets) : nil
prefixes = total > 0 ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil
citation_types = total > 0 ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil
relation_types = total > 0 ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil
registrants = total > 0 && params[:extra] ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil
pairings = total > 0 && params[:extra] ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil
dois = total > 0 && params[:extra] ? facet_by_dois(response.response.aggregations.dois.buckets) : nil
dois_usage = total > 0 && params[:extra] ? facet_by_dois(response.response.aggregations.dois_usage.dois.buckets) : nil
sources = total.positive? ? facet_by_source(response.response.aggregations.sources.buckets) : nil
prefixes = total.positive? ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil
citation_types = total.positive? ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil
relation_types = total.positive? ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil
registrants = total.positive? && params[:extra] ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil
pairings = total.positive? && params[:extra] ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil
dois = total.positive? && params[:extra] ? facet_by_dois(response.response.aggregations.dois.buckets) : nil
dois_usage = total.positive? && params[:extra] ? facet_by_dois(response.response.aggregations.dois_usage.dois.buckets) : nil
dois_citations = total.positive? && params[:extra] ? facet_citations_by_year(response.response.aggregations.dois_citations) : nil

@events = response.results.results

Expand All @@ -135,7 +129,8 @@ def index
pairings: pairings,
registrants: registrants,
"doisRelationTypes": dois,
"doisUsageTypes": dois_usage
"doisUsageTypes": dois_usage,
"doisCitations": dois_citations
}.compact

options[:links] = {
Expand Down Expand Up @@ -184,7 +179,7 @@ def load_event
def set_include
if params[:include].present?
@include = params[:include].split(",").map { |i| i.downcase.underscore.to_sym }
@include = @include & [:subj, :obj]
@include &= [:subj, :obj]
else
@include = []
end
Expand Down
28 changes: 24 additions & 4 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Event < ActiveRecord::Base
end
end


# after_transition :to => [:failed, :done] do |event|
# event.send_callback if event.callback.present?
# end
Expand All @@ -55,8 +54,21 @@ class Event < ActiveRecord::Base
serialize :obj, JSON
serialize :error_messages, JSON

alias_attribute :created, :created_at
alias_attribute :updated, :updated_at
alias_attribute :created, :created_at
alias_attribute :updated, :updated_at

INCLUDED_RELATION_TYPES = [
"cites", "is-cited-by",
"compiles", "is-compiled-by",
"documents", "is-documented-by",
"has-metadata", "is-metadata-for",
"is-supplement-to", "is-supplemented-by",
"is-derived-from", "is-source-of",
"references", "is-referenced-by",
"reviews", "is-reviewed-by",
"requires", "is-required-by",
"describes", "is-described-by"
]

validates :subj_id, :source_id, :source_token, presence: true

Expand Down Expand Up @@ -235,7 +247,15 @@ def self.query_aggregations
filter: { script: { script: "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: { relation_types: { terms: { field: 'relation_type_id',size: 50, min_doc_count: 1 }, aggs: { "total_by_type" => { sum: { field: 'total' }}}}} } }
}
},
dois_citations: {
filter: {
script: {
script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)"
}
},
aggs: { years: { date_histogram: { field: 'occurred_at', interval: 'year', min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: 'total' }}}},"sum_distribution"=>sum_year_distribution}
}
}
end

Expand Down
4 changes: 2 additions & 2 deletions app/serializers/object_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ class ObjectSerializer
set_key_transform :camel_lower
set_type :objects

attributes :subtype, :name, :author, :publisher, :periodical, :includedInDataCatalog, :version, :datePublished, :dateModified, :funder, :proxyIdentifiers, :registrantId
attributes :subtype, :name, :author, :publisher, :periodical, :included_in_data_catalog, :version, :date_published, :date_modified, :funder, :proxy_identifiers, :registrant_id

attribute :subtype do |object|
object.type
object["@type"]
end
end

0 comments on commit f9d1ded

Please sign in to comment.