feat(plugins): dashboard import for data sources is working! #4298

This commit is contained in:
Torkel Ödegaard
2016-03-12 10:13:49 +01:00
parent 3fb0b71822
commit 0398face05
18 changed files with 278 additions and 80 deletions
+34 -21
View File
@@ -1,8 +1,8 @@
package plugins
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"github.com/grafana/grafana/pkg/bus"
@@ -127,32 +127,45 @@ func (this *DashTemplateEvaluator) Eval() (*simplejson.Json, error) {
log.Info("Import: dashboard has no __import section")
}
this.EvalObject(this.template, this.result)
return this.result, nil
return simplejson.NewFromAny(this.evalObject(this.template)), nil
}
func (this *DashTemplateEvaluator) EvalObject(source *simplejson.Json, writer *simplejson.Json) {
func (this *DashTemplateEvaluator) evalValue(source *simplejson.Json) interface{} {
sourceValue := source.Interface()
switch v := sourceValue.(type) {
case string:
interpolated := this.varRegex.ReplaceAllStringFunc(v, func(match string) string {
return this.variables[match]
})
return interpolated
case bool:
return v
case json.Number:
return v
case map[string]interface{}:
return this.evalObject(source)
case []interface{}:
array := make([]interface{}, 0)
for _, item := range v {
array = append(array, this.evalValue(simplejson.NewFromAny(item)))
}
return array
}
return nil
}
func (this *DashTemplateEvaluator) evalObject(source *simplejson.Json) interface{} {
result := make(map[string]interface{})
for key, value := range source.MustMap() {
if key == "__inputs" {
continue
}
switch v := value.(type) {
case string:
interpolated := this.varRegex.ReplaceAllStringFunc(v, func(match string) string {
return this.variables[match]
})
writer.Set(key, interpolated)
case map[string]interface{}:
childSource := simplejson.NewFromAny(value)
childWriter := simplejson.New()
writer.Set(key, childWriter.Interface())
this.EvalObject(childSource, childWriter)
case []interface{}:
default:
log.Info("type: %v", reflect.TypeOf(value))
log.Error(3, "Unknown json type key: %v , type: %v", key, value)
}
result[key] = this.evalValue(simplejson.NewFromAny(value))
}
return result
}
+10 -5
View File
@@ -1,6 +1,7 @@
package plugins
import (
"io/ioutil"
"testing"
"github.com/grafana/grafana/pkg/bus"
@@ -34,7 +35,7 @@ func TestDashboardImport(t *testing.T) {
OrgId: 1,
UserId: 1,
Inputs: []ImportDashboardInput{
{Name: "*", Type: "datasource"},
{Name: "*", Type: "datasource", Value: "graphite"},
},
}
@@ -44,11 +45,15 @@ func TestDashboardImport(t *testing.T) {
Convey("should install dashboard", func() {
So(importedDash, ShouldNotBeNil)
dashStr, _ := importedDash.Data.EncodePretty()
So(string(dashStr), ShouldEqual, "")
resultStr, _ := importedDash.Data.EncodePretty()
expectedBytes, _ := ioutil.ReadFile("../../tests/test-app/dashboards/connections_result.json")
expectedJson, _ := simplejson.NewJson(expectedBytes)
expectedStr, _ := expectedJson.EncodePretty()
// So(panel["datasource"], ShouldEqual, "graphite")
// So(importedDash.Data["__inputs"], ShouldBeNil)
So(string(resultStr), ShouldEqual, string(expectedStr))
panel := importedDash.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
So(panel.Get("datasource").MustString(), ShouldEqual, "graphite")
})
})