Documentation
¶
Overview ¶
Package selection runs a selection over the export dataset: which rows (the grain), which of them (facets and text), what each row says (columns, following relations by path), in what order and how many. It is the second stage of the export pipeline (dataset, selection, format), and the same Selection value drives the web page, the API and the CLI.
Index ¶
- Constants
- Variables
- func Cell(v any) string
- func Cells(r []any) []string
- func Facets(ds *dataset.Dataset, grain string) (map[string][]FacetValue, error)
- func Parts(grain string) []string
- func Report(exp *encode.ClientExport) string
- type Aggregate
- type Column
- type Comparison
- type FacetValue
- type HostSource
- type Result
- type Selection
- type Sort
- type Stage
- type Transform
- type Where
Constants ¶
const ( GrainProfile = "profile" GrainKey = "key" GrainHost = "host" GrainToken = "token" // The records: history, the events log, snapshots and deployments. GrainOp = "op" GrainEvent = "event" GrainSnapshot = "snapshot" GrainDeployment = "deployment" )
The grains: what one row of the result is.
const ( // TransformTemplate writes As from a template naming fields in braces: // "{profile}/{alias}". "{{" and "}}" are literal braces. TransformTemplate = "template" // TransformMap replaces Field's value where Values has it: a status // needs-redeploy becomes pending. Other values stay unless Default is set. TransformMap = "map" // TransformFormat rewrites Field: upper, lower, trim, date (Layout, Go's // reference layout, 2006-01-02 by default), relative ("3 days ago") or // thousands (1,234). TransformFormat = "format" // TransformSplit makes Field a list, split on Sep (space by default); // with Rows, a row per item instead. TransformSplit = "split" // TransformJoin makes a list Field text, joined by Sep (", " by default). TransformJoin = "join" // TransformDedupe keeps the first row of each distinct By (every field // when By is empty). TransformDedupe = "dedupe" // TransformGroup makes a row per distinct By, with Aggregates; with // Totals, a last row over every group. TransformGroup = "group" // TransformPivot makes a row per distinct By and a column per distinct // value of Field, each cell the first of Aggregates (count by default) // over the rows with both; with Totals, a total column and a last row. TransformPivot = "pivot" )
The transforms: what a selection can do to its rows after the filters and before the columns. Each is declarative, with no expression evaluation: a template names fields, nothing more.
const PivotEmpty = "(none)"
PivotEmpty is the column a pivot gives rows whose Field is empty.
const TotalLabel = "total"
TotalLabel is what a totals row says in its first group column.
const Version = 1
Version is the Selection shape this package reads. A saved selection of another version is refused, never half-read.
Variables ¶
var Aggregates = []string{"count", "sum", "min", "max", "list", "first"}
The aggregates a group computes.
var Defaults = map[string][]Column{
GrainHost: {{Field: "profile"}, {Field: "alias"}, {Field: "hostname"}, {Field: "provider"}, {Field: "key"},
{Field: "status"}, {Field: "tags"}},
GrainKey: {{Field: "ref", As: "key"}, {Field: "profile"}, {Field: "type"}, {Field: "fingerprint"},
{Field: "created"}, {Field: "expires_on", As: "expires"}, {Field: "hosts"}, {Field: "on_disk"}, {Field: "state"}},
GrainProfile: {{Field: "profile"}, {Field: "category"}, {Field: "key_scope"}, {Field: "key_store"},
{Field: "hosts"}, {Field: "keys"}, {Field: "key_comment"}, {Field: "key_names"}},
GrainToken: {{Field: "target", As: "tool"}, {Field: "host"}, {Field: "kind"}, {Field: "description"},
{Field: "username", As: "user"}, {Field: "secret_present", As: "secret_set"}, {Field: "file"}},
GrainOp: {{Field: "op_id"}, {Field: "started_at"}, {Field: "finished_at"}, {Field: "command"}, {Field: "args"},
{Field: "result"}, {Field: "source"}, {Field: "user"}, {Field: "machine"}, {Field: "snapshot"},
{Field: "subjects"}, {Field: "events"}},
GrainEvent: {{Field: "time"}, {Field: "seen"}, {Field: "kind"}, {Field: "area"}, {Field: "path"}, {Field: "size"},
{Field: "mode"}, {Field: "detail"}},
GrainSnapshot: {{Field: "name", As: "snapshot"}, {Field: "taken"}, {Field: "bytes"}, {Field: "holds_private_keys"}},
GrainDeployment: {{Field: "key_path"}, {Field: "fingerprint"}, {Field: "status"}, {Field: "target"},
{Field: "method"}, {Field: "verified"}, {Field: "date"}},
}
Defaults are each grain's columns when a selection names none: exactly the columns, headings and order of the list's CSV before the pipeline (hosts.csv, keys.csv, profiles.csv, tokens.csv), which the parity test holds them to.
ErrNothingShared is two results with no column in common.
var ErrUnknownGrain = errors.New("selection: unknown grain")
ErrUnknownGrain is a grain this package does not have.
var Formats = []string{"upper", "lower", "trim", "date", "relative", "thousands"}
The formats TransformFormat knows.
var Grains = []string{GrainProfile, GrainKey, GrainHost, GrainToken, GrainOp, GrainEvent, GrainSnapshot, GrainDeployment}
Grains lists them, in the order a page offers them.
var Presets = map[string]Selection{ "hosts": {Version: Version, Grain: GrainHost}, "keys": {Version: Version, Grain: GrainKey}, "profiles": {Version: Version, Grain: GrainProfile}, "tokens": {Version: Version, Grain: GrainToken}, "history": {Version: Version, Grain: GrainOp}, "events": {Version: Version, Grain: GrainEvent}, "snapshots": {Version: Version, Grain: GrainSnapshot}, "deployments": {Version: Version, Grain: GrainDeployment}, }
Presets are the built-in selections, one per list the pages download, by the list's name: each is its grain with the default columns, so /exports/run?preset=hosts is the hosts CSV.
var Transforms = []string{TransformTemplate, TransformMap, TransformFormat, TransformSplit, TransformJoin, TransformDedupe, TransformGroup, TransformPivot}
Transforms lists them, in the order a page offers them.
Functions ¶
func Facets ¶
Facets is every value each facet can take over a grain's rows, before any filter, in first-seen order: what a page offers to tick. Keys come with their profile; a facet no row has a value for is left out.
func Parts ¶
Parts is the dataset parts a grain needs, so a selection reads only what it uses: profiles, keys and hosts are always read.
func Report ¶
func Report(exp *encode.ClientExport) string
Report is what a host export says beside its file: the hosts left out and what is left to do in the client, one line each.
Types ¶
type Aggregate ¶
type Aggregate struct {
Op string `json:"op"`
Field string `json:"field,omitempty"`
As string `json:"as,omitempty"`
}
Aggregate is one computed field of a group: Op over Field, named As ("count" needs no field; As defaults to op_field, or count).
type Column ¶
type Column struct {
Field string `json:"field"`
As string `json:"as,omitempty"` // "" is the field's own name
}
Column is one column of the result: a field, and its heading.
type Comparison ¶
type Comparison struct {
// On are the shared columns, in the first result's order.
On []string `json:"on"`
OnlyA [][]string `json:"only_a"`
OnlyB [][]string `json:"only_b"`
Both int `json:"both"`
// KeptA and KeptB are each result's rows.
KeptA int `json:"kept_a"`
KeptB int `json:"kept_b"`
}
Comparison is two results side by side: the columns they share, which the rows are matched on, and the rows only one of them holds.
func Compare ¶
func Compare(a, b Result) (Comparison, error)
Compare matches a's and b's rows on the columns they share. A row is in both when its shared cells are; each side's duplicates count once.
type FacetValue ¶
type FacetValue struct {
Value string `json:"value"`
Profile string `json:"profile,omitempty"` // a key's profile, so a page can group keys
Rows int `json:"rows"`
}
FacetValue is one value a facet can take, and how many of the grain's rows have it.
type HostSource ¶
type HostSource interface {
encode.HostSource
ManifestOrder(aliases []string) ([]string, error)
}
HostSource is the manifest as a host format reads it, and the order its hosts come in.
type Result ¶
type Result struct {
Columns []string `json:"columns"`
Rows [][]any `json:"rows"`
Stages []Stage `json:"stages"`
// Fields is every field the grain offers, after any transforms, for a
// page to choose columns from.
Fields []string `json:"fields"`
// Defaults are the columns a selection with none of its own gets: the
// grain's, followed by the fields its transforms wrote.
Defaults []Column `json:"defaults"`
}
Result is a selection's rows, typed: strings, ints, bools and lists of strings. Cells formats them as text.
func HostExport ¶
func HostExport(ds *dataset.Dataset, s Selection, f encode.Format, src HostSource, o encode.Options) (*encode.ClientExport, Result, error)
HostExport writes the hosts s keeps in the host format f.
type Selection ¶
type Selection struct {
Version int `json:"version"`
Grain string `json:"grain"`
Where Where `json:"where"`
// Narrow makes a row's lists and counts cover only what passed the
// facets: a profile kept for a key lists that key alone.
Narrow bool `json:"narrow,omitempty"`
// Transforms run in order after the filters, before the columns: a
// column can name a field a transform wrote.
Transforms []Transform `json:"transforms,omitempty"`
Columns []Column `json:"columns,omitempty"` // empty: the grain's default columns
Sort []Sort `json:"sort,omitempty"`
Limit int `json:"limit,omitempty"` // 0: every row
}
Selection is what an export holds.
type Stage ¶
type Stage struct {
Name string `json:"name"` // all, profile, key, facets, text
Rows int `json:"rows"`
}
Stage is how many rows were left after one step, so a page can say which step left rows out.
type Transform ¶
type Transform struct {
Kind string `json:"kind"`
// Field is the field read (map, format, split, join, and an aggregate's).
Field string `json:"field,omitempty"`
// As is the field written; "" rewrites Field. A template needs it.
As string `json:"as,omitempty"`
Template string `json:"template,omitempty"`
Values map[string]string `json:"values,omitempty"`
Default *string `json:"default,omitempty"` // map: the value for any other
Format string `json:"format,omitempty"`
Layout string `json:"layout,omitempty"`
Sep string `json:"sep,omitempty"`
Rows bool `json:"rows,omitempty"`
By []string `json:"by,omitempty"`
// Aggregates are a group's computed fields.
Aggregates []Aggregate `json:"aggregates,omitempty"`
Totals bool `json:"totals,omitempty"`
}
Transform is one step. Which fields it reads depends on its kind.
type Where ¶
type Where struct {
Profile []string `json:"profile,omitempty"`
Key []string `json:"key,omitempty"` // a key's name or its profile/name ref
Env []string `json:"env,omitempty"`
Role []string `json:"role,omitempty"`
Tag []string `json:"tag,omitempty"`
Status []string `json:"status,omitempty"` // a host's status, a key's state
Target []string `json:"target,omitempty"` // a token's tool
Kind []string `json:"kind,omitempty"` // an event's kind (added, changed, ...), a token's kind
Area []string `json:"area,omitempty"` // an event's area (ssh, config, auth)
Source []string `json:"source,omitempty"` // an operation's source (cli, web, ...)
// Command keeps operations whose command is one of these, or starts with
// one and a space ("host" keeps "host add").
Command []string `json:"command,omitempty"`
// Since and Until bound a row's time (an operation's start, an event's
// time, a snapshot's, a deployment's date), inclusive; a prefix such as
// 2026-11 or 2026-11-06 works.
Since string `json:"since,omitempty"`
Until string `json:"until,omitempty"`
Include string `json:"include,omitempty"`
Exclude string `json:"exclude,omitempty"`
}
Where holds the facets. An empty list is no filter; a value in a list is matched exactly. Include and Exclude match any column, ignoring case.