Skip to content

fix(cc): calculate Platform Pulse total_users from User accounts - #601

Open
prince-shakyaa wants to merge 1 commit into
GenAI-Security-Project:mainfrom
prince-shakyaa:fix/cc-pulse-total-users-count
Open

prince-shakyaa wants to merge 1 commit into
GenAI-Security-Project:mainfrom
prince-shakyaa:fix/cc-pulse-total-users-count

Conversation

@prince-shakyaa

Copy link
Copy Markdown

Fixes #600

What changed

finbot/apps/cc/routes/dashboard.py - _get_pulse_stats

Previously, the Platform Pulse card on the Command Center desktop (/cc) computed total_users by querying func.count(distinct(Vendor.namespace)) from the Vendor table.

This PR updates _get_pulse_stats to query func.count(User.id) from the User table, accurately counting registered user accounts regardless of whether they own vendor records or share a namespace.

Why

The Command Center desktop displays four top-level metrics in the "Platform Pulse" section, the first of which is "Total Users".

Querying distinct(Vendor.namespace) caused two major discrepancies:

  1. Registered user accounts (such as CTF players or users who have not yet added a vendor record) were omitted from the total user count.
  2. Multiple users sharing a single namespace (such as default or finbot) were collapsed into a single count of 1.
  3. The count contradicted the Command Center Users page (/cc/users), which correctly queries db.query(func.count(User.id)).scalar().

Importing User and counting User.id aligns the desktop pulse statistic with the actual user accounts in the database and ensures consistency across Command Center pages.

Changes

finbot/apps/cc/routes/dashboard.py

  from fastapi import APIRouter, Request
  from fastapi.responses import HTMLResponse
- from sqlalchemy import distinct, func
+ from sqlalchemy import func
 
  from finbot.config import settings
  from finbot.core.data.database import SessionLocal
- from finbot.core.data.models import UserBadge, UserChallengeProgress, Vendor
+ from finbot.core.data.models import (
+     User,
+     UserBadge,
+     UserChallengeProgress,
+     Vendor,
+ )
  from finbot.core.templates import TemplateResponse
 
  template_response = TemplateResponse("finbot/apps/cc/templates")
@@ -25,7 +30,7 @@ def _get_pulse_stats() -> dict:
              hour=0, minute=0, second=0, microsecond=0
          )
 
-         total_users = db.query(func.count(distinct(Vendor.namespace))).scalar() or 0
+         total_users = db.query(func.count(User.id)).scalar() or 0
 
          challenges_today = (
              db.query(func.count(UserChallengeProgress.id))

Behaviour preserved

  • vendors_total continues to query func.count(Vendor.id).
  • challenges_today continues to query completed challenges starting from today_start.
  • badges_total continues to query func.count(UserBadge.id).
  • When the database has 0 user accounts, total_users returns 0 via the existing or 0 fallback.

Testing

New test file added: tests/unit/apps/cc/test_dashboard_pulse.py

pytest tests/unit/apps/cc/test_dashboard_pulse.py -v
6 passed in 0.14s

@prince-shakyaa

Copy link
Copy Markdown
Author

Hi @saikishu , @e2hln

This PR resolves #600 where the Command Center desktop Platform Pulse calculated Total Users using distinct Vendor namespaces instead of actual User accounts. The issue was in _get_pulse_stats in finbot/apps/cc/routes/dashboard.py - querying distinct(Vendor.namespace) caused users without vendors to be excluded and collapsed users sharing a namespace into a count of 1. Fixed it by querying User.id (aligning with /cc/users) and added unit tests in tests/unit/apps/cc/test_dashboard_pulse.py to cover the fix and guard against regressions.

Let me know if you need any changes.
Thank You.

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.

[Bug] Command Center Platform Pulse calculates Total Users from Vendor namespaces instead of User accounts

1 participant