Dashboard Migrations: v18 - gauge options (#109496)

* migrate to v19

* migrate to v18

* Update v19.go
This commit is contained in:
Haris Rozajac
2025-08-13 15:01:50 -06:00
committed by GitHub
parent df70198038
commit 4a3b8a567f
5 changed files with 566 additions and 1 deletions
@@ -5,7 +5,7 @@ import (
)
const (
MIN_VERSION = 18
MIN_VERSION = 17
LATEST_VERSION = 41
)
@@ -38,6 +38,7 @@ type PanelPluginInfoProvider interface {
func GetMigrations(dsInfoProvider DataSourceInfoProvider, panelProvider PanelPluginInfoProvider) map[int]SchemaVersionMigrationFunc {
return map[int]SchemaVersionMigrationFunc{
18: V18,
19: V19,
20: V20,
21: V21,
@@ -0,0 +1,117 @@
package schemaversion
// V18 migrates gauge panel options from the legacy `options-gauge` format to the new `options` format.
// This migration restructures gauge panel configuration to use the modern options structure with valueOptions.
//
// Example before migration:
//
// "panels": [
// {
// "type": "gauge",
// "options-gauge": {
// "unit": "ms",
// "stat": "last",
// "decimals": 2,
// "prefix": "Value: ",
// "suffix": " ms",
// "thresholds": [
// { "color": "green", "value": 0 },
// { "color": "red", "value": 100 }
// ]
// }
// }
// ]
//
// Example after migration:
//
// "panels": [
// {
// "type": "gauge",
// "options": {
// "valueOptions": {
// "unit": "ms",
// "stat": "last",
// "decimals": 2,
// "prefix": "Value: ",
// "suffix": " ms"
// },
// "thresholds": [
// { "color": "red", "value": 100 },
// { "color": "green", "value": 0 }
// ]
// }
// }
// ]
func V18(dashboard map[string]interface{}) error {
dashboard["schemaVersion"] = 18
panels, ok := dashboard["panels"].([]interface{})
if !ok {
return nil
}
for _, p := range panels {
panel, ok := p.(map[string]interface{})
if !ok {
continue
}
migrateGaugePanelOptions(panel)
}
return nil
}
func migrateGaugePanelOptions(panel map[string]interface{}) {
optionsGauge, hasOptionsGauge := panel["options-gauge"].(map[string]interface{})
if !hasOptionsGauge {
return
}
options := map[string]interface{}{}
valueOptions := map[string]interface{}{}
if unit, ok := optionsGauge["unit"]; ok {
valueOptions["unit"] = unit
}
if stat, ok := optionsGauge["stat"]; ok {
valueOptions["stat"] = stat
}
if decimals, ok := optionsGauge["decimals"]; ok {
valueOptions["decimals"] = decimals
}
if prefix, ok := optionsGauge["prefix"]; ok {
valueOptions["prefix"] = prefix
}
if suffix, ok := optionsGauge["suffix"]; ok {
valueOptions["suffix"] = suffix
}
options["valueOptions"] = valueOptions
if thresholds, ok := optionsGauge["thresholds"].([]interface{}); ok && len(thresholds) > 0 {
reversedThresholds := make([]interface{}, len(thresholds))
for i, threshold := range thresholds {
reversedThresholds[len(thresholds)-1-i] = threshold
}
options["thresholds"] = reversedThresholds
}
// Copy any other properties from options-gauge to options
for key, value := range optionsGauge {
// Skip properties that were moved to valueOptions or are being deleted
if key == "options" || key == "unit" || key == "stat" || key == "decimals" || key == "prefix" || key == "suffix" || key == "thresholds" {
continue
}
options[key] = value
}
panel["options"] = options
delete(panel, "options-gauge")
// Clean up options.options property if it exists
// This options prop was due to a bug
if panelOptions, ok := panel["options"].(map[string]interface{}); ok {
delete(panelOptions, "options")
}
}
@@ -0,0 +1,219 @@
package schemaversion_test
import (
"testing"
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
)
func TestV18(t *testing.T) {
tests := []migrationTestCase{
{
name: "gauge panel with legacy options-gauge gets migrated to new options format",
input: map[string]interface{}{
"title": "V18 Gauge Options Migration Test Dashboard",
"schemaVersion": 17,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "gauge",
"title": "Gauge Panel",
"options-gauge": map[string]interface{}{
"unit": "ms",
"stat": "last",
"decimals": 2,
"prefix": "Value: ",
"suffix": " ms",
"thresholds": []interface{}{
map[string]interface{}{"color": "green", "value": 0},
map[string]interface{}{"color": "red", "value": 100},
},
},
},
},
},
expected: map[string]interface{}{
"title": "V18 Gauge Options Migration Test Dashboard",
"schemaVersion": 18,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "gauge",
"title": "Gauge Panel",
"options": map[string]interface{}{
"valueOptions": map[string]interface{}{
"unit": "ms",
"stat": "last",
"decimals": 2,
"prefix": "Value: ",
"suffix": " ms",
},
"thresholds": []interface{}{
map[string]interface{}{"color": "red", "value": 100},
map[string]interface{}{"color": "green", "value": 0},
},
},
},
},
},
},
{
name: "gauge panel with only some gauge options gets migrated correctly",
input: map[string]interface{}{
"title": "V18 Partial Gauge Options Migration Test Dashboard",
"schemaVersion": 17,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "gauge",
"title": "Partial Gauge Panel",
"options-gauge": map[string]interface{}{
"unit": "percent",
"decimals": 1,
},
},
},
},
expected: map[string]interface{}{
"title": "V18 Partial Gauge Options Migration Test Dashboard",
"schemaVersion": 18,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "gauge",
"title": "Partial Gauge Panel",
"options": map[string]interface{}{
"valueOptions": map[string]interface{}{
"unit": "percent",
"decimals": 1,
},
},
},
},
},
},
{
name: "gauge panel with buggy options property gets cleaned up",
input: map[string]interface{}{
"title": "V18 Buggy Options Cleanup Test Dashboard",
"schemaVersion": 17,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "gauge",
"title": "Buggy Gauge Panel",
"options-gauge": map[string]interface{}{
"unit": "bytes",
"options": "this should be deleted",
"stat": "avg",
"decimals": 0,
},
},
},
},
expected: map[string]interface{}{
"title": "V18 Buggy Options Cleanup Test Dashboard",
"schemaVersion": 18,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "gauge",
"title": "Buggy Gauge Panel",
"options": map[string]interface{}{
"valueOptions": map[string]interface{}{
"unit": "bytes",
"stat": "avg",
"decimals": 0,
},
},
},
},
},
},
{
name: "gauge panel with additional custom properties gets migrated correctly",
input: map[string]interface{}{
"title": "V18 Custom Properties Migration Test Dashboard",
"schemaVersion": 17,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "gauge",
"title": "Custom Gauge Panel",
"options-gauge": map[string]interface{}{
"unit": "short",
"customProperty": "customValue",
"anotherProp": 42,
},
},
},
},
expected: map[string]interface{}{
"title": "V18 Custom Properties Migration Test Dashboard",
"schemaVersion": 18,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "gauge",
"title": "Custom Gauge Panel",
"options": map[string]interface{}{
"valueOptions": map[string]interface{}{
"unit": "short",
},
"customProperty": "customValue",
"anotherProp": 42,
},
},
},
},
},
{
name: "non-gauge panel remains unchanged",
input: map[string]interface{}{
"title": "V18 Non-Gauge Panel Test Dashboard",
"schemaVersion": 17,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "graph",
"title": "Graph Panel",
"options": map[string]interface{}{
"legend": map[string]interface{}{
"show": true,
},
},
},
},
},
expected: map[string]interface{}{
"title": "V18 Non-Gauge Panel Test Dashboard",
"schemaVersion": 18,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "graph",
"title": "Graph Panel",
"options": map[string]interface{}{
"legend": map[string]interface{}{
"show": true,
},
},
},
},
},
},
{
name: "dashboard with no panels remains unchanged",
input: map[string]interface{}{
"title": "V18 No Panels Test Dashboard",
"schemaVersion": 17,
},
expected: map[string]interface{}{
"title": "V18 No Panels Test Dashboard",
"schemaVersion": 18,
},
},
}
runMigrationTests(t, tests, schemaversion.V18)
}