Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Decide what belongs in the data(.Frame) package #7

Closed
kylebrandt opened this issue Sep 6, 2019 · 0 comments
Closed

Decide what belongs in the data(.Frame) package #7

kylebrandt opened this issue Sep 6, 2019 · 0 comments

Comments

@kylebrandt
Copy link
Contributor

Currently the data Package has the Frame structure, with its Fields and Vectors. (Currently there are only *time.Time and *float64 vectors).

// Frame is a columnar container for data.
type Frame struct {
	Name   string `json:"name"`
	Fields Fields `json:"fields"`
	Labels Labels `json:"labels"`
	RefID  string `json:"refId"`
	// GrafanaType
}

// Field holds information and Type and its values.
type Field struct {
	Name   string    `json:"name"`
	Type   FieldType `json:"type"`
	Vector Vector    `json:"values"` // KMB thinks this should be vectors
}

and

// Vector is an Interface for the Vectors within a Field of Frame.
type Vector interface {
	// SetValue sets val at idx of the Vector. It will panic if the vector is not initialized, idx is out of range, or
	// val is not of the expected type.
	SetValue(idx int, val interface{})
	// GetValue returns the value at idx of the Vector. It will panic if the vector is not initialized or if idx is out of range.
	GetValue(idx int) interface{}
	// Make initializes the vector and makes of length len.
	Make(len int)
	Len() int
	copy() Vector
}

However, the mathexp package has certains "types/shapes" of data.Frame, in particular Scalar, Number, and Series. For example:

// Series has *time.Time and *float64 fields.
type Series struct{ *data.Frame }

// Type returns the Value type and allows it to fulfill the Value interface.
func (s Series) Type() parse.ReturnType { return parse.TypeSeriesSet }

// Value returns the actual value allows it to fulfill the Value interface.
func (s Series) Value() interface{} { return &s }

// AsDataFrame returns the underlying *data.Frame.
func (s Series) AsDataFrame() *data.Frame { return s.Frame }

// GetPoint returns the time and value at the specified index.
func (s Series) GetPoint(pointIdx int) (*time.Time, *float64) {
	return s.GetTime(pointIdx), s.GetValue(pointIdx)
}

// SetPoint sets the time and value on the corresponding vectors at the specified index.
func (s Series) SetPoint(pointIdx int, t *time.Time, f *float64) {
	s.Fields[0].Vector.SetValue(pointIdx, t) // We switch from tsdb's package value,time to time,value
	s.Fields[1].Vector.SetValue(pointIdx, f)
}

// Len returns the length of the series.
func (s Series) Len() int {
	return s.Fields[0].Vector.Len()
}

// GetTime returns the time at the specified index.
func (s Series) GetTime(pointIdx int) *time.Time {
	return s.Fields[0].Vector.GetValue(pointIdx).(*time.Time)
}

I'm not sure these shapes of DataFrames should be part of the data package that is open source. We probably want them to be available, but that might make iterating on them more annoying.

@kylebrandt kylebrandt changed the title Decide What Belong in the data(.Frame) Package Decide what belongs in the data(.Frame) package Sep 6, 2019
@marefr marefr added this to TODO in GEL TODOs Sep 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
GEL TODOs
  
TODO
Development

No branches or pull requests

1 participant