db

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: BSD-3-Clause Imports: 10 Imported by: 14

Documentation

Index

Constants

View Source
const PGTimestampFormat = "2006-01-02 15:04:05-07"

PGTimestampFormat is the string representation of PostgreSQL's `timestamptz`.

Variables

View Source
var DefaultQueries = []Query{
	{
		Name:        "appuio_cloud_memory",
		Description: "Memory usage (maximum of requested and used memory) aggregated by namespace",
		Query:       appuioCloudMemoryQuery,
		Unit:        "MiB",
		// contains filtered or unexported fields
	},
	{
		Name:        "appuio_cloud_loadbalancer",
		Description: "Number of services of type load balancer",
		Query:       appuioCloudLoadbalancerQuery,
	},
	{
		Name:        "appuio_cloud_persistent_storage",
		Description: "Persistent storage usage aggregated by namespace and storageclass",
		Query:       appuioCloudPersistentStorageQuery,
		Unit:        "GiB",
	},
	{
		Name:        "appuio_managed_openshift_vcpu",
		Description: "vCPU aggregated by cluster, node (app, storage), and service level",
		Query:       appuioManagedOpenShiftvCPUQuery,
		Unit:        "vCPU",
	},
	{
		Name:        "appuio_managed_openshift_clusters",
		Description: "Cluster base fee",
		Query:       appuioManagedOpenShiftClusterQuery,
		Unit:        "Clusters",
	},
	{
		Name:        "appuio_managed_kubernetes_vcpu",
		Description: "vCPU aggregated by cluster, node (app, storage), and service level",
		Query:       appuioManagedKubernetesvCPUQuery,
		Unit:        "vCPU",
	},
	{
		Name:        "appcat_services",
		Description: "AppCat service instances query",
		Query:       appcatQuery,
		Unit:        "instances",
	},
}

DefaultQueries consists of default starter queries.

View Source
var Migrations = func() []interface{} {
	m, err := loadMigrations()
	if err != nil {
		panic(fmt.Errorf("failed to load migrations: %w", err))
	}
	return m
}()

Migrations returns all registered migrations.

Functions

func GetNamed

func GetNamed(p NamedPreparer, dest interface{}, query string, arg interface{}) error

GetNamed is like sqlx.Get but for named statements.

func GetNamedContext

func GetNamedContext(ctx context.Context, p NamedPreparerContext, dest interface{}, query string, arg interface{}) error

GetNamedContext is like sqlx.GetContext but for named statements.

func InfiniteRange

func InfiniteRange() pgtype.Tstzrange

InfiniteRange returns an infinite PostgreSQL timerange [-Inf,Inf).

func Migrate

func Migrate(db *sql.DB) error

Migrate migrates the database to the newest migration.

func MustTimestamp

func MustTimestamp(from interface{}) pgtype.Timestamptz

MustTimestamp creates a Postgres timestamp from the given value. Valid values are nil, pgtype.Infinity/pgtype.NegativeInfinity, and a time.Time object. Panics if given an unsupported type.

func NewDBx

func NewDBx(db *sql.DB) *sqlx.DB

NewDBx wraps the given database in an sqlx.DB.

func Open

func Open(dataSourceName string) (*sql.DB, error)

Open opens a postgres database with the pgx driver.

func Openx

func Openx(dataSourceName string) (*sqlx.DB, error)

Openx opens a postgres database with the pgx driver and wraps it in an sqlx.DB.

func Pending

func Pending(db *sql.DB) ([]*migrator.Migration, error)

Pending returns all pending migrations.

func RunInTransaction

func RunInTransaction(ctx context.Context, db *sqlx.DB, cb func(tx *sqlx.Tx) error) error

RunInTransaction runs the given cb func in a transaction. If the func returns an error, the transaction is rolled back, otherwise committed.

func Seed

func Seed(db *sql.DB) error

Seed seeds the database with "starter" data. Is idempotent and thus can be executed multiple times in one database.

func SeedQueries added in v0.5.0

func SeedQueries(db *sql.DB, queries []Query) error

func SelectNamed

func SelectNamed(p sqlx.Ext, dest interface{}, query string, arg interface{}) error

SelectNamed is like sqlx.Select but for named statements.

func SelectNamedContext

func SelectNamedContext(ctx context.Context, p sqlx.ExtContext, dest interface{}, query string, arg interface{}) error

SelectNamedContext is like sqlx.SelectContext but for named statements.

func Timerange

func Timerange(lower, upper pgtype.Timestamptz) pgtype.Tstzrange

Timerange creates a Postgres timerange from two Postgres timestamps with [lower,upper) bounds.

func Timestamp

func Timestamp(from interface{}) (pgtype.Timestamptz, error)

Timestamp creates a Postgres timestamp from the given value. Valid values are nil, pgtype.Infinity/pgtype.NegativeInfinity, and a time.Time object.

Types

type Category

type Category struct {
	Id string

	// Source consists of the cluster id and namespace in the form of "zone:namespace".
	Source string
	Target sql.NullString
}

func (Category) ForeignKeyName added in v0.10.0

func (c Category) ForeignKeyName() string

func (Category) TableName added in v0.10.0

func (c Category) TableName() string

type DateTime

type DateTime struct {
	Id string

	Timestamp time.Time

	Year  int
	Month int
	Day   int
	Hour  int
}

func BuildDateTime

func BuildDateTime(ts time.Time) DateTime

BuildDateTime builds a DateTime object from the given timestamp.

func (DateTime) ForeignKeyName added in v0.10.0

func (d DateTime) ForeignKeyName() string

func (DateTime) TableName added in v0.10.0

func (d DateTime) TableName() string

type Discount

type Discount struct {
	Id string

	// Source is a string consisting of "query:zone:tenant:namespace:class" and can contain wildcards.
	// See package `sourcekey` for more information.
	Source   string
	Discount float64

	During pgtype.Tstzrange
}

func CreateDiscount added in v0.5.0

func CreateDiscount(p NamedPreparer, in Discount) (Discount, error)

CreateDiscount creates the given discount

func (Discount) ForeignKeyName added in v0.10.0

func (d Discount) ForeignKeyName() string

func (Discount) TableName added in v0.10.0

func (d Discount) TableName() string

type Fact

type Fact struct {
	Id string

	DateTimeId string `db:"date_time_id"`
	QueryId    string `db:"query_id"`
	TenantId   string `db:"tenant_id"`
	CategoryId string `db:"category_id"`
	ProductId  string `db:"product_id"`
	DiscountId string `db:"discount_id"`

	Quantity float64
}

func (Fact) ForeignKeyName added in v0.10.0

func (f Fact) ForeignKeyName() string

func (Fact) TableName added in v0.10.0

func (f Fact) TableName() string

type Model added in v0.10.0

type Model interface {
	TableName() string
	ForeignKeyName() string
}

type NamedPreparer

type NamedPreparer interface {
	PrepareNamed(query string) (*sqlx.NamedStmt, error)
}

NamedPreparer is an interface used by GetNamed.

type NamedPreparerContext

type NamedPreparerContext interface {
	PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error)
}

NamedPreparerContext is an interface used by GetNamedContext.

type Product

type Product struct {
	Id string

	// Source is a string consisting of "query:zone:tenant:namespace:class" and can contain wildcards.
	// See package `sourcekey` for more information.
	Source string
	Target sql.NullString
	Amount float64
	Unit   string

	During pgtype.Tstzrange
}

func CreateProduct added in v0.5.0

func CreateProduct(p NamedPreparer, in Product) (Product, error)

CreateProduct creates the given product

func (Product) ForeignKeyName added in v0.10.0

func (p Product) ForeignKeyName() string

func (Product) TableName added in v0.10.0

func (p Product) TableName() string

type Query

type Query struct {
	Id       string
	ParentID sql.NullString `db:"parent_id"`

	Name        string
	Description string
	Query       string
	Unit        string

	During pgtype.Tstzrange
	// contains filtered or unexported fields
}

func CreateQuery added in v0.5.0

func CreateQuery(p NamedPreparer, in Query) (Query, error)

CreateQuery creates the given query

func (Query) ForeignKeyName added in v0.10.0

func (q Query) ForeignKeyName() string

func (Query) TableName added in v0.10.0

func (q Query) TableName() string

type Tenant

type Tenant struct {
	Id string

	// Source is the tenant string read from the 'appuio.io/organization' label.
	Source string
	Target sql.NullString

	During pgtype.Tstzrange
}

func CreateTenant added in v0.11.0

func CreateTenant(p NamedPreparer, in Tenant) (Tenant, error)

CreateTenant creates the given tenant

func (Tenant) ForeignKeyName added in v0.10.0

func (t Tenant) ForeignKeyName() string

func (Tenant) TableName added in v0.10.0

func (t Tenant) TableName() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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