Storage: add support for snapshots, dataframes, and raw json objects (#57934)
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package jsonobj
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/store"
|
||||
)
|
||||
|
||||
func GetObjectKindInfo() models.ObjectKindInfo {
|
||||
return models.ObjectKindInfo{
|
||||
ID: models.StandardKindJSONObj,
|
||||
Name: "JSON Object",
|
||||
Description: "JSON Object",
|
||||
}
|
||||
}
|
||||
|
||||
func GetObjectSummaryBuilder() models.ObjectSummaryBuilder {
|
||||
return func(ctx context.Context, uid string, body []byte) (*models.ObjectSummary, []byte, error) {
|
||||
v := make(map[string]interface{})
|
||||
err := json.Unmarshal(body, &v)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
out, err := json.MarshalIndent(v, "", " ")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return &models.ObjectSummary{
|
||||
Kind: models.StandardKindJSONObj,
|
||||
Name: store.GuessNameFromUID(uid),
|
||||
UID: uid,
|
||||
}, out, err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package jsonobj
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDataFrameSummary(t *testing.T) {
|
||||
// Just creating a JSON blob
|
||||
df := data.NewFrame("http_requests_total",
|
||||
data.NewField("timestamp", nil, []time.Time{time.Now(), time.Now(), time.Now()}).SetConfig(&data.FieldConfig{
|
||||
DisplayName: "A time Column.",
|
||||
}),
|
||||
data.NewField("value", data.Labels{"service": "auth"}, []float64{1.0, 2.0, 3.0}),
|
||||
data.NewField("category", data.Labels{"service": "auth"}, []string{"foo", "bar", "test"}),
|
||||
data.NewField("valid", data.Labels{"service": "auth"}, []bool{true, false, true}),
|
||||
)
|
||||
in, err := data.FrameToJSON(df, data.IncludeAll)
|
||||
require.NoError(t, err)
|
||||
|
||||
summary, out, err := GetObjectSummaryBuilder()(context.Background(), "path/to/item", in)
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, string(in), string(out)) // same json
|
||||
|
||||
asjson, err := json.MarshalIndent(summary, "", " ")
|
||||
// fmt.Printf(string(asjson))
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, `{
|
||||
"name": "item",
|
||||
"uid": "path/to/item",
|
||||
"kind": "jsonobj"
|
||||
}`, string(asjson))
|
||||
}
|
||||
Reference in New Issue
Block a user