Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#
# Secrets required (set in the "dev" GitHub Environment):
# UNSPLASH_ACCESS_KEY — Unsplash API key for SSM
# SNS_FROM_NUMBER — SMS origination number (TF_VAR_sns_from_number)
# ALERT_EMAIL — owner email for the billing alarm + research-download notices
# Secrets required at repo level:
# SONAR_TOKEN — SonarCloud analysis token
#
Expand Down Expand Up @@ -226,6 +228,7 @@ jobs:
TF_VAR_environment: ${{ env.ENVIRONMENT }}
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: |
terraform apply -auto-approve \
-target=aws_ecr_repository.lambda \
Expand Down Expand Up @@ -253,13 +256,15 @@ jobs:
TF_VAR_environment: ${{ env.ENVIRONMENT }}
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: terraform apply -auto-approve -target=aws_cloudfront_distribution.main
- name: Terraform Apply
working-directory: infra
env:
TF_VAR_environment: ${{ env.ENVIRONMENT }}
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: terraform apply -auto-approve
- name: Read outputs
id: tf-out
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
TF_VAR_environment: ${{ env.ENVIRONMENT }}
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: |
terraform apply -auto-approve \
-target=aws_ecr_repository.lambda \
Expand Down Expand Up @@ -95,13 +96,15 @@ jobs:
TF_VAR_environment: ${{ env.ENVIRONMENT }}
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: terraform apply -auto-approve -target=aws_cloudfront_distribution.main
- name: Terraform Apply
working-directory: infra
env:
TF_VAR_environment: ${{ env.ENVIRONMENT }}
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: terraform apply -auto-approve
- name: Read outputs
id: tf-out
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/deploy-qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ jobs:
TF_VAR_environment: ${{ env.ENVIRONMENT }}
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: |
terraform apply -auto-approve \
-target=aws_ecr_repository.lambda \
Expand Down Expand Up @@ -233,13 +234,15 @@ jobs:
TF_VAR_environment: ${{ env.ENVIRONMENT }}
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: terraform apply -auto-approve -target=aws_cloudfront_distribution.main
- name: Terraform Apply
working-directory: infra
env:
TF_VAR_environment: ${{ env.ENVIRONMENT }}
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: terraform apply -auto-approve
- name: Read outputs
id: tf-out
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ jobs:
working-directory: infra
env:
TF_VAR_environment: ${{ inputs.environment }} # choice input; constrained to dev/qa/prod
# unsplash_access_key is required by the variable block even for destroy
# All three are required by the variable block even for destroy.
TF_VAR_unsplash_access_key: ${{ secrets.UNSPLASH_ACCESS_KEY }}
TF_VAR_sns_from_number: ${{ secrets.SNS_FROM_NUMBER }}
TF_VAR_alert_email: ${{ secrets.ALERT_EMAIL }}
run: |
terraform destroy -auto-approve
echo "Destroy complete for environment: $ENVIRONMENT"
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ coverage/
.eslintcache
.vite/

# === Photo pool (generated; metadata JSON + image cache live in S3, not the repo) ===
backend/photos.json
backend/photos-cache/
backend/.photos-build.pid

# === Python (legacy/) ===
__pycache__/
*.py[cod]
Expand Down
1 change: 1 addition & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ COGNITO_CLIENT_ID=

# --- Unsplash ---
# Leave blank locally to use placeholder images instead of calling the API.
# REQUIRED to build the photo pool: `npm run build:photos` reads this from .env.
UNSPLASH_ACCESS_KEY=
61 changes: 49 additions & 12 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ src/
recommender.ts confidence/exploration, batch selection, novelty (the A/B loop)
insights.ts taste→trait inference + "why this card?" reasons (A/B interpretation)
dossier.ts per-user A/B state + applyInteraction orchestration (persistable)
signatures.ts per-country feature signatures (from the cached pool, else a seed)
photos.types.ts CachedPhoto type (kept apart from the generated data)
photos.data.ts generated per-country photo pool (empty until build:photos)
signatures.ts per-country feature signatures (primed from the pool, else a seed)
photos.types.ts CachedPhoto type (data lives in photos.json / S3, not the repo)
photos.curate.ts pure dedup + quality-gate + cities/landscapes balance (tested)
pool.ts loads the photo-pool metadata from S3 (cold start, cached) → primes signatures
algorithm.ts preference-learning logic (ported from v1)
db.ts DynamoDB data access (preferences + learnProgress)
auth.ts Cognito token verification (+ local dev bypass)
ownerNotify.ts best-effort SES email to the owner (research-download notifications)
images.ts weighted country selection + Unsplash client
schema.ts Pothos GraphQL schema
yoga.ts GraphQL Yoga server instance
Expand Down Expand Up @@ -78,15 +80,50 @@ npm run dev # GraphQL at http://localhost:4000/graphql

## Scripts

| Command | Purpose |
| ------------------------- | -------------------------------------------------------------- |
| `npm run dev` | Local server with watch reload |
| `npm run build` | Bundle the Lambda handler with esbuild |
| `npm run typecheck` | `tsc --noEmit` |
| `npm test` | Run the Jest suites |
| `npm run lint` / `format` | ESLint + Prettier |
| `npm run printschema` | Regenerate `schema.graphql` |
| `npm run build:photos` | Regenerate the cached photo pool (needs `UNSPLASH_ACCESS_KEY`) |
| Command | Purpose |
| ------------------------- | ---------------------------------------------------------------------------------------------------- |
| `npm run dev` | Local server with watch reload |
| `npm run build` | Bundle the Lambda handler with esbuild |
| `npm run typecheck` | `tsc --noEmit` |
| `npm test` | Run the Jest suites |
| `npm run lint` / `format` | ESLint + Prettier |
| `npm run printschema` | Regenerate `schema.graphql` |
| `npm run build:photos` | Step A — fetch photo metadata → `photos.json` (needs `UNSPLASH_ACCESS_KEY`) |
| `npm run sync:photos` | Step B — download images + upload images & `photos.json` to S3 (needs `PHOTO_S3_BUCKET` + AWS creds) |

## Travel photo pool

The Travel feed serves from a pool the running app loads **from S3, not the code**, so
there are **zero Unsplash API calls at runtime** and the photo set can be regenerated
without a code deploy. The metadata (`photos.json`) and the image binaries both live in
the shared `bba-photos` bucket; the Lambda fetches `photos.json` once per cold start
(`pool.ts`, cached warm) and CloudFront serves the images at `/photos/*`.

Building it is two steps (owner-run; nothing photo-related is committed):

1. **`npm run build:photos`** (Step A, rate-limited, resumable). Two searches per
catalog country — a landscape/nature and a cityscape query — curated by
`photos.curate.ts` (dedup by id, min-likes gate, denylist for food/interiors/
people/text, cities↔landscapes balance) down to `PHOTOS_PER_COUNTRY` (default 15),
then each kept photo is **enriched** via `GET /photos/:id` for its real `tags`
(search results carry none) plus `location` + camera `exif`. Writes `photos.json`.
Because every call (searches + enrichment) shares the **demo-mode 50/hr** budget, a
full 228-country run is **~2.5–3 days** — a single global gate spaces all calls, the
run checkpoints after every country (only committing a country once fully enriched),
and skips already-populated ones, so a background run resumes cleanly. Knobs:
`PHOTOS_PER_COUNTRY`, `PHOTOS_MAX_COUNTRIES`, `PHOTOS_MIN_LIKES`, `PHOTOS_THROTTLE_MS`,
`PHOTOS_ENRICH` (`0` to skip enrichment → no tags, much faster), `PHOTOS_RESET`.
2. **`PHOTO_S3_BUCKET=bba-photos npm run sync:photos`** (Step B, fast, ~15–30 min).
Downloads each image from the Unsplash CDN (a file fetch — not rate-limited) into
`photos-cache/` (gitignored, resumable), then uploads **both** the images
(`s3://bba-photos/photos/<id>.jpg`) and `photos.json` (`s3://bba-photos/photos.json`).
Run **once** — the bucket is account-global, not per environment.

`imageUrl` is a same-origin `/photos/<id>.jpg` (`PHOTO_CDN_BASE`, default `/photos`)
served by CloudFront from `bba-photos`; local dev reads a local `photos.json` and falls
back to the retained Unsplash CDN url for images. Enrichment also captures
`locationName`/`lat`/`lng` when the photographer geotagged the shot, but **`country`
stays the search association** — the catalog key — not the geotag (see `photos.types.ts`).

## Deployment

Expand Down
Loading
Loading