Skip to content

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

PERF: optimize translation progress dashboard with batch query (#34861)

Rafael dos Santos Silvaa40149bd1bff
diff --git a/app/models/post.rb b/app/models/post.rb
index 9314325ca7b..c990302bf1a 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -1436,6 +1436,7 @@ end
# index_posts_on_id_topic_id_where_not_deleted_or_empty (id,topic_id) WHERE ((deleted_at IS NULL) AND (raw <> ''::text))
# index_posts_on_image_upload_id (image_upload_id)
# index_posts_on_last_editor_id (last_editor_id) WHERE (last_editor_id IS NOT NULL)
+# index_posts_on_locale (locale)
# index_posts_on_locked_by_id (locked_by_id) WHERE (locked_by_id IS NOT NULL)
# index_posts_on_reply_to_user_id (reply_to_user_id) WHERE (reply_to_user_id IS NOT NULL)
# index_posts_on_topic_id_and_created_at (topic_id,created_at)
diff --git a/db/migrate/20250925182715_add_index_to_posts_locale.rb b/db/migrate/20250925182715_add_index_to_posts_locale.rb
new file mode 100644
index 00000000000..4a1b190cb07
--- /dev/null
+++ b/db/migrate/20250925182715_add_index_to_posts_locale.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+class AddIndexToPostsLocale < ActiveRecord::Migration[8.0]
+ disable_ddl_transaction!
+
+ def up
+ remove_index :posts, :locale, algorithm: :concurrently, if_exists: true
+ add_index :posts, :locale, algorithm: :concurrently
+ end
+
+ def down
+ remove_index :posts, :locale, algorithm: :concurrently, if_exists: true
+ end
+end
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb
index 9de12b813de..a2b3f091579 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb
@@ -19,18 +19,15 @@ module DiscourseAi
end
candidates = DiscourseAi::Translation::PostCandidates
-
- result =
- supported_locales.map do |locale|
- candidates.get_completion_per_locale(locale) in { total:, done: }
- { locale:, total:, done: }
- end
-
candidates.get_total_and_with_locale_count in { total:, posts_with_detected_locale: }
render json:
base_result.merge(
- { translation_progress: result, total:, posts_with_detected_locale: },
+ {
+ translation_progress: candidates.get_completion_all_locales,
+ total:,
+ posts_with_detected_locale:,
+ },
)
end
diff --git a/plugins/discourse-ai/lib/translation/base_candidates.rb b/plugins/discourse-ai/lib/translation/base_candidates.rb
index e6461c14c47..7cd34a0545d 100644
--- a/plugins/discourse-ai/lib/translation/base_candidates.rb
+++ b/plugins/discourse-ai/lib/translation/base_candidates.rb
@@ -5,37 +5,6 @@ module DiscourseAi
class BaseCandidates
CACHE_TTL = 1.hour
- # Returns the total number of candidates and the number of candidates that have a detected locale.
- # The values are cached and provides an overview of how many posts are eligible for translation and how many have been detected with a locale.
- # @return [Hash] a hash with keys :total and :posts_with_detected_locale
- def self.get_total_and_with_locale_count
- Discourse
- .cache
- .fetch(get_total_cache_key, expires_in: CACHE_TTL) do
- total, with_locale = total_and_with_locale_count
- return { total: 0, posts_with_detected_locale: 0 } if total.zero?
- { total:, posts_with_detected_locale: with_locale }
- end
- end
-
- # Returns the number of posts that have been translated, and the total number of posts that need translation for a given locale.
- # The total number of posts is based off candidates that already have a locale.
- # @param locale [String] the locale for which to calculate the completion percentage
- # @return [Hash] a hash with keys :done and :total
- def self.get_completion_per_locale(locale)
- Discourse
- .cache
- .fetch(get_completion_cache_key(locale), expires_in: CACHE_TTL) do
- done, total = calculate_completion_per_locale(locale)
- return { done: 0, total: 0 } if total.zero?
- { done:, total: }
- end
- end
-
- def self.clear_completion_cache(locale)
- Discourse.cache.delete(get_completion_cache_key(locale))
- end
-
private
# ModelType that are eligible for translation based on site settings
@@ -44,43 +13,18 @@ module DiscourseAi
raise NotImplementedError
end
- # 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] the number of done and total translations for the given locale
- def self.calculate_completion_per_locale(locale)
- raise NotImplementedError
- end
-
- def self.completion_cache_key_for_type
- raise NotImplementedError
- end
-
- def self.total_cache_key_for_type
- raise NotImplementedError
- end
-
- def self.get_completion_cache_key(locale)
- "#{cache_key_for_type}_completion_#{locale}"
- end
-
- def self.get_total_cache_key
- "#{cache_key_for_type}_total"
- end
-
def self.total_and_with_locale_count
DB.query_single(<<~SQL)
- WITH eligible_posts AS (
+ WITH eligible AS (
#{get.to_sql}
),
total_count AS (
- SELECT COUNT(*) AS count FROM eligible_posts
+ SELECT COUNT(*) AS count FROM eligible
),
done_count AS (
- SELECT COUNT(DISTINCT p.id)
- FROM eligible_posts p
- WHERE p.locale IS NOT NULL
+ SELECT COUNT(DISTINCT e.id)
+ FROM eligible e
+ WHERE e.locale IS NOT NULL
)
SELECT t.count AS total, d.count AS done
FROM total_count t, done_count d
diff --git a/plugins/discourse-ai/lib/translation/category_candidates.rb b/plugins/discourse-ai/lib/translation/category_candidates.rb
index 03d50aad82b..b403de04c65 100644
--- a/plugins/discourse-ai/lib/translation/category_candidates.rb
+++ b/plugins/discourse-ai/lib/translation/category_candidates.rb
@@ -34,11 +34,8 @@ module DiscourseAi
FROM total_count t, done_count d
SQL
- DB.query_single(sql, base_locale:)
- end
-
- def self.cache_key_for_type
- "discourse_ai::translation::category_candidates"
+ done, total = DB.query_single(sql, base_locale:)
+ { done:, total: }
end
end
end
diff --git a/plugins/discourse-ai/lib/translation/post_candidates.rb b/plugins/discourse-ai/lib/translation/post_candidates.rb
index 88fe00dd536..d2f62425304 100644
--- a/plugins/discourse-ai/lib/translation/post_candidates.rb
+++ b/plugins/discourse-ai/lib/translation/post_candidates.rb
@@ -2,7 +2,36 @@
module DiscourseAi
module Translation
- class PostCandidates < BaseCandidates
+ class PostCandidates
+ CACHE_TTL = 1.hour
+
+ # Returns the total number of candidates and the number of candidates that have a detected locale.
+ # The values are cached and provides an overview of how many posts are eligible for translation and how many have been detected with a locale.
+ # @return [Hash] a hash with keys :total and :posts_with_detected_locale
+ def self.get_total_and_with_locale_count
+ Discourse
+ .cache
+ .fetch(get_total_cache_key, expires_in: CACHE_TTL) do
+ total, with_locale = total_and_with_locale_count
+ return { total: 0, posts_with_detected_locale: 0 } if total.zero?
+ { total:, posts_with_detected_locale: with_locale }
+ end
+ end
+
+ # Returns the number of posts that have been translated, and the total number of posts that need translation for a given locale.
+ # The total number of posts is based off candidates that already have a locale.
+ # @param locale [String] the locale for which to calculate the completion percentage
+ # @return [Hash] a hash with keys :done and :total
+ def self.get_completion_all_locales
+ Discourse
+ .cache
+ .fetch(get_completion_cache_key, expires_in: CACHE_TTL) { completion_all_locales }
+ end
+
+ def self.clear_completion_cache(locale)
+ Discourse.cache.delete(get_completion_cache_key(locale))
+ end
+
private
# all posts that are eligible for translation based on site settings,
@@ -36,32 +65,75 @@ module DiscourseAi
end
end
- def self.calculate_completion_per_locale(locale)
- base_locale = "#{locale.split("_").first}%"
-
- sql = <<~SQL
+ def self.total_and_with_locale_count
+ DB.query_single(<<~SQL)
WITH eligible_posts AS (
- #{get.where("posts.locale IS NOT NULL").to_sql}
+ #{get.to_sql}
),
total_count AS (
SELECT COUNT(*) AS count FROM eligible_posts
),
- done_count AS (
+ has_locale_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
+ WHERE p.locale IS NOT NULL
+ )
+ SELECT t.count AS total, d.count AS done
+ FROM total_count t, has_locale_count d
+ SQL
+ end
+
+ def self.completion_all_locales
+ supported = SiteSetting.content_localization_supported_locales.split("|")
+ values_rows = supported.map { |loc| "('#{loc}')" }.join(", ")
+
+ sql = <<~SQL
+ WITH supported AS (
+ SELECT localestr, split_part(localestr, '_', 1) AS base
+ FROM (VALUES #{values_rows}) AS t(localestr)
+ ),
+ eligible_posts AS (
+ #{get.where("posts.locale IS NOT NULL").to_sql}
+ ),
+ total_count AS (
+ SELECT COUNT(*)::bigint AS count FROM eligible_posts
+ ),
+ done_per_base AS (
+ SELECT s.base,
+ COUNT(*)::bigint AS done
+ FROM eligible_posts p
+ JOIN supported s ON TRUE
+ WHERE split_part(p.locale, '_', 1) = s.base
+ OR EXISTS (
+ SELECT 1
+ FROM post_localizations pl
+ WHERE pl.post_id = p.id
+ AND split_part(pl.locale, '_', 1) = s.base
+ )
+ GROUP BY s.base
)
- SELECT d.count AS done, t.count AS total
- FROM total_count t, done_count d
+ SELECT s.localestr AS locale,
+ COALESCE(d.done, 0) AS done,
+ t.count AS total
+ FROM supported s
+ LEFT JOIN done_per_base d ON d.base = s.base
+ CROSS JOIN total_count t
SQL
- DB.query_single(sql, base_locale:)
+ DB.query(sql).map { |r| { locale: r.locale, done: r.done, total: r.total } }
end
def self.cache_key_for_type
"discourse_ai::translation::post_candidates"
end
+
+ def self.get_total_cache_key
+ "#{cache_key_for_type}_total"
+ end
+
+ def self.get_completion_cache_key
+ "#{cache_key_for_type}_completion_all_locales"
+ end
end
end
end
diff --git a/plugins/discourse-ai/lib/translation/topic_candidates.rb b/plugins/discourse-ai/lib/translation/topic_candidates.rb
index 68a68868d41..967055f831f 100644
--- a/plugins/discourse-ai/lib/translation/topic_candidates.rb
+++ b/plugins/discourse-ai/lib/translation/topic_candidates.rb
@@ -54,11 +54,8 @@ module DiscourseAi
FROM total_count t, done_count d
SQL
- DB.query_single(sql, base_locale:)
- end
-
- def self.cache_key_for_type
- "discourse_ai::translation::topic_candidates"
+ done, total = DB.query_single(sql, base_locale:)
+ { done:, total: }
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb
index edd92ff11f1..e48296e30d9 100644
--- a/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb
@@ -17,7 +17,7 @@ describe DiscourseAi::Translation::CategoryCandidates do
end
end
- describe ".get_completion_per_locale" do
+ describe ".calculate_completion_per_locale" do
context "when (scenario A) completion determined by category's locale" do
it "returns done = total if all categories are in the locale" do
locale = "pt_BR"
@@ -25,7 +25,8 @@ describe DiscourseAi::Translation::CategoryCandidates do
Category.update_all(locale: locale)
Fabricate(:category, locale: "pt") # pt counts as pt_BR
- completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::CategoryCandidates.calculate_completion_per_locale(locale)
expect(completion).to eq({ done: Category.count, total: Category.count })
end
@@ -36,7 +37,8 @@ describe DiscourseAi::Translation::CategoryCandidates do
Fabricate(:category, locale:)
Fabricate(:category, locale: "ar") # not portuguese
- completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::CategoryCandidates.calculate_completion_per_locale(locale)
expect(completion).to eq({ done: 1, total: Category.count })
end
end
@@ -52,7 +54,8 @@ describe DiscourseAi::Translation::CategoryCandidates do
end
CategoryLocalization.order("RANDOM()").first.update(locale: "pt") # pt counts as pt_BR
- completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::CategoryCandidates.calculate_completion_per_locale(locale)
expect(completion).to eq({ done: Category.count, total: Category.count })
end
@@ -63,7 +66,8 @@ describe DiscourseAi::Translation::CategoryCandidates do
Fabricate(:category_localization, category: category1, locale:)
Fabricate(:category_localization, category: category2, locale: "ar") # not the target locale
- completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::CategoryCandidates.calculate_completion_per_locale(locale)
categories_with_locale = Category.where.not(locale: nil).count
expect(completion).to eq({ done: 1, total: categories_with_locale })
end
@@ -88,7 +92,8 @@ describe DiscourseAi::Translation::CategoryCandidates do
category4 = Fabricate(:category, read_restricted: true, locale: "de")
Fabricate(:category_localization, category: category4, locale:)
- completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::CategoryCandidates.calculate_completion_per_locale(locale)
translated_candidates = 2 # category1 + category2
total_candidates = Category.count - 1 # excluding the read restricted category
expect(completion).to eq({ done: translated_candidates, total: total_candidates })
@@ -100,7 +105,8 @@ describe DiscourseAi::Translation::CategoryCandidates do
category = Fabricate(:category, locale:)
Fabricate(:category_localization, category:, locale:)
- completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::CategoryCandidates.calculate_completion_per_locale(locale)
expect(completion).to eq({ done: Category.count, total: Category.count })
end
@@ -108,7 +114,8 @@ describe DiscourseAi::Translation::CategoryCandidates do
SiteSetting.ai_translation_backfill_limit_to_public_content = false
Category.destroy_all
- completion = DiscourseAi::Translation::CategoryCandidates.get_completion_per_locale("pt")
+ completion =
+ DiscourseAi::Translation::CategoryCandidates.calculate_completion_per_locale("pt")
expect(completion).to eq({ done: 0, total: 0 })
end
end
diff --git a/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb
index 906a5ad7622..06996767410 100644
--- a/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb
@@ -59,91 +59,52 @@ describe DiscourseAi::Translation::PostCandidates do
end
end
- describe ".get_completion_per_locale" do
- context "when (scenario A) 'done' determined by post's locale" do
- it "returns total = done if all posts are in the locale" do
- 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({ done: 2, total: 2 })
- end
-
- it "returns correct done and total if some posts are in the locale" do
- locale = "es"
- Fabricate(:post, locale:)
- Fabricate(:post, locale: "not_es")
-
- completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)
- expect(completion).to eq({ done: 1, total: 2 })
- end
- end
-
- context "when (scenario B) 'done' determined by post localizations" do
- it "returns done = total if all posts have a localization in the locale" do
- locale = "pt_BR"
- Fabricate(:post, locale: "en")
- Post.all.each do |post|
- post.update(locale: "en")
- Fabricate(:post_localization, post:, locale:)
- end
- PostLocalization.order("RANDOM()").first.update(locale: "pt")
-
- completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)
- expect(completion).to eq({ done: Post.count, total: Post.count })
- end
-
- it "returns correct done and total if some posts have a localization in the locale" do
- locale = "es"
- post1 = Fabricate(:post, locale: "en")
- post2 = Fabricate(:post, locale: "fr")
- Fabricate(:post_localization, post: post1, locale:)
- Fabricate(:post_localization, post: post2, locale: "not_es")
-
- completion = DiscourseAi::Translation::PostCandidates.get_completion_per_locale(locale)
- posts_with_locale = Post.where.not(locale: nil).count
- expect(completion).to eq({ done: 1, total: posts_with_locale })
- end
+ describe ".get_completion_all_locales" do
+ before { SiteSetting.content_localization_supported_locales = "en_GB|pt|es" }
+
+ it "returns empty state when no posts exist" do
+ Post.delete_all
+
+ result = DiscourseAi::Translation::PostCandidates.get_completion_all_locales
+ expect(result).to eq(
+ [
+ { done: 0, locale: "en_GB", total: 0 },
+ { done: 0, locale: "pt", total: 0 },
+ { done: 0, locale: "es", total: 0 },
+ ],
+ )
end
- it "returns the correct done and total based on (scenario A & B) `post.locale` and `PostLocalization` in the specified locale" do
- locale = "es"
-
- # translated candidates
- Fabricate(:post, locale:)
- post2 = Fabricate(:post, locale: "en")
- Fabricate(:post_localization, post: post2, locale:)
-
- # untranslated candidate
- post4 = Fabricate(:post, locale: "fr")
- Fabricate(:post_localization, post: post4, locale: "zh_CN")
-
- # not a candidate as it is a bot post
- post3 = Fabricate(:post, user: Discourse.system_user, locale: "de")
- 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({ done: translated_candidates, total: total_candidates })
- end
-
- it "does not allow done to exceed total 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({ done: 1, total: 1 })
- end
-
- it "returns nil - nil for done and total 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({ done: 0, total: 0 })
+ it "returns progress grouped by base locale (of en_GB) and correct totals" do
+ post1 = Fabricate(:post, locale: "en_GB")
+ post2 = Fabricate(:post, locale: "fr")
+ Fabricate(:post, locale: "es")
+ Fabricate(:post, locale: nil) # not eligible
+
+ # add an en_GB localization to a non-en base post
+ Fabricate(:post_localization, post: post2, locale: "en")
+
+ result = DiscourseAi::Translation::PostCandidates.completion_all_locales
+ expect(result.length).to eq(3)
+
+ expect(result).to all(include(:locale, :done, :total))
+
+ en_entry = result.find { |r| r[:locale] == "en_GB" }
+ expect(en_entry).to be_present
+ # post1 (en_GB base=en) + post2 (localization en_GB base=en)
+ expect(en_entry[:done]).to eq(2)
+ expect(en_entry[:total]).to eq(3)
+
+ pt_entry = result.find { |r| r[:locale] == "pt" }
+ expect(pt_entry).to be_present
+ expect(pt_entry[:done]).to eq(0)
+ expect(pt_entry[:total]).to eq(3)
+ es_entry = result.find { |r| r[:locale] == "es" }
+ expect(es_entry).to be_present
+ expect(es_entry[:done]).to eq(1)
+ expect(es_entry[:total]).to eq(3)
+ fr_entry = result.find { |r| r[:locale] == "fr" }
+ expect(fr_entry).to be_nil
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb
index b47dc5f629f..a0ecce1df51 100644
--- a/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb
@@ -51,7 +51,7 @@ describe DiscourseAi::Translation::TopicCandidates do
end
end
- describe ".get_completion_per_locale" do
+ describe ".calculate_completion_per_locale" do
context "when (scenario A) 'done' determined by topic's locale" do
it "returns total = done if all topics are in the locale" do
locale = "pt_BR"
@@ -59,7 +59,8 @@ describe DiscourseAi::Translation::TopicCandidates do
Topic.update_all(locale: locale)
Fabricate(:topic, locale: "pt")
- completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)
expect(completion).to eq({ done: 2, total: 2 })
end
@@ -68,7 +69,8 @@ describe DiscourseAi::Translation::TopicCandidates do
Fabricate(:topic, locale:)
Fabricate(:topic, locale: "not_es")
- completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)
expect(completion).to eq({ done: 1, total: 2 })
end
end
@@ -83,7 +85,8 @@ describe DiscourseAi::Translation::TopicCandidates do
end
TopicLocalization.order("RANDOM()").first.update(locale: "pt")
- completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)
expect(completion).to eq({ done: Topic.count, total: Topic.count })
end
@@ -94,7 +97,8 @@ describe DiscourseAi::Translation::TopicCandidates do
Fabricate(:topic_localization, topic: topic1, locale:)
Fabricate(:topic_localization, topic: topic2, locale: "not_es")
- completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)
+ completion =
+ DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)
topics_with_locale = Topic.where.not(locale: nil).count
expect(completion).to eq({ done: 1, total: topics_with_locale })
end
@@ -116,7 +120,7 @@ describe DiscourseAi::Translation::TopicCandidates do
topic3 = Fabricate(:topic, user: Discourse.system_user, locale: "de")
Fabricate(:topic_localization, topic: topic3, locale:)
- completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)
+ completion = DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)
translated_candidates = 2 # topic1 + topic2
total_candidates = Topic.count - 1 # excluding the bot topic
expect(completion).to eq({ done: translated_candidates, total: total_candidates })
@@ -127,14 +131,14 @@ describe DiscourseAi::Translation::TopicCandidates do
topic = Fabricate(:topic, locale:)
Fabricate(:topic_localization, topic:, locale:)
- completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale(locale)
+ completion = DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)
expect(completion).to eq({ done: 1, total: 1 })
end
it "returns nil - nil for done and total when no topics are present" do
SiteSetting.ai_translation_backfill_max_age_days = 0
- completion = DiscourseAi::Translation::TopicCandidates.get_completion_per_locale("es")
+ completion = DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale("es")
expect(completion).to eq({ done: 0, total: 0 })
end
end
diff --git a/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb b/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb
index b488a1082ef..ca00d9ad079 100644
--- a/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb
+++ b/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb
@@ -10,8 +10,14 @@ RSpec.describe "Admin AI translations", type: :system do
allow(DiscourseAi::Translation).to receive(:has_llm_model?).and_return(true)
allow(DiscourseAi::Translation::PostCandidates).to receive(
- :get_completion_per_locale,
- ).and_return({ total: 100, done: 50 })
+ :get_completion_all_locales,
+ ).and_return(
+ [
+ { done: 50, locale: "en", total: 100 },
+ { done: 50, locale: "fr", total: 100 },
+ { done: 50, locale: "es", total: 100 },
+ ],
+ )
sign_in(admin)
end