> ## 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

> Uma linha por cada mudança de estado online/offline de um agente.

# fact\_agent\_status\_changes

Cada linha representa uma mudança de estado (online/offline) de um agente na plataforma de agentes.

## Que perguntas responde?

* Quando os agentes se conectaram e desconectaram?
* Quantas horas cada agente esteve disponível?
* Qual foi o horário de conexão de cada agente?

## Colunas

| Coluna             | Tipo       | Descrição                                                                            |
| ------------------ | ---------- | ------------------------------------------------------------------------------------ |
| `status_change_id` | Int32      | Identificador único do evento                                                        |
| `company_id`       | Int32      | Identificador da empresa (filtrado automaticamente)                                  |
| `agent_id`         | Int32      | Identificador do agente                                                              |
| `agent_name`       | String     | Nome do agente                                                                       |
| `created_at`       | DateTime64 | Data e hora da mudança de estado                                                     |
| `old_status`       | Bool       | Estado anterior. `true` = online, `false` = offline. `NULL` se for o primeiro evento |
| `new_status`       | Bool       | Novo estado. `true` = online, `false` = offline                                      |

## Consultas de exemplo

### Horários de conexão de agentes hoje

```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 dia

```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
```
