From 577dfee086733a95e7a9fa01e026d94edaaaba20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 7 Jun 2017 13:36:03 +0200 Subject: [PATCH] dasboard_history: fixed json diff so only dashbord is compared and not the whole dashboard revision object (message and restoreFrom etc was showing up in json diff) --- pkg/components/dashdiffs/compare.go | 14 ++++++++++---- pkg/components/dashdiffs/formatter_basic.go | 11 ++++++++--- public/app/features/dashboard/history/history.html | 4 ++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pkg/components/dashdiffs/compare.go b/pkg/components/dashdiffs/compare.go index 51c8a947ce1..7e4df0e67b2 100644 --- a/pkg/components/dashdiffs/compare.go +++ b/pkg/components/dashdiffs/compare.go @@ -6,6 +6,7 @@ import ( "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/simplejson" + "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/models" diff "github.com/yudai/gojsondiff" deltaFormatter "github.com/yudai/gojsondiff/formatter" @@ -17,6 +18,8 @@ var ( // ErrNilDiff occurs when two compared interfaces are identical. ErrNilDiff = errors.New("dashdiff: diff is nil") + + diffLogger = log.New("dashdiffs") ) type DiffType int @@ -77,7 +80,10 @@ func CalculateDiff(options *Options) (*Result, error) { return nil, err } - left, jsonDiff, err := getDiff(baseVersionQuery.Result, newVersionQuery.Result) + baseData := baseVersionQuery.Result.Data + newData := newVersionQuery.Result.Data + + left, jsonDiff, err := getDiff(baseData, newData) if err != nil { return nil, err } @@ -115,13 +121,13 @@ func CalculateDiff(options *Options) (*Result, error) { } // getDiff computes the diff of two dashboard versions. -func getDiff(originalDash, newDash *models.DashboardVersion) (interface{}, diff.Diff, error) { - leftBytes, err := simplejson.NewFromAny(originalDash).Encode() +func getDiff(baseData, newData *simplejson.Json) (interface{}, diff.Diff, error) { + leftBytes, err := baseData.Encode() if err != nil { return nil, nil, err } - rightBytes, err := simplejson.NewFromAny(newDash).Encode() + rightBytes, err := newData.Encode() if err != nil { return nil, nil, err } diff --git a/pkg/components/dashdiffs/formatter_basic.go b/pkg/components/dashdiffs/formatter_basic.go index 819d0a1fd86..01c7757112d 100644 --- a/pkg/components/dashdiffs/formatter_basic.go +++ b/pkg/components/dashdiffs/formatter_basic.go @@ -97,14 +97,17 @@ func (b *BasicDiff) Basic(lines []*JSONLine) []*BasicBlock { // iterate through each line for _, line := range lines { - if b.LastIndent == 3 && line.Indent == 2 && line.Change == ChangeNil { + // TODO: this condition needs an explaination? what does it mean? + if b.LastIndent == 2 && line.Indent == 1 && line.Change == ChangeNil { if b.Block != nil { blocks = append(blocks, b.Block) } } + b.LastIndent = line.Indent - if line.Indent == 2 { + // TODO: why special handling for indent 2? + if line.Indent == 1 { switch line.Change { case ChangeNil: if line.Change == ChangeNil { @@ -143,12 +146,14 @@ func (b *BasicDiff) Basic(lines []*JSONLine) []*BasicBlock { } } + // TODO: why special handling for indent > 2 ? // Other Lines - if line.Indent > 2 { + if line.Indent > 1 { // Ensure single line change if line.Key != "" && line.Val != nil && !b.writing { switch line.Change { case ChangeAdded, ChangeDeleted: + b.Block.Changes = append(b.Block.Changes, &BasicChange{ Key: line.Key, Change: line.Change, diff --git a/public/app/features/dashboard/history/history.html b/public/app/features/dashboard/history/history.html index 4290cc7266b..375daa8d87f 100644 --- a/public/app/features/dashboard/history/history.html +++ b/public/app/features/dashboard/history/history.html @@ -11,7 +11,7 @@
  • - Version {{ctrl.baseInfo.version}} Version {{ctrl.newInfo.version}} + Version Comparison
  • @@ -123,7 +123,7 @@ Version {{ctrl.newInfo.version}} (Latest) -
    +

    Version {{ctrl.newInfo.version}} updated by {{ctrl.newInfo.createdBy}}