A platform for community discussion. Free, open, simple.
DEV: Return finished and total number of candidates needed for translation rather than a percentage (#34357)
Natalie Tayc002d10b1149
diff --git a/plugins/discourse-ai/lib/translation/base_candidates.rb b/plugins/discourse-ai/lib/translation/base_candidates.rbindex bd976256f6b..363fa429d3e 100644--- a/plugins/discourse-ai/lib/translation/base_candidates.rb+++ b/plugins/discourse-ai/lib/translation/base_candidates.rb@@ -19,8 +19,8 @@ module DiscourseAi.cache.fetch(get_completion_cache_key(locale), expires_in: COMPLETION_CACHE_TTL) dodone, total = calculate_completion_per_locale(locale)- return 1.0 if total.zero?- done / total.to_f+ return { done: 0, total: 0 } if total.zero?+ { done:, total: }endenddiff --git a/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rbindex 7e3de0bb5fb..935a45f1e24 100644--- a/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb@@ -19,48 +19,48 @@ describe DiscourseAi::Translation::CategoryCandidates dodescribe ".get_completion_per_locale" docontext "when (scenario A) percentage determined by category's locale" do- it "returns 100% completion if all categories are in the locale" do+ it "returns done = total if all categories are in the locale" dolocale = "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)+ expect(completion).to eq({ done: Category.count, total: Category.count })end- it "returns X% completion if some categories are in the locale" do+ it "returns correct done and total if some categories are in the locale" dolocale = "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)+ expect(completion).to eq({ done: 1, total: Category.count })endendcontext "when (scenario B) percentage determined by category localizations" do- it "returns 100% completion if all categories have a localization in the locale" do+ it "returns done = total if all categories have a localization in the locale" dolocale = "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)+ expect(completion).to eq({ done: Category.count, total: Category.count })end- it "returns X% completion if some categories have a localization in the locale" do+ it "returns correct done and total if some categories have a localization in the locale" dolocale = "es"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)+ expect(completion).to eq({ done: 1, total: Category.count })endend- it "returns the correct percentage based on (scenario A & B) `category.locale` and `CategoryLocalization` in the specified locale" do+ it "returns the correct done and total based on (scenario A & B) `category.locale` and `CategoryLocalization` in the specified locale" dolocale = "pt_BR"# translated candidates@@ -80,25 +80,25 @@ describe DiscourseAi::Translation::CategoryCandidates docompletion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)translated_candidates = 2 # category1 + category2total_candidates = Category.count - 1 # excluding the read restricted category- expect(completion).to eq(translated_candidates / total_candidates.to_f)+ expect(completion).to eq({ done: translated_candidates, total: total_candidates })end- it "does not exceed 100% completion when category.locale and category_localization both exist" do+ it "does not allow done to exceed total when category.locale and category_localization both exist" dolocale = "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)+ expect(completion).to eq({ done: Category.count, total: Category.count })end- it "returns 100% completion when there are no categories" do+ it "returns nil - nil for done and total when there are no categories" doSiteSetting.ai_translation_backfill_limit_to_public_content = falseCategory.destroy_allcompletion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale("pt")- expect(completion).to eq(1.0)+ expect(completion).to eq({ done: 0, total: 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 47042bae429..3f8682474bb 100644--- a/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb@@ -60,49 +60,49 @@ describe DiscourseAi::Translation::PostCandidates doenddescribe ".get_completion_per_locale" do- context "when (scenario A) percentage determined by post's locale" do- it "returns 100% completion if all posts are in the locale" do+ context "when (scenario A) 'done' determined by post's locale" do+ it "returns total = done if all posts are in the locale" dolocale = "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)+ expect(completion).to eq({ done: 2, total: 2 })end- it "returns X% completion if some posts are in the locale" do+ it "returns correct done and total 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)+ expect(completion).to eq({ done: 1, total: 2 })endend- context "when (scenario B) percentage determined by post localizations" do- it "returns 100% completion if all posts have a localization in the locale" do+ context "when (scenario B) 'done' determined by post localizations" do+ it "returns done = total if all posts have a localization in the locale" dolocale = "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)+ expect(completion).to eq({ done: Post.count, total: Post.count })end- it "returns X% completion if some posts have a localization in the locale" do+ it "returns correct done and total if some posts have a localization in the locale" dolocale = "es"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)+ expect(completion).to eq({ done: 1, total: Post.count })endend- it "returns the correct percentage based on (scenario A & B) `post.locale` and `PostLocalization` in the specified locale" do+ it "returns the correct done and total based on (scenario A & B) `post.locale` and `PostLocalization` in the specified locale" dolocale = "es"# translated candidates@@ -121,23 +121,23 @@ describe DiscourseAi::Translation::PostCandidates docompletion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)translated_candidates = 2 # post1 + post2total_candidates = Post.count - 1 # excluding the bot post- expect(completion).to eq(translated_candidates / total_candidates.to_f)+ expect(completion).to eq({ done: translated_candidates, total: total_candidates })end- it "does not exceed 100% completion when post.locale and post_localization both exist" do+ it "does not allow done to exceed total when post.locale and post_localization both exist" dolocale = "es"post = Fabricate(:post, locale:)Fabricate(:post_localization, post:, locale:)completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)- expect(completion).to be(1.0)+ expect(completion).to eq({ done: 1, total: 1 })end- it "returns 100% completion when no posts are present" do+ it "returns nil - nil for done and total when no posts are present" doSiteSetting.ai_translation_backfill_max_age_days = 0completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale("es")- expect(completion).to eq(1.0)+ expect(completion).to eq({ done: 0, total: 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 bc3b6bdb54a..b7bddde5603 100644--- a/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb@@ -52,28 +52,28 @@ describe DiscourseAi::Translation::TopicCandidates doenddescribe ".get_completion_per_locale" do- context "when (scenario A) percentage determined by topic's locale" do- it "returns 100% completion if all topics are in the locale" do+ context "when (scenario A) 'done' determined by topic's locale" do+ it "returns total = done if all topics are in the locale" dolocale = "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)+ expect(completion).to eq({ done: 2, total: 2 })end- it "returns X% completion if some topics are in the locale" do+ it "returns correct done and total 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)+ expect(completion).to eq({ done: 1, total: 2 })endend- context "when (scenario B) percentage determined by topic localizations" do+ context "when (scenario B) 'done' determined by topic localizations" doit "returns 100% completion if all topics have a localization in the locale" dolocale = "pt_BR"Fabricate(:topic)@@ -81,20 +81,20 @@ describe DiscourseAi::Translation::TopicCandidates doFabricate(:topic_localization, locale: "pt")completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)- expect(completion).to eq(1.0)+ expect(completion).to eq({ done: Topic.count, total: Topic.count })end- it "returns X% completion if some topics have a localization in the locale" do+ it "returns correct done and total if some topics have a localization in the locale" dolocale = "es"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)+ expect(completion).to eq({ done: 1, total: Topic.count })endend- it "returns the correct percentage based on (scenario A & B) `topic.locale` and `TopicLocalization` in the specified locale" do+ it "returns the correct done and total based on (scenario A & B) `topic.locale` and `TopicLocalization` in the specified locale" dolocale = "es"# translated candidates@@ -113,24 +113,24 @@ describe DiscourseAi::Translation::TopicCandidates docompletion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)translated_candidates = 2 # topic1 + topic2total_candidates = Topic.count - 1 # excluding the bot topic- expect(completion).to eq(translated_candidates / total_candidates.to_f)+ expect(completion).to eq({ done: 2, total: 3 })end- it "does not exceed 100% completion when topic.locale and topic_localization both exist" do+ it "does not allow done to exceed total when topic.locale and topic_localization both exist" dolocale = "es"topic = Fabricate(:topic, locale:)Fabricate(:topic_localization, topic:, locale:)completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)- expect(completion).to be(1.0)+ expect(completion).to eq({ done: 1, total: 1 })end- it "returns 100% if no topics" do+ it "returns nil - nil for done and total if no topics" doSiteSetting.ai_translation_backfill_max_age_days = 0SiteSetting.ai_translation_backfill_limit_to_public_content = falsecompletion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale("es")- expect(completion).to eq(1.0)+ expect(completion).to eq({ done: 0, total: 0 })endendend