analytics

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CurrencyAmount

type CurrencyAmount struct {
	Currency    string `json:"currency"`
	AmountCents int64  `json:"amount_cents"`
}

CurrencyAmount is an amount in one currency's minor units.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler serves analytics API endpoints.

All responses are tenant-scoped via RLS (postgres.TxTenant). Money is cents (int64), and periods are closed-open windows [start, end) in UTC.

func NewHandler

func NewHandler(db *postgres.DB) *Handler

func (*Handler) Routes

func (h *Handler) Routes() chi.Router

type OverviewResponse

type OverviewResponse struct {
	Period string `json:"period"`

	// Currency is the tenant's default currency. Revenue and PaidInvoices
	// are scoped to it: invoices in other currencies are left out rather
	// than added into a total that mixes currencies. Each ..._by_currency
	// list names every currency instead.
	Currency string `json:"currency"`

	// Money (cents)
	Revenue     int64 `json:"revenue"`
	RevenuePrev int64 `json:"revenue_prev"`

	// OutstandingByCurrency is the amount still due on the same open invoices
	// OpenInvoices counts, one row per currency. Default currency first, then
	// largest.
	OutstandingByCurrency []CurrencyAmount `json:"outstanding_by_currency"`

	// CreditBalanceByCurrency is the credit customers hold, one row per
	// currency (ADR-129): each ledger row stores its own currency, and
	// balances in two currencies are never added together. Default currency
	// first, then largest.
	CreditBalanceByCurrency []CurrencyAmount `json:"credit_balance_by_currency"`

	// CreditSoldByCurrency is the credit granted by the top-ups paid in the
	// period, per currency. A discounted top-up grants more than was paid,
	// so this is not part of Revenue. Ordered like CreditBalanceByCurrency.
	CreditSoldByCurrency []CurrencyAmount `json:"credit_sold_by_currency"`

	// UsageBilledByCurrency is the usage lines on the invoices issued in the
	// period, per currency. Ordered like CreditBalanceByCurrency.
	UsageBilledByCurrency []CurrencyAmount `json:"usage_billed_by_currency"`

	// Counts
	PaidInvoices     int   `json:"paid_invoices"`
	OpenInvoices     int   `json:"open_invoices"`
	DunningActive    int   `json:"dunning_active"`
	RefundsAttention int   `json:"refunds_needing_attention"`
	UsageEvents      int64 `json:"usage_events"`
}

OverviewResponse aggregates the headline billing metrics for a tenant over a requested period, plus matching values for the prior period so the UI can render period-over-period deltas without a second round-trip.

type Period

type Period struct {
	Key       string
	Start     time.Time
	End       time.Time
	PrevStart time.Time
	PrevEnd   time.Time
	Trunc     string // "day" | "month"
}

Period models the closed-open comparison window [Start, End) against a matching prior window [PrevStart, PrevEnd) of equal length.

type RevenueDataPoint

type RevenueDataPoint struct {
	Date         string `json:"date"`
	RevenueCents int64  `json:"revenue_cents"`
	InvoiceCount int    `json:"invoice_count"`
}

RevenueDataPoint is one bucket of paid-invoice revenue.

type TopMeter

type TopMeter struct {
	MeterID   string          `json:"meter_id"`
	MeterName string          `json:"meter_name"`
	EventName string          `json:"event_name"`
	Events    int64           `json:"events"`
	Quantity  decimal.Decimal `json:"quantity"`
}

TopMeter is one row of the top-N usage-by-meter breakdown.

type UsagePoint

type UsagePoint struct {
	Date     string          `json:"date"`
	Events   int64           `json:"events"`
	Quantity decimal.Decimal `json:"quantity"`
}

UsagePoint is one bucket of aggregated usage events.

Quantity is decimal, not int64: usage_events.quantity is NUMERIC(38,12) (ADR-005/ADR-045), so SUM() comes back as a decimal string. Scanning it into an int64 made every one of these three queries fail at the driver — this endpoint returned 500 for any tenant that had ingested a single event. It wire-encodes as a string for the same reason Aggregate.TotalUnits does: truncating to an integer would silently lose fractional usage.

Jump to

Keyboard shortcuts

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