diff --git a/app/models/daily_api_usage.rb b/app/models/daily_api_usage.rb index c512fa506..757f8e4a8 100644 --- a/app/models/daily_api_usage.rb +++ b/app/models/daily_api_usage.rb @@ -18,11 +18,22 @@ def self.increment(api_key_id:, date:) # rubocop:enable Rails/SkipsModelValidations end - sig { params(date_from: Date, date_to: Date, number: Integer).returns(T::Array[{ api_key: ApiKey, sum: Numeric }]) } + sig { params(date_from: Date, date_to: Date, number: Integer).returns(T::Array[{ api_key: ApiKey, sum: Numeric, min: Numeric, max: Numeric }]) } def self.top_usage_in_date_range(date_from:, date_to:, number:) api_keys = top_by_usage_in_date_range(date_from:, date_to:, number:) - sums = where(date: date_from..date_to, api_key: api_keys).group(:api_key_id).sum(:count) - api_keys.map { |api_key| { api_key:, sum: T.must(sums[api_key.id]) } } + grouped = where(date: date_from..date_to, api_key: api_keys).group(:api_key_id) + sums = grouped.sum(:count) + # Note that the "minimum" is only for the days on which there were requests + minimums = grouped.minimum(:count) + maximums = grouped.maximum(:count) + api_keys.map do |api_key| + { + api_key:, + sum: T.must(sums[api_key.id]), + min: minimums[api_key.id], + max: maximums[api_key.id] + } + end end sig { params(date_from: Date, date_to: Date, number: Integer).returns(T::Enumerable[ApiKey]) } @@ -36,7 +47,7 @@ def self.top_by_usage_in_date_range(date_from:, date_to:, number:) def self.top_average_usage_in_date_range(date_from:, date_to:, number:) n = (date_to - date_from + 1) top_usage_in_date_range(date_from:, date_to:, number:).map do |r| - { api_key: r[:api_key], mean: r[:sum].to_f / n } + { api_key: r[:api_key], mean: r[:sum].to_f / n, min: r[:min], max: r[:max] } end end end diff --git a/app/views/admin/api_usages/index.html.erb b/app/views/admin/api_usages/index.html.erb index 635cb498f..003bd6c5e 100644 --- a/app/views/admin/api_usages/index.html.erb +++ b/app/views/admin/api_usages/index.html.erb @@ -22,7 +22,9 @@