Skip to content

Commit

Permalink
added condition to get more info in totals api
Browse files Browse the repository at this point in the history
help to get doi production stats for billing.
drafts and and extra year needed for a projection.
  • Loading branch information
kjgarza committed Jul 1, 2019
1 parent 026d8de commit 1504c86
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ def totals
page = { size: 0, number: 1}
response = nil
bmt = Benchmark.ms {
response = Doi.query(nil, provider_id: params[:provider_id], state: "findable,registered", page: page, totals_agg: true)
state = authenticate_user!.present? && authenticate_user!.is_admin? && params[:state].present? ? params[:state] : "registered,findable"

This comment has been minimized.

Copy link
@mfenner

mfenner Jul 1, 2019

Contributor

I don't understand authenticate_user!.present? && authenticate_user!.is_admin?.

This comment has been minimized.

Copy link
@kjgarza

kjgarza Jul 1, 2019

Author Contributor

So authenticate_user! returns false if one sends no credential in the request.
But one cand send a random string, for example in bearer, and then authenticate_user! would return a user with role_id anonymous.

So with that line, I make sure that only admins can request custom states and if one sends no credentials they will get the default

This comment has been minimized.

Copy link
@mfenner

mfenner Jul 1, 2019

Contributor

There is a is_admin_or_staff? helper method in application_controller for use cases like this. And authenticate_user! is used in a slightly confusing way: the method returns current_user, so it would be clearer to use that (as does is_admin_or_staff?).

response = Doi.query(nil, provider_id: params[:provider_id], state: state, page: page, totals_agg: true)
}
if bmt > 10000
logger.warn "[Benchmark Warning] clients totals " + bmt.to_s + " ms"
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ def providers_totals(arr)
"this_month" => facet_anual(hsh.this_month.buckets),
"this_year" => facet_anual(hsh.this_year.buckets),
"last_year" => facet_anual(hsh.last_year.buckets)},
"two_years_ago" => facet_anual(hsh.two_years_ago.buckets),
"states" => facet_by_key(hsh.states.buckets)
}
end
Expand Down Expand Up @@ -309,7 +310,8 @@ def clients_totals(arr)
"temporal" => {
"this_month" => facet_anual(hsh.this_month.buckets),
"this_year" => facet_anual(hsh.this_year.buckets),
"last_year" => facet_anual(hsh.last_year.buckets)
"last_year" => facet_anual(hsh.last_year.buckets),
"two_years_ago" => facet_anual(hsh.two_years_ago.buckets)
},
"states" => facet_by_key(hsh.states.buckets)
}
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def totals
page = { size: 0, number: 1 }
response = nil
bmt = Benchmark.ms {
response = Doi.query(nil, state: "findable,registered", page: page, totals_agg: true)
state = authenticate_user!.present? && authenticate_user!.is_admin? && params[:state].present? ? params[:state] : "registered,findable"
response = Doi.query(nil, state: state, page: page, totals_agg: true)
}

if bmt > 10000
Expand Down
3 changes: 2 additions & 1 deletion app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ def self.sub_aggregations
states: { terms: { field: 'aasm_state', size: 4, min_doc_count: 1 } },
this_month: { date_range: { field: 'created', ranges: { from: "now/M", to: "now/d" } } },
this_year: { date_range: { field: 'created', ranges: { from: "now/y", to: "now/d" } } },
last_year: { date_range: { field: 'created', ranges: { from: "now-1y/y", to: "now/y-1d" } } }
last_year: { date_range: { field: 'created', ranges: { from: "now-1y/y", to: "now/y-1d" } } },
two_years_ago: { date_range: { field: 'created', ranges: { from: "now-2y/y", to: "now-1y/y-1d" } } }
}
end

Expand Down

0 comments on commit 1504c86

Please sign in to comment.