Skip to content

Commit 14e5131

Browse files
committed
feat: print stats at the end of sync_username
1 parent 423c06a commit 14e5131

File tree

1 file changed

+32
-5
lines changed
  • openedx/features/wikimedia_features/wikimedia_general/management/commands

1 file changed

+32
-5
lines changed

openedx/features/wikimedia_features/wikimedia_general/management/commands/sync_usernames.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
from django.contrib.auth import get_user_model
66
from django.db.models import F, Value, Case, When, CharField
7+
from django.db.models.query import QuerySet
78
from django.db.models.functions import Concat
89
from django.core.management.base import BaseCommand
910

11+
1012
log = getLogger(__name__)
1113
User = get_user_model()
1214

@@ -35,8 +37,13 @@ def handle(self, *args, **options):
3537

3638
if usernames:
3739
users = users.filter(username__in=usernames)
40+
41+
total = users.count()
42+
43+
log.info("Syncing %s users with wikimedia usernames", total)
3844

39-
self._update_user_with_wikimedia_username(users)
45+
stats = self._update_user_with_wikimedia_username(users)
46+
self._print_stats(total, stats)
4047

4148
def _get_tpa_users(self):
4249
"""
@@ -64,19 +71,33 @@ def _get_tpa_users(self):
6471

6572
return users
6673

67-
def _update_user_with_wikimedia_username(self, users: list[User]):
74+
def _update_user_with_wikimedia_username(self, users: QuerySet[User]) -> dict:
6875
"""
6976
Checks if "wiki_username" was the original username provided by Wikimedia
7077
and updates the Wikilearn username with it.
7178
"""
7279
total = len(users)
80+
stats = {
81+
"correct_username": 0,
82+
"updated_username": 0,
83+
"skipped_username": 0,
84+
}
7385
for i, user in enumerate(users):
7486
index = i + 1
75-
if self._username_exists(user.wiki_username):
87+
if self._username_exists(user.username):
88+
# This check is to avoid updating username if it is already same as Wikimedia's.
89+
log.info(f'{index}/{total}: SKIPPED: {user.username} is CORRECT')
90+
stats["correct_username"] += 1
91+
elif self._username_exists(user.wiki_username):
92+
log.info(f"{index}/{total}: UPDATING: {user.username} with {user.wiki_username}")
7693
self._update_user(user)
77-
log.info(f"{index}/{total}: UPDATED {user.username} with {user.wiki_username}")
94+
stats["updated_username"] += 1
7895
else:
79-
log.info(f'{index}/{total}: SKIPPED {user["username"]}')
96+
# This means both the username and our guess (first_name+last_name) is wrong.
97+
log.info(f'{index}/{total}: SKIPPED: {user.username}')
98+
stats["skipped_username"] += 1
99+
100+
return stats
80101

81102
def _username_exists(self, username: str) -> bool:
82103
"""
@@ -92,3 +113,9 @@ def _username_exists(self, username: str) -> bool:
92113
def _update_user(self, user: User):
93114
user.username = user.wiki_username
94115
user.save()
116+
117+
def _print_stats(self, total: int, stats: dict):
118+
log.info(f"Total mismatched users: {total}")
119+
log.info(f"Correct usernames: {stats['correct_username']}")
120+
log.info(f"Updated usernames: {stats['updated_username']}")
121+
log.info(f"Skipped usernames: {stats['skipped_username']}")

0 commit comments

Comments
 (0)