Skip to content

Commit

Permalink
Add historic Translation data to ActionLog (mozilla#3299)
Browse files Browse the repository at this point in the history
Also: add ability to override ActionLog.created_at value on instance creation
  • Loading branch information
RafaelJohn9 authored Sep 25, 2024
1 parent 9a9c516 commit 82a2f4b
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
19 changes: 19 additions & 0 deletions pontoon/actionlog/migrations/0004_alter_actionlog_created_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.11 on 2024-09-24 17:56

import django.utils.timezone

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("actionlog", "0003_existing_pretranslation_action"),
]

operations = [
migrations.AlterField(
model_name="actionlog",
name="created_at",
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Generated by Django 4.2.11 on 2024-09-24 17:56

import datetime

from django.db import migrations


def migrate_translations_to_actionlog(apps, schema_editor):
Translation = apps.get_model("base", "Translation")
ActionLog = apps.get_model("actionlog", "ActionLog")

translation_info = {
"date": (
"translation:created",
"user",
),
"approved_date": (
"translation:approved",
"approved_user",
),
"unapproved_date": (
"translation:unapproved",
"unapproved_user",
),
"rejected_date": (
"translation:rejected",
"rejected_user",
),
"unrejected_date": (
"translation:unrejected",
"unrejected_user",
),
}

# Timestamp of the first Translation created after the introduction of ActionLog
end_date = datetime.datetime(
2020, 1, 7, 9, 25, 11, 829125, tzinfo=datetime.timezone.utc
)

actions_to_log = []

BATCH_SIZE = 10000

# To improve performance:
# - Exclude translations that don't have any user set
# - Filter only the necessary fields
translations = (
Translation.objects.filter(date__lt=end_date)
.exclude(
user=None,
approved_user=None,
unapproved_user=None,
rejected_user=None,
unrejected_user=None,
)
.values(
"id",
"date",
"approved_date",
"unapproved_date",
"rejected_date",
"unrejected_date",
"user",
"approved_user",
"unapproved_user",
"rejected_user",
"unrejected_user",
)
).iterator(chunk_size=BATCH_SIZE)

for translation in translations:
for action_date, (action_type, action_user) in translation_info.items():
date = translation.get(action_date)
user_id = translation.get(action_user)

# Do not log Approve actions for self-approved translations
if action_type == "translation:approved" and date == translation.get(
"date"
):
continue

# Only log actions if the date is before the end_date, and user_id is set
if date is not None and date < end_date and user_id is not None:
actions_to_log.append(
ActionLog(
action_type=action_type,
created_at=date,
performed_by_id=user_id,
translation_id=translation.get("id"),
)
)

ActionLog.objects.bulk_create(actions_to_log, batch_size=BATCH_SIZE)


class Migration(migrations.Migration):
dependencies = [
("actionlog", "0004_alter_actionlog_created_at"),
]

operations = [
migrations.RunPython(
code=migrate_translations_to_actionlog,
reverse_code=migrations.RunPython.noop,
),
]
3 changes: 2 additions & 1 deletion pontoon/actionlog/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.utils import timezone


class ActionLog(models.Model):
Expand All @@ -20,7 +21,7 @@ class ActionType(models.TextChoices):
COMMENT_ADDED = "comment:added", "Comment added"

action_type = models.CharField(max_length=50, choices=ActionType.choices)
created_at = models.DateTimeField(auto_now_add=True)
created_at = models.DateTimeField(default=timezone.now)
performed_by = models.ForeignKey(
"auth.User", models.SET_NULL, related_name="actions", null=True
)
Expand Down

0 comments on commit 82a2f4b

Please sign in to comment.