mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
refactoring: moved compare dashboard version command out of sqlstore, the code in this command did not use any sql operations and was more high level, could be moved out and use existing queries to get the versions
This commit is contained in:
@@ -1,80 +1,15 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/components/formatter"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
|
||||
diff "github.com/yudai/gojsondiff"
|
||||
deltaFormatter "github.com/yudai/gojsondiff/formatter"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrUnsupportedDiffType occurs when an invalid diff type is used.
|
||||
ErrUnsupportedDiffType = errors.New("sqlstore: unsupported diff type")
|
||||
|
||||
// ErrNilDiff occurs when two compared interfaces are identical.
|
||||
ErrNilDiff = errors.New("sqlstore: diff is nil")
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", CompareDashboardVersionsCommand)
|
||||
bus.AddHandler("sql", GetDashboardVersion)
|
||||
bus.AddHandler("sql", GetDashboardVersions)
|
||||
}
|
||||
|
||||
// CompareDashboardVersionsCommand computes the JSON diff of two versions,
|
||||
// assigning the delta of the diff to the `Delta` field.
|
||||
func CompareDashboardVersionsCommand(cmd *m.CompareDashboardVersionsCommand) error {
|
||||
baseVersion, err := getDashboardVersion(cmd.DashboardId, cmd.BaseVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newVersion, err := getDashboardVersion(cmd.DashboardId, cmd.NewVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
left, jsonDiff, err := getDiff(baseVersion, newVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch cmd.DiffType {
|
||||
case m.DiffDelta:
|
||||
|
||||
deltaOutput, err := deltaFormatter.NewDeltaFormatter().Format(jsonDiff)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.Delta = []byte(deltaOutput)
|
||||
|
||||
case m.DiffJSON:
|
||||
jsonOutput, err := formatter.NewJSONFormatter(left).Format(jsonDiff)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.Delta = []byte(jsonOutput)
|
||||
|
||||
case m.DiffBasic:
|
||||
basicOutput, err := formatter.NewBasicFormatter(left).Format(jsonDiff)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.Delta = basicOutput
|
||||
|
||||
default:
|
||||
return ErrUnsupportedDiffType
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDashboardVersion gets the dashboard version for the given dashboard ID
|
||||
// and version number.
|
||||
func GetDashboardVersion(query *m.GetDashboardVersionQuery) error {
|
||||
@@ -145,29 +80,3 @@ func getDashboard(dashboardId int64) (*m.Dashboard, error) {
|
||||
}
|
||||
return &dashboard, nil
|
||||
}
|
||||
|
||||
// getDiff computes the diff of two dashboard versions.
|
||||
func getDiff(originalDash, newDash *m.DashboardVersion) (interface{}, diff.Diff, error) {
|
||||
leftBytes, err := simplejson.NewFromAny(originalDash).Encode()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
rightBytes, err := simplejson.NewFromAny(newDash).Encode()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
jsonDiff, err := diff.New().Compare(leftBytes, rightBytes)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if !jsonDiff.Modified() {
|
||||
return nil, nil, ErrNilDiff
|
||||
}
|
||||
|
||||
left := make(map[string]interface{})
|
||||
err = json.Unmarshal(leftBytes, &left)
|
||||
return left, jsonDiff, nil
|
||||
}
|
||||
|
||||
@@ -98,59 +98,3 @@ func TestGetDashboardVersions(t *testing.T) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestCompareDashboardVersions(t *testing.T) {
|
||||
Convey("Testing dashboard version comparison", t, func() {
|
||||
InitTestDB(t)
|
||||
|
||||
savedDash := insertTestDashboard("test dash 43", 1, "x")
|
||||
updateTestDashboard(savedDash, map[string]interface{}{
|
||||
"tags": "y",
|
||||
})
|
||||
|
||||
Convey("Compare two versions that are different", func() {
|
||||
query := m.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
|
||||
|
||||
err := GetDashboardVersions(&query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
|
||||
cmd := m.CompareDashboardVersionsCommand{
|
||||
DashboardId: savedDash.Id,
|
||||
BaseVersion: query.Result[0].Version,
|
||||
NewVersion: query.Result[1].Version,
|
||||
DiffType: m.DiffDelta,
|
||||
}
|
||||
|
||||
err = CompareDashboardVersionsCommand(&cmd)
|
||||
So(err, ShouldBeNil)
|
||||
So(cmd.Delta, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("Compare two versions that are the same", func() {
|
||||
cmd := m.CompareDashboardVersionsCommand{
|
||||
DashboardId: savedDash.Id,
|
||||
BaseVersion: savedDash.Version,
|
||||
NewVersion: savedDash.Version,
|
||||
DiffType: m.DiffDelta,
|
||||
}
|
||||
|
||||
err := CompareDashboardVersionsCommand(&cmd)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(cmd.Delta, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Compare two versions that don't exist", func() {
|
||||
cmd := m.CompareDashboardVersionsCommand{
|
||||
DashboardId: savedDash.Id,
|
||||
BaseVersion: 123,
|
||||
NewVersion: 456,
|
||||
DiffType: m.DiffDelta,
|
||||
}
|
||||
|
||||
err := CompareDashboardVersionsCommand(&cmd)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(cmd.Delta, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user