Skip to content

Take home assigment - #213

Open
Otitodev wants to merge 4 commits into
Base360-AI:mainfrom
Otitodev:main
Open

Otitodev wants to merge 4 commits into
Base360-AI:mainfrom
Otitodev:main

Conversation

@Otitodev

Copy link
Copy Markdown

No description provided.

Both tenants can have a property with the same id (see seed data),
so caching by property_id alone let one tenant's revenue get served
to another.
The property list was one hardcoded array shown to every user, so
tenants saw each other's property names against their own ids. Also
fixes the dashboard showing 0 revenue on first load since the initial
selected property depended on tenant data that hadn't loaded yet.
DatabasePool was reading settings that don't exist (supabase_db_*
instead of database_url), get_session was declared async but never
awaited anything so 'async with' on it failed, greenlet was missing
so sqlalchemy's async engine couldn't even import, and QueuePool
isn't valid for an async engine. All four together meant every
revenue query silently fell back to a hardcoded mock table, which is
what was actually causing the wrong/leaked numbers.
calculate_monthly_revenue was a dead placeholder that always returned
zero. Implemented it properly, converting check_in_date to the
property's own timezone before comparing against month boundaries.
A booking at 2024-02-29 23:30 UTC is already March 1st in Europe/Paris,
so a naive UTC comparison would have silently dropped it from March -
which matches the client's complaint about March totals being wrong.
Wired month/year as optional query params on /dashboard/summary.
Comment on lines +25 to +26
db_pool = DatabasePool()
await db_pool.initialize()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The calculate_monthly_revenue function creates a new DatabasePool on every call without closing it, which will leak database connections.
Severity: CRITICAL

Suggested Fix

Remove the local instantiation of DatabasePool within calculate_monthly_revenue. Instead, import and use the existing global db_pool instance from backend/app/db/database_pool.py. This will ensure that a single, shared connection pool is used for all database operations, preventing connection leaks.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: backend/app/services/reservations.py#L25-L26

Potential issue: The `calculate_monthly_revenue` function instantiates and initializes a
new `DatabasePool` on every invocation. This creates a new connection pool of up to 50
connections that is never closed or disposed of, as `db_pool.close()` is not called
before the function returns. Under load, each request to the corresponding endpoint will
leak database connections, quickly exhausting the PostgreSQL connection limit. This will
cause all subsequent database operations across the application to fail with connection
errors, leading to a service-wide outage.

Did we get this right? 👍 / 👎 to inform future reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant