forked from mozilla/pontoon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor stats, removing aggregate fields from project, projectlocale…
… & locale (mozilla#3536) Based on unscientific local testing, the rendering speed benefits from eagerly populating the aggregated stats into Project, ProjectLocale & Locale don't exist, compared to collating the data on the fly from TranslatedResource objects. Therefore, we should not be doing this complex extra work. This significantly simplifies the logic around stats updates, and makes the stats much easier to reason about. The AggregatedStats class no longer extends Model, and it's mostly a crutch for reducing the risks in this change; hence its move from base/models/ to base/. With it, fields like total_strings and strings_with_errors will continue to work for the Django models from which the corresponding DB fields were removed.
- Loading branch information
Showing
54 changed files
with
695 additions
and
923 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import math | ||
|
||
from functools import cached_property | ||
|
||
|
||
class AggregatedStats: | ||
aggregated_stats_query: object | ||
""" | ||
Must be set by the child class as a QuerySet of TranslatedResource objects. | ||
Should include any filters leaving out disabled or system projects. | ||
""" | ||
|
||
@cached_property | ||
def _stats(self) -> dict[str, int]: | ||
return self.aggregated_stats_query.string_stats( | ||
count_disabled=True, count_system_projects=True | ||
) | ||
|
||
@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], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
pontoon/base/migrations/0072_remove_locale_approved_strings_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.