Skip to content

Commit

Permalink
[ENHANCEMENT] Allow migration of empty panel dashboard (#1854)
Browse files Browse the repository at this point in the history
* [ENHANCEMENT] Allow migration of empty panel dashboard

Signed-off-by: Ying WANG <ying.wang@grafana.com>

* Remove cue schema changes

Signed-off-by: Ying WANG <ying.wang@grafana.com>

* remove logs from dashboard model api

Signed-off-by: Ying WANG <ying.wang@grafana.com>

---------

Signed-off-by: Ying WANG <ying.wang@grafana.com>
  • Loading branch information
ying-jeanne committed Mar 29, 2024
1 parent 331ec97 commit 083913c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 31 deletions.
4 changes: 2 additions & 2 deletions internal/api/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"github.com/perses/perses/internal/api/interface"
apiinterface "github.com/perses/perses/internal/api/interface"
"github.com/perses/perses/internal/api/schemas"
"github.com/perses/perses/pkg/model/api/config"
v1 "github.com/perses/perses/pkg/model/api/v1"
Expand Down Expand Up @@ -231,7 +231,7 @@ func rearrangeGrafanaPanelsWithinExpandedRows(grafanaDashboardRaw json.RawMessag
}

// iterate over the panels & achieve recomposition of parent->children relationship when needed
var newPanelList []map[string]any
newPanelList := make([]map[string]any, 0)
var parentRow map[string]any
for _, panelAsAny := range panels {
panel, ok := panelAsAny.(map[string]any)
Expand Down
6 changes: 6 additions & 0 deletions internal/api/migrate/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func TestMigrate(t *testing.T) {
expectedPersesDashboardFile: "old_grafana_query_perses_dashboard.json",
expectedErrorStr: "",
},
{
title: "dashboard without panels should be migrated without error",
inputGrafanaDashboardFile: "empty_panels_list_grafana_dashboard.json",
expectedPersesDashboardFile: "empty_panels_list_perses_dashboard.json",
expectedErrorStr: "",
},
}

for _, test := range testSuite {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "grafana",
"uid": "-- Grafana --"
},
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 28,
"links": [],
"panels": [],
"schemaVersion": 39,
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-6h",
"to": "now"
},
"timeRangeUpdatedDuringEditOrView": false,
"timepicker": {},
"timezone": "browser",
"title": "test dashboard",
"uid": "ddgn9k36v60owc",
"version": 1,
"weekStart": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"kind":"Dashboard",
"metadata": {
"createdAt":"0001-01-01T00:00:00Z",
"name":"ddgn9k36v60owc",
"project":"",
"updatedAt":"0001-01-01T00:00:00Z",
"version":0
},
"spec": {
"display":{
"name":"test dashboard"
},
"duration":"1h",
"layouts":[],
"panels":{}
}
}
2 changes: 1 addition & 1 deletion pkg/model/api/v1/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (d *DashboardSpec) UnmarshalYAML(unmarshal func(interface{}) error) error {

func (d *DashboardSpec) validate() error {
if len(d.Panels) == 0 {
return fmt.Errorf("spec.panels cannot be empty")
return nil
}
variables := make(map[string]bool, len(d.Variables))
for i, variable := range d.Variables {
Expand Down
14 changes: 0 additions & 14 deletions pkg/model/api/v1/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,20 +524,6 @@ func TestUnmarshalDashboardError(t *testing.T) {
`,
err: fmt.Errorf("spec cannot be empty"),
},
{
title: "panel list cannot be empty",
jason: `
{
"kind": "Dashboard",
"metadata": {
"name": "test",
"project": "perses"
},
"spec": {}
}
`,
err: fmt.Errorf("spec.panels cannot be empty"),
},
}
for _, test := range testSuite {
t.Run(test.title, func(t *testing.T) {
Expand Down
14 changes: 0 additions & 14 deletions pkg/model/api/v1/ephemeraldashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,20 +536,6 @@ func TestUnmarshalEphemeralDashboardError(t *testing.T) {
`,
err: fmt.Errorf("spec cannot be empty"),
},
{
title: "panel list cannot be empty",
jason: `
{
"kind": "EphemeralDashboard",
"metadata": {
"name": "test",
"project": "perses"
},
"spec": {}
}
`,
err: fmt.Errorf("spec.panels cannot be empty"),
},
}
for _, test := range testSuite {
t.Run(test.title, func(t *testing.T) {
Expand Down

0 comments on commit 083913c

Please sign in to comment.