Skip to content

Commit

Permalink
stats: provide migration path for stats calculated in older versions
Browse files Browse the repository at this point in the history
Few keys were moved from optional to required and we still need to deal
with them being missing.

Fixes #10439
Fixes WEBLATE-5K1
  • Loading branch information
nijel committed Nov 22, 2023
1 parent 8ee445e commit 872f989
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Not yet released.
**Bug fixes**

* Database backups compatibility with Alibaba Cloud Database PolarDB.
* Crash on loading statistics calculated by previous versions.

**Upgrading**

Expand Down
11 changes: 10 additions & 1 deletion weblate/utils/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
)
)

# TODO: Drop in Weblate 5.5
LEGACY_KEYS = {"unapproved", "unapproved_chars", "unapproved_words", "total_changes"}

SOURCE_MAP = {
"source_chars": "all_chars",
"source_words": "all_words",
Expand Down Expand Up @@ -213,6 +216,12 @@ def aggregate_get(self, name: str):
# Handle source_* keys as virtual on translation level for easier aggregation
if name.startswith("source_"):
return self._data[SOURCE_MAP[name]]
# Legacy keys were calculated on demand before and are precalculated
# since Weblate 5.2, so they are missing on stats calculated before.
# Using zero here is most likely a wrong value, but safe and cheap.
# TODO: Drop in Weblate 5.5
if name in LEGACY_KEYS:
return 0
raise

def __getattr__(self, name: str):
Expand All @@ -226,7 +235,7 @@ def __getattr__(self, name: str):
return self.calculate_percent(name)

if name == "stats_timestamp":
# TODO: Drop in Weblate 5.3
# TODO: Drop in Weblate 5.5
# Migration path for legacy stat data
return self._data.get(name, 0)

Expand Down

0 comments on commit 872f989

Please sign in to comment.