Documentation
¶
Overview ¶
Package store is the ledger: FOCUS rows in Postgres, one tenant at a time.
Every query runs inside a transaction that names its tenant with SET LOCAL, and row level security does the filtering. No statement here carries a tenant predicate of its own.
Index ¶
- Constants
- Variables
- func AddKey(ctx context.Context, ownerURL string, owner Binding, digest string) error
- func KeyDigest(key string) string
- func Migrate(ctx context.Context, ownerURL string) error
- func MigrateFrom(ctx context.Context, ownerURL string, files fs.FS) error
- func NewKey() (string, error)
- func Provision(ctx context.Context, ownerURL string, tenant Tenant) error
- func RevokeKey(ctx context.Context, ownerURL, digest string) error
- type Batch
- type Binding
- type Line
- type Receipt
- type Record
- type Store
- func (s *Store) AddEgress(ctx context.Context, counts []egress.Count) error
- func (s *Store) Close()
- func (s *Store) Egress(ctx context.Context) ([]egress.Count, error)
- func (s *Store) Ingest(ctx context.Context, tenant TenantID, batch Batch) (Receipt, error)
- func (s *Store) PullRequest(ctx context.Context, tenant TenantID, repo, pr string) ([]Line, error)
- func (s *Store) TenantForKey(ctx context.Context, keySHA256 string) (TenantID, error)
- func (s *Store) TenantForOwner(ctx context.Context, host, owner string) (TenantID, error)
- type Tenant
- type TenantID
Constants ¶
const ( HeadFromToken = "token" HeadStated = "stated" )
Values of HeadBasis.
const ( TagRepo = focus.TagRepo TagPR = focus.TagPR TagCommitSHA = focus.TagCommitSHA TagWorkflowRunID = focus.TagWorkflowRunID )
Tag keys of the convention that the focus package leaves to the join.
const KeyPrefix = "costroid_ik_"
KeyPrefix starts every ingest key. A bearer without it is a CI token.
Variables ¶
var ErrDatabaseAhead = errors.New("store: database is ahead of this binary")
ErrDatabaseAhead means the database holds a version this binary does not know. Migrations run forward only, so the binary is what has to change.
var ErrMigrationEdited = errors.New("store: an applied migration was edited")
ErrMigrationEdited means a migration already applied has changed on disk.
var ErrNotFound = errors.New("store: not found")
ErrNotFound means a lookup named nothing.
var ErrUnsafeRole = errors.New("store: unsafe application role")
ErrUnsafeRole means the service was given a role that could read past row level security.
Functions ¶
func AddKey ¶
AddKey stores a key's digest for the tenant holding an SCM owner, through the owner role's connection. An owner no tenant holds answers ErrNotFound.
func MigrateFrom ¶
MigrateFrom applies the migrations in files, each in its own transaction.
func NewKey ¶
NewKey mints an ingest key. Only its digest is ever stored, so a key that is lost is revoked and replaced.
Types ¶
type Batch ¶
type Batch struct {
Records []Record
// Drops counts fields dropped at the edge, by field name.
Drops map[string]int64
Day time.Time
}
Batch is one ingest request's writes.
type Line ¶
type Line struct {
Currency string
// BillingAccountID is a sender's statement, so a line never adds across it.
BillingAccountID string
ServiceCategory string
Agent string
Model string
SkuMeter string
ConsumedUnit string
CostBasis string
CostSource string
AttributionGrade string
RunIdentity string
BudgetEligible bool
Rows int64
ConsumedQuantity decimal.Decimal
ListCost decimal.Decimal
EffectiveCost decimal.Decimal
FirstCharge time.Time
LastCharge time.Time
}
Line is one group of a pull request's rows. Lines never add across currencies or across units.
type Record ¶
type Record struct {
Row focus.Row
RunAttempt string
// Actor is an SCM login or a per-tenant pseudonym, never an address.
Actor string
Event string
// TokenCommitSHA is the commit the CI token verifies, empty without one.
TokenCommitSHA string
// HeadBasis says where costroid/commit_sha came from, empty when the row
// has none.
HeadBasis string
}
Record is one row and the facts about its run that have no FOCUS column.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the application role's handle on the ledger.
func Open ¶
Open connects as the application role and refuses a role that could read past row level security.
func (*Store) AddEgress ¶
AddEgress adds to the deployment's egress counters, which belong to no tenant, so no tenant is set.
func (*Store) Ingest ¶
Ingest writes a batch whole or not at all. A row id already stored for this tenant is a duplicate and changes nothing.
func (*Store) PullRequest ¶
PullRequest derives one pull request's lines from the rows.
func (*Store) TenantForKey ¶
TenantForKey resolves the SHA-256 of an ingest key to its tenant.
type Tenant ¶
type Tenant struct {
ID TenantID
Name string
// Bindings are the SCM owners whose CI tokens resolve to this tenant.
Bindings []Binding
// KeySHA256 lists the SHA-256 of each ingest key.
KeySHA256 []string
}
Tenant is one provisioned tenant.
type TenantID ¶
type TenantID struct {
// contains filtered or unexported fields
}
TenantID is a validated tenant identifier. It is safe to place in SET LOCAL, which takes no bind parameters.
func ParseTenantID ¶
ParseTenantID accepts the canonical lower-case UUID form only.