Skip to content

Commit 6c2c08c

Browse files
authored
DEV: Change backfill rate handling (#256)
1 parent 0c71b6e commit 6c2c08c

16 files changed

+58
-66
lines changed

app/jobs/scheduled/automatic_translation_backfill.rb

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,41 @@ def execute(args = nil)
1414

1515
def fetch_untranslated_model_ids(model, content_column, limit, target_locale)
1616
m = model.name.downcase
17+
18+
# Query selects every model (post/topic) *except* those who are **both**
19+
# already locale detected and translated
1720
DB.query_single(<<~SQL, target_locale: target_locale, limit: limit)
18-
SELECT m.id
19-
FROM #{model.table_name} m
20-
#{limit_to_public_clause(model)}
21-
WHERE m.deleted_at IS NULL
22-
AND m.#{content_column} != ''
23-
AND m.user_id > 0
24-
#{max_age_clause}
25-
AND (
26-
NOT EXISTS (
27-
SELECT 1
28-
FROM discourse_translator_#{m}_locales
29-
WHERE #{m}_id = m.id
21+
SELECT * FROM
22+
(
23+
( -- every post / topic
24+
SELECT m.id
25+
FROM #{model.table_name} m
26+
#{limit_to_public_clause(model)}
27+
WHERE m.deleted_at IS NULL
28+
AND m.#{content_column} != ''
29+
AND m.user_id > 0
30+
#{max_age_clause}
31+
ORDER BY m.updated_at DESC
32+
)
33+
EXCEPT
34+
(
35+
( -- locale detected
36+
SELECT
37+
#{m}_id
38+
FROM
39+
discourse_translator_#{m}_locales
40+
WHERE
41+
detected_locale = :target_locale
3042
)
31-
OR EXISTS (
32-
SELECT 1
33-
FROM discourse_translator_#{m}_locales
34-
WHERE #{m}_id = m.id
35-
AND detected_locale != :target_locale
43+
INTERSECT
44+
( -- translated
45+
SELECT #{m}_id
46+
FROM discourse_translator_#{m}_translations
47+
WHERE
48+
locale = :target_locale
3649
)
3750
)
38-
AND NOT EXISTS (
39-
SELECT 1
40-
FROM discourse_translator_#{m}_translations
41-
WHERE #{m}_id = m.id
42-
AND locale = :target_locale
43-
)
44-
ORDER BY m.updated_at DESC
51+
) AS subquery
4552
LIMIT :limit
4653
SQL
4754
end
@@ -50,14 +57,10 @@ def fetch_untranslated_model_ids(model, content_column, limit, target_locale)
5057

5158
def should_backfill?
5259
return false if SiteSetting.automatic_translation_target_languages.blank?
53-
return false if SiteSetting.automatic_translation_backfill_maximum_translations_per_hour == 0
60+
return false if SiteSetting.automatic_translation_backfill_rate == 0
5461
true
5562
end
5663

57-
def translations_per_run
58-
[(SiteSetting.automatic_translation_backfill_maximum_translations_per_hour / 12), 1].max
59-
end
60-
6164
def backfill_locales
6265
@backfill_locales ||=
6366
SiteSetting.automatic_translation_target_languages.split("|").map { |l| l.gsub("_", "-") }
@@ -83,23 +86,12 @@ def translate_records(type, record_ids, target_locale)
8386
end
8487

8588
def process_batch
86-
models_translated = [Post, Topic].size
87-
avg_translations_per_model_per_language = [
88-
translations_per_run / models_translated / backfill_locales.size,
89-
1,
90-
].max
91-
92-
records_to_translate = avg_translations_per_model_per_language
89+
records_to_translate = SiteSetting.automatic_translation_backfill_rate
9390
backfill_locales.each_with_index do |target_locale, i|
9491
topic_ids =
9592
fetch_untranslated_model_ids(Topic, "title", records_to_translate, target_locale)
9693
post_ids = fetch_untranslated_model_ids(Post, "raw", records_to_translate, target_locale)
9794

98-
# if we end up translating fewer records than records_to_translate,
99-
# add to the value so that the next locales can have more quota
100-
records_to_translate =
101-
avg_translations_per_model_per_language +
102-
((records_to_translate - topic_ids.size - post_ids.size) / backfill_locales.size - i)
10395
next if topic_ids.empty? && post_ids.empty?
10496

10597
DiscourseTranslator::VerboseLogger.log(

config/locales/server.de.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ de:
2525
restrict_translation_by_poster_group: "Erlaube nur die Übersetzung von Beiträgen von Benutzern aus zulässigen Gruppen. Wenn das Feld leer ist, wird die Übersetzung von Beiträgen aller Benutzer erlaubt."
2626
experimental_anon_language_switcher: "Aktiviere die experimentelle Sprachumschaltung. Damit können nicht eingeloggte Besucher zwischen übersetzten Versionen von Discourse und von Benutzern beigetragenen Inhalten in Themen wechseln."
2727
errors:
28-
needs_nonzero_backfill: "Für die automatische Sprachübersetzung muss die versteckte Einstellung 'automatic_translation_backfill_maximum_translations_per_hour' ungleich Null sein. Bitte wende dich an deinen Website-Administrator, um dieses Limit zu erhöhen."
28+
needs_nonzero_backfill: "Für die automatische Sprachübersetzung muss die versteckte Einstellung 'automatic_translation_backfill_rate' ungleich Null sein. Bitte wende dich an deinen Website-Administrator, um dieses Limit zu erhöhen."
2929
experimental_anon_language_switcher_requirements: "Die experimentelle Sprachumschaltung setzt voraus, dass die Website-Einstellung `set locale from cookie` aktiviert ist und `automatic translation target languages` mindestens eine Sprache enthält."
3030
experimental_inline_translation: "Aktiviere die experimentelle Übersetzungsfunktion mit Überschreiben. Diese ersetzt die bisherige parallele Übersetzung und ermöglicht es Besuchern der Website, die nicht das Standardgebietsschema verwenden, Inhalte direkt in ihrer Sprache zu sehen."
3131
automatic_translation_target_languages: "Die Sprachen, in die Benutzerinhalte (Beiträge, Themen) automatisch übersetzt werden sollen. Wenn nichts ausgewählt ist, wird in keine Sprachen automatisch übersetzt."

config/locales/server.en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ en:
1919
restrict_translation_by_poster_group: "Only allow translation of posts made by users in allowed groups. If empty, allow translations of posts from all users."
2020
experimental_anon_language_switcher: "Enable experimental language switcher. This will allow site visitors who are not logged in to switch between translated versions of Discourse and user-contributed content in topics."
2121
errors:
22-
needs_nonzero_backfill: "Automatic language translation requires the 'automatic_translation_backfill_maximum_translations_per_hour' hidden setting to be a non-zero value. Please approach your site administrator to increase this limit."
22+
needs_nonzero_backfill: "Automatic language translation requires the 'automatic_translation_backfill_rate' hidden setting to be a non-zero value. Please approach your site administrator to increase this limit."
2323
experimental_anon_language_switcher_requirements: "The experimental language switcher requires the `set locale from cookie` site setting to be enabled, and the `automatic translation target languages` to have at least one language."
2424
experimental_inline_translation: "Enable experimental inline translation feature. This replaces existing parallel translation, allowing site visitors with a non-default locale to view content in their language."
2525
automatic_translation_target_languages: "The languages to automatically translate user content (posts, topics) to. If empty, no languages will be automatically translated."

config/locales/server.he.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ he:
2525
restrict_translation_by_poster_group: "לאפשר תרגום של פוסטים שנוצרו על ידי משתמשים בקבוצות מורשות בלבד. אם ריק, לאפשר תרגומים של פוסטים מכל המשתמשים."
2626
experimental_anon_language_switcher: "הפעלת בורר שפות ניסיוני. כך יוכלו מבקרי האתר שלא נכנסו לחשבונות המשתמש שלהם לעבור בין הגרסאות המתורגמות של Discourse ותוכן שנתרם על ידי המשתמשים בנושאים."
2727
errors:
28-
needs_nonzero_backfill: "תרגום שפה אוטומטי דורש שהערך של ההגדרה הנסתרת ‚automatic_translation_backfill_maximum_translations_per_hour’ יהיה שונה מאפס. נא לגשת להנהלת האתר שלך כדי להגדיל את המגבלה הזאת."
28+
needs_nonzero_backfill: "תרגום שפה אוטומטי דורש שהערך של ההגדרה הנסתרת ‚automatic_translation_backfill_rate’ יהיה שונה מאפס. נא לגשת להנהלת האתר שלך כדי להגדיל את המגבלה הזאת."
2929
experimental_anon_language_switcher_requirements: "בורר השפה הניסיוני דורש את הפעלת הגדרת האתר `set locale from cookie` (הגדרת השפה מהעוגיות), ושתחת `automatic translation target languages` (שפות יעד לתרגום אוטומטי) תהיה לפחות שפה אחת."
3030
experimental_inline_translation: "הפעלת יכולת ניסיונית לתרגום פנימי. מחליפה תרגום מקבילי קיים, מה שמאפשר למבקרי האתר עם שפה ששונה מברירת המחדל לצפות בתוכן בשפה שלהם."
3131
automatic_translation_target_languages: "השפה אליה לתרגם את תוכן המשתמשים אוטומטית (פוסטים, נושאים). אם השדה ריק, אף שפה לא תתורגם אוטומטית."

config/locales/server.ro.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ro:
2424
restrict_translation_by_group: "Doar grupurile permise pot traduce"
2525
restrict_translation_by_poster_group: "Permite traducerea numai a articolelor făcute de utilizatorii din grupurile permise. Dacă este gol, permite traducerea articolelor de la toți utilizatorii."
2626
errors:
27-
needs_nonzero_backfill: "Traducerea automată a limbii necesită ca setarea ascunsă „automatic_translation_backfill_maximum_translations_per_hour” să aibă o valoare diferită de zero. Te rugăm să te adresezi administratorului site-ului tău pentru a crește această limită."
27+
needs_nonzero_backfill: "Traducerea automată a limbii necesită ca setarea ascunsă „automatic_translation_backfill_rate” să aibă o valoare diferită de zero. Te rugăm să te adresezi administratorului site-ului tău pentru a crește această limită."
2828
experimental_inline_translation: "Activează funcția experimentală de traducere în-linie. Aceasta înlocuiește traducerea paralelă existentă, permițând vizitatorilor site-ului cu o locală care nu este implicită să vadă conținutul în limba lor."
2929
automatic_translation_target_languages: "Limbile pentru a traduce automat conținutul utilizatorului (postări, subiecte). Dacă este gol, nicio limbă nu va fi tradusă automat."
3030
translator:

config/settings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ discourse_translator:
2222
allow_any: false
2323
choices: "DiscourseTranslator::TranslatableLanguagesSetting.values"
2424
validator: "DiscourseTranslator::Validators::TranslatableLanguagesValidator"
25-
automatic_translation_backfill_maximum_translations_per_hour:
25+
automatic_translation_backfill_rate:
2626
default: 0
2727
client: false
2828
hidden: true

lib/discourse_translator/validators/translatable_languages_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(opts = {})
1010
def valid_value?(val)
1111
return true if val.blank?
1212

13-
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour > 0
13+
SiteSetting.automatic_translation_backfill_rate > 0
1414
end
1515

1616
def error_message

spec/jobs/automatic_translation_backfill_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ def expect_google_translate(text)
5959
end
6060

6161
it "does not backfill if backfill limit is set to 0" do
62-
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 1
62+
SiteSetting.automatic_translation_backfill_rate = 1
6363
SiteSetting.automatic_translation_target_languages = "de"
64-
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 0
64+
SiteSetting.automatic_translation_backfill_rate = 0
6565
expect_any_instance_of(Jobs::AutomaticTranslationBackfill).not_to receive(:process_batch)
6666
end
6767

6868
describe "with two locales ['de', 'es']" do
6969
before do
70-
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 100
70+
SiteSetting.automatic_translation_backfill_rate = 100
7171
SiteSetting.automatic_translation_target_languages = "de|es"
7272
SiteSetting.automatic_translation_backfill_limit_to_public_content = true
7373
SiteSetting.automatic_translation_backfill_max_age_days = 5
@@ -159,7 +159,7 @@ def expect_google_translate(text)
159159

160160
describe "with just one locale ['de']" do
161161
before do
162-
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 100
162+
SiteSetting.automatic_translation_backfill_rate = 100
163163
SiteSetting.automatic_translation_target_languages = "de"
164164
expect_google_check_language
165165
end

spec/jobs/translate_translatable_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
before do
99
SiteSetting.translator_enabled = true
1010
SiteSetting.translator_provider = "Google"
11-
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 100
11+
SiteSetting.automatic_translation_backfill_rate = 100
1212
SiteSetting.automatic_translation_target_languages = "es|fr"
1313
allow(DiscourseTranslator::Google).to receive(:translate)
1414
end

spec/lib/guardian_extension_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
before do
181181
SiteSetting.experimental_inline_translation = true
182182

183-
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 1
183+
SiteSetting.automatic_translation_backfill_rate = 1
184184
SiteSetting.automatic_translation_target_languages = "pt"
185185
end
186186

0 commit comments

Comments
 (0)