Dev hausond - #6
Conversation
Pull Request Review: Dev hausondSummaryThis PR adds statistics for marks by category, sorts them accordingly, and implements automatic level creation in the gamification system. Overall, the code is functional but there are several areas that need attention. 🔴 Critical Issues1. Race Condition in Level Creation (
|
Pull Request ReviewThank you for this contribution! I've reviewed the changes and have the following feedback: ✅ Strengths
🐛 Critical Issues1. Race Condition in Level Creation (realtimemap/modules/gamefication/service.py:73-81)The level creation logic has a potential race condition: next_level_exists = await self.level_repo.exist(user.level + 1, "level")
if not next_level_exists:
logger.info(f"{user.id} user achieved max level, create new")
new_level_data = LevelCreate(
level=current_level.level + 1 if current_level else 1,
required_exp=current_level.required_exp if current_level else None,
)
await self.level_repo.create(new_level_data)Problem: If multiple users reach the max level simultaneously, both could try to create the same level, causing a database integrity error (unique constraint violation on Solution: Wrap this in try/except to handle 2. Incorrect Validator Logic (realtimemap/modules/gamefication/schemas/level/crud.py:23-28)@field_validator("required_exp", mode="before")
def required_exp_multiplier(cls, value):
if isinstance(value, int):
result = value * Decimal("1.5")
return int(result.quantize(Decimal("1"), rounding=ROUND_HALF_UP))
return 500Problem: This validator always multiplies the input by 1.5, even when explicitly passed. This means if you pass Expected behavior: The validator should only apply the multiplier when generating new levels automatically, not when explicitly setting values. Consider:
3. Typo in Schema Field (realtimemap/modules/kpi/schemas.py:132)total_xp: intIn
|
added stat about marks, sorted by categories, updated gamefication: auto create new levels