discourse/plugins/discourse-ai/lib/translation/base_candidates.rb
Rafael dos Santos Silva a40149bd1b
PERF: optimize translation progress dashboard with batch query (#34861)
Replace individual per-locale queries with a single query that
calculates completion progress for all locales at once.

This commit also removes methods off the `base_candidate` (used for
Categories and Topics as well) which are not used any more, as the
progress dashboard only shows progress for posts.

t/162846/4

---------

Co-authored-by: Nat <natalie.tay@discourse.org>
2025-09-29 13:32:40 +08:00

35 lines
874 B
Ruby
Vendored

# frozen_string_literal: true
module DiscourseAi
module Translation
class BaseCandidates
CACHE_TTL = 1.hour
private
# 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
def self.total_and_with_locale_count
DB.query_single(<<~SQL)
WITH eligible AS (
#{get.to_sql}
),
total_count AS (
SELECT COUNT(*) AS count FROM eligible
),
done_count AS (
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
SQL
end
end
end
end