> ## Documentation Index
> Fetch the complete documentation index at: https://help.treble.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# fact_agent_status_changes

> Una fila por cada cambio de estado online/offline de un agente.

# fact\_agent\_status\_changes

Cada fila representa un cambio de estado (online/offline) de un agente en la plataforma de agentes.

## ¿Qué preguntas responde?

* ¿Cuándo se conectaron y desconectaron los agentes?
* ¿Cuántas horas estuvo disponible cada agente?
* ¿Cuál fue el horario de conexión de cada agente?

## Columnas

| Columna            | Tipo       | Descripción                                                                        |
| ------------------ | ---------- | ---------------------------------------------------------------------------------- |
| `status_change_id` | Int32      | Identificador único del evento                                                     |
| `company_id`       | Int32      | Identificador de la empresa (filtrado automáticamente)                             |
| `agent_id`         | Int32      | Identificador del agente                                                           |
| `agent_name`       | String     | Nombre del agente                                                                  |
| `created_at`       | DateTime64 | Fecha y hora del cambio de estado                                                  |
| `old_status`       | Bool       | Estado anterior. `true` = online, `false` = offline. `NULL` si es el primer evento |
| `new_status`       | Bool       | Nuevo estado. `true` = online, `false` = offline                                   |

## Consultas de ejemplo

### Horarios de conexión de agentes hoy

```sql theme={null}
SELECT
    agent_name,
    created_at,
    if(new_status, 'online', 'offline') AS estado
FROM client_analytics.fact_agent_status_changes
WHERE created_at >= today()
ORDER BY agent_name, created_at
```

### Minutos online por agente por día

```sql theme={null}
SELECT
    agent_name,
    toDate(created_at) AS dia,
    round(sum(if(new_status = true,
        dateDiff('minute', created_at,
            leadInFrame(created_at) OVER (PARTITION BY agent_id ORDER BY created_at)
        ), 0)) / 60, 1) AS horas_online
FROM client_analytics.fact_agent_status_changes
WHERE created_at >= now() - INTERVAL 7 DAY
GROUP BY agent_name, dia
ORDER BY dia DESC, agent_name
```
