first commit
This commit is contained in:
273
EXAMPLES.md
Normal file
273
EXAMPLES.md
Normal file
@@ -0,0 +1,273 @@
|
||||
# 🔍 Exemplos de Uso
|
||||
|
||||
## API REST da Aplicação
|
||||
|
||||
### 1. Verificar Status
|
||||
```bash
|
||||
curl -X GET http://localhost:5000/
|
||||
```
|
||||
|
||||
**Resposta:**
|
||||
```json
|
||||
{
|
||||
"status": "running",
|
||||
"version": "1.0.0",
|
||||
"timestamp": "2026-02-09T12:30:00.000000",
|
||||
"endpoints": {
|
||||
"health": "/health",
|
||||
"json": "/api/tickets",
|
||||
"prometheus": "/metrics",
|
||||
"timeseries": "/api/timeseries"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Health Check
|
||||
```bash
|
||||
curl -X GET http://localhost:5000/health
|
||||
```
|
||||
|
||||
**Resposta:**
|
||||
```json
|
||||
{
|
||||
"status": "healthy",
|
||||
"cached_tickets": 10,
|
||||
"last_update": "2026-02-09T12:30:00.000000"
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Buscar Tickets (JSON)
|
||||
```bash
|
||||
curl -X GET http://localhost:5000/api/tickets | jq '.'
|
||||
```
|
||||
|
||||
**Resposta (reformatada):**
|
||||
```json
|
||||
{
|
||||
"timestamp": "2026-02-09T12:30:00.000000",
|
||||
"total_tickets": 10,
|
||||
"tickets": [
|
||||
{
|
||||
"id": 26459,
|
||||
"contact_name": "Júlia Donelli",
|
||||
"duration_seconds": 56,
|
||||
"duration_formatted": "00:00:56",
|
||||
"status": "pending",
|
||||
"queue": "EQUIPAMENTO EM MANUTENÇÃO",
|
||||
"contact_number": "555199448030",
|
||||
"created_at": "2026-02-09T12:13:11.000Z",
|
||||
"updated_at": "2026-02-09T12:14:07.000Z",
|
||||
"whatsapp_name": "PersonalizeIT"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Resumo Estatístico
|
||||
```bash
|
||||
curl -X GET http://localhost:5000/api/summary | jq '.'
|
||||
```
|
||||
|
||||
**Resposta:**
|
||||
```json
|
||||
{
|
||||
"total_tickets": 10,
|
||||
"average_duration_seconds": 1234.5,
|
||||
"max_duration_seconds": 1651623,
|
||||
"min_duration_seconds": 56,
|
||||
"total_duration_seconds": 12345,
|
||||
"timestamp": "2026-02-09T12:30:00.000000",
|
||||
"contacts": [
|
||||
"Júlia Donelli",
|
||||
"Atendimento Grafica Almeida",
|
||||
"ADICOM POLYTEK"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 5. Métricas Prometheus
|
||||
```bash
|
||||
curl -X GET http://localhost:5000/metrics
|
||||
```
|
||||
|
||||
**Resposta:**
|
||||
```
|
||||
# HELP ticket_duration_seconds Duration of ticket in seconds
|
||||
# TYPE ticket_duration_seconds gauge
|
||||
ticket_duration_seconds{ticket_id="26459",contact_name="Júlia Donelli",status="pending",queue="EQUIPAMENTO EM MANUTENÇÃO"} 56
|
||||
ticket_duration_seconds{ticket_id="26363",contact_name="Atendimento Grafica Almeida",status="pending",queue="EQUIPAMENTO EM MANUTENÇÃO"} 15003
|
||||
|
||||
# HELP tickets_total Total number of pending tickets
|
||||
# TYPE tickets_total gauge
|
||||
tickets_total 10
|
||||
|
||||
# HELP ticket_duration_seconds_avg Average ticket duration in seconds
|
||||
# TYPE ticket_duration_seconds_avg gauge
|
||||
ticket_duration_seconds_avg 1234.50
|
||||
```
|
||||
|
||||
### 6. Série Temporal
|
||||
```bash
|
||||
curl -X GET http://localhost:5000/api/timeseries | jq '.'
|
||||
```
|
||||
|
||||
## Queries do Grafana
|
||||
|
||||
### Com Prometheus Datasource
|
||||
|
||||
**Todos os tickets por contato:**
|
||||
```promql
|
||||
ticket_duration_seconds
|
||||
```
|
||||
|
||||
**Média de duração:**
|
||||
```promql
|
||||
avg(ticket_duration_seconds)
|
||||
```
|
||||
|
||||
**Máximo por contato (últimas 5 min):**
|
||||
```promql
|
||||
max_over_time(ticket_duration_seconds[5m])
|
||||
```
|
||||
|
||||
**Total de tickets:**
|
||||
```promql
|
||||
tickets_total
|
||||
```
|
||||
|
||||
**Tickets demorados (mais de 1 hora = 3600 segundos):**
|
||||
```promql
|
||||
ticket_duration_seconds > 3600
|
||||
```
|
||||
|
||||
### Com JSON API Datasource
|
||||
|
||||
**Todos os tickets:**
|
||||
```json
|
||||
{
|
||||
"data": "tickets"
|
||||
}
|
||||
```
|
||||
|
||||
**Apenas resumo:**
|
||||
```json
|
||||
{
|
||||
"data": "total_tickets"
|
||||
}
|
||||
```
|
||||
|
||||
## Python requests
|
||||
|
||||
```python
|
||||
import requests
|
||||
|
||||
# URL base
|
||||
BASE_URL = "http://localhost:5000"
|
||||
|
||||
# 1. Health check
|
||||
response = requests.get(f"{BASE_URL}/health")
|
||||
print(response.json())
|
||||
|
||||
# 2. Buscar tickets
|
||||
response = requests.get(f"{BASE_URL}/api/tickets")
|
||||
tickets = response.json()["tickets"]
|
||||
for ticket in tickets:
|
||||
print(f"{ticket['contact_name']}: {ticket['duration_formatted']}")
|
||||
|
||||
# 3. Resumo
|
||||
response = requests.get(f"{BASE_URL}/api/summary")
|
||||
summary = response.json()
|
||||
print(f"Total: {summary['total_tickets']}")
|
||||
print(f"Média: {summary['average_duration_seconds']:.2f}s")
|
||||
print(f"Máximo: {summary['max_duration_seconds']}s")
|
||||
|
||||
# 4. Processar métricas Prometheus
|
||||
response = requests.get(f"{BASE_URL}/metrics")
|
||||
print(response.text)
|
||||
```
|
||||
|
||||
## Bash scripts
|
||||
|
||||
### Monitorar atendimentos
|
||||
```bash
|
||||
#!/bin/bash
|
||||
|
||||
while true; do
|
||||
clear
|
||||
echo "=== Monitoramento de Atendimento ==="
|
||||
echo ""
|
||||
curl -s http://localhost:5000/api/summary | jq '.tickets[] | "\(.contact_name): \(.duration_formatted)"'
|
||||
echo ""
|
||||
sleep 30
|
||||
done
|
||||
```
|
||||
|
||||
### Enviar para webhook
|
||||
```bash
|
||||
#!/bin/bash
|
||||
|
||||
# Buscar dados
|
||||
DATA=$(curl -s http://localhost:5000/api/summary)
|
||||
|
||||
# Enviar para webhook
|
||||
curl -X POST \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "$DATA" \
|
||||
https://seu-webhook.com/monitoramento
|
||||
```
|
||||
|
||||
## Integração com Ferramentas Externas
|
||||
|
||||
### Slack Alertas
|
||||
```bash
|
||||
# Via Grafana Notification Channel
|
||||
# Grafana → Alerts → Notification channels → Slack
|
||||
# Mensagem quando tempo > 1 hora
|
||||
```
|
||||
|
||||
### PagerDuty
|
||||
```bash
|
||||
# Via Prometheus alertas
|
||||
# Crie arquivo alerts.yml:
|
||||
groups:
|
||||
- name: tickets
|
||||
rules:
|
||||
- alert: AtendimentoDemorado
|
||||
expr: ticket_duration_seconds > 3600
|
||||
for: 5m
|
||||
```
|
||||
|
||||
### Email
|
||||
```bash
|
||||
# Configure notificações no Grafana
|
||||
# Grafana → Alerts → Notification channels → Email
|
||||
```
|
||||
|
||||
## Debug
|
||||
|
||||
### Ver logs em tempo real
|
||||
```bash
|
||||
docker-compose logs -f monitoring-api
|
||||
```
|
||||
|
||||
### Testar conectividade com API PersonalizeIT
|
||||
```bash
|
||||
curl -X POST \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"email":"seu@email.com","password":"sua_senha"}' \
|
||||
https://api-personalizeit.zapplataforma.chat/auth/login
|
||||
```
|
||||
|
||||
### Verificar variáveis de ambiente
|
||||
```bash
|
||||
docker-compose exec monitoring-api env | grep API
|
||||
```
|
||||
|
||||
### Acessar console do container
|
||||
```bash
|
||||
docker-compose exec monitoring-api /bin/bash
|
||||
python3
|
||||
>>> from api_client import APIClient
|
||||
>>> client = APIClient()
|
||||
>>> client.login()
|
||||
```
|
||||
Reference in New Issue
Block a user