Skip to content

Commit ad2a9f6

Browse files
Merge pull request #7019 from hotosm/fix/script-formatting
Use check and update stats function in refresh mapper level command
2 parents 6dd9c31 + e4ff401 commit ad2a9f6

File tree

1 file changed

+7
-46
lines changed

1 file changed

+7
-46
lines changed

scripts/commands/refresh_mapper_level.py

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,66 +22,27 @@ async def main():
2222

2323
logger.info("Started updating mapper levels...")
2424
users = await User.get_all_users_not_paginated(db)
25-
users_updated = 0
2625
total_users = len(users)
27-
failed_usernames = []
26+
failed_users = []
27+
users_updated = 0
2828

2929
for user in users:
3030
try:
31-
user = await UserService.get_user_by_id(user.id, db)
32-
user_level = await MappingLevel.get_by_id(user.mapping_level, db)
33-
try:
34-
stats = await UserService.get_and_save_stats(user.id, db)
35-
except Exception:
36-
failed_usernames.append(user.username)
37-
logger.exception(
38-
"Failed to update stats for user %s — continuing with next user",
39-
user.id,
40-
)
41-
continue
42-
43-
try:
44-
async with db.transaction():
45-
badges = await MappingBadge.available_badges_for_user(
46-
user.id, db
47-
)
48-
assignable_ids = []
49-
for badge in badges:
50-
if badge.all_requirements_satisfied(stats):
51-
assignable_ids.append(badge.id)
52-
await user.assign_badges(assignable_ids, db)
53-
54-
next_level = await MappingLevel.get_next(
55-
user_level.ordering, db
56-
)
57-
58-
if await MappingLevel.all_badges_satisfied(
59-
next_level.id, user.id, db
60-
):
61-
if next_level.approvals_required == 0:
62-
await user.set_mapping_level(next_level, db)
63-
else:
64-
await UserNextLevel.nominate(user.id, next_level.id, db)
65-
except Exception:
66-
logger.exception(
67-
"Failed to update mapper level for user %s — stats updated, but skipping badges/level",
68-
user.id,
69-
)
70-
31+
await UserService.check_and_update_mapper_level(user.id, db)
7132
except Exception:
72-
failed_usernames.append(user.username)
33+
failed_users.append(user.id)
7334
logger.exception(
74-
"Failed to fetch stats and update mapper level for user %s — continuing with next user",
35+
"Failed to update stats/mapper level for user %s",
7536
user.id,
7637
)
7738
continue
7839

7940
users_updated += 1
8041
if users_updated % 1000 == 0:
81-
print(f"{users_updated} users updated of {total_users}")
42+
logger.info(f"{users_updated} users updated of {total_users}")
8243

8344
logger.info(f"Finished. Updated {users_updated} user mapper levels.")
84-
logger.info(f"Failed stats update for these users: {failed_usernames}.")
45+
logger.info(f"Failed stats update for these users: {failed_users}.")
8546

8647
except Exception:
8748
logger.exception("Error while refreshing mapper levels")

0 commit comments

Comments
 (0)