admin-panel-backend/apps/reports/services/data_sources.py
Ali Asadi 4946ec8c8f Wire real Accounts/Advertising data into the standalone report endpoint
fetch_users/fetch_tickets were placeholder stubs returning empty
lists, so the generated Excel was always empty. Wire them to the
existing utils.accounts_client / utils.advertising_client integrations
(same ones the bundled-zip report on master uses), and update
UserReport/TicketsReport to the real Account/Ticket field names
instead of the old mock dict shape. Drop the now-unused local OAuth
helper in favor of each client's own client-credentials flow.

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

66 lines
1.8 KiB
Python

import logging
from utils.accounts_client import get_user_detailed_info
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):
tickets = self.fetch_tickets(filters)
users = []
seen = set()
for ticket in tickets:
user_id = str(ticket.user)
if user_id in seen:
continue
seen.add(user_id)
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)
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 {}