# EMEA Weekly Tenant Metrics Lambda

Weekly Lambda flow:

1. Read aggregated metrics from `emea_active_tenants`.
2. Count the previous complete UTC Sunday-to-Saturday signup window.
3. Send the metrics to a Feishu group through `FEISHU_WEBHOOK_URL`.

The task runs every Monday at 15:00 Asia/Shanghai, which is 07:00 UTC.

Metrics:

- EMEA stock tenants: `country_region = 'EMEA'` and `source_type = 'active_candidate'`
- Previous week new signups: `source_type = 'new_signup'`, `created_at >= week_start_utc`, `created_at < week_end_utc`
- Active P0 + P1 tenants: EMEA rows with `priority IN ('P0', 'P1')`
- Connected tenants: EMEA rows with `track_step = 2`
- Paid tenants: EMEA rows with `has_credit_card = 1`

Required environment variables:

```text
TARGET_DB_HOST
TARGET_DB_PORT
TARGET_DB_USER
TARGET_DB_PASSWORD
TARGET_DB_NAME
FEISHU_WEBHOOK_URL
```

Optional environment variables:

```text
TARGET_DATABASE_URL
DATABASE_URL
TARGET_TIDB_SSL_CA_PATH
TIDB_SSL_CA_PATH
```

For TiDB Serverless, prefer `TARGET_DATABASE_URL`. `DATABASE_URL` is accepted as a fallback.

## Deploy

Build:

```bash
sam build
```

Deploy:

```bash
sam deploy
```

Replace the placeholder parameter values in `samconfig.toml` before deploying.
