diff --git a/grafana/provisioning/dashboards/monitoring-dashboard.json b/grafana/provisioning/dashboards/monitoring-dashboard.json index 84882cd..42a9dee 100644 --- a/grafana/provisioning/dashboards/monitoring-dashboard.json +++ b/grafana/provisioning/dashboards/monitoring-dashboard.json @@ -455,7 +455,7 @@ }, "gridPos": { "h": 12, - "w": 24, + "w": 12, "x": 0, "y": 11 }, @@ -477,7 +477,145 @@ "type": "prometheus", "uid": "PBFA97CFB590B2093" }, - "expr": "ticket_duration_milliseconds", + "expr": "ticket_duration_milliseconds{status=\"group\"}", + "format": "table", + "instant": true, + "refId": "A" + } + ], + "title": "Grupos - Nome e Tempo", + "transformations": [ + { + "id": "organize", + "options": { + "excludeByName": { + "Time": true, + "__name__": true, + "duration_formatted": true, + "instance": true, + "job": true, + "queue": true, + "status": true, + "tag": true, + "ticket_duration_milliseconds": true, + "ticket_id": true + }, + "indexByName": { + "Value": 2, + "contact_name": 0, + "group_tag": 1 + }, + "renameByName": { + "Value": "TEMPO", + "contact_name": "PESSOA", + "group_tag": "TAG" + } + } + }, + { + "id": "convertFieldType", + "options": { + "conversions": [], + "fields": {} + } + } + ], + "type": "table" + }, + { + "fieldConfig": { + "defaults": { + "custom": { + "align": "left", + "cellOptions": { + "type": "auto" + }, + "filterable": false, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "TEMPO" + }, + "properties": [ + { + "id": "unit", + "value": "ms" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "color", + "value": { + "mode": "thresholds" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 60000 + } + ] + } + } + ] + } + ] + }, + "gridPos": { + "h": 12, + "w": 12, + "x": 12, + "y": 11 + }, + "id": 10, + "options": { + "cellHeight": "sm", + "showHeader": true, + "sortBy": [ + { + "desc": true, + "displayName": "TEMPO" + } + ] + }, + "pluginVersion": "12.3.2", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "expr": "ticket_duration_milliseconds{status!=\"group\"}", "format": "table", "instant": true, "refId": "A" @@ -492,8 +630,10 @@ "Time": true, "__name__": true, "duration_formatted": true, + "group_tag": true, "instance": true, "job": true, + "queue": true, "status": true, "ticket_duration_milliseconds": true, "ticket_id": true @@ -501,12 +641,12 @@ "indexByName": { "Value": 2, "contact_name": 0, - "queue": 1 + "tag": 1 }, "renameByName": { "Value": "TEMPO", "contact_name": "PESSOA", - "queue": "TAG" + "tag": "TAG" } } }, diff --git a/src/data_formatter.py b/src/data_formatter.py index ebc5bc5..acdd45c 100644 --- a/src/data_formatter.py +++ b/src/data_formatter.py @@ -132,28 +132,34 @@ class DataFormatter: tags = ticket.get('tags', []) group_tags = ticket.get('groupTags', []) - # Try to get queue name, or use tags/groupTags if available + # Extract group_tag from groupTags + group_tag = '' + if group_tags: + if isinstance(group_tags, str) and '||' in group_tags: + parts = group_tags.split('||') + if len(parts) >= 2: + group_tag = parts[1] + elif isinstance(group_tags, list) and len(group_tags) > 0: + if isinstance(group_tags[0], dict): + group_tag = group_tags[0].get('tag', {}).get('name', '') + else: + group_tag = str(group_tags[0]) + + # Extract tag from tags + tag = '' + if tags: + if isinstance(tags, list) and len(tags) > 0: + if isinstance(tags[0], dict): + tag = tags[0].get('name', '') + else: + tag = str(tags[0]) + elif isinstance(tags, str): + tag = tags + + # Get queue name for backward compatibility 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 + queue_name = group_tag or tag or 'no_queue' # Skip tickets with "EQUIPAMENTO EM MANUTENÇÃO" queue if 'EQUIPAMENTO EM MANUTENÇÃO' in queue_name.strip().upper(): @@ -164,6 +170,8 @@ class DataFormatter: ticket_id = ticket.get('id') status = ticket.get('status', 'unknown') queue_name = queue_name.replace('"', '\\"') + group_tag = group_tag.replace('"', '\\"') + tag = tag.replace('"', '\\"') # Calculate duration dynamically from createdAt to now duration = 0 @@ -184,7 +192,7 @@ class DataFormatter: 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}"' + labels = f'ticket_id="{ticket_id}",contact_name="{contact_name}",status="{status}",queue="{queue_name}",group_tag="{group_tag}",tag="{tag}",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)}")