# Number Guard Monitoring Dashboard

Simple guide to how the monitoring dashboard works today.

---

## 1. What it is

A **read-only** page that shows Number Guard calling health for one selected date.

- It only **reads** from `reports` and `calling_queue`
- It does **not** change live calling
- Access needs permission: `monitoring dashboard`

### Main files

| File | Purpose |
|------|---------|
| `app/Libraries/CallingMonitoring.php` | SQL counts and stage rules |
| `app/Http/Controllers/CallingMonitoringController.php` | Dashboard page + data endpoint |
| `resources/views/CallingMonitoring/index.blade.php` | UI and charts |
| `public/css/calling-monitoring.css` | Styles |
| `routes/web.php` | Admin page routes |

---

## 2. How to open it

**Page:** `/monitoring-dashboard`  
**Data API used by the page:** `/monitoring-dashboard/data`

### Filters on the page

| Filter | Meaning |
|--------|---------|
| Queue Type | Which queue to measure (default: Calling Queue) |
| Date | One calendar day (`Y-m-d`) |
| Compare Previous Date | Also load previous day for side-by-side compare |

### Example

```
GET /monitoring-dashboard/data?date=2026-09-16&queue_type=calling_queue&compare=1
```

---

## 3. Queue types

All counts are Number Guard only (`is_number_guard = 1` **or** Number Guard plan groups), then filtered by one queue type:

| Dropdown option | `queue_type` value | DB filter |
|-----------------|--------------------|-----------|
| Calling Queue (default) | `calling_queue` | `calling_type = calling_queue` |
| Dynamic Daily | `dynamic_daily` | `calling_type = dynamic_calling` and `call_type_notes` in `e-dynamic_daily`, `j-zoho_ticket` |
| Dynamic GTAD | `dynamic_gtad` | `calling_type = dynamic_calling` and `call_type_notes` in `g-dynamic_gtad`, `j-zoho_ticket` |
| Warmup Queue | `warmup_queue` | `calling_type = warm_up_queue` |

`j-zoho_ticket` is kept for dynamic queues so ticket/Zoho stages still count after Zoho remapping.

---

## 4. Page layout (top to bottom)

1. **Tiles**
2. **Workflow Volume Trend** + **Reconciliation Gap Trend**
3. **Carrier Hourly Breakup**

When **Compare Previous Date** is checked:

- Volume, Gap, and Carrier each show **two charts** (selected date | previous date) in one row
- Tiles also show `vs prev` count and % change

---

## 5. Stage tiles

Tiles come from `current.tiles`. Order on screen:

| Tile | Key | Meaning |
|------|-----|---------|
| Queue Created | `queue_created` | Queue volume for the day (`max(calling_queue count, reports count)`) |
| VSP / Sinch Processed | `vsp_sinch_processed` | Non-warmup: `detection_type = 021` and status 900–999. Warmup: `detection_type = 011` and status 0–9 |
| Android Retrieved | `android_retrieved` | Android observation retrieved (`app_response_status = 1`) |
| Blocked Call | `blocked_calls` | `is_blocked = 1` |
| Labeled Call | `labeled_calls` | `is_labeled = 1` |
| Retried Calls Count | `retried_calls` | `COUNT(device_retry_calls)` by `retry_date` + calling type / notes (same mapping as `CallingTotalCounts`) |
| Calls Initiated | `calls_initiated` | `reports` with `do_call = 1` and `status = 2` (same filters as `CallingTotalCounts`) |
| Tickets Created | `tickets_created` | `COUNT(pubsub_records)` for the selected date (ticket published) |
| Zoho Work Orders | `zoho_work_orders` | `COUNT(reptest_outgoing_response)` where `status = 1` for the selected date |

### Severity badge on tiles

- Pipeline tiles (Queue, VSP / Sinch, Android, Tickets, Zoho) use drop severity from the previous pipeline tile  
  - `< 1%` healthy, `1–5%` warning, `5–15%` danger, `≥ 15%` critical
- **Blocked Call**, **Labeled Call**, **Retried Calls Count**, and **Calls Initiated** do not use drop severity (they are subset counts, not the next pipeline step)

---

## 6. Charts and tables

### Workflow Volume Trend

Hourly lines for:

- Queue Created
- VSP / Sinch Processed
- Android Retrieved
- Tickets Created

### Reconciliation Gap Trend

Hourly gap lines:

- Android Gap = `max(vsp_sinch_processed - android_retrieved, 0)`
- Ticket Gap = `max(android_retrieved - tickets_created, 0)`
- Zoho Gap = `max(tickets_created - zoho_work_orders, 0)`
- Total Gap = `max(queue_created - zoho_work_orders, 0)`

### Hour window

| Environment | Hours |
|-------------|-------|
| Production | 8 AM – 10 PM |
| Local | 1 AM – 10 PM |

Hour bucket uses:

```sql
HOUR(COALESCE(NULLIF(telephony_call_time, '0000-00-00 00:00:00'), created_at))
```

### Carrier Hourly Breakup

Stacked bars: hour on X, carrier on series, call count on Y.

---

## 7. Dashboard data payload

`/monitoring-dashboard/data` returns:

```json
{
  "success": true,
  "data": {
    "date": "2026-09-16",
    "queue_type": "calling_queue",
    "previous_date": "2026-09-15",
    "current": {
      "tiles": { "...": "..." },
      "volume_trend": { "...": "..." },
      "carrier_hourly": { "...": "..." }
    },
    "previous": null
  }
}
```

If `compare=1`, `previous` includes:

- `tiles`
- `volume_trend`
- `carrier_hourly`

---

## 8. Tile count rules

| Key | Rule |
|-----|------|
| `queue_created` | `max(COUNT(calling_queue), COUNT(reports))` for the date + Number Guard + queue type |
| `android_retrieved` | `reports.app_response_status = 1` |
| `blocked_calls` | `reports.is_blocked = 1` |
| `labeled_calls` | `reports.is_labeled = 1` |
| `retried_calls` | `device_retry_calls` by `retry_date` + calling type / notes (same mapping as `CallingTotalCounts`) |
| `calls_initiated` | `reports` with `do_call = 1` and `status = 2` (same filters as `CallingTotalCounts`) |
| `vsp_sinch_processed` | `reptest_outgoing_response`: non-warmup `detection_type = 021` and status 900–999; warmup `detection_type = 011` and status 0–9 |
| `tickets_created` | `COUNT(pubsub_records)` where `created_date` = selected date |
| `zoho_work_orders` | `COUNT(reptest_outgoing_response)` where `status = 1` and `created_at` is on the selected date |

---

## 9. Quick notes

- Tickets Created counts `pubsub_records` for the date; Zoho Work Orders counts `reptest_outgoing_response` where `status = 1`
- VSP and Sinch share one tile and chart series: **VSP / Sinch Processed**
- Loader uses existing app class `loading_overlay`
- Dashboard is permission-gated; superadmin is allowed via `Gate::before`
