diff --git a/Dockerfile b/Dockerfile
index 1e0638b..87c94fb 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -25,5 +25,5 @@ EXPOSE 5000
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD curl -f http://localhost:5000/health || exit 1
-# Run application
-CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "4", "--timeout", "120", "app:app"]
+# Run application with single worker to allow background threads
+CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "1", "--timeout", "120", "app:app"]
diff --git a/config/.env.example b/config/.env.example
index b831ba5..72627f6 100644
--- a/config/.env.example
+++ b/config/.env.example
@@ -12,8 +12,9 @@ FLASK_PORT=5000
FLASK_HOST=0.0.0.0
DEBUG=False
-# Refresh interval (seconds)
-REFRESH_INTERVAL=30
+# Refresh intervals (seconds)
+TICKETS_REFRESH_INTERVAL=5
+DASHBOARD_REFRESH_INTERVAL=60
# Queue IDs (comma-separated)
QUEUE_IDS=-1,1,2,3,4,5,7
diff --git a/docker-compose.yml b/docker-compose.yml
index bdd5749..57bf1ee 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -41,6 +41,7 @@ services:
volumes:
- grafana-storage:/var/lib/grafana
- ./grafana/provisioning:/etc/grafana/provisioning:ro
+ - ./grafana/images:/usr/share/grafana/public/img/custom:ro
networks:
- monitoring
restart: unless-stopped
diff --git a/grafana/images/.gitkeep b/grafana/images/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/grafana/images/svg/branco-cinza.svg b/grafana/images/svg/branco-cinza.svg
new file mode 100755
index 0000000..2cc70e4
--- /dev/null
+++ b/grafana/images/svg/branco-cinza.svg
@@ -0,0 +1,15 @@
+
diff --git a/grafana/images/svg/laranja-branco.svg b/grafana/images/svg/laranja-branco.svg
new file mode 100755
index 0000000..b3d4d20
--- /dev/null
+++ b/grafana/images/svg/laranja-branco.svg
@@ -0,0 +1,15 @@
+
diff --git a/grafana/images/svg/laranja-cinza.svg b/grafana/images/svg/laranja-cinza.svg
new file mode 100755
index 0000000..d6f4b15
--- /dev/null
+++ b/grafana/images/svg/laranja-cinza.svg
@@ -0,0 +1,15 @@
+
diff --git a/grafana/provisioning/dashboards/monitoring-dashboard.json b/grafana/provisioning/dashboards/monitoring-dashboard.json
index 56dbfaa..e16ddb6 100644
--- a/grafana/provisioning/dashboards/monitoring-dashboard.json
+++ b/grafana/provisioning/dashboards/monitoring-dashboard.json
@@ -3,7 +3,9 @@
"list": [
{
"builtIn": 1,
- "datasource": "-- Grafana --",
+ "datasource": {
+ "uid": "-- Grafana --"
+ },
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
@@ -13,18 +15,250 @@
]
},
"editable": true,
- "gnetId": null,
+ "fiscalYearStartMonth": 0,
"graphTooltip": 0,
- "id": null,
+ "id": 1,
"links": [],
"panels": [
{
- "datasource": "Prometheus",
+ "fieldConfig": {
+ "defaults": {},
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 4,
+ "w": 24,
+ "x": 0,
+ "y": 0
+ },
+ "id": 9,
+ "options": {
+ "code": {
+ "language": "plaintext",
+ "showLineNumbers": false,
+ "showMiniMap": false
+ },
+ "content": "
\r\n

\r\n
\r\n",
+ "mode": "html"
+ },
+ "pluginVersion": "12.3.2",
+ "title": "",
+ "transparent": true,
+ "type": "text"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "thresholds"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": 0
+ }
+ ]
+ }
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 7,
+ "w": 5,
+ "x": 0,
+ "y": 4
+ },
+ "id": 5,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "none",
+ "justifyMode": "center",
+ "orientation": "auto",
+ "percentChangeColorMode": "standard",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "text": {
+ "valueSize": 150
+ },
+ "textMode": "value",
+ "wideLayout": true
+ },
+ "pluginVersion": "12.3.2",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
+ "expr": "chamados_abertos_hoje",
+ "format": "time_series",
+ "instant": true,
+ "refId": "A"
+ }
+ ],
+ "title": "Chamados Abertos Hoje",
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "thresholds"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": 0
+ }
+ ]
+ }
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 7,
+ "w": 5,
+ "x": 5,
+ "y": 4
+ },
+ "id": 6,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "none",
+ "justifyMode": "center",
+ "orientation": "auto",
+ "percentChangeColorMode": "standard",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "text": {
+ "valueSize": 150
+ },
+ "textMode": "value",
+ "wideLayout": true
+ },
+ "pluginVersion": "12.3.2",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
+ "expr": "chamados_do_mes",
+ "format": "time_series",
+ "instant": true,
+ "refId": "A"
+ }
+ ],
+ "title": "Chamados do Mês",
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "thresholds"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": 0
+ }
+ ]
+ }
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 7,
+ "w": 5,
+ "x": 10,
+ "y": 4
+ },
+ "id": 7,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "none",
+ "justifyMode": "center",
+ "orientation": "auto",
+ "percentChangeColorMode": "standard",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "text": {
+ "valueSize": 150
+ },
+ "textMode": "value",
+ "wideLayout": true
+ },
+ "pluginVersion": "12.3.2",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
+ "expr": "chamados_concluidos",
+ "format": "time_series",
+ "instant": true,
+ "refId": "A"
+ }
+ ],
+ "title": "Chamados Concluídos Hoje",
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
"fieldConfig": {
"defaults": {
"custom": {
"align": "center",
- "displayMode": "auto",
+ "cellOptions": {
+ "type": "auto"
+ },
+ "footer": {
+ "reducers": []
+ },
"inspect": false
},
"mappings": [],
@@ -33,7 +267,15 @@
"steps": [
{
"color": "green",
- "value": null
+ "value": 0
+ },
+ {
+ "color": "yellow",
+ "value": 3600
+ },
+ {
+ "color": "red",
+ "value": 7200
}
]
}
@@ -42,19 +284,137 @@
{
"matcher": {
"id": "byName",
- "options": "contact_name"
+ "options": "Value"
},
"properties": [
{
- "id": "custom.displayMode",
- "value": "color-background"
+ "id": "custom.cellOptions",
+ "value": {
+ "mode": "gradient",
+ "type": "color-background"
+ }
},
{
"id": "unit",
- "value": "short"
+ "value": "s"
+ },
+ {
+ "id": "custom.width",
+ "value": 150
}
]
},
+ {
+ "matcher": {
+ "id": "byName",
+ "options": "duration_formatted"
+ },
+ "properties": [
+ {
+ "id": "custom.width",
+ "value": 150
+ }
+ ]
+ }
+ ]
+ },
+ "gridPos": {
+ "h": 7,
+ "w": 9,
+ "x": 15,
+ "y": 4
+ },
+ "id": 8,
+ "options": {
+ "cellHeight": "sm",
+ "showHeader": true,
+ "sortBy": [
+ {
+ "desc": true,
+ "displayName": "Value"
+ }
+ ]
+ },
+ "pluginVersion": "12.3.2",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
+ "expr": "longest_ticket_duration_seconds",
+ "format": "table",
+ "instant": true,
+ "refId": "A"
+ }
+ ],
+ "title": "Lista de mais tempo em atendimento agora",
+ "transformations": [
+ {
+ "id": "organize",
+ "options": {
+ "excludeByName": {
+ "Time": true,
+ "__name__": true,
+ "instance": true,
+ "job": true,
+ "queue": true,
+ "ticket_id": true,
+ "user_name": true
+ },
+ "includeByName": {},
+ "indexByName": {
+ "Value": 4,
+ "contact_name": 0,
+ "duration_formatted": 3,
+ "queue": 2,
+ "user_name": 1
+ },
+ "renameByName": {
+ "Value": "Segundos",
+ "contact_name": "Usuário",
+ "duration_formatted": "Tempo",
+ "queue": "TAG",
+ "user_name": "Atendente"
+ }
+ }
+ }
+ ],
+ "type": "table"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
+ "fieldConfig": {
+ "defaults": {
+ "custom": {
+ "align": "center",
+ "cellOptions": {
+ "type": "auto"
+ },
+ "footer": {
+ "reducers": []
+ },
+ "inspect": false
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": 0
+ },
+ {
+ "color": "red",
+ "value": 600000
+ }
+ ]
+ }
+ },
+ "overrides": [
{
"matcher": {
"id": "byName",
@@ -63,11 +423,35 @@
"properties": [
{
"id": "unit",
- "value": "durationms"
+ "value": "ms"
},
{
- "id": "decimals",
- "value": 0
+ "id": "custom.cellOptions",
+ "value": {
+ "type": "color-background"
+ }
+ },
+ {
+ "id": "color",
+ "value": {
+ "mode": "thresholds"
+ }
+ },
+ {
+ "id": "thresholds",
+ "value": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": null
+ },
+ {
+ "color": "red",
+ "value": 60000
+ }
+ ]
+ }
}
]
}
@@ -77,29 +461,26 @@
"h": 12,
"w": 24,
"x": 0,
- "y": 0
+ "y": 11
},
"id": 4,
"options": {
- "footer": {
- "countRows": false,
- "fields": "",
- "reducer": [
- "sum"
- ],
- "show": false
- },
+ "cellHeight": "sm",
"showHeader": true,
"sortBy": [
{
- "displayName": "TEMPO",
- "desc": true
+ "desc": true,
+ "displayName": "TEMPO"
}
]
},
- "pluginVersion": "8.0.0",
+ "pluginVersion": "12.3.2",
"targets": [
{
+ "datasource": {
+ "type": "prometheus",
+ "uid": "PBFA97CFB590B2093"
+ },
"expr": "ticket_duration_milliseconds",
"format": "table",
"instant": true,
@@ -113,23 +494,23 @@
"options": {
"excludeByName": {
"Time": true,
+ "__name__": true,
+ "duration_formatted": true,
"instance": true,
"job": true,
"status": true,
- "ticket_id": true,
- "__name__": true,
- "Value": true,
- "ticket_duration_milliseconds": true
+ "ticket_duration_milliseconds": true,
+ "ticket_id": true
},
"indexByName": {
"contact_name": 0,
"queue": 1,
- "duration_formatted": 2
+ "Value": 2
},
"renameByName": {
"contact_name": "PESSOA",
- "queue": "TAG",
- "duration_formatted": "TEMPO"
+ "Value": "TEMPO",
+ "queue": "TAG"
}
}
},
@@ -144,9 +525,9 @@
"type": "table"
}
],
+ "preload": false,
"refresh": "5s",
- "schemaVersion": 27,
- "style": "dark",
+ "schemaVersion": 42,
"tags": [
"monitoring",
"tickets",
@@ -163,5 +544,5 @@
"timezone": "",
"title": "Monitoramento de Atendimento",
"uid": "monitoring-tickets",
- "version": 1
-}
+ "version": 2
+}
\ No newline at end of file
diff --git a/grafana/provisioning/datasources/datasources.yml b/grafana/provisioning/datasources/datasources.yml
index 2d864ad..d99af34 100644
--- a/grafana/provisioning/datasources/datasources.yml
+++ b/grafana/provisioning/datasources/datasources.yml
@@ -8,3 +8,5 @@ datasources:
isDefault: true
version: 1
editable: true
+
+
diff --git a/src/api_client.py b/src/api_client.py
index 9277544..3a36bd6 100644
--- a/src/api_client.py
+++ b/src/api_client.py
@@ -126,7 +126,7 @@ class APIClient:
def get_all_tickets(self) -> List[Dict[str, Any]]:
"""
- Fetch all pending tickets and group tickets
+ Fetch all pending, open and group tickets
Returns:
list: List of ticket dictionaries
@@ -151,6 +151,24 @@ class APIClient:
logger.info(f"Fetched {len(all_tickets)} pending tickets")
+ # Fetch open tickets
+ logger.info("Fetching open tickets...")
+ page = 1
+ while True:
+ result = self.get_tickets(page_number=page, status='open', is_group=False)
+
+ if not result or not result.get('tickets'):
+ break
+
+ all_tickets.extend(result.get('tickets', []))
+
+ if not result.get('hasMore', False):
+ break
+
+ page += 1
+
+ logger.info(f"Fetched {len(all_tickets)} open tickets (total so far: {len(all_tickets)})")
+
# Fetch group tickets
logger.info("Fetching group tickets...")
page = 1
@@ -167,5 +185,95 @@ class APIClient:
page += 1
- logger.info(f"Total tickets fetched (pending + group): {len(all_tickets)}")
+ logger.info(f"Total tickets fetched (pending + open + group): {len(all_tickets)}")
+ return all_tickets
+ def get_dashboard_resume(self, date_from: str, date_to: str) -> Optional[Dict[str, Any]]:
+ """
+ Fetch dashboard resume data from the API
+
+ Args:
+ date_from: Start date in format YYYY-MM-DD
+ date_to: End date in format YYYY-MM-DD
+
+ Returns:
+ dict: Dashboard resume data if successful, None otherwise
+ """
+ if not self.token:
+ logger.error("Not authenticated. Please login first.")
+ return None
+
+ try:
+ resume_url = f"{self.base_url}{Config.API_DASHBOARD_RESUME_ENDPOINT}"
+
+ params = {
+ 'dateFrom': date_from,
+ 'dateTo': date_to
+ }
+
+ logger.info(f"Fetching dashboard resume from {resume_url} with params: {params}")
+ response = self.session.get(
+ resume_url,
+ params=params,
+ timeout=15
+ )
+
+ if response.status_code == 200:
+ logger.info(f"Successfully fetched dashboard resume")
+ return response.json()
+ else:
+ logger.error(f"Failed to fetch dashboard resume: {response.status_code} - {response.text}")
+ return None
+
+ except requests.exceptions.RequestException as e:
+ logger.error(f"Error fetching dashboard resume: {str(e)}")
+ return None
+
+ def get_open_tickets(self) -> List[Dict[str, Any]]:
+ """
+ Fetch all open tickets sorted by duration (ascending)
+
+ Returns:
+ list: List of open ticket dictionaries
+ """
+ if not self.token:
+ logger.error("Not authenticated. Please login first.")
+ return []
+
+ all_tickets = []
+
+ 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:
+ 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}")
+
+ except requests.exceptions.RequestException as e:
+ logger.error(f"Error fetching open tickets: {str(e)}")
+
return all_tickets
diff --git a/src/app.py b/src/app.py
index efc7260..51330c1 100644
--- a/src/app.py
+++ b/src/app.py
@@ -1,7 +1,9 @@
import logging
import os
-from datetime import datetime, timedelta
-from flask import Flask, jsonify, Response
+import threading
+import time
+from datetime import datetime
+from flask import Flask, jsonify, Response, request
from api_client import APIClient
from data_formatter import DataFormatter
from config import Config
@@ -16,44 +18,100 @@ logger = logging.getLogger(__name__)
app = Flask(__name__)
# Global variables for caching
-last_update = None
cached_tickets = []
cached_json = None
cached_metrics = None
-CACHE_DURATION = timedelta(seconds=Config.REFRESH_INTERVAL)
+cached_dashboard_resume = None
+cached_longest_tickets = None
+last_tickets_update = None
+last_dashboard_update = None
+background_thread = None
+stop_background_thread = False
-def update_tickets():
- """Update cached tickets from API"""
- global last_update, cached_tickets, cached_json, cached_metrics
+def background_data_updater():
+ """Background thread that updates data continuously"""
+ global cached_tickets, cached_json, cached_metrics, cached_dashboard_resume, cached_longest_tickets
+ global last_tickets_update, last_dashboard_update, stop_background_thread
+
+ tickets_interval = max(1, Config.TICKETS_REFRESH_INTERVAL)
+ dashboard_interval = max(1, Config.DASHBOARD_REFRESH_INTERVAL)
+
+ next_tickets_run = time.monotonic()
+ next_dashboard_run = time.monotonic()
+
+ while not stop_background_thread:
+ now_monotonic = time.monotonic()
+
+ if now_monotonic >= next_tickets_run:
+ try:
+ logger.info("Background updater: Updating ticket data from API...")
+ client = APIClient()
+
+ # Login
+ if not client.login():
+ logger.error("Background updater: Failed to authenticate with API")
+ else:
+ cached_tickets = client.get_all_tickets()
+ cached_json = DataFormatter.format_tickets_for_json(cached_tickets)
+ cached_metrics = DataFormatter.format_for_prometheus(cached_tickets)
+ last_tickets_update = datetime.now()
+ logger.info(f"Background updater: Successfully updated {len(cached_tickets)} tickets")
+ except Exception as e:
+ logger.error(f"Background updater error (tickets): {str(e)}")
+
+ while next_tickets_run <= now_monotonic:
+ next_tickets_run += tickets_interval
+
+ if now_monotonic >= next_dashboard_run:
+ try:
+ logger.info("Background updater: Updating dashboard metrics from API...")
+ client = APIClient()
+
+ # Login
+ if not client.login():
+ logger.error("Background updater: Failed to authenticate with API")
+ else:
+ now = datetime.now()
+ date_today = now.strftime('%Y-%m-%d')
+ first_day_month = now.replace(day=1).strftime('%Y-%m-%d')
+
+ resume_today = client.get_dashboard_resume(date_today, date_today)
+ resume_month = client.get_dashboard_resume(first_day_month, date_today)
+ cached_dashboard_resume = {
+ 'today': resume_today,
+ 'month': resume_month
+ }
+
+ open_tickets = client.get_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")
+ except Exception as e:
+ logger.error(f"Background updater error (dashboard): {str(e)}")
+
+ while next_dashboard_run <= now_monotonic:
+ next_dashboard_run += dashboard_interval
+
+ sleep_until = min(next_tickets_run, next_dashboard_run)
+ sleep_for = max(0.0, sleep_until - time.monotonic())
+ time.sleep(sleep_for)
+
+def start_background_updater():
+ """Start the background data updater thread"""
+ global background_thread, stop_background_thread
- now = datetime.now()
+ stop_background_thread = False
+ background_thread = threading.Thread(target=background_data_updater, daemon=True)
+ background_thread.start()
+ logger.info("Background data updater thread started")
+
+def stop_background_updater():
+ """Stop the background data updater thread"""
+ global stop_background_thread
- # Only update if cache is expired or first run
- if last_update is None or (now - last_update) > CACHE_DURATION:
- try:
- logger.info("Updating ticket data from API...")
- client = APIClient()
-
- # Login
- if not client.login():
- logger.error("Failed to authenticate with API")
- return False
-
- # Fetch tickets
- cached_tickets = client.get_all_tickets()
-
- # Format data
- cached_json = DataFormatter.format_tickets_for_json(cached_tickets)
- cached_metrics = DataFormatter.format_for_prometheus(cached_tickets)
-
- last_update = now
- logger.info(f"Successfully updated {len(cached_tickets)} tickets")
- return True
- except Exception as e:
- logger.error(f"Error updating tickets: {str(e)}")
- return False
-
- return True
+ stop_background_thread = True
+ logger.info("Background data updater thread stopped")
+
@app.route('/')
def index():
@@ -66,7 +124,10 @@ def index():
'health': '/health',
'json': '/api/tickets',
'prometheus': '/metrics',
- 'timeseries': '/api/timeseries'
+ 'timeseries': '/api/timeseries',
+ 'summary': '/api/summary',
+ 'dashboard_resume': '/api/dashboard-resume',
+ 'longest_tickets': '/api/longest-tickets'
}
})
@@ -74,11 +135,11 @@ def index():
def health():
"""Health check endpoint"""
try:
- update_tickets()
return jsonify({
'status': 'healthy',
'cached_tickets': len(cached_tickets),
- 'last_update': last_update.isoformat() if last_update else None
+ 'dashboard_resume_cached': cached_dashboard_resume is not None,
+ 'longest_tickets_cached': cached_longest_tickets is not None
}), 200
except Exception as e:
logger.error(f"Health check failed: {str(e)}")
@@ -94,9 +155,6 @@ def get_tickets():
Useful for displaying data in Grafana tables and dashboards
"""
try:
- if not update_tickets():
- return jsonify({'error': 'Failed to fetch tickets'}), 500
-
if cached_json is None:
return jsonify({'error': 'No data available'}), 204
@@ -112,13 +170,28 @@ def metrics():
Can be scraped by Prometheus or used directly in Grafana
"""
try:
- if not update_tickets():
- return Response('# Error: Failed to fetch tickets\n', mimetype='text/plain'), 500
+ all_metrics = []
- if cached_metrics is None:
+ # Add ticket metrics
+ if cached_metrics:
+ all_metrics.append(cached_metrics)
+
+ # Add dashboard metrics
+ if cached_dashboard_resume and cached_longest_tickets:
+ resume_today = cached_dashboard_resume.get('today')
+ resume_month = cached_dashboard_resume.get('month')
+ dashboard_metrics = DataFormatter.format_dashboard_metrics(
+ resume_today,
+ resume_month,
+ cached_longest_tickets
+ )
+ if dashboard_metrics:
+ all_metrics.append(dashboard_metrics)
+
+ if not all_metrics:
return Response('# No data available\n', mimetype='text/plain'), 204
- return Response(cached_metrics, mimetype='text/plain'), 200
+ return Response('\n\n'.join(all_metrics), mimetype='text/plain'), 200
except Exception as e:
logger.error(f"Error in /metrics: {str(e)}")
return Response(f'# Error: {str(e)}\n', mimetype='text/plain'), 500
@@ -129,8 +202,8 @@ def timeseries():
Get time series data for Grafana time series visualization
"""
try:
- if not update_tickets():
- return jsonify({'error': 'Failed to fetch tickets'}), 500
+ if not cached_tickets:
+ return jsonify({'error': 'No data available'}), 204
time_series = DataFormatter.format_for_timeseries(cached_tickets)
return jsonify(time_series), 200
@@ -144,9 +217,6 @@ def summary():
Get summary statistics for tickets
"""
try:
- if not update_tickets():
- return jsonify({'error': 'Failed to fetch tickets'}), 500
-
if not cached_tickets:
return jsonify({
'total_tickets': 0,
@@ -170,6 +240,74 @@ def summary():
logger.error(f"Error in /api/summary: {str(e)}")
return jsonify({'error': str(e)}), 500
+@app.route('/api/dashboard-resume')
+def dashboard_resume():
+ """
+ Get dashboard resume with today's opened tickets, month tickets, and closed tickets
+ """
+ try:
+ client = APIClient()
+
+ # Login
+ if not client.login():
+ return jsonify({'error': 'Failed to authenticate'}), 500
+
+ now = datetime.now()
+ date_today = now.strftime('%Y-%m-%d')
+ first_day_month = now.replace(day=1).strftime('%Y-%m-%d')
+
+ # Fetch dashboard resume for today and month
+ resume_today = client.get_dashboard_resume(date_today, date_today)
+ resume_month = client.get_dashboard_resume(first_day_month, date_today)
+
+ if not resume_today and not resume_month:
+ return jsonify({'error': 'Failed to fetch dashboard resume'}), 500
+
+ # Format the data
+ formatted_resume = DataFormatter.format_dashboard_resume(resume_today, resume_month)
+
+ return jsonify(formatted_resume), 200
+ except Exception as e:
+ logger.error(f"Error in /api/dashboard-resume: {str(e)}")
+ return jsonify({'error': str(e)}), 500
+
+@app.route('/api/longest-tickets')
+def longest_tickets():
+ """
+ Get list of tickets with longest duration (most time in service)
+ """
+ try:
+ client = APIClient()
+
+ # Login
+ if not client.login():
+ return jsonify({'error': 'Failed to authenticate'}), 500
+
+ # Get limit parameter or use default
+ limit = request.args.get('limit', 5, type=int)
+
+ # Fetch open tickets
+ open_tickets = client.get_open_tickets()
+
+ if not open_tickets:
+ return jsonify({
+ 'tickets': [],
+ 'count': 0,
+ 'timestamp': datetime.now().isoformat()
+ }), 200
+
+ # Format the data
+ formatted_tickets = DataFormatter.format_longest_tickets(open_tickets, limit)
+
+ return jsonify({
+ 'tickets': formatted_tickets,
+ 'count': len(formatted_tickets),
+ 'timestamp': datetime.now().isoformat()
+ }), 200
+ except Exception as e:
+ logger.error(f"Error in /api/longest-tickets: {str(e)}")
+ return jsonify({'error': str(e)}), 500
+
@app.errorhandler(404)
def not_found(error):
"""Handle 404 errors"""
@@ -181,6 +319,10 @@ def internal_error(error):
logger.error(f"Internal server error: {str(error)}")
return jsonify({'error': 'Internal server error'}), 500
+# Initialize background updater when app starts
+logger.info("Initializing background data updater...")
+start_background_updater()
+
if __name__ == '__main__':
try:
# Validate configuration
@@ -188,9 +330,13 @@ if __name__ == '__main__':
logger.info("Starting Grafana Monitoring Service...")
logger.info(f"API Base URL: {Config.API_BASE_URL}")
- logger.info(f"Refresh Interval: {Config.REFRESH_INTERVAL} seconds")
+ logger.info(f"Tickets Refresh Interval: {Config.TICKETS_REFRESH_INTERVAL} seconds")
+ logger.info(f"Dashboard Refresh Interval: {Config.DASHBOARD_REFRESH_INTERVAL} seconds")
logger.info(f"Queue IDs: {Config.QUEUE_IDS}")
+ # Start background data updater
+ start_background_updater()
+
# Start Flask app
app.run(
host=Config.FLASK_HOST,
diff --git a/src/config.py b/src/config.py
index 51e0aa1..827609f 100644
--- a/src/config.py
+++ b/src/config.py
@@ -11,6 +11,7 @@ class Config:
API_BASE_URL = os.getenv('API_BASE_URL', 'https://api-personalizeit.zapplataforma.chat')
API_LOGIN_ENDPOINT = os.getenv('API_LOGIN_ENDPOINT', '/auth/login')
API_TICKETS_ENDPOINT = os.getenv('API_TICKETS_ENDPOINT', '/tickets')
+ API_DASHBOARD_RESUME_ENDPOINT = os.getenv('API_DASHBOARD_RESUME_ENDPOINT', '/dashboard/resume/')
# Login Credentials
API_USERNAME = os.getenv('API_USERNAME', '')
@@ -21,8 +22,9 @@ class Config:
FLASK_HOST = os.getenv('FLASK_HOST', '0.0.0.0')
DEBUG = os.getenv('DEBUG', 'False').lower() == 'true'
- # Refresh interval in seconds
- REFRESH_INTERVAL = int(os.getenv('REFRESH_INTERVAL', 30))
+ # Refresh intervals in seconds
+ TICKETS_REFRESH_INTERVAL = int(os.getenv('TICKETS_REFRESH_INTERVAL', 5))
+ DASHBOARD_REFRESH_INTERVAL = int(os.getenv('DASHBOARD_REFRESH_INTERVAL', 60))
# Queue IDs
QUEUE_IDS = os.getenv('QUEUE_IDS', '-1,1,2,3,4,5,7')
diff --git a/src/data_formatter.py b/src/data_formatter.py
index 67748aa..ebc5bc5 100644
--- a/src/data_formatter.py
+++ b/src/data_formatter.py
@@ -24,6 +24,7 @@ class DataFormatter:
for ticket in tickets:
try:
# Handle None values safely
+ ticket_id = ticket.get('id', 'unknown')
contact = ticket.get('contact') or {}
queue = ticket.get('queue') or {}
whatsapp = ticket.get('whatsapp') or {}
@@ -72,7 +73,8 @@ class DataFormatter:
duration = 0
# Skip tickets with "EQUIPAMENTO EM MANUTENÇÃO" queue
- if 'EQUIPAMENTO EM MANUTENÇÃO' in queue_name:
+ if 'EQUIPAMENTO EM MANUTENÇÃO' in queue_name.strip().upper():
+ logger.info(f"Filtering out ticket {ticket.get('id')} with queue: {queue_name}")
continue
# Format duration as HH:MM:SS
@@ -154,7 +156,8 @@ class DataFormatter:
queue_name = tags
# Skip tickets with "EQUIPAMENTO EM MANUTENÇÃO" queue
- if 'EQUIPAMENTO EM MANUTENÇÃO' in queue_name:
+ if 'EQUIPAMENTO EM MANUTENÇÃO' in queue_name.strip().upper():
+ logger.info(f"Prometheus: Filtering out ticket {ticket.get('id')} with queue: {queue_name}")
continue
contact_name = contact.get('name', 'unknown').replace('"', '\\"')
@@ -201,6 +204,57 @@ class DataFormatter:
return "\n".join(metrics)
+ @staticmethod
+ def format_dashboard_metrics(resume_today: Dict[str, Any], resume_month: Dict[str, Any], longest_tickets: List[Dict[str, Any]]) -> str:
+ """
+ Format dashboard data as Prometheus metrics
+
+ Args:
+ resume_today: Dashboard resume data for today from API
+ resume_month: Dashboard resume data for the month from API
+ longest_tickets: List of longest tickets
+
+ Returns:
+ str: Prometheus format metrics
+ """
+ metrics = []
+
+ # Dashboard resume metrics
+ if resume_today or resume_month:
+ chamados_abertos = (resume_today.get('open', 0) if resume_today else 0)
+ chamados_mes = (resume_month.get('total', 0) if resume_month else 0)
+ chamados_concluidos = (resume_today.get('closed', 0) if resume_today else 0)
+
+ metrics.append("# HELP chamados_abertos_hoje Number of tickets currently open")
+ metrics.append("# TYPE chamados_abertos_hoje gauge")
+ metrics.append(f"chamados_abertos_hoje {chamados_abertos}")
+
+ metrics.append("\n# HELP chamados_do_mes Total tickets this month")
+ metrics.append("# TYPE chamados_do_mes gauge")
+ metrics.append(f"chamados_do_mes {chamados_mes}")
+
+ metrics.append("\n# HELP chamados_concluidos Closed tickets today")
+ metrics.append("# TYPE chamados_concluidos gauge")
+ metrics.append(f"chamados_concluidos {chamados_concluidos}")
+
+ # Longest tickets metrics
+ if longest_tickets:
+ metrics.append("\n# HELP longest_ticket_duration_seconds Duration of longest open tickets")
+ metrics.append("# TYPE longest_ticket_duration_seconds gauge")
+
+ for ticket in longest_tickets:
+ contact_name = ticket.get('contact_name', 'unknown').replace('"', '\\"')
+ user_name = ticket.get('user_name', 'unknown').replace('"', '\\"')
+ queue = ticket.get('queue', 'unknown').replace('"', '\\"')
+ duration_formatted = ticket.get('duration_formatted', '0s')
+ ticket_id = ticket.get('id', 0)
+ duration = ticket.get('duration_seconds', 0)
+
+ labels = f'ticket_id="{ticket_id}",contact_name="{contact_name}",user_name="{user_name}",queue="{queue}",duration_formatted="{duration_formatted}"'
+ metrics.append(f'longest_ticket_duration_seconds{{{labels}}} {duration}')
+
+ return "\n".join(metrics)
+
@staticmethod
def format_for_timeseries(tickets: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
@@ -225,7 +279,8 @@ class DataFormatter:
queue_name = queue.get('name', 'No Queue')
# Skip tickets with "EQUIPAMENTO EM MANUTENÇÃO" queue
- if 'EQUIPAMENTO EM MANUTENÇÃO' in queue_name:
+ if 'EQUIPAMENTO EM MANUTENÇÃO' in queue_name.strip().upper():
+ logger.info(f"Timeseries: Filtering out ticket {ticket.get('id')} with queue: {queue_name}")
continue
contact_name = contact.get('name', 'Unknown')
@@ -264,4 +319,134 @@ class DataFormatter:
]
})
- return time_series
+ return time_series
+ @staticmethod
+ def format_dashboard_resume(resume_today: Dict[str, Any], resume_month: Dict[str, Any]) -> Dict[str, Any]:
+ """
+ Format dashboard resume data for display
+
+ Args:
+ resume_today: Dashboard resume data for today from API
+ resume_month: Dashboard resume data for the month from API
+
+ Returns:
+ dict: Formatted dashboard resume data
+ """
+ if not resume_today and not resume_month:
+ return {
+ 'chamados_abertos_hoje': 0,
+ 'chamados_do_mes': 0,
+ 'chamados_concluidos': 0,
+ 'timestamp': datetime.now().isoformat()
+ }
+
+ # Chamados abertos hoje = tickets que estão abertos/em atendimento agora
+ chamados_abertos = (resume_today.get('open', 0) if resume_today else 0)
+
+ # Chamados do mês = total de tickets criados no mês
+ chamados_mes = (resume_month.get('total', 0) if resume_month else 0)
+
+ # Chamados concluídos = tickets fechados HOJE
+ chamados_concluidos = (resume_today.get('closed', 0) if resume_today else 0)
+
+ return {
+ 'chamados_abertos_hoje': chamados_abertos,
+ 'chamados_do_mes': chamados_mes,
+ 'chamados_concluidos': chamados_concluidos,
+ 'timestamp': datetime.now().isoformat()
+ }
+
+ @staticmethod
+ def format_longest_tickets(tickets: List[Dict[str, Any]], limit: int = 5) -> List[Dict[str, Any]]:
+ """
+ Format the tickets with longest duration
+
+ Args:
+ tickets: List of open ticket dictionaries from API
+ limit: Number of tickets to return (default: 5)
+
+ Returns:
+ list: List of formatted tickets sorted by duration (descending)
+ """
+ formatted_tickets = []
+ now = datetime.now()
+
+ logger.info(f"format_longest_tickets: Processing {len(tickets)} tickets")
+
+ for ticket in tickets:
+ try:
+ # Handle None values safely
+ contact = ticket.get('contact') or {}
+ user = ticket.get('user') or {}
+ queue = ticket.get('queue') or {}
+ tags = ticket.get('tags', [])
+ group_tags = ticket.get('groupTags', [])
+
+ contact_name = contact.get('name', 'Unknown')
+ user_name = user.get('name', 'Sem atendente')
+ ticket_id = ticket.get('id')
+ created_at = ticket.get('createdAt')
+
+ # Try to get queue name, or use tags/groupTags if available
+ queue_name = queue.get('name', 'No Queue')
+ if queue_name == 'No Queue':
+ # Try groupTags first - format is "id||name||color"
+ if group_tags:
+ if isinstance(group_tags, str) and '||' in group_tags:
+ parts = group_tags.split('||')
+ if len(parts) >= 2:
+ queue_name = parts[1]
+ elif isinstance(group_tags, list) and len(group_tags) > 0:
+ if isinstance(group_tags[0], dict):
+ queue_name = group_tags[0].get('tag', {}).get('name', 'No Queue')
+ else:
+ queue_name = str(group_tags[0])
+ # Then try tags
+ elif tags and isinstance(tags, list) and len(tags) > 0:
+ if isinstance(tags[0], dict):
+ queue_name = tags[0].get('name', 'No Queue')
+ else:
+ queue_name = str(tags[0])
+ elif isinstance(tags, str):
+ queue_name = tags
+
+ # Skip tickets with "EQUIPAMENTO EM MANUTENÇÃO" queue
+ if 'EQUIPAMENTO EM MANUTENÇÃO' in queue_name.strip().upper():
+ logger.info(f"Longest: Filtering out ticket {ticket.get('id')} with queue: {queue_name}")
+ continue
+
+ # Calculate duration dynamically from createdAt to now
+ duration = 0
+ if created_at:
+ try:
+ created_datetime = datetime.fromisoformat(created_at.replace('Z', '+00:00'))
+ # If timezone-aware, convert to naive for subtraction
+ if created_datetime.tzinfo is not None:
+ created_datetime = created_datetime.replace(tzinfo=None)
+ duration = int((now - created_datetime).total_seconds())
+ except (ValueError, AttributeError):
+ duration = 0
+
+ # Format duration as HH:MM:SS
+ hours = duration // 3600
+ minutes = (duration % 3600) // 60
+ seconds = duration % 60
+ duration_formatted = f"{hours:02d}h:{minutes:02d}m:{seconds:02d}s"
+
+ formatted_tickets.append({
+ 'id': ticket_id,
+ 'contact_name': contact_name,
+ 'user_name': user_name,
+ 'queue': queue_name,
+ 'duration_seconds': duration,
+ 'duration_formatted': duration_formatted,
+ 'created_at': created_at
+ })
+ except Exception as e:
+ logger.error(f"Error formatting ticket {ticket.get('id')}: {str(e)}")
+ continue
+
+ # Sort by duration (descending) and take the top N
+ formatted_tickets.sort(key=lambda x: x['duration_seconds'], reverse=True)
+
+ return formatted_tickets[:limit]
\ No newline at end of file