Skip to content

Commit

Permalink
numeric frame type
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram committed Jul 16, 2024
1 parent ff0c584 commit ce568fd
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/go/jsonframer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @grafana/infinity-jsonframer

## 1.1.1

### Patch Changes

- Added numeric frame type

## 1.1.0

- removed sqlite3 framer
Expand Down
40 changes: 39 additions & 1 deletion lib/go/jsonframer/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func TestJsonStringToMultiFrame(t *testing.T) {
{"name": "bar", "age": 3}
],[
{"name": "foo", "salary": 2430 },
{"name": "foo", "salary": 3000 }
{"name": "bar", "salary": 3000 }
]
]`, jsonframer.FramerOptions{})
require.Nil(t, err)
Expand Down Expand Up @@ -328,6 +328,44 @@ func TestJsonStringToMultiFrame(t *testing.T) {
require.NotNil(t, frames)
experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "regular-object", &backend.DataResponse{Frames: frames}, true)
})
t.Run("numeric frame", func(t *testing.T) {
t.Run("array of arrays", func(t *testing.T) {
frames, err := jsonframer.ToFrames(`[
[
{"name": "foo", "age": 2},
{"name": "bar", "age": 3}
],[
{"name": "foo", "salary": 2430 },
{"name": "bar", "salary": 3000 }
]
]`, jsonframer.FramerOptions{FrameFormat: jsonframer.FrameFormatNumeric})
require.Nil(t, err)
require.NotNil(t, frames)
experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "array-of-array-numeric", &backend.DataResponse{Frames: frames}, true)
})
t.Run("array of objects", func(t *testing.T) {
frames, err := jsonframer.ToFrames(`[
{ "name": "foo", "age": 2, "salary": 2430 },
{ "name": "bar", "age": 3, "salary": 3000 }
]`, jsonframer.FramerOptions{FrameFormat: jsonframer.FrameFormatNumeric})
require.Nil(t, err)
require.NotNil(t, frames)
experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "array-of-objects-numeric", &backend.DataResponse{Frames: frames}, true)
})
t.Run("array of items", func(t *testing.T) {
frames, err := jsonframer.ToFrames(`["foo","bar"]`, jsonframer.FramerOptions{FrameName: "result", FrameFormat: jsonframer.FrameFormatNumeric})
require.Nil(t, err)
require.NotNil(t, frames)
experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "array-of-items-numeric", &backend.DataResponse{Frames: frames}, true)
})
t.Run("regular object", func(t *testing.T) {
frames, err := jsonframer.ToFrames(`{"name": "foo", "age": 2, "salary": 2430}`, jsonframer.FramerOptions{FrameFormat: jsonframer.FrameFormatNumeric})
require.Nil(t, err)
require.NotNil(t, frames)
experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "regular-object-numeric", &backend.DataResponse{Frames: frames}, true)
})

})
}

func TestAzureFrame(t *testing.T) {
Expand Down
13 changes: 10 additions & 3 deletions lib/go/jsonframer/jsonframer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type FrameFormat string
const (
FrameFormatTable FrameFormat = "table"
FrameFormatTimeSeries FrameFormat = "timeseries"
FrameFormatNumeric FrameFormat = "numeric"
)

type FramerOptions struct {
Expand Down Expand Up @@ -105,7 +106,6 @@ func ToFrames(jsonString string, options FramerOptions) (frames []*data.Frame, e
return frames, err
}
if frame != nil {

if options.FrameFormat == FrameFormatTimeSeries && frame.TimeSeriesSchema().Type == data.TimeSeriesTypeLong {
frame, err = data.LongToWide(frame, nil)
if err != nil {
Expand Down Expand Up @@ -164,7 +164,6 @@ func GetRootData(jsonString string, rootSelector string) (string, error) {
return string(r2), nil
}
return jsonString, nil

}

func getColumnValuesFromResponseString(responseString string, columns []ColumnSelector) (string, error) {
Expand Down Expand Up @@ -230,11 +229,19 @@ func getFrameFromResponseString(responseString string, options FramerOptions) (f
TimeFormat: c.TimeFormat,
})
}
return gframer.ToDataFrame(out, gframer.FramerOptions{
frame, err = gframer.ToDataFrame(out, gframer.FramerOptions{
FrameName: options.FrameName,
Columns: columns,
OverrideColumns: overrides,
})
if options.FrameFormat == FrameFormatNumeric {
if frame.Meta == nil {
frame.Meta = &data.FrameMeta{}
}
frame.Meta.Type = data.FrameTypeNumericLong
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
}
return frame, err
}

func convertFieldValueType(input interface{}, _ ColumnSelector) interface{} {
Expand Down
2 changes: 1 addition & 1 deletion lib/go/jsonframer/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@grafana/infinity-jsonframer",
"private": true,
"version": "1.1.0",
"version": "1.1.1",
"scripts": {
"tidy": "go mod tidy",
"test:backend": "go test -v ./..."
Expand Down
129 changes: 129 additions & 0 deletions lib/go/jsonframer/testdata/multiframer/array-of-array-numeric.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// 🌟 This was machine generated. Do not edit. 🌟
//
// Frame[0] {
// "type": "numeric-long",
// "typeVersion": [
// 0,
// 1
// ]
// }
// Name:
// Dimensions: 2 Fields by 2 Rows
// +------------------+-----------------+
// | Name: age | Name: name |
// | Labels: | Labels: |
// | Type: []*float64 | Type: []*string |
// +------------------+-----------------+
// | 2 | foo |
// | 3 | bar |
// +------------------+-----------------+
//
//
//
// Frame[1] {
// "type": "numeric-long",
// "typeVersion": [
// 0,
// 1
// ]
// }
// Name:
// Dimensions: 2 Fields by 2 Rows
// +-----------------+------------------+
// | Name: name | Name: salary |
// | Labels: | Labels: |
// | Type: []*string | Type: []*float64 |
// +-----------------+------------------+
// | foo | 2430 |
// | bar | 3000 |
// +-----------------+------------------+
//
//
// 🌟 This was machine generated. Do not edit. 🌟
{
"status": 200,
"frames": [
{
"schema": {
"meta": {
"type": "numeric-long",
"typeVersion": [
0,
1
]
},
"fields": [
{
"name": "age",
"type": "number",
"typeInfo": {
"frame": "float64",
"nullable": true
}
},
{
"name": "name",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
}
}
]
},
"data": {
"values": [
[
2,
3
],
[
"foo",
"bar"
]
]
}
},
{
"schema": {
"meta": {
"type": "numeric-long",
"typeVersion": [
0,
1
]
},
"fields": [
{
"name": "name",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
}
},
{
"name": "salary",
"type": "number",
"typeInfo": {
"frame": "float64",
"nullable": true
}
}
]
},
"data": {
"values": [
[
"foo",
"bar"
],
[
2430,
3000
]
]
}
}
]
}
4 changes: 2 additions & 2 deletions lib/go/jsonframer/testdata/multiframer/array-of-array.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// | Type: []*string | Type: []*float64 |
// +-----------------+------------------+
// | foo | 2430 |
// | foo | 3000 |
// | bar | 3000 |
// +-----------------+------------------+
//
//
Expand Down Expand Up @@ -90,7 +90,7 @@
"values": [
[
"foo",
"foo"
"bar"
],
[
2430,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 🌟 This was machine generated. Do not edit. 🌟
//
// Frame[0] {
// "type": "numeric-long",
// "typeVersion": [
// 0,
// 1
// ]
// }
// Name: result
// Dimensions: 1 Fields by 2 Rows
// +-----------------+
// | Name: result |
// | Labels: |
// | Type: []*string |
// +-----------------+
// | foo |
// | bar |
// +-----------------+
//
//
// 🌟 This was machine generated. Do not edit. 🌟
{
"status": 200,
"frames": [
{
"schema": {
"name": "result",
"meta": {
"type": "numeric-long",
"typeVersion": [
0,
1
]
},
"fields": [
{
"name": "result",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
}
}
]
},
"data": {
"values": [
[
"foo",
"bar"
]
]
}
}
]
}
Loading

0 comments on commit ce568fd

Please sign in to comment.