Skip to content

Commit 917b063

Browse files
committed
Implement quota for auto-evaluation metrics
This adds the ability to limit the number of auto-evaluation metrics that can be run per hour. If the limit is reached, further evaluations are skipped and a warning is logged. We've set the limit to 300 metrics per hour based on discussion with data science.
1 parent bed412f commit 917b063

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

app/jobs/answer_analysis/answer_relevancy_job.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module AnswerAnalysis
22
class AnswerRelevancyJob < BaseMetricJob
33
def perform(answer_id)
44
return unless eligible_for_answer_analysis?(answer_id)
5+
return if quota_limit_reached?
56

67
answer = Answer.includes(:question, :answer_relevancy_aggregate).find_by(id: answer_id)
78
return logger.warn(aggregate_exists_warn_message(answer.id)) if answer.answer_relevancy_aggregate.present?

app/jobs/answer_analysis/base_metric_job.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,23 @@ def eligible_for_answer_analysis?(answer_id)
1515

1616
eligible
1717
end
18+
19+
def quota_limit_reached?
20+
quota = Rails.configuration.max_auto_evaluation_metrics_per_hour
21+
current_count = Rails.cache.read("auto_evaluation_metrics_run_count")
22+
23+
if current_count.nil?
24+
Rails.cache.write("auto_evaluation_metrics_run_count", 1, expires_in: 1.hour)
25+
return false
26+
end
27+
28+
if current_count >= quota
29+
logger.warn("Auto-evaluation quota limit of #{quota} metrics per hour reached")
30+
return true
31+
end
32+
33+
Rails.cache.increment("auto_evaluation_metrics_run_count")
34+
false
35+
end
1836
end
1937
end

config/application.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,7 @@ class Application < Rails::Application
9191
.topic_tagger
9292
.dig("tool_spec", "input_schema", "$defs", "govuk_topic_tags", "enum")
9393
.sort
94+
95+
config.max_auto_evaluation_metrics_per_hour = 300
9496
end
9597
end

spec/jobs/answer_analysis/answer_relevancy_job_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
allow(AutoEvaluation::AnswerRelevancy)
5454
.to receive(:call).and_return(first_result, second_result, third_result)
5555
stub_const("AnswerAnalysis::BaseMetricJob::NUMBER_OF_RUNS", 3)
56+
allow(Rails.configuration).to receive(:max_auto_evaluation_metrics_per_hour).and_return(2)
5657
end
5758

5859
it_behaves_like "a job in queue", "default"
@@ -98,6 +99,31 @@
9899
end
99100
end
100101

102+
it "writes the auto_evaluation_metrics_run_count cache key on the first metric run" do
103+
expect(Rails.cache).to receive(:write)
104+
.with("auto_evaluation_metrics_run_count", 1, expires_in: 1.hour)
105+
106+
described_class.new.perform(answer.id)
107+
end
108+
109+
it "increments the auto_evaluation_metrics_run_count cache key in subsequent runs" do
110+
allow(Rails.cache).to receive(:read).with("auto_evaluation_metrics_run_count").and_return(1)
111+
expect(Rails.cache).to receive(:increment)
112+
.with("auto_evaluation_metrics_run_count")
113+
114+
described_class.new.perform(answer.id)
115+
end
116+
117+
it "logs info and does not perform evaluation when quota limit is reached" do
118+
allow(Rails.cache).to receive(:read).with("auto_evaluation_metrics_run_count").and_return(2)
119+
expect(described_class.logger)
120+
.to receive(:warn)
121+
.with("Auto-evaluation quota limit of 2 metrics per hour reached")
122+
expect(AutoEvaluation::AnswerRelevancy).not_to receive(:call)
123+
124+
described_class.new.perform(answer.id)
125+
end
126+
101127
context "when the answer has a rephrased_question" do
102128
let(:rephrased_question) { "This is a rephrased_question" }
103129

0 commit comments

Comments
 (0)