Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/jobs/scheduled/automatic_translation_backfill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ def execute(args = nil)
end
end

def fetch_untranslated_model_ids(model = Post, limit = 100, target_locales = backfill_locales)
def fetch_untranslated_model_ids(
model,
content_column,
limit,
target_locales = backfill_locales
)
m = model.name.downcase
DB.query_single(<<~SQL, target_locales: target_locales, limit: limit)
SELECT m.id
Expand All @@ -39,6 +44,8 @@ def fetch_untranslated_model_ids(model = Post, limit = 100, target_locales = bac
ARRAY[]::text[]
))
)
AND m.deleted_at IS NULL
AND m.#{content_column} != ''
ORDER BY m.id DESC
LIMIT :limit
SQL
Expand Down Expand Up @@ -92,9 +99,9 @@ def translate_records(type, record_ids)
def process_batch
models_translated = [Post, Topic].size
translations_per_model = [translations_per_run / models_translated, 1].max
topic_ids = fetch_untranslated_model_ids(Topic, translations_per_model)
topic_ids = fetch_untranslated_model_ids(Topic, "title", translations_per_model)
translations_per_model = translations_per_run - topic_ids.size
post_ids = fetch_untranslated_model_ids(Post, translations_per_model)
post_ids = fetch_untranslated_model_ids(Post, "cooked", translations_per_model)
return if topic_ids.empty? && post_ids.empty?

translate_records(Topic, topic_ids)
Expand Down
15 changes: 14 additions & 1 deletion spec/jobs/automatic_translation_backfill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,21 @@ def expect_google_translate(text)
end

it "returns correct post ids needing translation in descending id" do
result = described_class.new.fetch_untranslated_model_ids(Post, 50, %w[de es])
result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es])
expect(result).to include(post_7.id, post_6.id, post_3.id, post_2.id, post_1.id)
end

it "does not return posts that are deleted" do
post_1.trash!
result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es])
expect(result).not_to include(post_1.id)
end

it "does not return posts that are empty" do
post_1.cooked = ""
post_1.save!(validate: false)
result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es])
expect(result).not_to include(post_1.id)
end
end
end
Loading