compare.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package dashdiffs
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/models"
  8. diff "github.com/yudai/gojsondiff"
  9. deltaFormatter "github.com/yudai/gojsondiff/formatter"
  10. )
  11. var (
  12. // ErrUnsupportedDiffType occurs when an invalid diff type is used.
  13. ErrUnsupportedDiffType = errors.New("dashdiff: unsupported diff type")
  14. // ErrNilDiff occurs when two compared interfaces are identical.
  15. ErrNilDiff = errors.New("dashdiff: diff is nil")
  16. )
  17. type DiffType int
  18. const (
  19. DiffJSON DiffType = iota
  20. DiffBasic
  21. DiffDelta
  22. )
  23. type Options struct {
  24. OrgId int64
  25. Base DiffTarget
  26. New DiffTarget
  27. DiffType DiffType
  28. }
  29. type DiffTarget struct {
  30. DashboardId int64
  31. Version int
  32. UnsavedDashboard *simplejson.Json
  33. }
  34. type Result struct {
  35. Delta []byte `json:"delta"`
  36. }
  37. func ParseDiffType(diff string) DiffType {
  38. switch diff {
  39. case "json":
  40. return DiffJSON
  41. case "basic":
  42. return DiffBasic
  43. case "delta":
  44. return DiffDelta
  45. }
  46. return DiffBasic
  47. }
  48. // CompareDashboardVersionsCommand computes the JSON diff of two versions,
  49. // assigning the delta of the diff to the `Delta` field.
  50. func CalculateDiff(options *Options) (*Result, error) {
  51. baseVersionQuery := models.GetDashboardVersionQuery{
  52. DashboardId: options.Base.DashboardId,
  53. Version: options.Base.Version,
  54. }
  55. if err := bus.Dispatch(&baseVersionQuery); err != nil {
  56. return nil, err
  57. }
  58. newVersionQuery := models.GetDashboardVersionQuery{
  59. DashboardId: options.New.DashboardId,
  60. Version: options.New.Version,
  61. }
  62. if err := bus.Dispatch(&newVersionQuery); err != nil {
  63. return nil, err
  64. }
  65. left, jsonDiff, err := getDiff(baseVersionQuery.Result, newVersionQuery.Result)
  66. if err != nil {
  67. return nil, err
  68. }
  69. result := &Result{}
  70. switch options.DiffType {
  71. case DiffDelta:
  72. deltaOutput, err := deltaFormatter.NewDeltaFormatter().Format(jsonDiff)
  73. if err != nil {
  74. return nil, err
  75. }
  76. result.Delta = []byte(deltaOutput)
  77. case DiffJSON:
  78. jsonOutput, err := NewJSONFormatter(left).Format(jsonDiff)
  79. if err != nil {
  80. return nil, err
  81. }
  82. result.Delta = []byte(jsonOutput)
  83. case DiffBasic:
  84. basicOutput, err := NewBasicFormatter(left).Format(jsonDiff)
  85. if err != nil {
  86. return nil, err
  87. }
  88. result.Delta = basicOutput
  89. default:
  90. return nil, ErrUnsupportedDiffType
  91. }
  92. return result, nil
  93. }
  94. // getDiff computes the diff of two dashboard versions.
  95. func getDiff(originalDash, newDash *models.DashboardVersion) (interface{}, diff.Diff, error) {
  96. leftBytes, err := simplejson.NewFromAny(originalDash).Encode()
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. rightBytes, err := simplejson.NewFromAny(newDash).Encode()
  101. if err != nil {
  102. return nil, nil, err
  103. }
  104. jsonDiff, err := diff.New().Compare(leftBytes, rightBytes)
  105. if err != nil {
  106. return nil, nil, err
  107. }
  108. if !jsonDiff.Modified() {
  109. return nil, nil, ErrNilDiff
  110. }
  111. left := make(map[string]interface{})
  112. err = json.Unmarshal(leftBytes, &left)
  113. return left, jsonDiff, nil
  114. }