Skip to content

Commit

Permalink
Merge pull request #755 from datacite/avoid_multiple_aggs_calls_graphql
Browse files Browse the repository at this point in the history
Use instance variables to reduce  number of ES calls in other types
  • Loading branch information
kjgarza authored Aug 31, 2021
2 parents da3bb67 + f304fd0 commit a605818
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
18 changes: 12 additions & 6 deletions app/graphql/types/data_catalog_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,30 @@ def datasets(**args)
end

def view_count
if response.results.total.positive?
aggregate_count(response.response.aggregations.views.buckets)
args = { first: 0 }
@r = response(args) if @r.nil?
if @r.results.total.positive?
aggregate_count(@r.response.aggregations.views.buckets)
else
0
end
end

def download_count
if response.results.total.positive?
aggregate_count(response.response.aggregations.downloads.buckets)
args = { first: 0 }
@r = response(args) if @r.nil?
if @r.results.total.positive?
aggregate_count(@r.response.aggregations.downloads.buckets)
else
0
end
end

def citation_count
if response.results.total.positive?
aggregate_count(response.response.aggregations.citations.buckets)
args = { first: 0 }
@r = response(args) if @r.nil?
if @r.results.total.positive?
aggregate_count(@r.response.aggregations.citations.buckets)
else
0
end
Expand Down
12 changes: 6 additions & 6 deletions app/graphql/types/funder_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,20 @@ def works(**args)

def view_count
args = { first: 0 }
r = response(args)
r.response.aggregations.view_count.value.to_i
@r = response(args) if @r.nil?
@r.response.aggregations.view_count.value.to_i
end

def download_count
args = { first: 0 }
r = response(args)
r.response.aggregations.download_count.value.to_i
@r = response(args) if @r.nil?
@r.response.aggregations.download_count.value.to_i
end

def citation_count
args = { first: 0 }
r = response(args)
r.response.aggregations.citation_count.value.to_i
@r = response(args) if @r.nil?
@r.response.aggregations.citation_count.value.to_i
end

def response(**args)
Expand Down
18 changes: 9 additions & 9 deletions app/graphql/types/member_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,29 +305,29 @@ def repositories(**args)

def view_count
args = { first: 0 }
r = response(args)
if r.results.total.positive?
aggregate_count(r.response.aggregations.views.buckets)
@r = response(args) if @r.nil?
if @r.results.total.positive?
aggregate_count(@r.response.aggregations.views.buckets)
else
0
end
end

def download_count
args = { first: 0 }
r = response(args)
if r.results.total.positive?
aggregate_count(r.response.aggregations.downloads.buckets)
@r = response(args) if @r.nil?
if @r.results.total.positive?
aggregate_count(@r.response.aggregations.downloads.buckets)
else
0
end
end

def citation_count
args = { first: 0 }
r = response(args)
if r.results.total.positive?
aggregate_count(r.response.aggregations.citations.buckets)
@r = response(args) if @r.nil?
if @r.results.total.positive?
aggregate_count(@r.response.aggregations.citations.buckets)
else
0
end
Expand Down
12 changes: 6 additions & 6 deletions app/graphql/types/organization_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,20 @@ def people(**args)

def view_count
args = { first: 0 }
r = response(args)
r.response.aggregations.view_count.value.to_i
@r = response(args) if @r.nil?
@r.response.aggregations.view_count.value.to_i
end

def download_count
args = { first: 0 }
r = response(args)
r.response.aggregations.download_count.value.to_i
@r = response(args) if @r.nil?
@r.response.aggregations.download_count.value.to_i
end

def citation_count
args = { first: 0 }
r = response(args)
r.response.aggregations.citation_count.value.to_i
@r = response(args) if @r.nil?
@r.response.aggregations.citation_count.value.to_i
end

def response(**args)
Expand Down
12 changes: 6 additions & 6 deletions app/graphql/types/repository_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,20 @@ def prefixes(**args)

def view_count
args = { first: 0 }
r = response(args)
r.response.aggregations.view_count.value.to_i
@r = response(args) if @r.nil?
@r.response.aggregations.view_count.value.to_i
end

def download_count
args = { first: 0 }
r = response(args)
r.response.aggregations.download_count.value.to_i
@r = response(args) if @r.nil?
@r.response.aggregations.download_count.value.to_i
end

def citation_count
args = { first: 0 }
r = response(args)
r.response.aggregations.citation_count.value.to_i
@r = response(args) if @r.nil?
@r.response.aggregations.citation_count.value.to_i
end

def response(**args)
Expand Down

0 comments on commit a605818

Please sign in to comment.