admin-panel-backend/apps/reports/services/data_sources.py
Ali Asadi 6aa925992f Fetch all accounts users for the user report, not just ticket owners
fetch_users previously only surfaced users who appeared as ticket
owners. Add list_users(limit, offset) to hit the accounts user-list
endpoint directly with raw limit/offset params -- the generated OpenAPI
wrapper only supports page, which this endpoint silently ignores.
fetch_users now paginates that list and hydrates each entry with
get_user_detailed_info to keep the existing email/phone/address columns.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-19 17:29:21 +03:30

75 lines
2 KiB
Python

import logging
from utils.accounts_client import get_user_detailed_info, list_users
from utils.advertising_client import get_crm_application_tickets
logger = logging.getLogger(__name__)
class ReportDataSources:
def __init__(self):
self._tickets_cache = None
def fetch_tickets(self, filters):
if self._tickets_cache is not None:
return self._tickets_cache
tickets = []
offset = 0
limit = 1000
while True:
response = get_crm_application_tickets(limit=limit, offset=offset)
if not response or not response.results:
break
tickets.extend(response.results)
if response.next_ is None:
break
offset += limit
self._tickets_cache = tickets
return tickets
def fetch_users(self, filters):
users = []
limit = 100
offset = 0
while True:
page = list_users(limit=limit, offset=offset)
results = page.get("results") or []
if not results:
break
for entry in results:
user_id = entry.get("uuid")
if not user_id:
continue
try:
user = get_user_detailed_info(user_id)
except Exception:
logger.exception("Failed to fetch user %s", user_id)
continue
if user is not None:
users.append(user)
if not page.get("next"):
break
offset += limit
return users
def fetch_user_metrics(self, user_ids, filters):
tickets = self.fetch_tickets(filters)
counts = {}
for ticket in tickets:
user_id = str(ticket.user)
counts[user_id] = counts.get(user_id, 0) + 1
return {user_id: {"ticket_count": count} for user_id, count in counts.items()}
def fetch_ticket_metrics(self, ticket_ids, filters):
return {}