focusfile

package
v1.0.0-alpha.39 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 15 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 focusfile reads a FOCUS cost file that a step of the customer's own CI wrote, such as an infrastructure estimator's output, and turns each of its rows into one Costroid row with the basis estimate.

The file is the customer's. Its figure, currency, charge period, resource and service are kept. Its tags are not, and the pull request's join keys come from the CI system, never from the file.

Index

Constants

View Source
const (
	// MaxBytes bounds the file: 4 MiB holds tens of thousands of resource rows.
	MaxBytes = 4 << 20
	// DefaultSource is the costroid/cost_source of a row when the caller names
	// none.
	DefaultSource = "infrastructure"
	// Basis is the costroid/cost_basis of every row this package writes.
	Basis = "estimate"

	// MaxText is the row contract's limit on a text column.
	MaxText = 512
)
View Source
const MaxUnavailableBytes = 4096

MaxUnavailableBytes bounds the note a reader takes. The note Write makes is under 100 bytes.

View Source
const ReasonNoFile = "no_file"

ReasonNoFile is why an optional estimate is not available: the file was not there.

Variables

View Source
var (
	ErrTooLarge      = fmt.Errorf("the file is over %d bytes: write one row per resource, for this pull request alone", MaxBytes)
	ErrNoHeader      = errors.New("the file is empty: write a header line of FOCUS column names, then one row per resource")
	ErrNotCSV        = errors.New("the file is not CSV: write comma-separated values, every row with as many cells as the header")
	ErrColumnTwice   = errors.New("the header names this column twice, and nothing says which one holds the value: name it once")
	ErrNoCostColumn  = errors.New("the header names no cost column: write the estimate in ListCost")
	ErrAmbiguousCost = errors.New("the header has no ListCost and more than one of these, which mean different things: write the estimate in ListCost")
	ErrMissingColumn = errors.New("the header lacks this column, which every row needs")
	ErrNoAmount      = errors.New("the row has no amount: write the estimate as a decimal, such as 25.488")
	ErrNotDecimal    = errors.New("not a plain decimal: write digits with at most one point, such as 25.488, with no sign, exponent, grouping or space, and at most 26 digits before the point")
	ErrNegative      = errors.New("negative: the row contract holds amounts of 0 and above, so a saving cannot be one row. Write what the resources will cost, not the change")
	ErrScale         = errors.New("more than 12 digits after the point, which the row contract holds: round it where the estimator computes it")
	ErrNoCurrency    = errors.New("not a currency: write the ISO 4217 code of the row's amount, three upper-case letters such as EUR")
	ErrNoPeriod      = errors.New("the row has no charge period: write the start and the end of the period the amount covers")
	ErrInstant       = errors.New("not a FOCUS date and time: write UTC to the second, such as 2026-12-01T00:00:00Z")
	ErrPeriodOrder   = errors.New("the period ends at or before it starts: ChargePeriodEnd is exclusive and after ChargePeriodStart")
	ErrNotUsage      = errors.New("the row contract holds Usage charges alone: leave purchases, taxes, credits and adjustments out of the file")
	ErrChargeClass   = errors.New("the row contract holds no correction: leave ChargeClass empty and write the corrected amount")
	ErrTooLong       = fmt.Errorf("longer than %d characters, which the row contract holds", MaxText)
)

The refusals. Each says what is wrong and what to write instead.

View Source
var CostColumns = []string{columnListCost, columnEffectiveCost, columnContractedCost, columnBilledCost}

CostColumns are the columns a figure is read from, in the order they are preferred. ListCost is where the row contract puts an estimate. Without it, the file must hold exactly one of the other three, because each means a different thing and nothing says which is the estimate.

Functions

func CheckSource

func CheckSource(name string) error

CheckSource holds a source name to its rule: 1 to 64 lower-case letters, digits and single hyphens, opening and closing with a letter or a digit.

func ReadFigure

func ReadFigure(text string) (decimal.Decimal, error)

ReadFigure reads an estimate's figure as the file states it: a plain decimal of at most 26 integer digits and 12 fractional digits, never negative. The error is one of the refusals.

func Resource

func Resource(description string) string

Resource is the resource a row's ChargeDescription names: its name, else its id, empty when it names neither.

Types

type Estimate

type Estimate struct {
	Figure                                           decimal.Decimal
	Currency                                         string
	Start, End                                       time.Time
	Description                                      string
	BillingAccount, BillingAccountName               string
	SubAccount, SubAccountName                       string
	Provider, Host, InvoiceIssuer                    string
	ServiceName, ServiceCategory, ServiceSubcategory string
	SkuID, SkuMeter, SkuPriceID                      string
	Region, RegionName                               string
	Tags                                             map[string]string
	RowID                                            string
}

Estimate is what one estimate row states. Row fills what the row contract requires and the estimate leaves empty, so the file's reader and the ledger write an estimate's row the same way.

func (Estimate) Row

func (e Estimate) Row() focus.Row

Row is the estimate's row: the figure in ListCost, ListUnitPrice, ContractedCost and ContractedUnitPrice, 0 in BilledCost and EffectiveCost, one unit of Estimate, and a Usage charge.

type File

type File struct {
	Rows []focus.Row
	// CostColumn is the column every figure was read from.
	CostColumn string
	// TaggedRows counts the rows whose Tags the reader dropped.
	TaggedRows int
}

File is what Read made of a file.

func Read

func Read(source io.Reader, options Options) (File, error)

Read reads a FOCUS cost file as CSV with a header, and returns one row per row of the file. One row the reader refuses refuses the file, so a partial estimate never reads as a smaller one. The same bytes and options give the same rows.

type Options

type Options struct {
	// Source is costroid/cost_source, DefaultSource when empty.
	Source string
	// Tags are the CI system's join keys: repository, pull request, commit,
	// run and visibility. The file never sets them.
	Tags map[string]string
	// BillingAccount fills BillingAccountId on a row whose file states none.
	BillingAccount string
}

Options are what the caller adds to every row.

type Refusal

type Refusal struct {
	Line   int
	Column string
	Value  string
	Err    error
}

Refusal is a file Read refused: the line and the column at fault, the value it holds, and why. Line 0 is the file as a whole, and line 1 its header.

func (*Refusal) Error

func (r *Refusal) Error() string

func (*Refusal) Unwrap

func (r *Refusal) Unwrap() error

type Unavailable

type Unavailable struct {
	Estimate string `json:"estimate"`
	Reason   string `json:"reason"`
	Source   string `json:"source"`
}

Unavailable is the note ingest-run --optional leaves beside the rows when the estimate it was given is not there, so the report can say so. The agent's job writes it, so a reader takes a reason from the closed list and a source that passes CheckSource, and nothing else.

func NotAvailable

func NotAvailable(source string) Unavailable

NotAvailable is the note for a source whose file was not there.

func ParseUnavailable

func ParseUnavailable(data []byte) (Unavailable, error)

ParseUnavailable reads a note and holds it to the closed list.

func (Unavailable) Sentence

func (u Unavailable) Sentence() string

Sentence is the words for the note's reason.

Jump to

Keyboard shortcuts

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