268 lines
12 KiB
Python
268 lines
12 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
|
|
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:
|
|
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:
|
|
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_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:
|
|
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
|