diff --git a/grafana/provisioning/dashboards/monitoring-dashboard.json b/grafana/provisioning/dashboards/monitoring-dashboard.json index a6f1b2d..59fe17e 100644 --- a/grafana/provisioning/dashboards/monitoring-dashboard.json +++ b/grafana/provisioning/dashboards/monitoring-dashboard.json @@ -109,7 +109,7 @@ "refId": "A" } ], - "title": "Chamados Total", + "title": "Chamados em vigor", "type": "stat" }, { diff --git a/src/api_client.py b/src/api_client.py index 2100d59..e0061b8 100644 --- a/src/api_client.py +++ b/src/api_client.py @@ -240,38 +240,63 @@ class APIClient: return [] all_tickets = [] + page = 1 + expected_total = None try: import json queue_ids = Config.get_queue_ids_list() tickets_url = f"{self.base_url}{Config.API_TICKETS_ENDPOINT}" - - # Use showAll=true for open tickets as per API example - params = { - 'pageNumber': 1, - 'status': 'open', - 'showAll': 'true', - 'queueIds': json.dumps(queue_ids), - 'tagIds': '[]', - 'isGroup': 'false', - 'sortDir': 'ASC', - 'isSearching': 'false' - } - - logger.info(f"Fetching open tickets from {tickets_url} with params: {params}") - response = self.session.get( - tickets_url, - params=params, - timeout=15 - ) - - if response.status_code == 200: + + while True: + # Use showAll=true and paginate until all open tickets are fetched + params = { + 'pageNumber': page, + 'status': 'open', + 'showAll': 'true', + 'queueIds': json.dumps(queue_ids), + 'tagIds': '[]', + 'isGroup': 'false', + 'sortDir': 'ASC', + 'isSearching': 'false' + } + + logger.info(f"Fetching open tickets from {tickets_url} with params: {params}") + response = self.session.get( + tickets_url, + params=params, + timeout=15 + ) + + if response.status_code != 200: + logger.error(f"Failed to fetch open tickets: {response.status_code} - {response.text}") + break + result = response.json() - all_tickets = result.get('tickets', []) - logger.info(f"Fetched {len(all_tickets)} open tickets") - else: - logger.error(f"Failed to fetch open tickets: {response.status_code} - {response.text}") + page_tickets = result.get('tickets', []) + + if expected_total is None and isinstance(result.get('count'), int): + expected_total = result.get('count') + + if not page_tickets: + break + + all_tickets.extend(page_tickets) + + has_more = result.get('hasMore') + if isinstance(has_more, bool): + if not has_more: + break + elif expected_total is not None: + if len(all_tickets) >= expected_total: + break + else: + break + + page += 1 + + logger.info(f"Fetched {len(all_tickets)} open tickets (all pages)") except requests.exceptions.RequestException as e: logger.error(f"Error fetching open tickets: {str(e)}") @@ -280,7 +305,7 @@ class APIClient: def get_total_open_tickets(self) -> int: """ - Fetch total number of open tickets using showAll=true + Fetch total number of open tickets using full pagination Returns: int: Total number of open tickets @@ -289,45 +314,7 @@ class APIClient: logger.error("Not authenticated. Please login first.") return 0 - try: - import json - - queue_ids = Config.get_queue_ids_list() - tickets_url = f"{self.base_url}{Config.API_TICKETS_ENDPOINT}" - - params = { - 'pageNumber': 1, - 'status': 'open', - 'showAll': 'true', - 'queueIds': json.dumps(queue_ids), - 'tagIds': '[]', - 'isGroup': 'false', - 'sortDir': 'DESC', - 'isSearching': 'false' - } - - logger.info(f"Fetching total open tickets from {tickets_url} with params: {params}") - response = self.session.get( - tickets_url, - params=params, - timeout=15 - ) - - if response.status_code == 200: - result = response.json() - total = result.get('count') - if isinstance(total, int): - logger.info(f"Fetched total open tickets: {total}") - return total - - tickets = result.get('tickets', []) - fallback_total = len(tickets) if isinstance(tickets, list) else 0 - logger.info(f"Open tickets count field not found, fallback total: {fallback_total}") - return fallback_total - - logger.error(f"Failed to fetch total open tickets: {response.status_code} - {response.text}") - return 0 - - except requests.exceptions.RequestException as e: - logger.error(f"Error fetching total open tickets: {str(e)}") - return 0 + open_tickets = self.get_open_tickets() + total_open = len(open_tickets) + logger.info(f"Fetched total open tickets: {total_open}") + return total_open diff --git a/src/app.py b/src/app.py index 2de6245..8008c4b 100644 --- a/src/app.py +++ b/src/app.py @@ -84,7 +84,7 @@ def background_data_updater(): } open_tickets = client.get_open_tickets() - cached_total_open_tickets = client.get_total_open_tickets() + cached_total_open_tickets = len(open_tickets) cached_longest_tickets = DataFormatter.format_longest_tickets(open_tickets, limit=5) last_dashboard_update = datetime.now() logger.info("Background updater: Successfully updated dashboard metrics")