@beesolve/analytics-processor

中文文档
0.10.0发布于 2026/10/20MITTypeScriptES Module构建来源已验证近 30 天下载 2687

CloudFront analytics processor — Athena-to-DynamoDB hourly totals, breakdowns, and rollups

原始使用文档
文档目录6 节
  1. Architecture
  2. Installation
  3. Exports
  4. CDK Usage
  5. Backfill
  6. Compact

@beesolve/analytics-processor

CloudFront analytics processor. Reads CloudFront standard-logs via Athena and maintains a single-table DynamoDB design of hourly totals, per-dimension breakdowns, and daily/monthly rollups using atomic counter updates with per-period idempotency markers.

[!WARNING] Pre-1.0 — unstable API. This package is 0.x and under active development. Breaking changes can land in any 0.x release without a major version bump. Pin an exact version and review the changelog before upgrading. Once the API stabilizes and the package reaches 1.0.0, it follows semantic versioning and this notice is removed.

Architecture

┌──────────────┐   hourly    ┌────────┐   ADD + marker   ┌──────────┐
│ CloudFront   │ ──────────► │ Athena │ ───────────────► │ DynamoDB │
│ v2 Parquet   │   query     │        │   (transaction)  │  totals  │
│ (S3 + Glue)  │             └────────┘                  │  breakdowns│
└──────────────┘                                         └──────────┘
                                                               ▲
                      daily / monthly rollups (read-back) ─────┘

An hourly Lambda runs exactly one partition-pruned Athena query over a closed hour of v2 Parquet data and writes totals + per-dimension breakdowns with atomic ADD updates, guarded by a per-hour applied#... marker written conditionally in the same TransactWriteCommand. Daily and monthly rollup Lambdas sum finer-grain DynamoDB items (never re-querying Athena). A one-time standalone backfill script walks historic data through the same write path, and a compact command rewrites legacy v1 logs into the v2 Parquet shape so they can be read through that same path.

Installation

npm install @beesolve/analytics-processor

Exports

  • . — schemas/types (totalsSchema/TotalsItem, breakdownSchema/BreakdownItem, appliedMarkerSchema/AppliedMarker, configSchema/AnalyticsConfig, grains/Grain, dimensions/Dimension), time/window helpers (hourKey, dayKey, monthKey, maxWindowFor, windowKeys, shiftAnchor, hoursBetween, parsePeriodKey, grainForPeriodKey), model classes (Totals, Breakdowns, AppliedMarkers, Config, IpInfoCache + lookupIpInfo + IpInfo/IpInfoCacheItem), pure helpers (buildHourlyQuery, classifyDevice, sumTotals, sumBreakdown, topNWithOther), live-aggregation resolvers (resolveTotalsLive/resolveBreakdownLive), and processHour (+ applyDailyRollup/applyMonthlyRollup).
  • ./cdk — the AnalyticsProcessor CDK construct.

The Lambda handlers are not part of the importable API. The operational commands ship as executable bins — analytics-backfill, analytics-watch-backfill, and analytics-compact — runnable via npx (see the how-to guide).

CDK Usage

import { AnalyticsProcessor } from "@beesolve/analytics-processor/cdk";

const processor = new AnalyticsProcessor(this, "Analytics", {
  athenaDatabase: logging.glueDatabaseName,
  athenaV2Table: logging.glueTableName,
  athenaWorkgroup: logging.workgroupName,
  logBucketArn: logging.logBucket.bucketArn,
  athenaResultsBucketArn: logging.athenaResultsBucket.bucketArn,
  ipInfoApiKey: process.env.IPINFO_API_KEY,
});

// Grant read-only access to a dashboard Lambda. Sets ANALYTICS_TABLE_NAME and
// ANALYTICS_REVERSE_INDEX env vars on the handler.
processor.grantRead(dashboardLambda);

The construct provisions:

  • DynamoDB table (on-demand, composite key pk/sk, reverse GSI reverse, TTL)
  • Three Lambdas (Node.js 24): Hourly, DailyRollup, MonthlyRollup
  • EventBridge schedules: hourly rate(1 hour), daily cron (00:15), monthly cron (01:30 on the 1st)
  • Least-privilege Athena/Glue/S3 access for the hourly Lambda

Backfill

Cost warning: if your source is legacy v1 logs (many small gzip files), run compact first and point ATHENA_TABLE at the compacted table. Backfilling a raw v1 table directly makes every hourly query fan out across tens of thousands of S3 objects — Athena issues at least one GET per file, which has cost hundreds of dollars in S3 GET requests for a single full backfill. Compacting first reduces that to cents.

The one-time backfill script walks historic log data through the same write path as the hourly Lambda. It is resumable and exactly-once via the idempotency markers — already-applied hours skip instantly. It reads the v2 Parquet table; to backfill legacy v1 logs, first run compact to rewrite them into the v2 shape, then point ATHENA_TABLE at the compacted table.

TABLE_NAME=... \
ATHENA_DATABASE=... ATHENA_TABLE=... ATHENA_WORKGROUP=... \
bun run backfill -- --from 2026-01-01T00:00:00Z --to 2026-01-31T23:00:00Z --concurrency 2 --rollups
  • --from / --to — inclusive ISO hour range (required).
  • --concurrency — bounded parallelism (default 2).
  • --rollups — after the per-hour fill, run daily and monthly rollups over the range.

Credentials are resolved by the default AWS SDK chain (honors AWS_PROFILE).

Compact

The compact command converts a legacy v1 LazySimpleSerDe log table into a v2-shaped Parquet table (one file per hour-partition, v1 columns renamed to the v2 schema), so queries stop fanning out across tens of thousands of tiny S3 objects. See the how-to guide.

bun run compact -- \
  --database cf_logs_db --source-table cf_logs_v1_backfill \
  --target-table cf_logs_v1_compact \
  --external-location s3://log-bucket/compact/v1/ \
  --workgroup primary --output-location s3://athena-results/ctas/ \
  --from 2025-10-16 --to 2026-10-13

版本与发布记录

@beesolve/analytics-processor 共发布 20 个版本,首次发布于 2026/10/12;过去 12 个月发布了 20 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.10.0latest2026/10/2019 天前
0.9.02026/10/1820 天前
0.8.02026/10/1820 天前
0.7.12026/10/1821 天前
0.7.02026/10/1721 天前
0.6.22026/10/1524 天前

关于 @beesolve/analytics-processor 的常见问题

@beesolve/analytics-processor 是什么?

CloudFront analytics processor — Athena-to-DynamoDB hourly totals, breakdowns, and rollups

如何安装 @beesolve/analytics-processor?

在项目目录运行 npm install @beesolve/analytics-processor,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:constructs(^10.8.1)、aws-cdk-lib(^2.273.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 analytics-compact、analytics-backfill、analytics-watch-backfill,可以用 npx analytics-compact 直接运行。

@beesolve/analytics-processor 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@beesolve/analytics-processor 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@beesolve/analytics-processor 可以商用吗?

@beesolve/analytics-processor 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@beesolve/analytics-processor 的下载量有多少?

根据 npm 官方下载统计,@beesolve/analytics-processor 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,687 次,日均约 90 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@beesolve/analytics-processor 还在维护吗?

最新版本 0.10.0 发布于 2026/10/20(19 天前);至今共发布 20 个版本,其中过去 12 个月发布了 20 个;首次发布于 2026/10/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。