Skip to content

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

PERF: Optimize AI translation stats query and remove caching (#35597)

Rafael dos Santos Silva3af793eaf240
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 103f389cf63..60d9c194dbc 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
@@ -23,10 +23,9 @@ module DiscourseAi
)
end
- totals = DiscourseAi::Translation::PostCandidates.get_total_and_with_locale_count
- progress = DiscourseAi::Translation::PostCandidates.get_completion_all_locales
+ data = DiscourseAi::Translation::PostCandidates.get_completion_all_locales
- render json: base_result.merge(totals.merge(translation_progress: progress))
+ render json: base_result.merge(data)
end
private
diff --git a/plugins/discourse-ai/lib/translation/post_candidates.rb b/plugins/discourse-ai/lib/translation/post_candidates.rb
index ee726753f9a..51f32b5d690 100644
--- a/plugins/discourse-ai/lib/translation/post_candidates.rb
+++ b/plugins/discourse-ai/lib/translation/post_candidates.rb
@@ -3,33 +3,12 @@
module DiscourseAi
module Translation
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
+ # Also returns aggregate counts for total eligible posts and posts with detected locale.
+ # @return [Hash] a hash with keys :translation_progress (array), :total (integer), and :posts_with_detected_locale (integer)
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))
+ completion_all_locales
end
private
@@ -65,24 +44,6 @@ module DiscourseAi
end
end
- def self.total_and_with_locale_count
- DB.query_single(<<~SQL)
- WITH eligible_posts AS (
- #{get.to_sql}
- ),
- total_count AS (
- SELECT COUNT(*) AS count FROM eligible_posts
- ),
- has_locale_count AS (
- SELECT COUNT(DISTINCT p.id)
- FROM eligible_posts p
- 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(", ")
@@ -93,8 +54,14 @@ module DiscourseAi
split_part(localestr, '_', 1) AS base
FROM (VALUES #{values_rows}) AS t(localestr)
),
+ all_eligible_posts AS (
+ #{get.to_sql}
+ ),
+ total_eligible_count AS (
+ SELECT COUNT(*)::bigint AS count FROM all_eligible_posts
+ ),
eligible_posts AS (
- #{get.where.not(posts: { locale: nil }).to_sql}
+ SELECT * FROM all_eligible_posts WHERE locale IS NOT NULL
),
all_posts_count AS (
SELECT COUNT(*)::bigint AS count FROM eligible_posts
@@ -122,30 +89,39 @@ module DiscourseAi
)
SELECT s.localestr AS locale,
COALESCE(d.done, 0) AS done,
- COALESCE(ntl.count, 0) AS total
+ COALESCE(ntl.count, 0) AS total,
+ (SELECT count FROM total_eligible_count) AS total_eligible,
+ (SELECT count FROM all_posts_count) AS posts_with_locale
FROM supported s
LEFT JOIN done_per_base d ON d.base = s.base
LEFT JOIN non_target_locale_counts ntl ON ntl.base = s.base
SQL
- results = DB.query(sql).map { |r| { locale: r.locale, done: r.done, total: r.total } }
+ results = DB.query(sql)
- results.sort_by do |r|
- percentage = r[:total] > 0 ? r[:done].to_f / r[:total] : 0
- -percentage
+ if results.empty?
+ return { translation_progress: [], total: 0, posts_with_detected_locale: 0 }
end
- end
- def self.cache_key_for_type
- "discourse_ai::translation::post_candidates"
- end
+ # Extract aggregate counts from first row (same for all rows)
+ total_eligible = results.first.total_eligible
+ posts_with_locale = results.first.posts_with_locale
- def self.get_total_cache_key
- "#{cache_key_for_type}_total"
- end
+ # Build per-locale progress array
+ translation_progress =
+ results.map { |r| { locale: r.locale, done: r.done, total: r.total } }
+
+ translation_progress =
+ translation_progress.sort_by do |r|
+ percentage = r[:total] > 0 ? r[:done].to_f / r[:total] : 0
+ -percentage
+ end
- def self.get_completion_cache_key
- "#{cache_key_for_type}_completion_all_locales"
+ {
+ translation_progress: translation_progress,
+ total: total_eligible,
+ posts_with_detected_locale: posts_with_locale,
+ }
end
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 de883cb5d90..11207b77984 100644
--- a/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb
@@ -70,15 +70,18 @@ describe DiscourseAi::Translation::PostCandidates do
Post.delete_all
result = DiscourseAi::Translation::PostCandidates.get_completion_all_locales
- expect(result.length).to eq(3)
- expect(result).to all(include(done: 0, total: 0))
+ expect(result).to be_a(Hash)
+ expect(result[:translation_progress].length).to eq(3)
+ expect(result[:translation_progress]).to all(include(done: 0, total: 0))
+ expect(result[:total]).to eq(0)
+ expect(result[:posts_with_detected_locale]).to eq(0)
end
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")
post3 = Fabricate(:post, locale: "es")
- Fabricate(:post, locale: nil) # not eligible
+ post_without_locale = Fabricate(:post, locale: nil) # not eligible for translation
# add an en_GB localization to a non-en base post
PostLocalization.create!(
@@ -91,27 +94,31 @@ describe DiscourseAi::Translation::PostCandidates do
)
result = DiscourseAi::Translation::PostCandidates.completion_all_locales
- expect(result.length).to eq(3)
+ expect(result).to be_a(Hash)
+ expect(result[:translation_progress].length).to eq(3)
+ expect(result[:total]).to eq(4) # all eligible posts (including one without locale)
+ expect(result[:posts_with_detected_locale]).to eq(3) # only posts with locale
- expect(result).to all(include(:locale, :done, :total))
+ progress = result[:translation_progress]
+ expect(progress).to all(include(:locale, :done, :total))
- expect(result.first[:locale]).to eq("en_GB")
+ expect(progress.first[:locale]).to eq("en_GB")
- en_entry = result.find { |r| r[:locale] == "en_GB" }
+ en_entry = progress.find { |r| r[:locale] == "en_GB" }
expect(en_entry).to be_present
# total is non-English posts (post2 + post3)
expect(en_entry[:done]).to eq(1)
expect(en_entry[:total]).to eq(2)
- pt_entry = result.find { |r| r[:locale] == "pt" }
+ pt_entry = progress.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" }
+ es_entry = progress.find { |r| r[:locale] == "es" }
expect(es_entry).to be_present
expect(es_entry[:done]).to eq(0)
expect(es_entry[:total]).to eq(2)
- fr_entry = result.find { |r| r[:locale] == "fr" }
+ fr_entry = progress.find { |r| r[:locale] == "fr" }
expect(fr_entry).to be_nil
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 d2074554e18..64fc94e566b 100644
--- a/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb
+++ b/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb
@@ -12,11 +12,15 @@ RSpec.describe "Admin AI translations", type: :system do
allow(DiscourseAi::Translation::PostCandidates).to receive(
:get_completion_all_locales,
).and_return(
- [
- { done: 50, locale: "en", total: 100 },
- { done: 50, locale: "fr", total: 100 },
- { done: 50, locale: "es", total: 100 },
- ],
+ {
+ translation_progress: [
+ { done: 50, locale: "en", total: 100 },
+ { done: 50, locale: "fr", total: 100 },
+ { done: 50, locale: "es", total: 100 },
+ ],
+ total: 300,
+ posts_with_detected_locale: 150,
+ },
)
sign_in(admin)
@@ -124,7 +128,16 @@ RSpec.describe "Admin AI translations", type: :system do
allow(DiscourseAi::Translation::PostCandidates).to receive(
:get_completion_all_locales,
- ).and_return([{ done: 50, locale: "en", total: 100 }, { done: 50, locale: "fr", total: 100 }])
+ ).and_return(
+ {
+ translation_progress: [
+ { done: 50, locale: "en", total: 100 },
+ { done: 50, locale: "fr", total: 100 },
+ ],
+ total: 200,
+ posts_with_detected_locale: 100,
+ },
+ )
end
it "displays the translation toggle" do