metering

package
v1.6.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package metering writes per-send ledger rows and exposes a query for the billing surface. Rows land in the `meter` collection synchronously (one INSERT per send); the optional rollup goroutine sweeps them daily into S3 prefixed by tenant.

Two cost numbers per row:

vendor_cost_micros — what the provider charged Hanzo (Plivo, Twilio, …)
retail_price_micros — what Hanzo charges the tenant

The markup factor lives in the catalog, not here — metering only persists the snapshot the caller passes in.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Write

func Write(app core.App, rec Record) (string, error)

Write inserts one meter row. Returns the persisted record id or an error. Idempotency must be handled at the caller layer — the meter is append-only and does not dedup.

Types

type ProviderRoll

type ProviderRoll struct {
	Units        int   `json:"units"`
	VendorMicros int64 `json:"vendor_cost_micros"`
	RetailMicros int64 `json:"retail_price_micros"`
}

ProviderRoll is the per-(provider,channel) breakdown.

type Record

type Record struct {
	TenantSlug        string
	Event             string
	Provider          string
	Channel           types.Channel
	Units             int
	VendorCostMicros  int64
	RetailPriceMicros int64
	MessageID         string
}

Record is the input shape callers pass to Write — a flat, pre-priced row ready for insertion.

type Summary

type Summary struct {
	TenantSlug        string                  `json:"tenant_slug"`
	TotalUnits        int                     `json:"total_units"`
	TotalVendorMicros int64                   `json:"total_vendor_cost_micros"`
	TotalRetailMicros int64                   `json:"total_retail_price_micros"`
	ByProviderChannel map[string]ProviderRoll `json:"by_provider_channel"`
}

Summary aggregates meter rows for one tenant in a time window. Used by GET /v1/notify/metering. The from/to params are ISO-8601 strings; empty means open-ended.

func Aggregate

func Aggregate(app core.App, tenant, from, to string, maxRows int) (*Summary, error)

Aggregate scans meter rows for the tenant and returns a roll-up. It loads up to maxRows records — callers that need windowed billing should pass a tight (from,to) and a high cap; the daily-rollup job uses streaming reads via the underlying dbx connection directly.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL