blob: 90d60193b942dbf9bc2cc756d101446348acf456 [file] [log] [blame]
package types
import (
"encoding/gob"
"fmt"
"go.skia.org/infra/go/tiling"
)
func init() {
// Register *GoldenTrace in gob so that it can be used as a
// concrete type for Trace when writing and reading Tiles in gobs.
gob.Register(&GoldenTrace{})
}
const (
// Primary key field that uniquely identifies a key.
PRIMARY_KEY_FIELD = "name"
// Field that contains the corpus identifier.
CORPUS_FIELD = "source_type"
// MAXIMUM_NAME_LENGTH is the maximum length in bytes a test name can be.
MAXIMUM_NAME_LENGTH = 256
MasterBranch = int64(-1)
// There was a time when 0 meant MasterBranch in some places and -1 was used in others.
// It should all be -1 now, but we have this in place to work around some old code.
// TODO(kjlubick): remove this on/after Aug 2019 once the 0 has faded away from the
// latest and greatest.
LegacyMasterBranch = int64(0)
)
func IsMasterBranch(b int64) bool {
return b == MasterBranch || b == LegacyMasterBranch
}
// Label for classifying digests.
type Label int
const (
// Classifications for observed digests.
UNTRIAGED Label = iota // == 0
POSITIVE
NEGATIVE
)
// String representation for Labels. The order must match order above.
var labelStringRepresentation = []string{
"untriaged",
"positive",
"negative",
}
func (l Label) String() string {
return labelStringRepresentation[l]
}
var labels = map[string]Label{
"untriaged": UNTRIAGED,
"positive": POSITIVE,
"negative": NEGATIVE,
}
func LabelFromString(s string) Label {
if l, ok := labels[s]; ok {
return l
}
return UNTRIAGED
}
// ValidLabel returns true if the given label is a valid label string.
func ValidLabel(s string) bool {
_, ok := labels[s]
return ok
}
// Strings are used a lot, so these type "aliases" can help document
// which are meant where. See also tiling.TraceId
// Of note, Digest exclusively means a unique image, identified by
// the MD5 hash of its pixels.
type Digest string
type TestName string
// Stores the digests and their associated labels.
// Note: The name of the test is assumed to be handled by the client of this
// type. Most likely in the keys of a map.
type TestClassification map[Digest]Label
func (tc *TestClassification) DeepCopy() TestClassification {
result := make(map[Digest]Label, len(*tc))
for k, v := range *tc {
result[k] = v
}
return result
}
// The IgnoreState enum gives a human-readable way to determine if the
// tile or whatever is dealing with the full amount of information
// (IncludeIgnoredTraces) or the information with the ignore rules applied
// (ExcludeIgnoredTraces).
type IgnoreState int
const (
ExcludeIgnoredTraces IgnoreState = iota
IncludeIgnoredTraces // i.e. all digests
)
var IgnoreStates = []IgnoreState{ExcludeIgnoredTraces, IncludeIgnoredTraces}
const (
// No digest available.
MISSING_DIGEST = Digest("")
)
// GoldenTrace represents all the Digests of a single test across a series
// of Commits. GoldenTrace implements the Trace interface.
type GoldenTrace struct {
// The JSON keys are named this way to maintain backwards compatibility
// with JSON already written to disk.
Keys map[string]string `json:"Params_"`
Digests []Digest `json:"Values"`
}
// Params implements the tiling.Trace interface.
func (g *GoldenTrace) Params() map[string]string {
return g.Keys
}
// TestName is a helper for extracting just the test name for this
// trace, of which there should always be exactly one.
func (g *GoldenTrace) TestName() TestName {
return TestName(g.Keys[PRIMARY_KEY_FIELD])
}
// Corpus is a helper for extracting just the corpus key for this
// trace, of which there should always be exactly one.
func (g *GoldenTrace) Corpus() string {
return g.Keys[CORPUS_FIELD]
}
// Len implements the tiling.Trace interface.
func (g *GoldenTrace) Len() int {
return len(g.Digests)
}
// IsMissing implements the tiling.Trace interface.
func (g *GoldenTrace) IsMissing(i int) bool {
return g.Digests[i] == MISSING_DIGEST
}
// DeepCopy implements the tiling.Trace interface.
func (g *GoldenTrace) DeepCopy() tiling.Trace {
n := len(g.Digests)
cp := &GoldenTrace{
Digests: make([]Digest, n),
Keys: make(map[string]string),
}
copy(cp.Digests, g.Digests)
for k, v := range g.Keys {
cp.Keys[k] = v
}
return cp
}
// Merge implements the tiling.Trace interface.
func (g *GoldenTrace) Merge(next tiling.Trace) tiling.Trace {
nextGold := next.(*GoldenTrace)
n := len(g.Digests) + len(nextGold.Digests)
n1 := len(g.Digests)
merged := NewGoldenTraceN(n)
merged.Keys = g.Keys
for k, v := range nextGold.Keys {
merged.Keys[k] = v
}
copy(merged.Digests, g.Digests)
for i, v := range nextGold.Digests {
merged.Digests[n1+i] = v
}
return merged
}
// Grow implements the tiling.Trace interface.
func (g *GoldenTrace) Grow(n int, fill tiling.FillType) {
if n < len(g.Digests) {
panic(fmt.Sprintf("Grow must take a value (%d) larger than the current Trace size: %d", n, len(g.Digests)))
}
delta := n - len(g.Digests)
newDigests := make([]Digest, n)
if fill == tiling.FILL_AFTER {
copy(newDigests, g.Digests)
for i := 0; i < delta; i++ {
newDigests[i+len(g.Digests)] = MISSING_DIGEST
}
} else {
for i := 0; i < delta; i++ {
newDigests[i] = MISSING_DIGEST
}
copy(newDigests[delta:], g.Digests)
}
g.Digests = newDigests
}
// Trim implements the tiling.Trace interface.
func (g *GoldenTrace) Trim(begin, end int) error {
if end < begin || end > g.Len() || begin < 0 {
return fmt.Errorf("Invalid Trim range [%d, %d) of [0, %d]", begin, end, g.Len())
}
n := end - begin
newDigests := make([]Digest, n)
for i := 0; i < n; i++ {
newDigests[i] = g.Digests[i+begin]
}
g.Digests = newDigests
return nil
}
// SetAt implements the tiling.Trace interface.
func (g *GoldenTrace) SetAt(index int, value []byte) error {
if index < 0 || index > len(g.Digests) {
return fmt.Errorf("Invalid index: %d", index)
}
g.Digests[index] = Digest(value)
return nil
}
// LastDigest returns the last digest in the trace (HEAD) or the empty string otherwise.
func (g *GoldenTrace) LastDigest() Digest {
if idx := g.LastIndex(); idx >= 0 {
return g.Digests[idx]
}
return Digest("")
}
// LastIndex returns the index of last non-empty value in this trace and -1 if
// if the entire trace is empty.
func (g *GoldenTrace) LastIndex() int {
for i := len(g.Digests) - 1; i >= 0; i-- {
if g.Digests[i] != MISSING_DIGEST {
return i
}
}
return -1
}
// String prints a human friendly version of this trace.
func (g *GoldenTrace) String() string {
return fmt.Sprintf("Keys: %#v, Digests: %q", g.Keys, g.Digests)
}
// NewGoldenTrace allocates a new Trace set up for the given number of samples.
//
// The Trace Digests are pre-filled in with the missing data sentinel since not
// all tests will be run on all commits.
func NewGoldenTrace() *GoldenTrace {
return NewGoldenTraceN(tiling.TILE_SIZE)
}
// NewGoldenTraceN allocates a new Trace set up for the given number of samples.
//
// The Trace Digests are pre-filled in with the missing data sentinel since not
// all tests will be run on all commits.
func NewGoldenTraceN(n int) *GoldenTrace {
g := &GoldenTrace{
Digests: make([]Digest, n),
Keys: make(map[string]string),
}
for i := range g.Digests {
g.Digests[i] = MISSING_DIGEST
}
return g
}
func GoldenTraceBuilder(n int) tiling.Trace {
return NewGoldenTraceN(n)
}