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 {}