From 5153673884085eec701450ecea8a29f60ddace63 Mon Sep 17 00:00:00 2001 From: Kyle Brandt Date: Thu, 9 Apr 2020 15:47:30 -0400 Subject: [PATCH] BackendPlugins: update to sdk v0.42.0 misc cleanup --- go.mod | 2 +- go.sum | 4 ++ pkg/plugins/transform_plugin.go | 5 +- pkg/services/alerting/conditions/query.go | 3 +- .../alerting/conditions/query_test.go | 2 +- pkg/tsdb/frame_util.go | 13 ---- .../grafana-plugin-sdk-go/data/arrow.go | 25 ++++---- .../data/conversion_input.go | 10 ++-- .../grafana-plugin-sdk-go/data/field.go | 59 ++++++++++++++----- .../grafana-plugin-sdk-go/data/frame.go | 34 ++++++++--- vendor/modules.txt | 2 +- 11 files changed, 99 insertions(+), 60 deletions(-) diff --git a/go.mod b/go.mod index b558be056f6..cc87f65d21f 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/gorilla/websocket v1.4.1 github.com/gosimple/slug v1.4.2 github.com/grafana/grafana-plugin-model v0.0.0-20190930120109-1fc953a61fb4 - github.com/grafana/grafana-plugin-sdk-go v0.39.0 + github.com/grafana/grafana-plugin-sdk-go v0.42.0 github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd github.com/hashicorp/go-plugin v1.2.2 github.com/hashicorp/go-version v1.1.0 diff --git a/go.sum b/go.sum index 94dcf60d0cd..38f6be924bc 100644 --- a/go.sum +++ b/go.sum @@ -135,6 +135,10 @@ github.com/grafana/grafana-plugin-model v0.0.0-20190930120109-1fc953a61fb4 h1:SP github.com/grafana/grafana-plugin-model v0.0.0-20190930120109-1fc953a61fb4/go.mod h1:nc0XxBzjeGcrMltCDw269LoWF9S8ibhgxolCdA1R8To= github.com/grafana/grafana-plugin-sdk-go v0.39.0 h1:tPP83HeY9gN4q8O3tYka1vd82OQ/3CFdwx4QeEhJ0Qc= github.com/grafana/grafana-plugin-sdk-go v0.39.0/go.mod h1:xRhfTHl+Dkqf2Py6Lr4pcHBC5pm8/N+IwPJ0R/iAHMM= +github.com/grafana/grafana-plugin-sdk-go v0.40.1-0.20200409163705-fd66aee09a52 h1:WEfl8G9uHk31r3pnAmsK+NRcHGpXnXauWmbhic3KuVU= +github.com/grafana/grafana-plugin-sdk-go v0.40.1-0.20200409163705-fd66aee09a52/go.mod h1:xRhfTHl+Dkqf2Py6Lr4pcHBC5pm8/N+IwPJ0R/iAHMM= +github.com/grafana/grafana-plugin-sdk-go v0.42.0 h1:8oiAQa/uABBFT70GDAv3BnqHfdMOxy/P8SzYVURJH6Y= +github.com/grafana/grafana-plugin-sdk-go v0.42.0/go.mod h1:xRhfTHl+Dkqf2Py6Lr4pcHBC5pm8/N+IwPJ0R/iAHMM= github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd h1:rNuUHR+CvK1IS89MMtcF0EpcVMZtjKfPRp4MEmt/aTs= github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI= github.com/hashicorp/go-plugin v1.0.1/go.mod h1:++UyYGoz3o5w9ZzAdZxtQKrWWP+iqPBn3cQptSMzBuY= diff --git a/pkg/plugins/transform_plugin.go b/pkg/plugins/transform_plugin.go index e040f1dc7e6..bbbd4d008ef 100644 --- a/pkg/plugins/transform_plugin.go +++ b/pkg/plugins/transform_plugin.go @@ -7,7 +7,6 @@ import ( "path" "strconv" - "github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/simplejson" @@ -198,7 +197,7 @@ func (s *transformCallback) QueryData(ctx context.Context, req *pluginv2.QueryDa if err != nil { return nil, err } - encFrame, err := data.MarshalArrow(frame) + encFrame, err := frame.MarshalArrow() if err != nil { return nil, err } @@ -207,7 +206,7 @@ func (s *transformCallback) QueryData(ctx context.Context, req *pluginv2.QueryDa if res.Meta != nil { b, err := res.Meta.MarshalJSON() if err != nil { - s.logger.Error("failed to marhsal json metadata", err) + s.logger.Error("failed to marshal json metadata", err) } pRes.JsonMeta = b } diff --git a/pkg/services/alerting/conditions/query.go b/pkg/services/alerting/conditions/query.go index e95142e4eef..55679caac15 100644 --- a/pkg/services/alerting/conditions/query.go +++ b/pkg/services/alerting/conditions/query.go @@ -7,6 +7,7 @@ import ( gocontext "context" + "github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/null" "github.com/grafana/grafana/pkg/components/simplejson" @@ -173,7 +174,7 @@ func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange * useDataframes := v.Dataframes != nil && (v.Series == nil || len(v.Series) == 0) if useDataframes { // convert the dataframes to tsdb.TimeSeries - frames, err := tsdb.FramesFromBytes(v.Dataframes) + frames, err := data.UnmarshalArrowFrames(v.Dataframes) if err != nil { return nil, errutil.Wrap("tsdb.HandleRequest() failed to unmarshal arrow dataframes from bytes", err) } diff --git a/pkg/services/alerting/conditions/query_test.go b/pkg/services/alerting/conditions/query_test.go index 3e655d53286..727552192dc 100644 --- a/pkg/services/alerting/conditions/query_test.go +++ b/pkg/services/alerting/conditions/query_test.go @@ -198,7 +198,7 @@ func (ctx *queryConditionTestContext) exec() (*alerting.ConditionResult, error) } if ctx.frame != nil { - bFrame, err := data.MarshalArrow(ctx.frame) + bFrame, err := ctx.frame.MarshalArrow() if err != nil { return nil, err } diff --git a/pkg/tsdb/frame_util.go b/pkg/tsdb/frame_util.go index deaa6cbfbb5..6add81f43fc 100644 --- a/pkg/tsdb/frame_util.go +++ b/pkg/tsdb/frame_util.go @@ -94,16 +94,3 @@ func FrameToSeriesSlice(frame *data.Frame) (TimeSeriesSlice, error) { return seriesSlice, nil } - -// FramesFromBytes returns a data.Frame slice from marshalled arrow dataframes. -func FramesFromBytes(bFrames [][]byte) ([]*data.Frame, error) { - frames := make([]*data.Frame, len(bFrames)) - for i, bFrame := range bFrames { - var err error - frames[i], err = data.UnmarshalArrow(bFrame) - if err != nil { - return nil, err - } - } - return frames, nil -} diff --git a/vendor/github.com/grafana/grafana-plugin-sdk-go/data/arrow.go b/vendor/github.com/grafana/grafana-plugin-sdk-go/data/arrow.go index 2a8bf726c99..352ade5fc9e 100644 --- a/vendor/github.com/grafana/grafana-plugin-sdk-go/data/arrow.go +++ b/vendor/github.com/grafana/grafana-plugin-sdk-go/data/arrow.go @@ -17,7 +17,7 @@ import ( // MarshalArrow converts the Frame to an arrow table and returns a byte // representation of that table. -func MarshalArrow(f *Frame) ([]byte, error) { +func (f *Frame) MarshalArrow() ([]byte, error) { arrowFields, err := buildArrowFields(f) if err != nil { return nil, err @@ -640,8 +640,8 @@ func populateFrameFields(fR *ipc.FileReader, nullable []bool, frame *Frame) erro return nil } -// UnmarshalArrow converts a byte representation of an arrow table to a Frame -func UnmarshalArrow(b []byte) (*Frame, error) { +// UnmarshalArrowFrame converts a byte representation of an arrow table to a Frame. +func UnmarshalArrowFrame(b []byte) (*Frame, error) { fB := filebuffer.New(b) fR, err := ipc.NewFileReader(fB) if err != nil { @@ -693,12 +693,15 @@ func toJSONString(val interface{}) (string, error) { return string(b), nil } -// BytesSliceToFrames decodes a slice of encoded Arrow frames to a slice of *Frame. -func BytesSliceToFrames(bFrames [][]byte) ([]*Frame, error) { - frames := make([]*Frame, len(bFrames)) +// UnmarshalArrowFrames decodes a slice of Arrow encoded frames to Frames ([]*Frame) by calling +// the UnmarshalArrow function on each encoded frame. +// If an error occurs Frames will be nil. +// See Frames.UnMarshalArrow() for the inverse operation. +func UnmarshalArrowFrames(bFrames [][]byte) (Frames, error) { + frames := make(Frames, len(bFrames)) var err error for i, encodedFrame := range bFrames { - frames[i], err = UnmarshalArrow(encodedFrame) + frames[i], err = UnmarshalArrowFrame(encodedFrame) if err != nil { return nil, err } @@ -706,12 +709,14 @@ func BytesSliceToFrames(bFrames [][]byte) ([]*Frame, error) { return frames, nil } -// FramesToBytesSlice encodes a slice of Frames into a slice of []byte. -func FramesToBytesSlice(frames []*Frame) ([][]byte, error) { +// MarshalArrow encodes Frames into a slice of []byte using *Frame's MarshalArrow method on each Frame. +// If an error occurs [][]byte will be nil. +// See UnmarshalArrowFrames for the inverse operation. +func (frames Frames) MarshalArrow() ([][]byte, error) { bs := make([][]byte, len(frames)) var err error for i, frame := range frames { - bs[i], err = MarshalArrow(frame) + bs[i], err = frame.MarshalArrow() if err != nil { return nil, err } diff --git a/vendor/github.com/grafana/grafana-plugin-sdk-go/data/conversion_input.go b/vendor/github.com/grafana/grafana-plugin-sdk-go/data/conversion_input.go index e46727fbacf..f5499a70910 100644 --- a/vendor/github.com/grafana/grafana-plugin-sdk-go/data/conversion_input.go +++ b/vendor/github.com/grafana/grafana-plugin-sdk-go/data/conversion_input.go @@ -46,16 +46,16 @@ func NewFrameInputConverter(fieldConvs []FieldConverter, rowLen int) (*FrameInpu // Converter is not nil, then the Converter function is called before setting the value (otherwise Frame.Set is called directly). // If an error is returned from the Converter function this function returns that error. // Like Frame.Set and Field.Set, it will panic if fieldIdx or rowIdx are out of range. -func (fcb *FrameInputConverter) Set(fieldIdx, rowIdx int, val interface{}) error { - if fcb.fieldConverters[fieldIdx].Converter == nil { - fcb.Frame.Set(fieldIdx, rowIdx, val) +func (fic *FrameInputConverter) Set(fieldIdx, rowIdx int, val interface{}) error { + if fic.fieldConverters[fieldIdx].Converter == nil { + fic.Frame.Set(fieldIdx, rowIdx, val) return nil } - convertedVal, err := fcb.fieldConverters[fieldIdx].Converter(val) + convertedVal, err := fic.fieldConverters[fieldIdx].Converter(val) if err != nil { return err } - fcb.Frame.Set(fieldIdx, rowIdx, convertedVal) + fic.Frame.Set(fieldIdx, rowIdx, convertedVal) return nil } diff --git a/vendor/github.com/grafana/grafana-plugin-sdk-go/data/field.go b/vendor/github.com/grafana/grafana-plugin-sdk-go/data/field.go index e0062e8b446..2b7f299d6db 100644 --- a/vendor/github.com/grafana/grafana-plugin-sdk-go/data/field.go +++ b/vendor/github.com/grafana/grafana-plugin-sdk-go/data/field.go @@ -8,23 +8,41 @@ import ( ) // Field represents a typed column of data within a Frame. -// The data in the Field is a not exported, so methods on the Field are used to to manipulate its data. +// +// A Field is essentially a slice of various types with extra properties and methods. +// See NewField() for supported types. +// +// The slice data in the Field is a not exported, so methods on the Field are used to to manipulate its data. type Field struct { - Name string - Config *FieldConfig - vector vector + // Name is default identifer of the field. The name does not have to be unique, but the combination + // of name and Labels should be unique for proper behavior in all situations. + Name string + + // Labels is an optional set of key=value pairs that in addition to the name, should uniquely + // identify a Field within a Frame. Labels Labels + + // Config is optional display configuration information for Grafana + Config *FieldConfig + + // vector is the unexported values. it is unexported so we can change the underlying structure without + // major breaking changes. + vector vector } // Fields is a slice of Field pointers. type Fields []*Field -// NewField returns a instance of *Field. +// NewField returns a instance of *Field. Supported types for values are: // -// Supported types for values are: []int8, []*int8, []int16, []*int16, []int32, []*int32, []int64, []*int64, -// []uint8, []*uint8, []uint16, []*uint16, []uint32, []*uint32, []uint64, []*uint64, -// []float32, []*float32, []float64, []*float64, -// []string, []*string, []bool, []*bool, []time.Time, and []*time.Time. +// Integers: +// []int8, []*int8, []int16, []*int16, []int32, []*int32, []int64, []*int64 +// Unsigned Integers: +// []uint8, []*uint8, []uint16, []*uint16, []uint32, []*uint32, []uint64, []*uint64 +// Floats: +// []float32, []*float32, []float64, []*float64 +// String, Bool, and Time: +// []string, []*string, []bool, []*bool, []time.Time, and []*time.Time. // // If an unsupported values type is passed, NewField will panic. func NewField(name string, labels Labels, values interface{}) *Field { @@ -172,17 +190,21 @@ func NewField(name string, labels Labels, values interface{}) *Field { } // Set sets the Field's value at index idx to val. -// It will panic if idx is out of range. +// It will panic if idx is out of range or if +// the underlying type of val does not match the element type of the Field. func (f *Field) Set(idx int, val interface{}) { f.vector.Set(idx, val) } -// Append appends element i to the Field. -func (f *Field) Append(i interface{}) { - f.vector.Append(i) +// Append appends element e to the Field. +// it will panic if the underlying type of e does not match the element type of the Field. +func (f *Field) Append(e interface{}) { + f.vector.Append(e) } // Extend extends the Field length by i. +// Consider using Frame.Extend() when possible since all Fields within +// a Frame need to be of the same length before marshalling and transmission. func (f *Field) Extend(i int) { f.vector.Extend(i) } @@ -198,7 +220,7 @@ func (f *Field) Len() int { return f.vector.Len() } -// Type returns the underlying primitive type of the Field. +// Type returns the FieldType of the Field, which indicates what type of slice it is. func (f *Field) Type() FieldType { return f.vector.Type() } @@ -228,12 +250,17 @@ func (f *Field) Nullable() bool { return f.Type().Nullable() } -// FloatAt returns a float64 at the specified index idx. +// FloatAt returns a float64 at the specified index idx for all supported Field types. // It will panic if idx is out of range. -// If the Field type is numeric and the value at idx is nil, NaN is returned. Precision may be lost on large numbers. +// +// If the Field type is numeric and the value at idx is nil, NaN is returned. +// Precision may be lost on large numbers. +// // If the Field type is a bool then 0 is return if false or nil, and 1 if true. +// // If the Field type is time.Time, then the millisecond epoch representation of the time // is returned, or NaN is the value is nil. +// // If the Field type is a string, then strconv.ParseFloat is called on it and will return // an error if ParseFloat errors. If the value is nil, NaN is returned. func (f *Field) FloatAt(idx int) (float64, error) { diff --git a/vendor/github.com/grafana/grafana-plugin-sdk-go/data/frame.go b/vendor/github.com/grafana/grafana-plugin-sdk-go/data/frame.go index 356ed91adcf..12d34f82f90 100644 --- a/vendor/github.com/grafana/grafana-plugin-sdk-go/data/frame.go +++ b/vendor/github.com/grafana/grafana-plugin-sdk-go/data/frame.go @@ -20,18 +20,33 @@ import ( "github.com/olekukonko/tablewriter" ) -// Frame represents a columnar storage with optional labels. -// Each Field in Fields represents a column, all Fields -// must be of the same the length. +// Frame is a columnar data structure where each column is a Field. +// +// Each Field is well typed by its FieldType and supports optional Labels. +// +// A Frame is a general data container for Grafana. A Frame can be table data +// or time series data depending on its content and field types. type Frame struct { - Name string + // Name is used in some Grafana visualizations. + Name string + + // Fields are the columns of a frame. + // All Fields must be of the same the length when marshalling the Frame for transmission. Fields []*Field - RefID string - Meta *FrameMeta - Warnings []Warning + // RefID is a property that can be set to match a Frame to its orginating query + RefID string + + // Meta is metadata about the Frame, and includes space for custom metadata. + Meta *FrameMeta + + Warnings []Warning // TODO: Remove, will be replaced with FrameMeta.Notices. } +// Frames is a slice of Frame pointers. +// It is the main data container within a backend.DataResponse. +type Frames []*Frame + // AppendRow adds a new row to the Frame by appending to each element of vals to // the corresponding Field in the data. // The Frame's Fields must be initalized or AppendRow will panic. @@ -188,7 +203,8 @@ func (f *Frame) CopyAt(fieldIdx int, rowIdx int) interface{} { } // Set set the val to the specified fieldIdx and rowIdx. -// It will panic if either the fieldIdx or rowIdx are out of range. +// It will panic if either the fieldIdx or rowIdx are out of range or +// if the underlying type of val does not match the element type of the Field. func (f *Frame) Set(fieldIdx int, rowIdx int, val interface{}) { f.Fields[fieldIdx].vector.Set(rowIdx, val) } @@ -255,7 +271,7 @@ func (f *Frame) SetFieldNames(names ...string) error { return nil } -// FrameTestCompareOptions returns go-cmp testing options to allow testing of Frame equivelnce. +// FrameTestCompareOptions returns go-cmp testing options to allow testing of Frame equivalence. // Since the data within a Frame's Fields is not exported, this function allows the unexported // values to be tested. // The intent is to only use this for testing. diff --git a/vendor/modules.txt b/vendor/modules.txt index da423a42351..3130dad48d3 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -175,7 +175,7 @@ github.com/gosimple/slug ## explicit github.com/grafana/grafana-plugin-model/go/datasource github.com/grafana/grafana-plugin-model/go/renderer -# github.com/grafana/grafana-plugin-sdk-go v0.39.0 +# github.com/grafana/grafana-plugin-sdk-go v0.42.0 ## explicit github.com/grafana/grafana-plugin-sdk-go/backend/grpcplugin github.com/grafana/grafana-plugin-sdk-go/data