Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor stats, removing aggregate fields from project, projectlocale & locale #3536

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 2 additions & 3 deletions pontoon/administration/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pontoon.administration.views import _create_or_update_translated_resources
from pontoon.base.models import (
Entity,
Locale,
Project,
ProjectLocale,
Resource,
Expand Down Expand Up @@ -188,8 +187,8 @@ def test_manage_project_strings_translated_resource(client_superuser):
assert project.total_strings == strings_count * locales_count

for loc in locales:
locale = Locale.objects.get(id=loc.id)
assert locale.total_strings == strings_count
pl = ProjectLocale.objects.get(locale=loc, project=project)
assert pl.total_strings == strings_count


@pytest.mark.django_db
Expand Down
21 changes: 21 additions & 0 deletions pontoon/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class Meta:


class ProjectLocale(DjangoObjectType, Stats):
total_strings = graphene.Int()
approved_strings = graphene.Int()
pretranslated_strings = graphene.Int()
strings_with_errors = graphene.Int()
strings_with_warnings = graphene.Int()
unreviewed_strings = graphene.Int()

class Meta:
model = ProjectLocaleModel
fields = (
Expand All @@ -44,6 +51,13 @@ class Meta:


class Project(DjangoObjectType, Stats):
total_strings = graphene.Int()
approved_strings = graphene.Int()
pretranslated_strings = graphene.Int()
strings_with_errors = graphene.Int()
strings_with_warnings = graphene.Int()
unreviewed_strings = graphene.Int()

class Meta:
convert_choices_to_enum = False
model = ProjectModel
Expand Down Expand Up @@ -78,6 +92,13 @@ def resolve_tags(obj, info):


class Locale(DjangoObjectType, Stats):
total_strings = graphene.Int()
approved_strings = graphene.Int()
pretranslated_strings = graphene.Int()
strings_with_errors = graphene.Int()
strings_with_warnings = graphene.Int()
unreviewed_strings = graphene.Int()

class Meta:
model = LocaleModel
fields = (
Expand Down
11 changes: 9 additions & 2 deletions pontoon/api/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def test_project_localizations(client):
project(slug: "pontoon-intro") {
localizations {
locale {
name
name,
stringsWithErrors
}
}
}
Expand All @@ -147,7 +148,13 @@ def test_project_localizations(client):

assert response.status_code == 200
assert response.json() == {
"data": {"project": {"localizations": [{"locale": {"name": "English"}}]}}
"data": {
"project": {
"localizations": [
{"locale": {"name": "English", "stringsWithErrors": 0}}
]
}
}
}


Expand Down
85 changes: 85 additions & 0 deletions pontoon/base/aggregated_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import math

from functools import cached_property


class AggregatedStats:
trans_res_query: object

@cached_property
def _stats(self) -> dict[str, int]:
return self.trans_res_query.string_stats()

@property
def total_strings(self) -> int:
return self._stats["total"]

@property
def approved_strings(self) -> int:
return self._stats["approved"]

@property
def pretranslated_strings(self) -> int:
return self._stats["pretranslated"]

@property
def strings_with_errors(self) -> int:
return self._stats["errors"]

@property
def strings_with_warnings(self) -> int:
return self._stats["warnings"]

@property
def unreviewed_strings(self) -> int:
return self._stats["unreviewed"]

@property
def missing_strings(self):
return (
self.total_strings
- self.approved_strings
- self.pretranslated_strings
- self.strings_with_errors
- self.strings_with_warnings
)


def get_completed_percent(obj):
if not obj.total_strings:
return 0
completed_strings = (
obj.approved_strings + obj.pretranslated_strings + obj.strings_with_warnings
)
return completed_strings / obj.total_strings * 100


def get_chart_dict(obj: "AggregatedStats"):
"""Get chart data dictionary"""
if ts := obj.total_strings:
return {
"total": ts,
"approved": obj.approved_strings,
"pretranslated": obj.pretranslated_strings,
"errors": obj.strings_with_errors,
"warnings": obj.strings_with_warnings,
"unreviewed": obj.unreviewed_strings,
"approved_share": round(obj.approved_strings / ts * 100),
"pretranslated_share": round(obj.pretranslated_strings / ts * 100),
"errors_share": round(obj.strings_with_errors / ts * 100),
"warnings_share": round(obj.strings_with_warnings / ts * 100),
"unreviewed_share": round(obj.unreviewed_strings / ts * 100),
"completion_percent": int(math.floor(get_completed_percent(obj))),
}


def get_top_instances(qs):
"""
Get top instances in the queryset.
"""
return {
"most_strings": sorted(qs, key=lambda x: x.total_strings)[-1],
"most_translations": sorted(qs, key=lambda x: x.approved_strings)[-1],
"most_suggestions": sorted(qs, key=lambda x: x.unreviewed_strings)[-1],
"most_missing": sorted(qs, key=lambda x: x.missing_strings)[-1],
}
5 changes: 2 additions & 3 deletions pontoon/base/management/commands/calculate_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db.models import Count

from pontoon.base.models import Project
from pontoon.sync.core.stats import update_locale_stats, update_stats
from pontoon.sync.core.stats import update_stats


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -34,7 +34,6 @@ def handle(self, *args, **options):

log.info(f"Calculating stats for {len(projects)} projects...")
for project in projects:
update_stats(project, update_locales=False)
update_locale_stats()
update_stats(project)

log.info("Calculating stats complete for all projects.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Generated by Django 4.2.17 on 2025-01-17 19:02

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("base", "0071_alter_repository_type"),
]

operations = [
migrations.RemoveField(
model_name="locale",
name="approved_strings",
),
migrations.RemoveField(
model_name="locale",
name="pretranslated_strings",
),
migrations.RemoveField(
model_name="locale",
name="strings_with_errors",
),
migrations.RemoveField(
model_name="locale",
name="strings_with_warnings",
),
migrations.RemoveField(
model_name="locale",
name="total_strings",
),
migrations.RemoveField(
model_name="locale",
name="unreviewed_strings",
),
migrations.RemoveField(
model_name="project",
name="approved_strings",
),
migrations.RemoveField(
model_name="project",
name="pretranslated_strings",
),
migrations.RemoveField(
model_name="project",
name="strings_with_errors",
),
migrations.RemoveField(
model_name="project",
name="strings_with_warnings",
),
migrations.RemoveField(
model_name="project",
name="total_strings",
),
migrations.RemoveField(
model_name="project",
name="unreviewed_strings",
),
migrations.RemoveField(
model_name="projectlocale",
name="approved_strings",
),
migrations.RemoveField(
model_name="projectlocale",
name="pretranslated_strings",
),
migrations.RemoveField(
model_name="projectlocale",
name="strings_with_errors",
),
migrations.RemoveField(
model_name="projectlocale",
name="strings_with_warnings",
),
migrations.RemoveField(
model_name="projectlocale",
name="total_strings",
),
migrations.RemoveField(
model_name="projectlocale",
name="unreviewed_strings",
),
]
2 changes: 0 additions & 2 deletions pontoon/base/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from pontoon.base.models.aggregated_stats import AggregatedStats
from pontoon.base.models.changed_entity_locale import ChangedEntityLocale
from pontoon.base.models.comment import Comment
from pontoon.base.models.entity import Entity, get_word_count
Expand All @@ -18,7 +17,6 @@


__all__ = [
"AggregatedStats",
"ChangedEntityLocale",
"Comment",
"Entity",
Expand Down
Loading
Loading