dataplan

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package dataplan turns recorded per-network usage into "how much of my monthly data allowance is gone" — the number a tethered user would otherwise have to open their carrier's app to see. It is pure logic: billing-cycle arithmetic plus a quota status, with no I/O and no platform dependencies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CycleEnd

func CycleEnd(now time.Time, startDay int) time.Time

CycleEnd returns the exclusive end of the cycle containing now, i.e. the next cycle's start.

func CycleKey

func CycleKey(now time.Time, startDay int) string

CycleKey identifies the cycle containing now, for once-per-cycle alert de-duplication (e.g. "2026-07-15").

func CycleStart

func CycleStart(now time.Time, startDay int) time.Time

CycleStart returns local midnight on the first day of the billing cycle that contains now. A startDay past the end of a short month clamps to that month's last day (a 31st cycle starts Feb 28/29).

Types

type Config

type Config struct {
	LimitBytes int64  `json:"limitBytes"` // monthly allowance in bytes (e.g. 100 GB)
	StartDay   int    `json:"startDay"`   // billing cycle start day-of-month, 1-31 (0 → 1)
	Iface      string `json:"iface"`      // BSD name to meter; "" = every tethering network
	// WarnPercent is the early-warning threshold; 0 uses 80. The 100% crossing
	// always alerts regardless.
	WarnPercent int `json:"warnPercent"`
}

Config is the user's metered-data plan. A zero LimitBytes disables everything.

func (Config) Enabled

func (c Config) Enabled() bool

Enabled reports whether a limit is set.

func (Config) Warn

func (c Config) Warn() int

Warn returns the early-warning percentage, defaulting to 80.

type Status

type Status struct {
	Enabled        bool    `json:"enabled"`
	LimitBytes     int64   `json:"limitBytes"`
	UsedBytes      int64   `json:"usedBytes"`
	RemainingBytes int64   `json:"remainingBytes"` // 0 once over the limit
	Percent        float64 `json:"percent"`        // may exceed 100
	CycleStart     int64   `json:"cycleStart"`     // unix seconds, local midnight
	CycleEnd       int64   `json:"cycleEnd"`       // unix seconds, exclusive
	DaysLeft       int     `json:"daysLeft"`       // whole days until the cycle rolls over
	// ProjectedBytes extrapolates the current daily average to the end of the
	// cycle — "at this rate you'll finish at X". 0 when there's nothing to
	// extrapolate from yet.
	ProjectedBytes int64 `json:"projectedBytes"`
}

Status is a plan's state at a point in time — what the dashboard renders and what the alert checker compares against.

func Compute

func Compute(cfg Config, used int64, now time.Time) Status

Compute builds the Status for used bytes in the cycle containing now.

Jump to

Keyboard shortcuts

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