Skip to content

Commit

Permalink
codeclimate suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgarza committed Aug 1, 2019
1 parent 88705c7 commit 77a72d6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 69 deletions.
24 changes: 16 additions & 8 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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|
Expand Down
16 changes: 8 additions & 8 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
65 changes: 13 additions & 52 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit 77a72d6

Please sign in to comment.