Skip to content

Commit

Permalink
reduce conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgarza committed Nov 30, 2019
1 parent 24d1a32 commit 5edfb07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
24 changes: 12 additions & 12 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,18 @@ def index
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? && params[:doi].present? ? EventsQuery.new.usage(params[:doi]) : nil
dois_usage = total.positive? ? EventsQuery.new.usage(params[:doi]) : nil
# dois_citations = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_citations_by_year_v1(response.response.aggregations.dois_citations) : nil
citations = total.positive? && params[:doi].present? ? EventsQuery.new.citations(params[:doi]) : nil
citations_histogram = total.positive? && params[:doi].present? ? EventsQuery.new.citations_histogram(params[:doi]) : nil
references = total.positive? && params[:doi].present? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.references.dois.buckets) : nil
relations = total.positive? && params[:doi].present? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.relations.dois.buckets) : nil
citations = total.positive? ? EventsQuery.new.citations(params[:doi]) : nil
citations_histogram = total.positive? ? EventsQuery.new.citations_histogram(params[:doi]) : nil
references = total.positive? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.references.dois.buckets) : nil
relations = total.positive? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.relations.dois.buckets) : nil

views_histogram = total.positive? && params[:doi].present? ? EventsQuery.new.views_histogram(params[:doi]) : nil
downloads_histogram = total.positive? && params[:doi].present? ? EventsQuery.new.downloads_histogram(params[:doi]) : nil
views_histogram = total.positive? ? EventsQuery.new.views_histogram(params[:doi]) : nil
downloads_histogram = total.positive? ? EventsQuery.new.downloads_histogram(params[:doi]) : nil

views = total.positive? && params[:doi].present? ? EventsQuery.new.views(params[:doi]) : nil
downloads = total.positive? && params[:doi].present? ? EventsQuery.new.downloads(params[:doi]) : nil
# views = total.positive? ? EventsQuery.new.views(params[:doi]) : nil
# downloads = total.positive? ? EventsQuery.new.downloads(params[:doi]) : nil
unique_obj_count = total.positive? && aggregations.include?("advanced_aggregations") ? response.response.aggregations.unique_obj_count.value : nil
unique_subj_count = total.positive? && aggregations.include?("advanced_aggregations") ? response.response.aggregations.unique_subj_count.value : nil

Expand Down Expand Up @@ -162,9 +162,9 @@ def index
"subjCount": unique_subj_count
},
"viewsHistogram": views_histogram,
"views": views,
"downloadsHistogram": downloads_histogram,
"downloads": downloads
# "views": views,
"downloadsHistogram": downloads_histogram
# "downloads": downloads
}.compact

options[:links] = {
Expand Down
10 changes: 10 additions & 0 deletions app/queries/events_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ def initialize
end

def doi_citations(doi)
return nil unless doi.present?
pid = Event.new.normalize_doi(doi)
query = "(subj_id:\"#{pid}\" AND (relation_type_id:#{PASSIVE_RELATION_TYPES.join(' OR relation_type_id:')})) OR (obj_id:\"#{pid}\" AND (relation_type_id:#{ACTIVE_RELATION_TYPES.join(' OR relation_type_id:')}))"
results = Event.query(query, doi: doi, aggregations: "citation_count_aggregation", page: { size: 1, cursor: [] }).response.aggregations.citations.buckets
results.any? ? results.first.total.value : 0
end

def citations(doi)
return {} unless doi.present?
doi.downcase.split(",").map do |item|
{ id: item, count: EventsQuery.new.doi_citations(item) }
end
end

def citations_histogram(doi)
return {} unless doi.present?
pid = Event.new.normalize_doi(doi.downcase.split(",").first)
query = "(subj_id:\"#{pid}\" AND (relation_type_id:#{PASSIVE_RELATION_TYPES.join(' OR relation_type_id:')})) OR (obj_id:\"#{pid}\" AND (relation_type_id:#{ACTIVE_RELATION_TYPES.join(' OR relation_type_id:')}))"
results = Event.query(query, doi: doi, aggregations: "yearly_histogram_aggregation", page: { size: 1, cursor: [] }).response.aggregations
Expand All @@ -40,44 +43,51 @@ def citations_histogram(doi)


def doi_views(doi)
return nil unless doi.present?
query = "(relation_type_id:unique-dataset-investigations-regular AND source_id:datacite-usage)"
results = Event.query(query, doi: doi, aggregations: "usage_count_aggregation", page: { size: 1, cursor: [] }).response.aggregations.usage.buckets
results.any? ? results.first.dig("total_by_type", "value") : 0
end

def views(doi)
return {} unless doi.present?
doi.downcase.split(",").map do |item|
{ id: item, count: EventsQuery.new.doi_views(item) }
end
end

def views_histogram(doi)
return {} unless doi.present?
doi = doi.downcase.split(",").first
query = "(relation_type_id:unique-dataset-investigations-regular AND source_id:datacite-usage)"
results = Event.query(query, doi: doi, aggregations: "monthly_histogram_aggregation", page: { size: 1, cursor: [] }).response.aggregations
facet_counts_by_year_month(results)
end

def doi_downloads(doi)
return nil unless doi.present?
query = "(relation_type_id:unique-dataset-requests-regular AND source_id:datacite-usage)"
results = Event.query(query, doi: doi, aggregations: "usage_count_aggregation", page: { size: 1, cursor: [] }).response.aggregations.usage.buckets
results.any? ? results.first.dig("total_by_type", "value") : 0
end

def downloads(doi)
return {} unless doi.present?
doi.downcase.split(",").map do |item|
{ id: item, count: EventsQuery.new.doi_downloads(item) }
end
end

def downloads_histogram(doi)
return {} unless doi.present?
doi = doi.downcase.split(",").first
query = "(relation_type_id:unique-dataset-requests-regular AND source_id:datacite-usage)"
results = Event.query(query, doi: doi, aggregations: "monthly_histogram_aggregation", page: { size: 1, cursor: [] }).response.aggregations
facet_counts_by_year_month(results)
end

def usage(doi)
return {} unless doi.present?
doi.downcase.split(",").map do |item|
pid = Event.new.normalize_doi(item)
requests = EventsQuery.new.doi_downloads(item)
Expand Down

0 comments on commit 5edfb07

Please sign in to comment.