**Highlights**
* **Single-version migrations**: add `targetVersion` to migrator & model, separate outputs, enforce exact version.
* **Datasource fixes**: include `apiVersion` in tests, empty-string → `{}`, preserve `{}` refs, drop unwanted defaults.
* **Panel defaults & nesting**: only top-level panels get defaults; preserve empty `transformations` context-aware; filter repeated panels.
* **Migration parity**
* V16: collapsed rows, grid height parsing (`px`).
* V17: omit `maxPerRow` when `minSpan=1`.
* V19–V20: cleanup defaults (`targetBlank`, style).
* V23–V24: template vars + table panel consistency.
* V28: full singlestat/stat parity, mappings & color.
* V30–V36: threshold logic, empty refs, nested targets.
* **Save-model cleanup**: replicate frontend defaults/filtering, drop null IDs, metadata, unused props.
* **Testing**: unified suites, dev dashboards (v42), full unit coverage for major migrations.
Co-authored-by: Ivan Ortega [ivanortegaalba@gmail.com](mailto:ivanortegaalba@gmail.com)
Co-authored-by: Dominik Prokop [dominik.prokop@grafana.com](mailto:dominik.prokop@grafana.com)
120 lines
2.6 KiB
Go
120 lines
2.6 KiB
Go
package schemaversion
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
)
|
|
|
|
// V21 migrates data links to replace __series.labels with __field.labels.
|
|
// This migration updates the variable syntax used in data links from the old series-based
|
|
// syntax to the new field-based syntax.
|
|
//
|
|
// Example before migration:
|
|
//
|
|
// "panels": [
|
|
// {
|
|
// "options": {
|
|
// "dataLinks": [
|
|
// {
|
|
// "url": "http://example.com?series=${__series.labels}&${__series.labels.a}"
|
|
// }
|
|
// ],
|
|
// "fieldOptions": {
|
|
// "defaults": {
|
|
// "links": [
|
|
// {
|
|
// "url": "http://example.com?series=${__series.labels}&${__series.labels.x}"
|
|
// }
|
|
// ]
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// ]
|
|
//
|
|
// Example after migration:
|
|
//
|
|
// "panels": [
|
|
// {
|
|
// "options": {
|
|
// "dataLinks": [
|
|
// {
|
|
// "url": "http://example.com?series=${__field.labels}&${__field.labels.a}"
|
|
// }
|
|
// ],
|
|
// "fieldOptions": {
|
|
// "defaults": {
|
|
// "links": [
|
|
// {
|
|
// "url": "http://example.com?series=${__field.labels}&${__field.labels.x}"
|
|
// }
|
|
// ]
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// ]
|
|
func V21(_ context.Context, dashboard map[string]interface{}) error {
|
|
dashboard["schemaVersion"] = 21
|
|
|
|
panels, ok := dashboard["panels"].([]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
for _, p := range panels {
|
|
panel, ok := p.(map[string]interface{})
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
// Update data links in panel options
|
|
if options, ok := panel["options"].(map[string]interface{}); ok {
|
|
updateDataLinks(options)
|
|
updateFieldOptionsLinks(options)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func updateDataLinks(options map[string]interface{}) {
|
|
dataLinks, ok := options["dataLinks"].([]interface{})
|
|
if !ok || !IsArray(dataLinks) {
|
|
return
|
|
}
|
|
|
|
for _, link := range dataLinks {
|
|
if linkMap, ok := link.(map[string]interface{}); ok {
|
|
if url, ok := linkMap["url"].(string); ok {
|
|
linkMap["url"] = strings.ReplaceAll(url, "__series.labels", "__field.labels")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func updateFieldOptionsLinks(options map[string]interface{}) {
|
|
fieldOptions, ok := options["fieldOptions"].(map[string]interface{})
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
defaults, ok := fieldOptions["defaults"].(map[string]interface{})
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
links, ok := defaults["links"].([]interface{})
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
for _, link := range links {
|
|
if linkMap, ok := link.(map[string]interface{}); ok {
|
|
if url, ok := linkMap["url"].(string); ok {
|
|
linkMap["url"] = strings.ReplaceAll(url, "__series.labels", "__field.labels")
|
|
}
|
|
}
|
|
}
|
|
}
|