# APAC Weekly Tenant Metrics Lambda

Weekly Lambda flow:

1. Read aggregated metrics from `apac_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:

- APAC stock tenants: active external `active_candidate` rows with
  `sales_force_region = 'APAC'`, excluding JP and plans `free_trial`,
  `new_user`, and `closing`.
- Previous week new signups: APAC-country-region rows excluding JP, with
  `created_at >= week_start_utc` and `created_at < week_end_utc`, where those UTC
  timestamps represent CST Sunday 00:00 boundaries. This metric
  intentionally does not depend on `source_type`, because an active-candidate
  update can supersede the new-signup source on the unified tenant row.
- Active P0 + P1, connected, and paid metrics use the same APAC stock scope.

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.
