{"id":22546627,"url":"https://github.com/prx/dovetail-cdn-usage","last_synced_at":"2025-07-11T08:12:05.540Z","repository":{"id":237700853,"uuid":"795078918","full_name":"PRX/dovetail-cdn-usage","owner":"PRX","description":"Lambda to query Dovetail CloudFront usage and insert into BigQuery","archived":false,"fork":false,"pushed_at":"2024-12-20T17:13:24.000Z","size":171,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-28T08:45:53.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PRX.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-02T14:41:49.000Z","updated_at":"2024-12-20T17:13:27.000Z","dependencies_parsed_at":"2024-12-20T18:34:08.230Z","dependency_job_id":null,"html_url":"https://github.com/PRX/dovetail-cdn-usage","commit_stats":null,"previous_names":["prx/dovetail-cdn-usage"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/PRX/dovetail-cdn-usage","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PRX%2Fdovetail-cdn-usage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PRX%2Fdovetail-cdn-usage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PRX%2Fdovetail-cdn-usage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PRX%2Fdovetail-cdn-usage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PRX","download_url":"https://codeload.github.com/PRX/dovetail-cdn-usage/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PRX%2Fdovetail-cdn-usage/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264763640,"owners_count":23660318,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-07T15:08:24.368Z","updated_at":"2025-07-11T08:12:05.500Z","avatar_url":"https://github.com/PRX.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dovetail CDN Usage\n\nAWS Lambda to query Dovetail CloudFront usage and insert into BigQuery\n\n## Overview\n\n1. Requests to the [Dovetail CDN](https://github.com/PRX/Infrastructure/tree/main/cdn/dovetail-cdn) are logged to an S3 bucket.\n2. This lambda BigQuery-queries for the `MAX(day) FROM dt_bytes`, and processes days \u003e= the result (or all the way back to the S3 expiration date).\n3. Then we Athena-query for a day of logs, grouping by path and summing bytes sent.\n4. Paths are parsed and grouped as `/\u003cpodcast\u003e/\u003cepisode\u003e/...` or `/\u003cpodcast\u003e/\u003cfeed\u003e/episode/...`. Unrecognized paths that use a bunch of bandwidth are warning-logged.\n5. Resulting bytes usage is inserted back into BigQuery:\n\n   ```\n   {day: \"2024-04-23\", feeder_podcast: 123, feeder_episode: \"abcd-efgh\", feeder_feed: null, bytes: 123456789}\n   ```\n\n## Development\n\nLocal development is dependency free! Just:\n\n```sh\nyarn install\nyarn test\nyarn lint\n```\n\nHowever, if you actually want to hit Athena/BigQuery, you'll need to `cp env-example .env` and fill in several dependencies:\n\n- `ATHENA_DB` the athena database you're using\n- `ATHENA_TABLE` the athena table that has been configured to [query to the Dovetail CDN S3 logs](https://docs.aws.amazon.com/athena/latest/ug/cloudfront-logs.html#create-cloudfront-table-standard-logs)\n  - **NOTE:** you must have your AWS credentials setup and configured locally to reach/query Athena\n- `BQ_DATASET` the BigQuery dataset to load the `dt_bytes` table in. You should use `development` or something locally (not `staging` or `production`)\n\nThen run `yarn start` and you're off!\n\n## Deployment\n\nThis function's code is deployed as part of the usual\n[PRX CI/CD](https://github.com/PRX/Infrastructure/tree/main?tab=readme-ov-file#cicd) process.\nThe lambda zip is built via `yarn build`, uploaded to S3, and deployed into the wild.\n\nWhile that's all straightforward, there are some gotchas setting up access:\n\n1. AWS permissions are (Athena, S3, Glue, etc) are documented in the [Cloudformation Stack](https://github.com/PRX/Infrastructure/blob/main/spire/templates/apps/dovetail-cdn-usage.yml) for this app.\n2. Google is configured via the `BQ_CLIENT_CONFIG` ENV and [Federated Access](https://github.com/PRX/internal/wiki/Guide:-Google-Cloud-Workload-Identity-Federation)\n3. _In addition to the steps documented in (2)_, the Service Account you create must have the following permissions:\n   - `BigQuery Job User` in your BigQuery project\n   - _Any_ role on the BigQuery dataset that provides `bigquery.tables.create`, so the table load jobs can execute. We have a custom role to provide this minimal access, but any role with that create permission will work.\n   - `BigQuery Data Editor` _only_ on the `dt_bytes` table in the dataset for this environment (click the table name in BigQuery UI -\u003e Share -\u003e Manage Permissions)\n\n## License\n\n[AGPL-3.0 License](https://www.gnu.org/licenses/agpl-3.0.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprx%2Fdovetail-cdn-usage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprx%2Fdovetail-cdn-usage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprx%2Fdovetail-cdn-usage/lists"}