# ngauge-datadash

Yii2 module for NGauge that provides configurable data-feed endpoints, CSV/order downloads, and an external database DSN configuration UI.

## Features

- **Data-feed endpoints** — Upload, order downloads, CSV exports, and Tour Operator data feeds (SLT, SOH, SOD, STA, SLA)
- **Database config page** — Web UI to build and persist an external database DSN (MySQL, PostgreSQL, SQL Server, SQLite)
- **Credential management** — Username / password with optional encryption at rest via the backend `Security` component
- **Test Connection** — Live PDO connection test returning driver and server version details
- **List Tables** — Connects and returns all tables with row counts, data size, index size, and total size (MB)
- **Auto-bootstrap** — Registers a `datadash` Yii db component on every bootstrapping application from saved params

---

## Requirements

| Dependency | Version |
|---|---|
| PHP | ≥ 8.0 |
| yiisoft/yii2 | ~2.0.45 |
| ext-pdo | * |
| ext-json | * |
| ext-mbstring | * |

---

## Installation

Add to your application's `composer.json`:

```json
"portalpeople/ngauge-datadash": "^1.1"
```

Then register the bootstrap in your application config:

```php
// common/config/main.php
'bootstrap' => ['ngauge-datadash'],
'modules'   => [
    'ngauge-datadash' => [
        'class' => PortalPeople\datadash\Module::class,
    ],
],
```

---

## Configuration

### Via the web UI

Navigate to `/ngauge-datadash/config` (requires an authenticated user).

Fill in:

| Field | Description |
|---|---|
| **Database Driver** | `mysql`, `pgsql`, `sqlsrv`, or `sqlite` |
| **Host** | Server hostname or IP |
| **Port** | Defaults auto-fill per driver (MySQL 3306, PostgreSQL 5432, SQL Server 1433) |
| **Database Name** | Schema / database to connect to |
| **Charset** | Recommended `utf8mb4` for MySQL |
| **Username** | Database account username |
| **Password** | Stored encrypted when *Encrypt Credentials at Rest* is enabled |
| **Encrypt at Rest** | Encrypts the password via `backend\components\Security` before writing to `params-local.php` |
| **Persistent Connections** | Enables PDO persistent connections |

Click **Save Configuration** to persist, **Test Connection** to verify credentials live, or **List Tables** to inspect the target database.

### Via params-local.php (manual)

```php
// backend/config/params-local.php
return [
    'plugins' => [
        'ngauge-datadash' => [
            'enabled'       => true,
            'driver'        => 'mysql',
            'host'          => 'db.example.com',
            'port'          => 3306,
            'dbname'        => 'my_database',
            'charset'       => 'utf8mb4',
            'username'      => 'db_user',
            'password'      => 'encrypted-or-plain-password',
            'encryptAtRest' => true,
            'persistent'    => false,
        ],
    ],
];
```

---

## Bootstrapped DB Component

Once saved, the module registers a `datadash` Yii db component automatically on every app bootstrap:

```php
$db = Yii::$app->get('datadash');   // yii\db\Connection
$rows = $db->createCommand('SELECT * FROM my_table')->queryAll();
```

If the component is already defined in your application config it will not be overwritten.

---

## Routes

### Config UI

| Method | URL | Action |
|---|---|---|
| GET / POST | `/ngauge-datadash/config` | Show / save config form |
| POST | `/ngauge-datadash/config/test-connection` | JSON — test PDO credentials |
| POST | `/ngauge-datadash/config/list-tables` | JSON — list tables with sizes |
| POST | `/ngauge-datadash/config/validate` | JSON — ActiveForm AJAX validation |

### Data-feed endpoints

| Method | URL | Description |
|---|---|---|
| POST | `/df/data/upload` | Accept compressed data file upload |
| GET | `/df/data/downloadorders` | Un-downloaded Paid/Ordered orders |
| GET | `/df/data/downloadpaidorders` | Paid orders download |
| GET | `/df/data/downloadcsv` | Orders as CSV |
| GET | `/df/data/downloadpaidcsv` | Paid orders as CSV |
| GET | `/df/data/downloadcsvgroup` | Group CSV download |
| POST | `/df/data/downloaded` | Mark orders as downloaded |
| GET | `/df/data-services/slt` | Tour Operator SLT feed |
| GET | `/df/data-services/soh` | Tour Operator SOH feed |
| GET | `/df/data-services/sod` | Tour Operator SOD feed |
| GET | `/df/data-services/sta` | Tour Operator STA feed |
| GET | `/df/data-services/sla` | Tour Operator SLA feed |

---

## Changelog

See [CHANGELOG.md](CHANGELOG.md).
