Skip to content

feat: Consolidate rate limiting and usage tracking into usagePlugin - #178

Merged
Ardnived merged 8 commits into
mainfrom
177-usage-plugin
Jan 21, 2026
Merged

Ardnived merged 8 commits into
mainfrom
177-usage-plugin

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Summary

Closes #177

This PR consolidates all rate limiting and usage tracking code into a single usagePlugin, following the established accessPlugin pattern. The plugin auto-applies hooks to collections, eliminating manual hook registration across 17 collections.

Key Changes

  • New usagePlugin (src/lib/usage/) - Plugin auto-applies hooks to collections:

    • beforeOperation hook for rate limiting (Cloudflare Workers binding)
    • afterRead hook for usage tracking (queues job)
    • beforeChange hook for consumer collections (initializes usage stats)
  • Field Renames (with migration):

    • usageStats → usage (field group)
    • maxDailyRequests → peakDailyRequests
    • Removed unused totalRequests field
  • Task Renames:

    • trackClientUsage → trackUsage
    • resetClientUsage → resetUsage
  • Removed Manual Hooks from 17 collections:

    • Content: Meditations, Lessons, Music, Albums, Videos, Pages
    • Resources: Narrators, Images, Authors, Lectures
    • System: Frames, Files
    • Tags: MeditationTags, MusicTags, PageTags, VideoTags, ImageTags
  • Deleted Files:

    • src/jobs/tasks/TrackUsage.ts (moved to plugin)
    • src/lib/rateLimiting.ts (integrated into hooks.ts)

Plugin Configuration

usagePlugin({
  enabled: !isE2ETest,
  consumers: [
    {
      collection: 'clients',
      statsFieldPath: 'usage',
      highUsageThreshold: 1000,
    },
  ],
})

Test plan

  • All integration tests pass (456 tests)
  • Build succeeds
  • TypeScript compiles without errors
  • Rate limiting utilities (buildRateLimitKey, validateUserId) exported for testing
  • Run migration on dev database: pnpm payload migrate
  • Verify high usage alert appears in admin UI when threshold exceeded
  • Verify rate limiting works in production (Cloudflare binding)

Test Results

  • Integration tests: 456 passed
  • Build: ✓ Success

🤖 Generated with Claude Code

Ardnived and others added 8 commits January 20, 2026 23:23
…177)

Creates a unified usagePlugin that auto-applies hooks to all collections,
eliminating manual hook registration across 17 collections.

Key changes:
- Create src/lib/usage/ module with plugin, hooks, tasks, and types
- Rename usageStats field group to usage in Clients collection
- Rename maxDailyRequests to peakDailyRequests
- Remove unused totalRequests field
- Task slugs changed: trackClientUsage → trackUsage, resetClientUsage → resetUsage
- Remove manual hook registrations from all 17 content/resource/tag collections
- Delete old src/lib/rateLimiting.ts and src/jobs/tasks/TrackUsage.ts
- Create database migration for field renames
- Update tests and documentation

Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Move consumer collections to CONSUMER_COLLECTIONS constant
- Add STATS_FIELD_PATH and HIGH_USAGE_THRESHOLD constants
- Remove createInitStatsHook (rely on field defaults)
- Simplify UsagePluginOptions to just enabled + exclude
- Streamline checkRateLimit function
- Export tasks as configs instead of factory functions
- Remove consumerCollection from TrackUsageInput

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@Ardnived
Ardnived merged commit 808c405 into main Jan 21, 2026
@Ardnived
Ardnived deleted the 177-usage-plugin branch January 21, 2026 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consolidate Rate Limiting and Usage Tracking into usagePlugin

1 participant