Module supabase/functions/sgtm_container_usage

Returns current billing period usage statistics for an SGTM container.

Overview

This Edge Function provides real-time usage data for billing and analytics, combining aggregated daily usage with today's live log count to show accurate hits used, remaining quota, and over-limit status.

Endpoint

GET /functions/v1/sgtm_container_usage?container_id=<uuid>

Authentication

Service role - uses SUPABASE_SERVICE_ROLE_KEY.

Behavior

  • Fetches active subscription and billing plan for container
  • Calculates current billing period (calendar month)
  • Sums aggregated usage from sgtm_usage_daily
  • Adds today's real-time log count from sgtm_access_logs
  • Returns plan details, period dates, and usage statistics

Tables Touched

  • sgtm_container_subscriptions (read)
  • billing_plans (read)
  • sgtm_usage_daily (read)
  • sgtm_access_logs (read)

Notes

  • Read-only operation, safe to call frequently
  • Today's logs counted separately as they're not yet aggregated
  • Used by billing UI and usage dashboards