Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numeric dataplane frame types #1067

Draft
wants to merge 3 commits into
base: dev-3.0.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fluffy-forks-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'grafana-infinity-datasource': minor
---

Added frame type to dataplane compliant numeric data frames. This will help us to handle the results correctly in alerts, recorded queries, SSE etc.
1 change: 1 addition & 0 deletions cspell.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"countif",
"csvframer",
"dataframe",
"dataplane",
"datapoints",
"dataproxy",
"datasource",
Expand Down
104 changes: 104 additions & 0 deletions pkg/dataplane/dataplane.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package dataplane

import "github.com/grafana/grafana-plugin-sdk-go/data"

type fieldTypeCount struct {
unknownFields int
numericFields int
boolFields int
stringFields int
timeFields int
jsonFields int
enumFields int
}

func getFieldTypesCount(frame *data.Frame) fieldTypeCount {
res := fieldTypeCount{
unknownFields: 0,
numericFields: 0,
boolFields: 0,
stringFields: 0,
timeFields: 0,
jsonFields: 0,
}
for _, field := range frame.Fields {
switch field.Type() {
yesoreyeram marked this conversation as resolved.
Show resolved Hide resolved
case data.FieldTypeFloat64,
data.FieldTypeFloat32,
data.FieldTypeNullableFloat64,
data.FieldTypeNullableFloat32,
data.FieldTypeInt64,
data.FieldTypeInt32,
data.FieldTypeInt16,
data.FieldTypeInt8,
data.FieldTypeNullableInt64,
data.FieldTypeNullableInt32,
data.FieldTypeNullableInt16,
data.FieldTypeNullableInt8,
data.FieldTypeUint64,
data.FieldTypeUint32,
data.FieldTypeUint16,
data.FieldTypeUint8,
data.FieldTypeNullableUint64,
data.FieldTypeNullableUint32,
data.FieldTypeNullableUint16,
data.FieldTypeNullableUint8:
res.numericFields++
case data.FieldTypeBool,
data.FieldTypeNullableBool:
res.boolFields++
case data.FieldTypeString,
data.FieldTypeNullableString:
res.stringFields++
case data.FieldTypeTime,
data.FieldTypeNullableTime:
res.timeFields++
case data.FieldTypeJSON,
data.FieldTypeNullableJSON:
res.jsonFields++
case data.FieldTypeEnum,
data.FieldTypeNullableEnum:
res.enumFields++
default:
res.unknownFields++
}
}
return res
}

// IsNumericWideFrame asserts if the data frame comply with numeric wide type
// https://grafana.com/developers/dataplane/numeric#numeric-wide-format-numericwide
func IsNumericWideFrame(frame *data.Frame) bool {
yesoreyeram marked this conversation as resolved.
Show resolved Hide resolved
if frame == nil {
return false
}
ftCount := getFieldTypesCount(frame)
rowLen, err := frame.RowLen()
if err != nil {
return false
}
if rowLen <= 1 && (ftCount.numericFields+ftCount.boolFields) > 0 {
return true
}
return false
}

// IsNumericLongFrame asserts if the data frame comply with numeric long type
// https://grafana.com/developers/dataplane/numeric#numeric-long-format-numericlong-sql-table-like
func IsNumericLongFrame(frame *data.Frame) bool {
if frame == nil {
return false
}
ftCount := getFieldTypesCount(frame)
rowLen, err := frame.RowLen()
if err != nil {
return false
}
if rowLen == 1 && ftCount.numericFields > 0 && ftCount.stringFields == 0 {
return true
}
if rowLen > 1 && ftCount.numericFields > 0 && ftCount.stringFields > 0 {
return true
}
return false
}
32 changes: 24 additions & 8 deletions pkg/infinity/postprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"

"github.com/grafana/grafana-infinity-datasource/pkg/dataplane"
"github.com/grafana/grafana-infinity-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
Expand Down Expand Up @@ -59,27 +60,42 @@ func PostProcessFrame(ctx context.Context, frame *data.Frame, query models.Query
return wFrame, err
}
}
if query.Parser != models.InfinityParserBackend {
return frame, nil
}
if frame.Meta == nil {
frame.Meta = &data.FrameMeta{}
}
if dataplane.IsNumericWideFrame(frame) {
frame.Meta.Type = data.FrameTypeNumericWide
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
return frame, nil
}
if dataplane.IsNumericLongFrame(frame) {
frame.Meta.Type = data.FrameTypeNumericLong
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
return frame, nil
}
return frame, nil
}

func addErrorSourceToTransformError(err error) error {
downstreamErrors := []error{
t.ErrSummarizeByFieldNotFound,
t.ErrNotUniqueFieldNames,
t.ErrEvaluatingFilterExpression,
t.ErrMergeTransformationNoFrameSupplied,
t.ErrMergeTransformationDifferentFields,
t.ErrMergeTransformationDifferentFieldNames,
t.ErrSummarizeByFieldNotFound,
t.ErrNotUniqueFieldNames,
t.ErrEvaluatingFilterExpression,
t.ErrMergeTransformationNoFrameSupplied,
t.ErrMergeTransformationDifferentFields,
t.ErrMergeTransformationDifferentFieldNames,
t.ErrMergeTransformationDifferentFieldTypes,
t.ErrInvalidFilterExpression,
framesql.ErrEmptySummarizeExpression,
framesql.ErrExpressionNotFoundInFields,
}

for _, e := range downstreamErrors {
if errors.Is(err, e) {
return errorsource.DownstreamError(err, false)
}
}
return errorsource.PluginError(err, false)
}
}
6 changes: 4 additions & 2 deletions pkg/testsuite/golden/html_backend_url_default.jsonc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// 🌟 This was machine generated. Do not edit. 🌟
//
// Frame[0] {
// "type": "numeric-long",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "custom": {
// "query": {
Expand Down Expand Up @@ -116,9 +117,10 @@
"schema": {
"name": "response",
"meta": {
"type": "numeric-long",
"typeVersion": [
0,
0
1
],
"custom": {
"query": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// 🌟 This was machine generated. Do not edit. 🌟
//
// Frame[0] {
// "type": "numeric-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "custom": {
// "query": {
Expand Down Expand Up @@ -80,9 +81,10 @@
"schema": {
"name": "q1",
"meta": {
"type": "numeric-wide",
"typeVersion": [
0,
0
1
],
"custom": {
"query": {
Expand Down
Loading