This commit is contained in:
@@ -520,7 +520,7 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"gridPos": {
|
"gridPos": {
|
||||||
"h": 12,
|
"h": 8,
|
||||||
"w": 12,
|
"w": 12,
|
||||||
"x": 0,
|
"x": 0,
|
||||||
"y": 11
|
"y": 11
|
||||||
@@ -588,6 +588,132 @@
|
|||||||
],
|
],
|
||||||
"type": "table"
|
"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": "red",
|
||||||
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "orange",
|
||||||
|
"value": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "yellow",
|
||||||
|
"value": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": 5
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"overrides": [
|
||||||
|
{
|
||||||
|
"matcher": {
|
||||||
|
"id": "byName",
|
||||||
|
"options": "Value"
|
||||||
|
},
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"id": "custom.cellOptions",
|
||||||
|
"value": {
|
||||||
|
"type": "color-background"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "decimals",
|
||||||
|
"value": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "custom.width",
|
||||||
|
"value": 90
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 4,
|
||||||
|
"w": 12,
|
||||||
|
"x": 0,
|
||||||
|
"y": 19
|
||||||
|
},
|
||||||
|
"id": 13,
|
||||||
|
"options": {
|
||||||
|
"cellHeight": "sm",
|
||||||
|
"showHeader": true,
|
||||||
|
"sortBy": [
|
||||||
|
{
|
||||||
|
"desc": false,
|
||||||
|
"displayName": "Nota"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"pluginVersion": "12.3.2",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "PBFA97CFB590B2093"
|
||||||
|
},
|
||||||
|
"expr": "closed_ticket_rate_below_5",
|
||||||
|
"format": "table",
|
||||||
|
"instant": true,
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "Avaliações abaixo de 5 (Hoje)",
|
||||||
|
"transformations": [
|
||||||
|
{
|
||||||
|
"id": "organize",
|
||||||
|
"options": {
|
||||||
|
"excludeByName": {
|
||||||
|
"Time": true,
|
||||||
|
"__name__": true,
|
||||||
|
"instance": true,
|
||||||
|
"job": true,
|
||||||
|
"ticket_id": true
|
||||||
|
},
|
||||||
|
"indexByName": {
|
||||||
|
"Value": 3,
|
||||||
|
"closed_at": 4,
|
||||||
|
"contact_name": 0,
|
||||||
|
"queue": 2,
|
||||||
|
"user_name": 1
|
||||||
|
},
|
||||||
|
"renameByName": {
|
||||||
|
"Value": "Nota",
|
||||||
|
"closed_at": "Fechado em",
|
||||||
|
"contact_name": "Cliente",
|
||||||
|
"queue": "TAG",
|
||||||
|
"user_name": "Atendente"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"type": "table"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"fieldConfig": {
|
"fieldConfig": {
|
||||||
"defaults": {
|
"defaults": {
|
||||||
|
|||||||
@@ -318,3 +318,103 @@ class APIClient:
|
|||||||
total_open = len(open_tickets)
|
total_open = len(open_tickets)
|
||||||
logger.info(f"Fetched total open tickets: {total_open}")
|
logger.info(f"Fetched total open tickets: {total_open}")
|
||||||
return total_open
|
return total_open
|
||||||
|
|
||||||
|
def get_closed_tickets_with_low_rate(
|
||||||
|
self,
|
||||||
|
min_rate: float = 5,
|
||||||
|
limit: int = 30,
|
||||||
|
max_pages: int = 15
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Fetch recent closed tickets with customer rate below a threshold.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
min_rate: Maximum accepted score is min_rate - epsilon (default: < 5)
|
||||||
|
limit: Maximum number of tickets to return
|
||||||
|
max_pages: Maximum number of API pages to scan
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: Closed tickets with low ratings
|
||||||
|
"""
|
||||||
|
if not self.token:
|
||||||
|
logger.error("Not authenticated. Please login first.")
|
||||||
|
return []
|
||||||
|
|
||||||
|
low_rated_tickets: List[Dict[str, Any]] = []
|
||||||
|
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}"
|
||||||
|
|
||||||
|
while page <= max_pages and len(low_rated_tickets) < limit:
|
||||||
|
params = {
|
||||||
|
'pageNumber': page,
|
||||||
|
'status': 'closed',
|
||||||
|
'showAll': 'true',
|
||||||
|
'queueIds': json.dumps(queue_ids),
|
||||||
|
'tagIds': '[]',
|
||||||
|
'isGroup': 'false',
|
||||||
|
'sortDir': 'DESC',
|
||||||
|
'isSearching': 'false'
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(f"Fetching closed 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 closed tickets: {response.status_code} - {response.text}")
|
||||||
|
break
|
||||||
|
|
||||||
|
result = response.json()
|
||||||
|
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
|
||||||
|
|
||||||
|
for ticket in page_tickets:
|
||||||
|
rate = ticket.get('rate')
|
||||||
|
if rate is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
numeric_rate = float(rate)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if numeric_rate < min_rate:
|
||||||
|
low_rated_tickets.append(ticket)
|
||||||
|
if len(low_rated_tickets) >= limit:
|
||||||
|
break
|
||||||
|
|
||||||
|
has_more = result.get('hasMore')
|
||||||
|
if isinstance(has_more, bool):
|
||||||
|
if not has_more:
|
||||||
|
break
|
||||||
|
elif expected_total is not None:
|
||||||
|
scanned = page * len(page_tickets)
|
||||||
|
if scanned >= expected_total:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
page += 1
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"Fetched {len(low_rated_tickets)} closed tickets with rate below {min_rate} "
|
||||||
|
f"(scanned up to page {page})"
|
||||||
|
)
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
logger.error(f"Error fetching closed tickets with low rate: {str(e)}")
|
||||||
|
|
||||||
|
return low_rated_tickets
|
||||||
|
|||||||
22
src/app.py
22
src/app.py
@@ -23,6 +23,7 @@ cached_json = None
|
|||||||
cached_metrics = None
|
cached_metrics = None
|
||||||
cached_dashboard_resume = None
|
cached_dashboard_resume = None
|
||||||
cached_longest_tickets = None
|
cached_longest_tickets = None
|
||||||
|
cached_low_rated_closed_tickets = []
|
||||||
cached_total_open_tickets = 0
|
cached_total_open_tickets = 0
|
||||||
last_tickets_update = None
|
last_tickets_update = None
|
||||||
last_dashboard_update = None
|
last_dashboard_update = None
|
||||||
@@ -31,7 +32,7 @@ stop_background_thread = False
|
|||||||
|
|
||||||
def background_data_updater():
|
def background_data_updater():
|
||||||
"""Background thread that updates data continuously"""
|
"""Background thread that updates data continuously"""
|
||||||
global cached_tickets, cached_json, cached_metrics, cached_dashboard_resume, cached_longest_tickets, cached_total_open_tickets
|
global cached_tickets, cached_json, cached_metrics, cached_dashboard_resume, cached_longest_tickets, cached_low_rated_closed_tickets, cached_total_open_tickets
|
||||||
global last_tickets_update, last_dashboard_update, stop_background_thread
|
global last_tickets_update, last_dashboard_update, stop_background_thread
|
||||||
|
|
||||||
tickets_interval = max(1, Config.TICKETS_REFRESH_INTERVAL)
|
tickets_interval = max(1, Config.TICKETS_REFRESH_INTERVAL)
|
||||||
@@ -86,6 +87,15 @@ def background_data_updater():
|
|||||||
open_tickets = client.get_open_tickets()
|
open_tickets = client.get_open_tickets()
|
||||||
cached_total_open_tickets = len(open_tickets)
|
cached_total_open_tickets = len(open_tickets)
|
||||||
cached_longest_tickets = DataFormatter.format_longest_tickets(open_tickets, limit=5)
|
cached_longest_tickets = DataFormatter.format_longest_tickets(open_tickets, limit=5)
|
||||||
|
|
||||||
|
low_rated_closed = client.get_closed_tickets_with_low_rate(min_rate=5, limit=15)
|
||||||
|
cached_low_rated_closed_tickets = DataFormatter.format_low_rated_closed_tickets(
|
||||||
|
low_rated_closed,
|
||||||
|
max_rate=5,
|
||||||
|
limit=15,
|
||||||
|
only_today=True
|
||||||
|
)
|
||||||
|
|
||||||
last_dashboard_update = datetime.now()
|
last_dashboard_update = datetime.now()
|
||||||
logger.info("Background updater: Successfully updated dashboard metrics")
|
logger.info("Background updater: Successfully updated dashboard metrics")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -141,7 +151,8 @@ def health():
|
|||||||
'status': 'healthy',
|
'status': 'healthy',
|
||||||
'cached_tickets': len(cached_tickets),
|
'cached_tickets': len(cached_tickets),
|
||||||
'dashboard_resume_cached': cached_dashboard_resume is not None,
|
'dashboard_resume_cached': cached_dashboard_resume is not None,
|
||||||
'longest_tickets_cached': cached_longest_tickets is not None
|
'longest_tickets_cached': cached_longest_tickets is not None,
|
||||||
|
'low_rated_closed_cached': cached_low_rated_closed_tickets is not None
|
||||||
}), 200
|
}), 200
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Health check failed: {str(e)}")
|
logger.error(f"Health check failed: {str(e)}")
|
||||||
@@ -179,14 +190,15 @@ def metrics():
|
|||||||
all_metrics.append(cached_metrics)
|
all_metrics.append(cached_metrics)
|
||||||
|
|
||||||
# Add dashboard metrics
|
# Add dashboard metrics
|
||||||
if cached_dashboard_resume and cached_longest_tickets:
|
if cached_dashboard_resume:
|
||||||
resume_today = cached_dashboard_resume.get('today')
|
resume_today = cached_dashboard_resume.get('today')
|
||||||
resume_month = cached_dashboard_resume.get('month')
|
resume_month = cached_dashboard_resume.get('month')
|
||||||
dashboard_metrics = DataFormatter.format_dashboard_metrics(
|
dashboard_metrics = DataFormatter.format_dashboard_metrics(
|
||||||
resume_today,
|
resume_today,
|
||||||
resume_month,
|
resume_month,
|
||||||
cached_longest_tickets,
|
cached_longest_tickets or [],
|
||||||
cached_total_open_tickets
|
cached_total_open_tickets,
|
||||||
|
cached_low_rated_closed_tickets or []
|
||||||
)
|
)
|
||||||
if dashboard_metrics:
|
if dashboard_metrics:
|
||||||
all_metrics.append(dashboard_metrics)
|
all_metrics.append(dashboard_metrics)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import List, Dict, Any
|
from typing import List, Dict, Any, Optional
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -217,7 +217,8 @@ class DataFormatter:
|
|||||||
resume_today: Dict[str, Any],
|
resume_today: Dict[str, Any],
|
||||||
resume_month: Dict[str, Any],
|
resume_month: Dict[str, Any],
|
||||||
longest_tickets: List[Dict[str, Any]],
|
longest_tickets: List[Dict[str, Any]],
|
||||||
chamados_total: int = 0
|
chamados_total: int = 0,
|
||||||
|
low_rated_closed_tickets: Optional[List[Dict[str, Any]]] = None
|
||||||
) -> str:
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Format dashboard data as Prometheus metrics
|
Format dashboard data as Prometheus metrics
|
||||||
@@ -227,6 +228,7 @@ class DataFormatter:
|
|||||||
resume_month: Dashboard resume data for the month from API
|
resume_month: Dashboard resume data for the month from API
|
||||||
longest_tickets: List of longest tickets
|
longest_tickets: List of longest tickets
|
||||||
chamados_total: Total number of open tickets (showAll=true)
|
chamados_total: Total number of open tickets (showAll=true)
|
||||||
|
low_rated_closed_tickets: Closed tickets where customer score is below 5
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: Prometheus format metrics
|
str: Prometheus format metrics
|
||||||
@@ -280,8 +282,124 @@ class DataFormatter:
|
|||||||
|
|
||||||
labels = f'ticket_id="{ticket_id}",contact_name="{contact_name}",user_name="{user_name}",queue="{queue}",duration_formatted="{duration_formatted}"'
|
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}')
|
metrics.append(f'longest_ticket_duration_seconds{{{labels}}} {duration}')
|
||||||
|
|
||||||
|
if low_rated_closed_tickets:
|
||||||
|
metrics.append("\n# HELP closed_ticket_rate_below_5 Customer rating for closed tickets below 5")
|
||||||
|
metrics.append("# TYPE closed_ticket_rate_below_5 gauge")
|
||||||
|
|
||||||
|
for ticket in low_rated_closed_tickets:
|
||||||
|
contact_name = ticket.get('contact_name', 'unknown').replace('"', '\\"')
|
||||||
|
user_name = ticket.get('user_name', 'unknown').replace('"', '\\"')
|
||||||
|
queue = ticket.get('queue', 'unknown').replace('"', '\\"')
|
||||||
|
closed_at = ticket.get('updated_at', 'unknown').replace('"', '\\"')
|
||||||
|
ticket_id = ticket.get('id', 0)
|
||||||
|
rate = ticket.get('rate', 0)
|
||||||
|
|
||||||
|
labels = f'ticket_id="{ticket_id}",contact_name="{contact_name}",user_name="{user_name}",queue="{queue}",closed_at="{closed_at}"'
|
||||||
|
metrics.append(f'closed_ticket_rate_below_5{{{labels}}} {rate}')
|
||||||
|
|
||||||
return "\n".join(metrics)
|
return "\n".join(metrics)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def format_low_rated_closed_tickets(
|
||||||
|
tickets: List[Dict[str, Any]],
|
||||||
|
max_rate: float = 5,
|
||||||
|
limit: int = 15,
|
||||||
|
only_today: bool = True
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Format closed tickets where the customer score (rate) is below max_rate.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
tickets: List of closed ticket dictionaries from API
|
||||||
|
max_rate: Maximum score threshold (exclusive)
|
||||||
|
limit: Maximum number of rows to return
|
||||||
|
only_today: When True, include only tickets closed on current day
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: Formatted list sorted by lower score first
|
||||||
|
"""
|
||||||
|
formatted_tickets = []
|
||||||
|
today = datetime.now().date()
|
||||||
|
|
||||||
|
for ticket in tickets:
|
||||||
|
try:
|
||||||
|
rate = ticket.get('rate')
|
||||||
|
if rate is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
rate_value = float(rate)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if rate_value >= max_rate:
|
||||||
|
continue
|
||||||
|
|
||||||
|
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')
|
||||||
|
updated_at = ticket.get('updatedAt') or ticket.get('createdAt') or ''
|
||||||
|
|
||||||
|
if only_today:
|
||||||
|
closed_date = None
|
||||||
|
try:
|
||||||
|
closed_datetime = datetime.fromisoformat(updated_at.replace('Z', '+00:00'))
|
||||||
|
closed_date = closed_datetime.date()
|
||||||
|
except (ValueError, AttributeError):
|
||||||
|
# Fallback for unexpected formats that still start with YYYY-MM-DD.
|
||||||
|
if isinstance(updated_at, str) and len(updated_at) >= 10:
|
||||||
|
try:
|
||||||
|
closed_date = datetime.strptime(updated_at[:10], '%Y-%m-%d').date()
|
||||||
|
except ValueError:
|
||||||
|
closed_date = None
|
||||||
|
|
||||||
|
if closed_date != today:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Try to derive queue/tag name from queue, group tags, or plain tags.
|
||||||
|
queue_name = queue.get('name') if isinstance(queue, dict) else None
|
||||||
|
if not queue_name:
|
||||||
|
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')
|
||||||
|
else:
|
||||||
|
queue_name = str(group_tags[0])
|
||||||
|
|
||||||
|
if not queue_name:
|
||||||
|
if tags and isinstance(tags, list) and len(tags) > 0:
|
||||||
|
if isinstance(tags[0], dict):
|
||||||
|
queue_name = tags[0].get('name')
|
||||||
|
else:
|
||||||
|
queue_name = str(tags[0])
|
||||||
|
elif isinstance(tags, str):
|
||||||
|
queue_name = tags
|
||||||
|
|
||||||
|
formatted_tickets.append({
|
||||||
|
'id': ticket_id,
|
||||||
|
'contact_name': contact_name,
|
||||||
|
'user_name': user_name,
|
||||||
|
'queue': queue_name or 'Sem fila',
|
||||||
|
'rate': rate_value,
|
||||||
|
'updated_at': updated_at
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error formatting low-rated closed ticket {ticket.get('id')}: {str(e)}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
formatted_tickets.sort(key=lambda item: (item.get('rate', 5), item.get('updated_at', '')), reverse=False)
|
||||||
|
return formatted_tickets[:limit]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def format_for_timeseries(tickets: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
def format_for_timeseries(tickets: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||||
|
|||||||
Reference in New Issue
Block a user