A platform for community discussion. Free, open, simple.
FIX: Completion rate double-count and normalize (#33963)
Natalie Taydfd0c5208c86
diff --git a/plugins/discourse-ai/lib/translation/base_candidates.rb b/plugins/discourse-ai/lib/translation/base_candidates.rbnew file mode 100644index 00000000000..bd976256f6b--- /dev/null+++ b/plugins/discourse-ai/lib/translation/base_candidates.rb@@ -0,0 +1,51 @@+# frozen_string_literal: true++module DiscourseAi+ module Translation+ class BaseCandidates+ COMPLETION_CACHE_TTL = 1.hour++ # ModelType that are eligible for translation based on site settings+ # @return [ActiveRecord::Relation] the ActiveRecord relation of the candidates+ def self.get+ raise NotImplementedError+ end++ # The completion in float percentage for the given locale+ # @param locale [String] the locale for which to calculate the completion percentage+ # @return [Float] the completion percentage for the given locale e.g. 0.75 for 75%+ def self.get_completion_per_locale(locale)+ Discourse+ .cache+ .fetch(get_completion_cache_key(locale), expires_in: COMPLETION_CACHE_TTL) do+ done, total = calculate_completion_per_locale(locale)+ return 1.0 if total.zero?+ done / total.to_f+ end+ end++ def self.clear_completion_cache(locale)+ Discourse.cache.delete(get_completion_cache_key(locale))+ end++ private++ # This method should return [completed_translations, total_needed_translations]+ #+ # This allows flexibility for the implementation to determine how the completion percentage is calculated.+ # # @param locale [String] the locale for which to calculate the completion percentage+ # @return [integer, integer]+ def self.calculate_completion_per_locale(locale)+ raise NotImplementedError+ end++ def self.completion_cache_key_for_type+ raise NotImplementedError+ end++ def self.get_completion_cache_key(locale)+ "#{completion_cache_key_for_type}_#{locale}"+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/translation/category_candidates.rb b/plugins/discourse-ai/lib/translation/category_candidates.rbindex 21484d18cc6..b0196f0692f 100644--- a/plugins/discourse-ai/lib/translation/category_candidates.rb+++ b/plugins/discourse-ai/lib/translation/category_candidates.rb@@ -2,7 +2,7 @@module DiscourseAimodule Translation- class CategoryCandidates+ class CategoryCandidates < BaseCandidatesdef self.getcategories = Category.allif SiteSetting.ai_translation_backfill_limit_to_public_content@@ -11,13 +11,32 @@ module DiscourseAicategoriesend- def self.get_completion_per_locale(locale)- done = get.where(locale:).count- done += CategoryLocalization.where(locale:).count+ private- total = get.count+ def self.calculate_completion_per_locale(locale)+ base_locale = "#{locale.split("_").first}%"+ sql = <<~SQL+ WITH eligible_categories AS (+ #{get.to_sql}+ ),+ total_count AS (+ SELECT COUNT(*) AS count FROM eligible_categories+ ),+ done_count AS (+ SELECT COUNT(DISTINCT c.id)+ FROM eligible_categories c+ LEFT JOIN category_localizations cl ON c.id = cl.category_id AND cl.locale LIKE :base_locale+ WHERE c.locale LIKE :base_locale OR cl.category_id IS NOT NULL+ )+ SELECT d.count AS done, t.count AS total+ FROM total_count t, done_count d+ SQL- done / total.to_f+ DB.query_single(sql, base_locale: "#{base_locale}%")+ end++ def self.completion_cache_key_for_type+ "discourse_ai::translation::category_candidates"endendenddiff --git a/plugins/discourse-ai/lib/translation/post_candidates.rb b/plugins/discourse-ai/lib/translation/post_candidates.rbindex bffc5da26bc..855dd6125e5 100644--- a/plugins/discourse-ai/lib/translation/post_candidates.rb+++ b/plugins/discourse-ai/lib/translation/post_candidates.rb@@ -2,7 +2,7 @@module DiscourseAimodule Translation- class PostCandidates+ class PostCandidates < BaseCandidates# Posts that are eligible for translation based on site settingsdef self.getposts =@@ -33,13 +33,33 @@ module DiscourseAiendend- def self.get_completion_per_locale(locale)- done = get.where(locale:).count- done += PostLocalization.where(locale:).count+ private- total = get.count+ def self.calculate_completion_per_locale(locale)+ base_locale = "#{locale.split("_").first}%"- done / total.to_f+ sql = <<~SQL+ WITH eligible_posts AS (+ #{get.to_sql}+ ),+ total_count AS (+ SELECT COUNT(*) AS count FROM eligible_posts+ ),+ done_count AS (+ SELECT COUNT(DISTINCT p.id)+ FROM eligible_posts p+ LEFT JOIN post_localizations pl ON p.id = pl.post_id AND pl.locale LIKE :base_locale+ WHERE p.locale LIKE :base_locale OR pl.post_id IS NOT NULL+ )+ SELECT d.count AS done, t.count AS total+ FROM total_count t, done_count d+ SQL++ DB.query_single(sql, base_locale: "#{base_locale}%")+ end++ def self.completion_cache_key_for_type+ "discourse_ai::translation::post_candidates"endendenddiff --git a/plugins/discourse-ai/lib/translation/topic_candidates.rb b/plugins/discourse-ai/lib/translation/topic_candidates.rbindex 666a65cd4bf..52c8ac2eb1e 100644--- a/plugins/discourse-ai/lib/translation/topic_candidates.rb+++ b/plugins/discourse-ai/lib/translation/topic_candidates.rb@@ -2,8 +2,7 @@module DiscourseAimodule Translation- class TopicCandidates- # Topics that are eligible for translation based on site settings+ class TopicCandidates < BaseCandidatesdef self.gettopics =Topic@@ -16,7 +15,7 @@ module DiscourseAiif SiteSetting.ai_translation_backfill_limit_to_public_content# exclude all PMs- # and only include posts from public categories+ # and only include topics from public categoriestopics =topics.where.not(archetype: Archetype.private_message)@@ -31,13 +30,33 @@ module DiscourseAiendend- def self.get_completion_per_locale(locale)- done = get.where(locale:).count- done += TopicLocalization.where(locale:).count+ private- total = get.count+ def self.calculate_completion_per_locale(locale)+ base_locale = locale.split("_").first- done / total.to_f+ sql = <<~SQL+ WITH eligible_topics AS (+ #{get.to_sql}+ ),+ total_count AS (+ SELECT COUNT(*) AS count FROM eligible_topics+ ),+ done_count AS (+ SELECT COUNT(DISTINCT t.id)+ FROM eligible_topics t+ LEFT JOIN topic_localizations tl ON t.id = tl.topic_id AND tl.locale LIKE :base_locale+ WHERE t.locale LIKE :base_locale OR tl.topic_id IS NOT NULL+ )+ SELECT d.count AS done, t.count AS total+ FROM total_count t, done_count d+ SQL++ DB.query_single(sql, base_locale: "#{base_locale}%")+ end++ def self.completion_cache_key_for_type+ "discourse_ai::translation::topic_candidates"endendenddiff --git a/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rbindex d282a553d5b..7e3de0bb5fb 100644--- a/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb@@ -20,17 +20,19 @@ describe DiscourseAi::Translation::CategoryCandidates dodescribe ".get_completion_per_locale" docontext "when (scenario A) percentage determined by category's locale" doit "returns 100% completion if all categories are in the locale" do- locale = "es"+ locale = "pt_BR"Fabricate(:category, locale:)Category.update_all(locale: locale)+ Fabricate(:category, locale: "pt")completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)expect(completion).to eq(1.0)endit "returns X% completion if some categories are in the locale" do- locale = "es"+ locale = "pt_BR"Fabricate(:category, locale:)+ Fabricate(:category, locale: "not_pt")completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)expect(completion).to eq(1 / Category.count.to_f)@@ -39,9 +41,10 @@ describe DiscourseAi::Translation::CategoryCandidates docontext "when (scenario B) percentage determined by category localizations" doit "returns 100% completion if all categories have a localization in the locale" do- locale = "es"+ locale = "pt_BR"Fabricate(:category)Category.all.each { |category| Fabricate(:category_localization, category:, locale:) }+ Fabricate(:category_localization, locale: "pt")completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)expect(completion).to eq(1.0)@@ -49,8 +52,8 @@ describe DiscourseAi::Translation::CategoryCandidates doit "returns X% completion if some categories have a localization in the locale" dolocale = "es"- cat = Fabricate(:category)- Fabricate(:category_localization, category: cat, locale:)+ Fabricate(:category_localization, locale:)+ Fabricate(:category_localization, locale: "pt")completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)expect(completion).to eq(1 / Category.count.to_f)@@ -58,13 +61,44 @@ describe DiscourseAi::Translation::CategoryCandidates doendit "returns the correct percentage based on (scenario A & B) `category.locale` and `CategoryLocalization` in the specified locale" do- locale = "es"+ locale = "pt_BR"++ # translated candidatesFabricate(:category, locale:)- cat = Fabricate(:category)- Fabricate(:category_localization, category: cat, locale:)+ category2 = Fabricate(:category)+ Fabricate(:category_localization, category: category2, locale:)++ # untranslated candidate+ category3 = Fabricate(:category)+ Fabricate(:category_localization, category: category3, locale: "zh_CN")++ # not a candidate as it is read restricted+ SiteSetting.ai_translation_backfill_limit_to_public_content = true+ category4 = Fabricate(:category, read_restricted: true)+ Fabricate(:category_localization, category: category4, locale:)completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)- expect(completion).to eq(2 / Category.count.to_f)+ translated_candidates = 2 # category1 + category2+ total_candidates = Category.count - 1 # excluding the read restricted category+ expect(completion).to eq(translated_candidates / total_candidates.to_f)+ end++ it "does not exceed 100% completion when category.locale and category_localization both exist" do+ locale = "pt_BR"+ Category.update_all(locale:)+ category = Fabricate(:category, locale:)+ Fabricate(:category_localization, category:, locale:)++ completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)+ expect(completion).to be(1.0)+ end++ it "returns 100% completion when there are no categories" do+ SiteSetting.ai_translation_backfill_limit_to_public_content = false+ Category.destroy_all++ completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale("pt")+ expect(completion).to eq(1.0)endendenddiff --git a/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rbindex 8a808a2128c..47042bae429 100644--- a/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb@@ -62,9 +62,10 @@ describe DiscourseAi::Translation::PostCandidates dodescribe ".get_completion_per_locale" docontext "when (scenario A) percentage determined by post's locale" doit "returns 100% completion if all posts are in the locale" do- locale = "es"+ locale = "pt_BR"Fabricate(:post, locale:)Post.update_all(locale: locale)+ Fabricate(:post, locale: "pt")completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)expect(completion).to eq(1.0)@@ -73,6 +74,7 @@ describe DiscourseAi::Translation::PostCandidates doit "returns X% completion if some posts are in the locale" dolocale = "es"Fabricate(:post, locale:)+ Fabricate(:post, locale: "not_es")completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)expect(completion).to eq(1 / Post.count.to_f)@@ -81,9 +83,10 @@ describe DiscourseAi::Translation::PostCandidates docontext "when (scenario B) percentage determined by post localizations" doit "returns 100% completion if all posts have a localization in the locale" do- locale = "es"+ locale = "pt_BR"Fabricate(:post)Post.all.each { |post| Fabricate(:post_localization, post:, locale:) }+ Fabricate(:post_localization, locale: "pt")completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)expect(completion).to eq(1.0)@@ -91,8 +94,8 @@ describe DiscourseAi::Translation::PostCandidates doit "returns X% completion if some posts have a localization in the locale" dolocale = "es"- post = Fabricate(:post)- Fabricate(:post_localization, post:, locale:)+ Fabricate(:post_localization, locale:)+ Fabricate(:post_localization, locale: "not_es")completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)expect(completion).to eq(1 / Post.count.to_f)@@ -101,12 +104,40 @@ describe DiscourseAi::Translation::PostCandidates doit "returns the correct percentage based on (scenario A & B) `post.locale` and `PostLocalization` in the specified locale" dolocale = "es"++ # translated candidatesFabricate(:post, locale:)- post = Fabricate(:post)+ post2 = Fabricate(:post)+ Fabricate(:post_localization, post: post2, locale:)++ # untranslated candidate+ post4 = Fabricate(:post)+ Fabricate(:post_localization, post: post4, locale: "zh_CN")++ # not a candidate as it is a bot post+ post3 = Fabricate(:post, user: Discourse.system_user)+ Fabricate(:post_localization, post: post3, locale:)++ completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)+ translated_candidates = 2 # post1 + post2+ total_candidates = Post.count - 1 # excluding the bot post+ expect(completion).to eq(translated_candidates / total_candidates.to_f)+ end++ it "does not exceed 100% completion when post.locale and post_localization both exist" do+ locale = "es"+ post = Fabricate(:post, locale:)Fabricate(:post_localization, post:, locale:)completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)- expect(completion).to eq(2 / Post.count.to_f)+ expect(completion).to be(1.0)+ end++ it "returns 100% completion when no posts are present" do+ SiteSetting.ai_translation_backfill_max_age_days = 0++ completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale("es")+ expect(completion).to eq(1.0)endendenddiff --git a/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rbindex d766071d798..bc3b6bdb54a 100644--- a/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb@@ -54,9 +54,10 @@ describe DiscourseAi::Translation::TopicCandidates dodescribe ".get_completion_per_locale" docontext "when (scenario A) percentage determined by topic's locale" doit "returns 100% completion if all topics are in the locale" do- locale = "es"+ locale = "pt_BR"Fabricate(:topic, locale:)Topic.update_all(locale: locale)+ Fabricate(:topic, locale: "pt")completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)expect(completion).to eq(1.0)@@ -65,6 +66,7 @@ describe DiscourseAi::Translation::TopicCandidates doit "returns X% completion if some topics are in the locale" dolocale = "es"Fabricate(:topic, locale:)+ Fabricate(:topic, locale: "not_es")completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)expect(completion).to eq(1 / Topic.count.to_f)@@ -73,9 +75,10 @@ describe DiscourseAi::Translation::TopicCandidates docontext "when (scenario B) percentage determined by topic localizations" doit "returns 100% completion if all topics have a localization in the locale" do- locale = "es"+ locale = "pt_BR"Fabricate(:topic)Topic.all.each { |topic| Fabricate(:topic_localization, topic:, locale:) }+ Fabricate(:topic_localization, locale: "pt")completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)expect(completion).to eq(1.0)@@ -83,8 +86,8 @@ describe DiscourseAi::Translation::TopicCandidates doit "returns X% completion if some topics have a localization in the locale" dolocale = "es"- topic = Fabricate(:topic)- Fabricate(:topic_localization, topic:, locale:)+ Fabricate(:topic_localization, locale:)+ Fabricate(:topic_localization, locale: "not_es")completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)expect(completion).to eq(1 / Topic.count.to_f)@@ -93,12 +96,41 @@ describe DiscourseAi::Translation::TopicCandidates doit "returns the correct percentage based on (scenario A & B) `topic.locale` and `TopicLocalization` in the specified locale" dolocale = "es"++ # translated candidatesFabricate(:topic, locale:)- topic = Fabricate(:topic)+ topic2 = Fabricate(:topic)+ Fabricate(:topic_localization, topic: topic2, locale:)++ # untranslated candidate+ topic4 = Fabricate(:topic)+ Fabricate(:topic_localization, topic: topic4, locale: "zh_CN")++ # not a candidate as it is a bot topic+ topic3 = Fabricate(:topic, user: Discourse.system_user)+ Fabricate(:topic_localization, topic: topic3, locale:)++ completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)+ translated_candidates = 2 # topic1 + topic2+ total_candidates = Topic.count - 1 # excluding the bot topic+ expect(completion).to eq(translated_candidates / total_candidates.to_f)+ end++ it "does not exceed 100% completion when topic.locale and topic_localization both exist" do+ locale = "es"+ topic = Fabricate(:topic, locale:)Fabricate(:topic_localization, topic:, locale:)completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)- expect(completion).to eq(2 / Topic.count.to_f)+ expect(completion).to be(1.0)+ end++ it "returns 100% if no topics" do+ SiteSetting.ai_translation_backfill_max_age_days = 0+ SiteSetting.ai_translation_backfill_limit_to_public_content = false++ completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale("es")+ expect(completion).to eq(1.0)endendend