452 lines
20 KiB
Python
452 lines
20 KiB
Python
import logging
|
|
from typing import List, Dict, Any
|
|
from datetime import datetime
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class DataFormatter:
|
|
"""Formats ticket data for Grafana consumption"""
|
|
|
|
@staticmethod
|
|
def format_tickets_for_json(tickets: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
"""
|
|
Format tickets as JSON for Grafana
|
|
|
|
Args:
|
|
tickets: List of ticket dictionaries from API
|
|
|
|
Returns:
|
|
dict: Formatted data structure for Grafana
|
|
"""
|
|
formatted_tickets = []
|
|
now = datetime.now()
|
|
|
|
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 {}
|
|
tags = ticket.get('tags', [])
|
|
group_tags = ticket.get('groupTags', [])
|
|
|
|
contact_name = contact.get('name', 'Unknown')
|
|
ticket_id = ticket.get('id')
|
|
status = ticket.get('status')
|
|
created_at = ticket.get('createdAt')
|
|
updated_at = ticket.get('updatedAt')
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
# Skip tickets with "EQUIPAMENTO EM MANUTENÇÃO" queue
|
|
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
|
|
hours = duration // 3600
|
|
minutes = (duration % 3600) // 60
|
|
seconds = duration % 60
|
|
duration_formatted = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
|
|
|
|
formatted_tickets.append({
|
|
'id': ticket_id,
|
|
'contact_name': contact_name,
|
|
'duration_seconds': duration,
|
|
'duration_formatted': duration_formatted,
|
|
'status': status,
|
|
'queue': queue_name,
|
|
'created_at': created_at,
|
|
'updated_at': updated_at,
|
|
'contact_number': contact.get('number', ''),
|
|
'whatsapp_name': whatsapp.get('name', ''),
|
|
})
|
|
except Exception as e:
|
|
logger.error(f"Error formatting ticket {ticket.get('id')}: {str(e)}")
|
|
continue
|
|
|
|
return {
|
|
'timestamp': datetime.now().isoformat(),
|
|
'total_tickets': len(formatted_tickets),
|
|
'tickets': formatted_tickets
|
|
}
|
|
|
|
@staticmethod
|
|
def format_for_prometheus(tickets: List[Dict[str, Any]]) -> str:
|
|
"""
|
|
Format tickets as Prometheus metrics
|
|
|
|
Args:
|
|
tickets: List of ticket dictionaries from API
|
|
|
|
Returns:
|
|
str: Prometheus format metrics
|
|
"""
|
|
metrics = []
|
|
|
|
# Add help text
|
|
metrics.append("# HELP ticket_duration_milliseconds Duration of ticket in milliseconds")
|
|
metrics.append("# TYPE ticket_duration_milliseconds gauge")
|
|
|
|
# Add metrics for each ticket
|
|
now = datetime.now()
|
|
for ticket in tickets:
|
|
try:
|
|
# Handle None values safely
|
|
contact = ticket.get('contact') or {}
|
|
queue = ticket.get('queue') or {}
|
|
tags = ticket.get('tags', [])
|
|
group_tags = ticket.get('groupTags', [])
|
|
|
|
# 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"Prometheus: Filtering out ticket {ticket.get('id')} with queue: {queue_name}")
|
|
continue
|
|
|
|
contact_name = contact.get('name', 'unknown').replace('"', '\\"')
|
|
ticket_id = ticket.get('id')
|
|
status = ticket.get('status', 'unknown')
|
|
queue_name = queue_name.replace('"', '\\"')
|
|
|
|
# Calculate duration dynamically from createdAt to now
|
|
duration = 0
|
|
created_at = ticket.get('createdAt')
|
|
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 MM:SS for display
|
|
minutes = duration // 60
|
|
seconds = duration % 60
|
|
duration_formatted = f"{minutes}:{seconds:02d}s"
|
|
|
|
# Create metric with labels including formatted duration, use milliseconds for the value
|
|
labels = f'ticket_id="{ticket_id}",contact_name="{contact_name}",status="{status}",queue="{queue_name}",duration_formatted="{duration_formatted}"'
|
|
metrics.append(f'ticket_duration_milliseconds{{{labels}}} {duration * 1000}')
|
|
except Exception as e:
|
|
logger.error(f"Error formatting Prometheus metric for ticket {ticket.get('id')}: {str(e)}")
|
|
continue
|
|
|
|
# Add summary metrics
|
|
metrics.append("\n# HELP tickets_total Total number of pending tickets")
|
|
metrics.append("# TYPE tickets_total gauge")
|
|
metrics.append(f"tickets_total {len(tickets)}")
|
|
|
|
# Average duration
|
|
if tickets:
|
|
avg_duration = sum(t.get('duration', 0) for t in tickets) / len(tickets)
|
|
metrics.append("\n# HELP ticket_duration_seconds_avg Average ticket duration in seconds")
|
|
metrics.append("# TYPE ticket_duration_seconds_avg gauge")
|
|
metrics.append(f"ticket_duration_seconds_avg {avg_duration:.2f}")
|
|
|
|
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]]:
|
|
"""
|
|
Format tickets as time series data for Grafana
|
|
|
|
Args:
|
|
tickets: List of ticket dictionaries from API
|
|
|
|
Returns:
|
|
list: Time series formatted data
|
|
"""
|
|
time_series = []
|
|
now = datetime.now()
|
|
|
|
# Group by contact name
|
|
contacts = {}
|
|
for ticket in tickets:
|
|
# Handle None values safely
|
|
contact = ticket.get('contact') or {}
|
|
queue = ticket.get('queue') or {}
|
|
|
|
queue_name = queue.get('name', 'No Queue')
|
|
|
|
# Skip tickets with "EQUIPAMENTO EM MANUTENÇÃO" queue
|
|
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')
|
|
|
|
# Calculate duration dynamically from createdAt to now
|
|
duration = 0
|
|
created_at = ticket.get('createdAt')
|
|
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
|
|
|
|
if contact_name not in contacts:
|
|
contacts[contact_name] = {
|
|
'duration': 0,
|
|
'count': 0,
|
|
'tickets': []
|
|
}
|
|
|
|
contacts[contact_name]['duration'] += duration
|
|
contacts[contact_name]['count'] += 1
|
|
contacts[contact_name]['tickets'].append(ticket.get('id'))
|
|
|
|
# Format as time series
|
|
for contact_name, data in contacts.items():
|
|
time_series.append({
|
|
'target': contact_name,
|
|
'datapoints': [
|
|
[data['duration'], int(now.timestamp() * 1000)],
|
|
[data['count'], int(now.timestamp() * 1000)]
|
|
]
|
|
})
|
|
|
|
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] |