store

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package store is the demo console's read-only seam over the engine database. The console renders billing periods, runs, statements, pricing versions and correction deltas; the engine exposes no read API for them, so the console opens that database directly. Nothing in this package writes: queries.sql holds SELECTs alone, and every method here reads.

It is also the seam a read API of the engine would be swapped in at. The pages call these methods and see plain Go values rather than pgx types, so replacing the queries with HTTP calls is a change to this package alone.

Every method wraps a failed query as "<QueryName>: %w" with the name the query carries in queries.sql, so an error page can name the read that failed while errors.Is(err, pgx.ErrNoRows) still holds for a lookup that found nothing.

A stored amount reaches a page as a decimal. amountOf refuses a NULL and a NaN numeric, which is not an amount anything can be shown for: a list drops such a row and logs it, a single read reports it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Delta

type Delta struct {
	Cloud        string
	Platform     string
	ResourceType string
	ResourceID   string
	ProjectID    string
	Dimension    string
	OldAmount    decimal.Decimal
	NewAmount    decimal.Decimal
	Delta        decimal.Decimal
	Currency     string
}

Delta is one row a correction run moved: the key it belongs to and the two amounts it moved between.

type Kickback

type Kickback struct {
	RunID      uuid.UUID
	PeriodFrom time.Time
	Kind       string
	Status     string
	// StatementKey is the key the statement of the adjusted project is stored
	// under, and Cloud and ProjectID are its two halves.
	StatementKey     string
	Cloud, ProjectID string
	RelationID       uuid.UUID
	Scope            string
	Rate             decimal.Decimal
	Base, Amount     decimal.Decimal
	Currency         string
}

Kickback is one kickback of one run as the settlement report reads it: what a partner is owed for one project, under the run that owes it. A correction's rows are the difference to the run it corrects, which is what makes the kickbacks of a period add up the way its statements do.

type Period

type Period struct {
	From           time.Time
	To             time.Time
	Status         string
	FinalizedRunID uuid.UUID
	FinalizedAt    time.Time
}

Period is one billing month. FinalizedRunID and FinalizedAt are zero for a period that is not finalized, which is the pairing the schema admits.

type PricingDocument

type PricingDocument struct {
	PricingModel
	Document []byte
}

PricingDocument is a catalog version with the document it was imported from.

type PricingModel

type PricingModel struct {
	Version    string
	ValidFrom  time.Time
	Currency   string
	ImportedAt time.Time
}

PricingModel is one imported catalog version without its document.

type ProjectStatementRow

type ProjectStatementRow struct {
	RunID      uuid.UUID
	PeriodFrom time.Time
	Kind       string
	Status     string
	Total      decimal.Decimal
	Currency   string
}

ProjectStatementRow is one month of a project's history: the run that billed it and what that run charged.

type Run

type Run struct {
	ID             uuid.UUID
	PeriodFrom     time.Time
	PeriodTo       time.Time
	Kind           string
	CorrectsRunID  uuid.UUID
	PricingVersion string
	Status         string
	Clouds         []string
	Stats          []byte
	StartedAt      time.Time
	CompletedAt    time.Time
}

Run is one metering run. CorrectsRunID is zero for a regular run, PricingVersion empty for a run of a period no model priced, and CompletedAt zero for a run no end was written for.

type RunTotal

type RunTotal struct {
	RunID      uuid.UUID
	Currency   string
	Statements int64
	Total      decimal.Decimal
}

RunTotal is what one run of a period billed in one currency: how many statements it wrote and what they add up to.

type Segment

type Segment struct {
	State     string
	From      time.Time
	To        time.Time
	Seconds   int64
	Usage     []byte
	Dimension string
	Amount    decimal.Decimal
	Currency  string
}

Segment is one metered interval of a resource at one rated dimension. A resource metered over three intervals and rated on two dimensions each has six of them.

type Statement

type Statement struct {
	Document []byte
	Total    decimal.Decimal
	Currency string
}

Statement is the stored document of one project statement with the total it sums to.

type StatementRow

type StatementRow struct {
	Key      string
	Total    decimal.Decimal
	Currency string
}

StatementRow is one line of a run's statement table: the project key and what the run billed it.

type Store

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

Store owns the connection pool the console reads the engine database through.

func New

func New(ctx context.Context, dbURL string, logger *slog.Logger) (*Store, error)

New parses dbURL and prepares the pool for it. Connections are established lazily, the way the engine's own store opens: a database that is down does not keep the console from starting, it fails the pages that read it.

func (*Store) Close

func (s *Store) Close()

Close releases every connection the pool holds and waits for in-flight ones to be returned.

func (*Store) GetPeriod

func (s *Store) GetPeriod(ctx context.Context, from time.Time) (Period, error)

GetPeriod returns one billing month. A month no run ever opened comes back as pgx.ErrNoRows, which is what the page turns into its not-found answer.

func (*Store) GetPricingModel

func (s *Store) GetPricingModel(ctx context.Context, version string) (PricingDocument, error)

GetPricingModel returns one catalog version with its document. A version that was never imported comes back as pgx.ErrNoRows.

func (*Store) GetRun

func (s *Store) GetRun(ctx context.Context, id uuid.UUID) (Run, error)

GetRun returns one run. A run that does not exist comes back as pgx.ErrNoRows, which is what the page turns into its not-found answer.

func (*Store) GetStatement

func (s *Store) GetStatement(ctx context.Context, runID uuid.UUID, key string) (Statement, error)

GetStatement returns the stored document of one project statement. A pair that was never billed comes back as pgx.ErrNoRows. A total that is not a number is an error rather than a skipped row: the page exists to show that one statement, and there is nothing left of it to render.

func (*Store) LatestRunWithResource

func (s *Store) LatestRunWithResource(ctx context.Context, cloud, resourceType, resourceID string) (uuid.UUID, bool, error)

LatestRunWithResource returns the newest run that metered a resource, and reports whether any run did. A resource no run ever metered is a page that has nothing to show rather than a failed read, so it comes back as false and no error.

func (*Store) ListCorrectionDeltas

func (s *Store) ListCorrectionDeltas(ctx context.Context, runID uuid.UUID) ([]Delta, error)

ListCorrectionDeltas returns the rows a correction run moved. A run that corrected nothing, and every regular run, has none.

func (*Store) ListKickbacksForBeneficiary

func (s *Store) ListKickbacksForBeneficiary(ctx context.Context, beneficiary string) ([]Kickback, error)

ListKickbacksForBeneficiary returns what every run that stands settles for one partner, oldest period first and inside a period in the order the runs started.

The arithmetic is the engine's own: each run is read through export.LoadKickbacks, which renders a regular run's kickback records as they stand and a correction's as the difference to the run it corrects. Doing it here a second time would be a second implementation of what a partner is paid, and the two would drift.

The runs are read one by one rather than in one query, because a correction's settlement is a diff of two runs and only that read knows which run to diff against. A partner is settled by one regular run and its corrections per period, so the reads are one per run of the periods the partner appears in.

func (*Store) ListPeriods

func (s *Store) ListPeriods(ctx context.Context) ([]Period, error)

ListPeriods returns every billing period, newest month first.

func (*Store) ListPricingModels

func (s *Store) ListPricingModels(ctx context.Context) ([]PricingModel, error)

ListPricingModels returns the imported catalog versions, the one in force last first.

func (*Store) ListResourceSegments

func (s *Store) ListResourceSegments(ctx context.Context, runID uuid.UUID, cloud, resourceType, resourceID string) ([]Segment, error)

ListResourceSegments returns what one run metered and rated for one resource, ordered by interval and then dimension.

func (*Store) ListRunTotalsForPeriod

func (s *Store) ListRunTotalsForPeriod(ctx context.Context, from time.Time) ([]RunTotal, error)

ListRunTotalsForPeriod returns, per run of one billing month and currency, how many statements the run wrote and what they add up to, by run and then by currency. One statement total that is not a number makes the sum of its run one too, and that sum is dropped and logged the way a listing drops any such row.

func (*Store) ListRuns

func (s *Store) ListRuns(ctx context.Context, limit int32) ([]Run, error)

ListRuns returns the newest runs by start, at most limit of them.

func (*Store) ListRunsForPeriod

func (s *Store) ListRunsForPeriod(ctx context.Context, from time.Time) ([]Run, error)

ListRunsForPeriod returns every run of one billing month in the order the runs started. A month no run was recorded for has none.

func (*Store) ListStatements

func (s *Store) ListStatements(ctx context.Context, runID uuid.UUID) ([]StatementRow, error)

ListStatements returns the statements of a run, largest total first.

func (*Store) ListStatementsForProject

func (s *Store) ListStatementsForProject(ctx context.Context, key string) ([]ProjectStatementRow, error)

ListStatementsForProject returns what every run billed one project, oldest month first.

func (*Store) LoadRunExport

func (s *Store) LoadRunExport(ctx context.Context, runID uuid.UUID) (export.Run, error)

LoadRunExport returns one run with what the JSON export renders its run.json and its kickbacks.json from: the run row, its statements and what it settles for its partners, read through export.LoadIndex under one snapshot. The read and the rendering are the export's own, for the reason ListKickbacksForBeneficiary gives: a second implementation of what an ERP receives would drift. A run the export does not read is refused with export.ErrRunNotExportable, which errors.Is still finds.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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