Skip to content

A platform for community discussion. Free, open, simple.

FIX: Also make sure quota is applied to detection (#35838)

Natalie Tayba63bbeacf37
diff --git a/plugins/discourse-ai/app/jobs/regular/localize_posts.rb b/plugins/discourse-ai/app/jobs/regular/localize_posts.rb
index 747b7e119a7..e6ccc18b936 100644
--- a/plugins/discourse-ai/app/jobs/regular/localize_posts.rb
+++ b/plugins/discourse-ai/app/jobs/regular/localize_posts.rb
@@ -37,6 +37,8 @@ module Jobs
next if posts.empty?
posts.each do |post|
+ next unless DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, locale)
+
begin
DiscourseAi::Translation::PostLocalizer.localize(post, locale)
rescue FinalDestination::SSRFDetector::LookupFailedError
diff --git a/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb
index 5d5e64277be..b7db5a0a2fb 100644
--- a/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb
+++ b/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb
@@ -32,6 +32,8 @@ module Jobs
posts.each do |post|
begin
+ next if !DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, "")
+
DiscourseAi::Translation::PostLocaleDetector.detect_locale(post)
rescue FinalDestination::SSRFDetector::LookupFailedError
# do nothing, there are too many sporadic lookup failures
diff --git a/plugins/discourse-ai/lib/translation/post_localizer.rb b/plugins/discourse-ai/lib/translation/post_localizer.rb
index 0fa15a30d7a..4a84d85c255 100644
--- a/plugins/discourse-ai/lib/translation/post_localizer.rb
+++ b/plugins/discourse-ai/lib/translation/post_localizer.rb
@@ -3,6 +3,8 @@
module DiscourseAi
module Translation
class PostLocalizer
+ MAX_QUOTA_PER_DAY = 2
+
def self.localize(post, target_locale = I18n.locale)
if post.blank? || target_locale.blank? ||
LocaleNormalizer.is_same?(post.locale, target_locale) || post.raw.blank?
@@ -38,7 +40,7 @@ module DiscourseAi
end
def self.has_relocalize_quota?(post, locale, skip_incr: false)
- return false if get_relocalize_quota(post, locale).to_i >= 2
+ return false if get_relocalize_quota(post, locale).to_i >= MAX_QUOTA_PER_DAY
incr_relocalize_quota(post, locale) unless skip_incr
true
diff --git a/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb b/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb
index da0d1f48920..6b812871a81 100644
--- a/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb
+++ b/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb
@@ -145,6 +145,22 @@ describe Jobs::LocalizePosts do
end
end
+ context "when relocalize quota is exhausted" do
+ before { post.update(locale: "es") }
+
+ it "skips localization for posts that have exceeded quota for a specific locale" do
+ DiscourseAi::Translation::PostLocalizer::MAX_QUOTA_PER_DAY.times do
+ DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, "en")
+ end
+
+ DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "en").never
+ DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "ja").once
+ DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "de").once
+
+ job.execute({ limit: 10 })
+ end
+ end
+
describe "with public content limitation" do
fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
fab!(:private_topic) { Fabricate(:topic, category: private_category) }
diff --git a/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb
index fa9e44e5137..edc54e49168 100644
--- a/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb
+++ b/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb
@@ -78,6 +78,21 @@ describe Jobs::PostsLocaleDetectionBackfill do
expect { job.execute({}) }.not_to raise_error
end
+ context "when relocalize quota is exhausted" do
+ before do
+ # max out quota
+ DiscourseAi::Translation::PostLocalizer::MAX_QUOTA_PER_DAY.times do
+ DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, "")
+ end
+ end
+
+ it "skips locale detection for posts that have exceeded quota" do
+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).never
+
+ job.execute({})
+ end
+ end
+
it "logs a summary after running" do
DiscourseAi::Translation::PostLocaleDetector.stubs(:detect_locale)
DiscourseAi::Translation::VerboseLogger.expects(:log).with(includes("Detected 1 post locales"))