Repository navigation
Bug Fixes - Cache Isolation, Async DB Pool, Timezone Dates, Currency Precision #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,32 @@ | ||
| from fastapi import APIRouter, Depends, HTTPException | ||
| from typing import Dict, Any | ||
| from decimal import Decimal | ||
| from app.services.cache import get_revenue_summary | ||
| from app.core.auth import authenticate_request as get_current_user | ||
|
|
||
| router = APIRouter() | ||
|
|
||
| CURRENCY_EXPONENTS = {'USD': 2, 'EUR': 2, 'GBP': 2, 'JPY': 0, 'KWD': 3} | ||
|
|
||
| @router.get("/dashboard/summary") | ||
| async def get_dashboard_summary( | ||
| property_id: str, | ||
| current_user: dict = Depends(get_current_user) | ||
| ) -> Dict[str, Any]: | ||
|
|
||
| tenant_id = getattr(current_user, "tenant_id", "default_tenant") or "default_tenant" | ||
|
|
||
| revenue_data = await get_revenue_summary(property_id, tenant_id) | ||
|
|
||
| total_revenue_float = float(revenue_data['total']) | ||
|
|
||
|
|
||
| currency = revenue_data['currency'] | ||
| currency_exponent = CURRENCY_EXPONENTS.get(currency, 2) | ||
| total_decimal = Decimal(str(revenue_data['total'])) | ||
| amount_minor_units = int(total_decimal * (10 ** currency_exponent)) | ||
|
|
||
| return { | ||
| "property_id": revenue_data['property_id'], | ||
| "total_revenue": total_revenue_float, | ||
| "currency": revenue_data['currency'], | ||
| "amount_minor_units": amount_minor_units, | ||
| "currency": currency, | ||
| "currency_exponent": currency_exponent, | ||
| "reservations_count": revenue_data['count'] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import asyncio | ||
| from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker | ||
| from sqlalchemy.pool import QueuePool | ||
| from sqlalchemy.pool import AsyncAdaptedQueuePool | ||
| import logging | ||
| from ..config import settings | ||
|
|
||
|
|
@@ -15,11 +15,11 @@ async def initialize(self): | |
| """Initialize database connection pool""" | ||
| try: | ||
| # Create async engine with connection pooling | ||
| database_url = f"postgresql+asyncpg://{settings.supabase_db_user}:{settings.supabase_db_password}@{settings.supabase_db_host}:{settings.supabase_db_port}/{settings.supabase_db_name}" | ||
| database_url = settings.database_url.replace("postgresql://", "postgresql+asyncpg://", 1) | ||
|
|
||
| self.engine = create_async_engine( | ||
| database_url, | ||
| poolclass=QueuePool, | ||
| poolclass=AsyncAdaptedQueuePool, | ||
|
Comment on lines
+18
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: A new Suggested FixUse a shared singleton instance of Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| pool_size=20, # Number of connections to maintain | ||
| max_overflow=30, # Additional connections when needed | ||
| pool_pre_ping=True, # Validate connections | ||
|
|
@@ -45,7 +45,7 @@ async def close(self): | |
| if self.engine: | ||
| await self.engine.dispose() | ||
|
|
||
| async def get_session(self) -> AsyncSession: | ||
| def get_session(self) -> AsyncSession: | ||
| """Get database session from pool""" | ||
| if not self.session_factory: | ||
| raise Exception("Database pool not initialized") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Converting
total_decimalto minor units usingint()truncates decimal values instead of rounding, causing precision loss for amounts with three decimal places.Severity: MEDIUM
Suggested Fix
Use
round()before converting toint, or use decimal quantization:int(round(total_decimal * (10 ** currency_exponent))).Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.