# LATAM Weekly Tenant Metrics Lambda

Weekly Lambda flow:

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

- LATAM stock tenants: active external `active_candidate` rows with
  `sales_force_region = 'LATAM'`, excluding plans `free_trial`, `new_user`,
  and `closing`.
- Previous week new signups: LATAM-country-region rows with
  `created_at >= week_start_utc` and `created_at < week_end_utc`. 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 LATAM 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.

## Build and deploy

This function is deployed through the unified LATAM SAM stack. From the
`latam_lambda_tasks` directory, build all five functions with:

```bash
sam build --template-file template.yaml
```

Deploy the built stack only through the approved secret-injection workflow
described in the root README. This task directory intentionally has no separate
`samconfig.toml` or standalone CloudFormation stack.
