focus

package
v1.0.0-alpha.21 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 16 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

Overview

Package focus holds the Costroid FOCUS row, version 0.3, and its writer.

Index

Constants

View Source
const (

	// InstantLayout is the row contract's form of a UTC instant.
	InstantLayout = "2006-01-02T15:04:05Z"

	// RegionGlobal is default routing, which is no distinct region, so a row
	// served there carries a null RegionId.
	RegionGlobal = "global"
)
View Source
const (
	NoteInformational = "informational"
	NoteFallback      = "priced at the catalog fallback tier: the sheet does not name this model, tier and class"
)

Notes more than one writer of rows puts in ChargeDescription.

View Source
const (
	TagWorkItem = "costroid/work_item"
	TagArm      = "costroid/arm"
	TagTeam     = "costroid/team"
)

Tag keys version 0.3 of the convention adds. A caller names each one, and no writer of rows sets one on its own.

View Source
const (
	TagSessionID        = "costroid/session_id"
	TagAgent            = "costroid/agent"
	TagAgentVersion     = "costroid/agent_version"
	TagModel            = "costroid/model"
	TagCostSource       = "costroid/cost_source"
	TagCostBasis        = "costroid/cost_basis"
	TagAttributionGrade = "costroid/attribution_grade"
	TagEnforcementGrade = "costroid/enforcement_grade"
	TagRunIdentity      = "costroid/run_identity"
	TagRepo             = "costroid/repo"
	TagPR               = "costroid/pr"
	TagCommitSHA        = "costroid/commit_sha"
	TagWorkflowRunID    = "costroid/workflow_run_id"
	TagAssumedTTL       = "costroid/assumed_ttl"
)

Tag keys of the per-PR convention, version 0.2.

View Source
const (
	VisibilityPublic   = "public"
	VisibilityPrivate  = "private"
	VisibilityInternal = "internal"
)

The values costroid/repo_visibility takes, and no other.

View Source
const ColumnMeasuredCost = "x_CostroidMeasuredCost"

ColumnMeasuredCost is the column a strict row carries the default profile's EffectiveCost in, byte for byte.

View Source
const MaxDataLength = 128

MaxDataLength caps any string that came from a pull request.

View Source
const MaxLineBytes = 1 << 20

MaxLineBytes bounds one line of a rows file.

View Source
const MaxScale = 12

MaxScale is the row contract's limit on fractional digits.

View Source
const TagRepoVisibility = "costroid/repo_visibility"

TagRepoVisibility is the key version 0.4 of the convention adds: who can read the repository a row's run belongs to, as the CI system states it. No writer of rows asks a host for it, and a row whose CI system did not state it leaves the tag out.

Variables

View Source
var DataPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._:/+_-]*$`)

DataPattern is the character allowlist every string from a pull request passes. It holds no character that can close a code span, open a tag or start a mention.

View Source
var ErrKeyShaped = errors.New("takes a label, never a key: this value reads like a key")

ErrKeyShaped is a named tag's value that reads like a provider key.

View Source
var ErrLongLine = fmt.Errorf("the line is past %d bytes", MaxLineBytes)

ErrLongLine is the fault of a line past MaxLineBytes.

View Source
var NamedTags = map[string]string{
	"work_item": TagWorkItem,
	"arm":       TagArm,
	"team":      TagTeam,
}

NamedTags are the tags a caller names, by the key --tag takes for each.

Profiles are the profiles in the order help names them.

Functions

func BillingPeriod

func BillingPeriod(at time.Time) (time.Time, time.Time)

BillingPeriod is the UTC month holding an instant.

func ChargePeriod

func ChargePeriod(start, end time.Time) (time.Time, time.Time)

ChargePeriod widens a span to whole UTC seconds. The end is exclusive, so a span inside one second still ends after it starts.

func CheckNamedTag

func CheckNamedTag(tag, value string) error

CheckNamedTag holds the value of one of NamedTags to its rule. The error says what the tag takes and never repeats the value.

func CheckRepoVisibility

func CheckRepoVisibility(value string) error

CheckRepoVisibility holds a value of costroid/repo_visibility to the rule IsRepoVisibility states. The error says what the tag takes and never repeats the value.

func EachLine

func EachLine(source io.Reader, read func(line []byte) error) error

EachLine calls read with each line of a rows file in order. The line is valid only until read returns. Lines count from one, and an empty line is counted and skipped. The first error read returns stops the file and comes back as a *LineError, as does a line past MaxLineBytes. An error of source itself comes back as it is.

func IsRepoVisibility

func IsRepoVisibility(value string) bool

IsRepoVisibility reports whether value is one of the three values of costroid/repo_visibility. A value outside them never reaches a row.

func ReadsLikeKey

func ReadsLikeKey(value string) bool

ReadsLikeKey reports whether a value opens as every provider key this product reads does. A value that does never reaches a row.

func ResidencyNote

func ResidencyNote(multiplier decimal.Decimal, region string) string

ResidencyNote states a residency multiplier in ChargeDescription.

func RowIdentity

func RowIdentity(requestID string, class TokenClass) string

RowIdentity is the row contract's id: SHA-256 of the provider's request id and the token class. Two writers of one request arrive at one id.

func Shaped

func Shaped(out io.Writer, shape Shape) io.Writer

Shaped returns a writer that takes rows as Writer writes them, whole JSON lines, and writes each to out with the shape applied. A shape that changes nothing returns out itself, so a default row reaches out as Writer wrote it.

func WriteCSV

func WriteCSV(out io.Writer, lines io.Reader) (int, error)

WriteCSV reads rows as JSON lines and writes them as the row contract's CSV: a header of every column, then one record per row. A null is a blank cell, and Tags is its JSON object, compacted, in the order the line names its keys. It returns the number of rows. A line that is not a row refuses the file, and the caller discards what was written.

Types

type Charge

type Charge struct {
	RowID               string
	Provider            string
	ServiceProviderName string
	Model               string
	Class               TokenClass
	Tokens              int64
	Start, End          time.Time
	Account             string
	Credential          string
	Region              string
	ServiceTier         string
	CatalogVersion      string
	Price               *Price
	Basis               CostBasis
	Tags                map[string]string
	Notes               []string
	OvershootBound      *decimal.Decimal
}

Charge holds the facts of one request's one token class. A nil Price means an informational class, which never carries a cost. Its SkuPriceId takes the shape of the catalog's fallback ids, provider:informational:class.

type CostBasis

type CostBasis string

CostBasis is a value of costroid/cost_basis.

const (
	Measured       CostBasis = "measured"
	ListEquivalent CostBasis = "list_equivalent"
)

The two bases a wrapped run can carry.

type LineError

type LineError struct {
	Line int
	Err  error
}

LineError names the line that stopped a read and why: the caller's refusal of it, or ErrLongLine.

func (*LineError) Error

func (e *LineError) Error() string

func (*LineError) Unwrap

func (e *LineError) Unwrap() error

type Price

type Price struct {
	SkuPriceID    string
	PricingUnit   string
	ListUnitPrice decimal.Decimal
}

Price is what a row needs from a catalog answer.

type Profile

type Profile string

Profile is how a row states the figure Costroid measured.

const (
	// ProfileDefault keeps the figure in EffectiveCost, where Costroid
	// Platform reads it.
	ProfileDefault Profile = "default"
	// ProfileStrict writes EffectiveCost as 0, as BilledCost always is, and
	// moves the figure to x_CostroidMeasuredCost, so a reader that merges the
	// rows with the provider's own FOCUS data counts the cost once.
	ProfileStrict Profile = "strict"
)

The profiles a row can be written in.

type Row

type Row struct {
	BilledCost          decimal.Decimal   `json:"BilledCost"`
	BillingAccountID    string            `json:"BillingAccountId"`
	BillingAccountName  *string           `json:"BillingAccountName"`
	BillingCurrency     string            `json:"BillingCurrency"`
	BillingPeriodEnd    string            `json:"BillingPeriodEnd"`
	BillingPeriodStart  string            `json:"BillingPeriodStart"`
	ChargeCategory      string            `json:"ChargeCategory"`
	ChargeClass         *string           `json:"ChargeClass"`
	ChargeDescription   string            `json:"ChargeDescription"`
	ChargeFrequency     string            `json:"ChargeFrequency"`
	ChargePeriodEnd     string            `json:"ChargePeriodEnd"`
	ChargePeriodStart   string            `json:"ChargePeriodStart"`
	ConsumedQuantity    decimal.Decimal   `json:"ConsumedQuantity"`
	ConsumedUnit        string            `json:"ConsumedUnit"`
	ContractedCost      decimal.Decimal   `json:"ContractedCost"`
	ContractedUnitPrice decimal.Decimal   `json:"ContractedUnitPrice"`
	EffectiveCost       decimal.Decimal   `json:"EffectiveCost"`
	HostProviderName    *string           `json:"HostProviderName"`
	InvoiceIssuerName   string            `json:"InvoiceIssuerName"`
	ListCost            decimal.Decimal   `json:"ListCost"`
	ListUnitPrice       decimal.Decimal   `json:"ListUnitPrice"`
	PricingCategory     string            `json:"PricingCategory"`
	PricingQuantity     decimal.Decimal   `json:"PricingQuantity"`
	PricingUnit         string            `json:"PricingUnit"`
	RegionID            *string           `json:"RegionId"`
	RegionName          *string           `json:"RegionName"`
	ServiceCategory     string            `json:"ServiceCategory"`
	ServiceName         string            `json:"ServiceName"`
	ServiceProviderName string            `json:"ServiceProviderName"`
	ServiceSubcategory  string            `json:"ServiceSubcategory"`
	SkuID               string            `json:"SkuId"`
	SkuMeter            TokenClass        `json:"SkuMeter"`
	SkuPriceID          string            `json:"SkuPriceId"`
	SubAccountID        *string           `json:"SubAccountId"`
	SubAccountName      *string           `json:"SubAccountName"`
	Tags                map[string]string `json:"Tags"`
	CatalogVersion      *string           `json:"x_CostroidCatalogVersion"`
	OvershootBound      *decimal.Decimal  `json:"x_CostroidOvershootBound"`
	RowID               string            `json:"x_CostroidRowId"`
	ServiceTier         *string           `json:"x_CostroidServiceTier"`
}

Row is one model request, or the finest grain an agent reports, for one token class. In JSON lines every column is present and an absent value is null.

func NewRow

func NewRow(c Charge) Row

NewRow builds the row of one charge. Both the CLI and the ingest service write rows through it, so a row reads the same whichever of them priced it.

func (Row) Check

func (r Row) Check() error

Check refuses a row the contract's arithmetic rules out. Rounding to fit is never an option: a figure past the scale limit is an error.

type Shape

type Shape struct {
	Tags    map[string]string
	Profile Profile
}

Shape is what a caller adds to every row a command writes: the tags it names and the profile.

type Tags

type Tags map[string]json.RawMessage

Tags is a row's Tags column, each value as its line states it. A user tag may hold any JSON value; the convention's own tags are strings.

func (Tags) Text

func (t Tags) Text(key string) (string, error)

Text reads one tag as a string. A tag that is absent, or that holds a JSON value other than a string or null, is an error and reads as empty.

type TokenClass

type TokenClass string

TokenClass is a value of SkuMeter.

const (
	InputUncached       TokenClass = "input_uncached"
	CacheRead           TokenClass = "cache_read"
	CacheWrite5m        TokenClass = "cache_write_5m"
	CacheWrite1h        TokenClass = "cache_write_1h"
	CacheWrite30m       TokenClass = "cache_write_30m"
	CacheWrite12h       TokenClass = "cache_write_12h"
	Output              TokenClass = "output"
	ReasoningOutput     TokenClass = "reasoning_output"
	CachedInputReported TokenClass = "cached_input_reported"
)

The classes a row can carry.

func (TokenClass) Informational

func (c TokenClass) Informational() bool

Informational reports whether a class repeats tokens a billed class counts.

func (TokenClass) Lifetime

func (c TokenClass) Lifetime() string

Lifetime is the value costroid/assumed_ttl takes on a row of this class, and empty for a class that is not a cache write.

type Writer

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

Writer writes rows as JSON lines.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a Writer over w.

func (*Writer) Write

func (w *Writer) Write(row Row) error

Write checks a row and writes it as one line.

Jump to

Keyboard shortcuts

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