sqlcgen

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdjustmentRecord

type AdjustmentRecord struct {
	ID             pgtype.UUID
	RunID          pgtype.UUID
	ProjectID      string
	RelationID     pgtype.UUID
	RelationType   string
	RelationTarget string
	Beneficiary    pgtype.Text
	Type           string
	Scope          string
	Rate           pgtype.Numeric
	Base           pgtype.Numeric
	Amount         pgtype.Numeric
	Currency       string
}

type BillingPeriod

type BillingPeriod struct {
	PeriodFrom     pgtype.Timestamptz
	PeriodTo       pgtype.Timestamptz
	Status         string
	FinalizedRunID pgtype.UUID
	FinalizedAt    pgtype.Timestamptz
}

type CompleteRunParams

type CompleteRunParams struct {
	ID    pgtype.UUID
	Stats []byte
}

type ConsecutiveFailedRunsRow

type ConsecutiveFailedRunsRow struct {
	Failures    int32
	LastFailure pgtype.Timestamptz
}

type CorrectionDelta

type CorrectionDelta struct {
	ID            pgtype.UUID
	RunID         pgtype.UUID
	CorrectsRunID pgtype.UUID
	Cloud         string
	Platform      string
	ResourceType  string
	ResourceID    string
	ProjectID     string
	Dimension     string
	OldAmount     pgtype.Numeric
	NewAmount     pgtype.Numeric
	Delta         pgtype.Numeric
	Currency      string
}

type CreateAdjustmentRecordsParams

type CreateAdjustmentRecordsParams struct {
	ID             pgtype.UUID
	RunID          pgtype.UUID
	ProjectID      string
	RelationID     pgtype.UUID
	RelationType   string
	RelationTarget string
	Beneficiary    pgtype.Text
	Type           string
	Scope          string
	Rate           pgtype.Numeric
	Base           pgtype.Numeric
	Amount         pgtype.Numeric
	Currency       string
}

type CreateCorrectionDeltasParams

type CreateCorrectionDeltasParams struct {
	ID            pgtype.UUID
	RunID         pgtype.UUID
	CorrectsRunID pgtype.UUID
	Cloud         string
	Platform      string
	ResourceType  string
	ResourceID    string
	ProjectID     string
	Dimension     string
	OldAmount     pgtype.Numeric
	NewAmount     pgtype.Numeric
	Delta         pgtype.Numeric
	Currency      string
}

type CreateRatedRecordsParams

type CreateRatedRecordsParams struct {
	ID            pgtype.UUID
	RunID         pgtype.UUID
	UsageRecordID pgtype.UUID
	Dimension     string
	Amount        pgtype.Numeric
	Currency      string
}

type CreateUsageRecordsParams

type CreateUsageRecordsParams struct {
	ID           pgtype.UUID
	RunID        pgtype.UUID
	Cloud        string
	Platform     string
	ResourceType string
	ResourceID   string
	ProjectID    string
	State        string
	FromTs       pgtype.Timestamptz
	ToTs         pgtype.Timestamptz
	Seconds      int64
	Usage        []byte
}

type DBTX

type DBTX interface {
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
	QueryRow(context.Context, string, ...interface{}) pgx.Row
	CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
}

type FailRunParams

type FailRunParams struct {
	ID    pgtype.UUID
	Stats []byte
}

type FinalizeBillingPeriodParams

type FinalizeBillingPeriodParams struct {
	PeriodFrom     pgtype.Timestamptz
	FinalizedRunID pgtype.UUID
}

type InsertPricingModelParams

type InsertPricingModelParams struct {
	Version   string
	ValidFrom pgtype.Timestamptz
	Currency  string
	Document  []byte
}

type InsertProjectStatementParams

type InsertProjectStatementParams struct {
	RunID     pgtype.UUID
	ProjectID string
	Document  []byte
	Total     pgtype.Numeric
	Currency  string
}

type InsertRunParams

type InsertRunParams struct {
	PeriodFrom     pgtype.Timestamptz
	PeriodTo       pgtype.Timestamptz
	Kind           string
	CorrectsRunID  pgtype.UUID
	PricingVersion pgtype.Text
	Clouds         []string
}

type InsertRunRow

type InsertRunRow struct {
	ID        pgtype.UUID
	StartedAt pgtype.Timestamptz
}

type LatestFinalizedRunRow

type LatestFinalizedRunRow struct {
	ID             pgtype.UUID
	Kind           string
	CorrectsRunID  pgtype.UUID
	PricingVersion pgtype.Text
	Clouds         []string
	Stats          []byte
	StartedAt      pgtype.Timestamptz
}

type ListBillingPeriodsRow

type ListBillingPeriodsRow struct {
	PeriodFrom     pgtype.Timestamptz
	Status         string
	FinalizedRunID pgtype.UUID
	FinalizedAt    pgtype.Timestamptz
}

type ListCorrectionDeltasRow

type ListCorrectionDeltasRow struct {
	Cloud        string
	Platform     string
	ResourceType string
	ResourceID   string
	ProjectID    string
	Dimension    string
	OldAmount    pgtype.Numeric
	NewAmount    pgtype.Numeric
	Delta        pgtype.Numeric
	Currency     string
}

type ListPricingModelsRow

type ListPricingModelsRow struct {
	Version    string
	ValidFrom  pgtype.Timestamptz
	Currency   string
	ImportedAt pgtype.Timestamptz
}

type ListRatedRecordsRow

type ListRatedRecordsRow struct {
	Cloud        string
	Platform     string
	ResourceType string
	ResourceID   string
	ProjectID    string
	State        string
	FromTs       pgtype.Timestamptz
	ToTs         pgtype.Timestamptz
	Usage        []byte
	Dimension    string
	Amount       pgtype.Numeric
	Currency     string
}

type PricingModel

type PricingModel struct {
	Version    string
	ValidFrom  pgtype.Timestamptz
	Currency   string
	Document   []byte
	ImportedAt pgtype.Timestamptz
}

type ProjectStatement

type ProjectStatement struct {
	ID        pgtype.UUID
	RunID     pgtype.UUID
	ProjectID string
	Document  []byte
	Total     pgtype.Numeric
	Currency  string
}

type Queries

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

func New

func New(db DBTX) *Queries

func (*Queries) CompleteRun

func (q *Queries) CompleteRun(ctx context.Context, arg CompleteRunParams) (int64, error)

The three ends of a run. stats carries what the run counted, and the failed one keeps it: a run that broke halfway is read for how far it got.

The two ends of a run in flight name the status they move from and report how many rows they moved. A run this process opened can have been failed underneath it by the reclaim below, and putting it back would return a run the period has already replaced to that period's numbers: trg_runs_immutable fires on OLD.status = 'finalized' alone, so 'failed' -> 'completed' passes the database untouched. The caller reads the count and refuses rather than writing over a status another process settled.

func (*Queries) ConsecutiveFailedRuns

func (q *Queries) ConsecutiveFailedRuns(ctx context.Context, periodFrom pgtype.Timestamptz) (ConsecutiveFailedRunsRow, error)

How often the metering of a period failed in a row: the failed regular runs it carries since the last one that got to the end, and when the last of them stopped. Without this the tick meters a month again on every hourly pass for as long as it keeps failing, and a month that never stops failing -- an event ordering its history carries and no later event repairs -- costs a full metering pass and another stats blob every hour for the life of the deployment. The scheduler spaces its retries out over this count.

func (*Queries) CreateAdjustmentRecords

func (q *Queries) CreateAdjustmentRecords(ctx context.Context, arg []CreateAdjustmentRecordsParams) (int64, error)

The adjustment rows of a run, over the same protocol and naming id for the same reason: COPY evaluates no defaults.

func (*Queries) CreateCorrectionDeltas

func (q *Queries) CreateCorrectionDeltas(ctx context.Context, arg []CreateCorrectionDeltasParams) (int64, error)

func (*Queries) CreateRatedRecords

func (q *Queries) CreateRatedRecords(ctx context.Context, arg []CreateRatedRecordsParams) (int64, error)

func (*Queries) CreateUsageRecords

func (q *Queries) CreateUsageRecords(ctx context.Context, arg []CreateUsageRecordsParams) (int64, error)

The record writes of a run, over the COPY protocol: a month of metering is tens of thousands of rows, and one round trip per row is what that costs. All three name id rather than leaving it to the column default, because COPY evaluates no defaults. The caller generating the ids is also what lets a rated record name the usage record it was rated from before either row is written.

func (*Queries) EarliestBillingPeriod

func (q *Queries) EarliestBillingPeriod(ctx context.Context) (pgtype.Timestamptz, error)

The first period the engine knows, where the scheduler starts its walk. The aggregate answers over an empty table too, with one NULL row.

func (*Queries) EarliestPricingValidFrom added in v0.5.0

func (q *Queries) EarliestPricingValidFrom(ctx context.Context) (pgtype.Timestamptz, error)

The first instant any pricing model is valid from, which bounds the scheduler's walk from below: a month that begins before it is priced by no model. The aggregate answers over an empty table too, with one NULL row.

func (*Queries) FailRun

func (q *Queries) FailRun(ctx context.Context, arg FailRunParams) (int64, error)

func (*Queries) FinalizeBillingPeriod

func (q *Queries) FinalizeBillingPeriod(ctx context.Context, arg FinalizeBillingPeriodParams) error

Closes a period. The three columns move in one statement because the check constraint on billing_periods ties them together: a period is finalized exactly when it names the run that closed it and the time that happened.

func (*Queries) FinalizeRun

func (q *Queries) FinalizeRun(ctx context.Context, id pgtype.UUID) error

func (*Queries) GetBillingPeriod

func (q *Queries) GetBillingPeriod(ctx context.Context, periodFrom pgtype.Timestamptz) (BillingPeriod, error)

func (*Queries) GetPricingModel

func (q *Queries) GetPricingModel(ctx context.Context, version string) (PricingModel, error)

func (*Queries) GetRun

func (q *Queries) GetRun(ctx context.Context, id pgtype.UUID) (Run, error)

The same eleven columns without a FOR clause. An export only reads a run, so it takes no row lock: the FOR NO KEY UPDATE above would queue the export behind a run being finalized, and hold that finalization up in turn.

func (*Queries) GetRunForUpdate

func (q *Queries) GetRunForUpdate(ctx context.Context, id pgtype.UUID) (Run, error)

Reads a run under that same lock, which is how finalization sees a status that no concurrent writer can move underneath it.

func (*Queries) HasCompleteRegularRun

func (q *Queries) HasCompleteRegularRun(ctx context.Context, periodFrom pgtype.Timestamptz) (bool, error)

Whether the period already has a regular run that got to the end. A finalized run counts as one: it is a completed run that closed its period.

func (*Queries) InsertPricingModel

func (q *Queries) InsertPricingModel(ctx context.Context, arg InsertPricingModelParams) (int64, error)

func (*Queries) InsertProjectStatement

func (q *Queries) InsertProjectStatement(ctx context.Context, arg InsertProjectStatementParams) error

func (*Queries) InsertRun

func (q *Queries) InsertRun(ctx context.Context, arg InsertRunParams) (InsertRunRow, error)

Opens a run. status stays at the column default 'running' and completed_at stays null until the run ends. The caller writes its records under the returned id and reports the returned start time. A regular run binds NULL for corrects_run_id, which is an invalid pgtype.UUID; a correction binds the run it corrects.

func (*Queries) LatestCompletedRegularRun

func (q *Queries) LatestCompletedRegularRun(ctx context.Context, periodFrom pgtype.Timestamptz) (pgtype.UUID, error)

The run a period's current numbers come from. Superseding leaves at most one completed regular run per period, and the ordering settles which one a reader takes if it ever sees more.

func (*Queries) LatestFinalizedRun

func (q *Queries) LatestFinalizedRun(ctx context.Context, periodFrom pgtype.Timestamptz) (LatestFinalizedRunRow, error)

The latest finalized truth of a period, which a correction diffs against and then names in corrects_run_id: the regular run that closed the period for the first correction, the last finalized correction after that (roadmap WP 3.9, item 5). detect-late reads the snapshot time out of its stats.

func (*Queries) ListAdjustmentRecords

func (q *Queries) ListAdjustmentRecords(ctx context.Context, runID pgtype.UUID) ([]AdjustmentRecord, error)

The adjustments a run applied, in a total order over the rows one run writes: a project's adjustments come from distinct relations, and one relation contributes at most one line per type and scope. A correction diffs these rows against its own, which is what the order is for.

func (*Queries) ListBillingPeriods

func (q *Queries) ListBillingPeriods(ctx context.Context) ([]ListBillingPeriodsRow, error)

func (*Queries) ListCorrectionDeltas

func (q *Queries) ListCorrectionDeltas(ctx context.Context, runID pgtype.UUID) ([]ListCorrectionDeltasRow, error)

The delta rows of a correction run, in the order corrections.Diff sorts by (internal/engine/corrections/corrections.go), so an export prints them as the correction computed them. idx_delta_run serves the filter.

func (*Queries) ListPricingModels

func (q *Queries) ListPricingModels(ctx context.Context) ([]ListPricingModelsRow, error)

func (*Queries) ListProjectStatements

func (q *Queries) ListProjectStatements(ctx context.Context, runID pgtype.UUID) ([]ProjectStatement, error)

func (*Queries) ListRatedRecords

func (q *Queries) ListRatedRecords(ctx context.Context, runID pgtype.UUID) ([]ListRatedRecordsRow, error)

One row per rated record of a run, joined to the usage record it rates. The ordering is a total one: the drafts of one resource never overlap (draftsOf in internal/engine/metering/metering.go builds them from the intervals of a folded history), so from_ts is unique per resource within a run, and rating emits one record per usage record and dimension. That total order is what makes two exports of one run byte-identical. idx_rated_run serves the filter.

func (*Queries) LockRun

func (q *Queries) LockRun(ctx context.Context, id pgtype.UUID) (pgtype.UUID, error)

The first statement of a transaction that writes records. The trigger on the record tables locks the run FOR SHARE for every row it checks, and a transaction holding that lock which then updates the same run row has to escalate to FOR NO KEY UPDATE, which deadlocks two writers of one run. Taking the stronger lock up front is the ordering the comment on forbid_finalized_mutation in migration 0001 asks for.

func (*Queries) PricingModelForPeriod

func (q *Queries) PricingModelForPeriod(ctx context.Context, validFrom pgtype.Timestamptz) (PricingModel, error)

func (*Queries) ReclaimStaleRuns

func (q *Queries) ReclaimStaleRuns(ctx context.Context, periodFrom pgtype.Timestamptz) ([]pgtype.UUID, error)

Reclaims the runs of a period that are still in flight with no process behind them. A killed process leaves its run at 'running' forever, and the period is blocked for as long as that row reads as a run in progress. The reason is merged into stats rather than written over it, so whatever the run had already counted survives.

Age is what stands for the missing process, and the period lock cannot: that lock is session scoped on one pooled connection which stays protocol-idle for the whole run, so anything that closes only that connection -- an idle timeout, a reaper, a failover -- releases it while the process keeps metering. Two hours is longer than a run that is still alive: the CronJob's Job is killed after fifty minutes (activeDeadlineSeconds), and a month an operator meters by hand is minutes of work. Waiting costs nothing, because no query counts a 'running' row.

func (*Queries) SetBillingPeriodStatus

func (q *Queries) SetBillingPeriodStatus(ctx context.Context, arg SetBillingPeriodStatusParams) error

func (*Queries) SumRatedByRun

func (q *Queries) SumRatedByRun(ctx context.Context, runID pgtype.UUID) ([]SumRatedByRunRow, error)

The amounts of one run summed per the key a correction diffs by (decision D6: cloud, platform, resource type, resource id, project id, dimension). The currency is grouped too, so a run rated in one currency yields one row per key. idx_rated_run serves the filter.

func (*Queries) SupersedeCompletedRuns

func (q *Queries) SupersedeCompletedRuns(ctx context.Context, arg SupersedeCompletedRunsParams) ([]pgtype.UUID, error)

Retires the completed runs of the given kind that a new run of that kind replaces, so that a period carries at most one completed run per kind and a sum over that kind counts one set of records. It runs inside the new run's write transaction while that run is still 'running', so the filter on 'completed' never matches the run doing the superseding.

func (*Queries) TryLockPeriod

func (q *Queries) TryLockPeriod(ctx context.Context, periodFrom string) (bool, error)

One process at a time meters a period. The lock is an advisory one rather than a row lock, because it is taken before the billing_periods row of the period exists. Its key is hashed from the period start as text, which the caller renders in UTC and RFC 3339, so that the same instant always reaches the same key: a timestamptz argument would be rendered by the server and the key would follow that session's DateStyle and TimeZone.

func (*Queries) UnlockPeriod

func (q *Queries) UnlockPeriod(ctx context.Context, periodFrom string) (bool, error)

Releases what TryLockPeriod took. An advisory lock is released by key, so the key expression has to stay identical to the one above.

func (*Queries) UpsertBillingPeriod

func (q *Queries) UpsertBillingPeriod(ctx context.Context, arg UpsertBillingPeriodParams) error

Records that a period exists. The scheduler walks the months it is responsible for and calls this for each of them, so a period already known keeps the status and the finalization columns it has.

func (*Queries) WithTx

func (q *Queries) WithTx(tx pgx.Tx) *Queries

type RatedRecord

type RatedRecord struct {
	ID            pgtype.UUID
	RunID         pgtype.UUID
	UsageRecordID pgtype.UUID
	Dimension     string
	Amount        pgtype.Numeric
	Currency      string
}

type Run

type Run struct {
	ID             pgtype.UUID
	PeriodFrom     pgtype.Timestamptz
	PeriodTo       pgtype.Timestamptz
	Kind           string
	CorrectsRunID  pgtype.UUID
	PricingVersion pgtype.Text
	Status         string
	Clouds         []string
	Stats          []byte
	StartedAt      pgtype.Timestamptz
	CompletedAt    pgtype.Timestamptz
}

type SetBillingPeriodStatusParams

type SetBillingPeriodStatusParams struct {
	PeriodFrom pgtype.Timestamptz
	Status     string
}

type SumRatedByRunRow

type SumRatedByRunRow struct {
	Cloud        string
	Platform     string
	ResourceType string
	ResourceID   string
	ProjectID    string
	Dimension    string
	Amount       pgtype.Numeric
	Currency     string
}

type SupersedeCompletedRunsParams

type SupersedeCompletedRunsParams struct {
	PeriodFrom pgtype.Timestamptz
	Kind       string
}

type UpsertBillingPeriodParams

type UpsertBillingPeriodParams struct {
	PeriodFrom pgtype.Timestamptz
	PeriodTo   pgtype.Timestamptz
}

type UsageRecord

type UsageRecord struct {
	ID           pgtype.UUID
	RunID        pgtype.UUID
	Cloud        string
	Platform     string
	ResourceType string
	ResourceID   string
	ProjectID    string
	State        string
	FromTs       pgtype.Timestamptz
	ToTs         pgtype.Timestamptz
	Seconds      int64
	Usage        []byte
}

Jump to

Keyboard shortcuts

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