Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 109 additions & 67 deletions statchart/schemas/migrate/migrate.cue
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
package migrate

import (
"list"
"strings"
commonMigrate "github.com/perses/perses/cue/common/migrate"
)
import "list"

#grafanaType: "stat"
#panel: _
Expand All @@ -34,10 +35,21 @@ import "list"
kind: "StatChart"
spec: {
calculation: *commonMigrate.#mapping.calc[#panel.options.reduceOptions.calcs[0]] | commonMigrate.#defaultCalc // only consider [0] here as Perses's GaugeChart doesn't support individual calcs
#metricLabel: *#panel.options.reduceOptions.fields | null
if #metricLabel != null {
metricLabel: #metricLabel

// metricLabel
#textMode: *#panel.options.textMode | null
if #textMode == "name" && (*#panel.targets[0].legendFormat | null) != null {
// /!\ best effort logic
// - if legendFormat contains a more complex expression than {{label}}, the result will be broken (but manually fixable afterwards still)
// - Perses's metricLabel is a single setting at panel level, hence the [0], so the result wont fit in case of multiple queries using different legendFormat
metricLabel: strings.Trim(#panel.targets[0].legendFormat, "{}")
}
// /!\ here too using [0] thus not perfect, even though the field getting remapped is unique to the whole panel in that case
if #textMode == "auto" && (*#panel.targets[0].format | null) == "table" && (*#panel.options.reduceOptions.fields | null) != null {
metricLabel: strings.Trim(#panel.options.reduceOptions.fields, "/^$")
}

// format
#unit: *commonMigrate.#mapping.unit[#panel.fieldConfig.defaults.unit] | null
if #unit != null {
format: unit: #unit
Expand All @@ -52,11 +64,13 @@ spec: {
}
}

// valueFontSize
#fontsize: *#panel.options.text.valueSize | null
if #fontsize != null {
valueFontSize: #fontsize
}
if #fontsize != null {
valueFontSize: #fontsize
}

// thresholds
#steps: *#panel.fieldConfig.defaults.thresholds.steps | null
if #steps != null {
thresholds: {
Expand All @@ -71,79 +85,107 @@ spec: {
}
}

// sparkline
#sparkline: *#panel.options.graphMode | "none"
if #sparkline == "area" {
sparkline: {}
}

// Using flatten to avoid having an array of arrays with "value" mappings
// (https://cuelang.org/docs/howto/use-list-flattenn-to-flatten-lists/)
let x = list.FlattenN([
if (*#panel.fieldConfig.defaults.mappings | null) != null for mapping in #panel.fieldConfig.defaults.mappings {
if mapping.type == "value" {
[for key, option in mapping.options {
{
kind: "Value"
spec: {
value: key
result: {
if option.text != _|_ {
value: option.text
}
if option.color != _|_ {
color: *commonMigrate.#mapping.color[option.color] | option.color
// mappings
#mappings: list.Concat([
// Using flatten to get rid of the nested array for "value" mappings
// (https://cuelang.org/docs/howto/use-list-flattenn-to-flatten-lists/)
list.FlattenN([
for mapping in (*#panel.fieldConfig.defaults.mappings | []) {
if mapping.type == "value" {
[for key, option in mapping.options {
{
kind: "Value"
spec: {
value: key
result: {
if option.text != _|_ {
value: option.text
}
if option.color != _|_ {
color: *commonMigrate.#mapping.color[option.color] | option.color
}
}
}
}
}

}]
}

if mapping.type == "range" || mapping.type == "regex" || mapping.type == "special" {
#result: {
value: *mapping.options.result.text | ""
if mapping.options.result.color != _|_ {
color: *commonMigrate.#mapping.color[mapping.options.result.color] | mapping.options.result.color
}
}]
}
[//switch
if mapping.type == "range" {
kind: "Range"
spec: {
if mapping.options.from != _|_ {
from: mapping.options.from
if mapping.type != "value" { // else
#result: {
value: *mapping.options.result.text | ""
if mapping.options.result.color != _|_ {
color: *commonMigrate.#mapping.color[mapping.options.result.color] | mapping.options.result.color
}
}
[//switch
if mapping.type == "range" {
kind: "Range"
spec: {
if mapping.options.from != _|_ {
from: mapping.options.from
}
if mapping.options.to != _|_ {
to: mapping.options.to
}
result: #result
}
if mapping.options.to != _|_ {
to: mapping.options.to
},
if mapping.type == "regex" {
kind: "Regex"
spec: {
pattern: mapping.options.pattern
result: #result
}
result: #result
}
},
if mapping.type == "regex" {
kind: "Regex"
spec: {
pattern: mapping.options.pattern
result: #result
}
},
if mapping.type == "special" {
kind: "Misc"
},
if mapping.type == "special" {
kind: "Misc"
spec: {
value: [//switch
if mapping.options.match == "nan" {"NaN"},
if mapping.options.match == "null+nan" {"null"},
mapping.options.match,
][0]
result: #result
}
},
][0]
}
}
], 1),
for override in (*#panel.fieldConfig.overrides | [])
if override.matcher.id == "byName" && override.matcher.options == "Value"
for property in override.properties
if property.id == "mappings" {
list.FlattenN([
for v in property.value {
if v.type == "value" {
[for oK, oV in v.options {
kind: "Value"
spec: {
result: value: oV.text
value: oK
}
}]
}
if v.type == "range" {
kind: "Range"
spec: {
value: [//switch
if mapping.options.match == "nan" {"NaN"},
if mapping.options.match == "null+nan" {"null"},
mapping.options.match,
][0]
result: #result
result: value: v.options.result.text
from: v.options.from
to: v.options.to
}
},
][0]
}
},
], 1)

if len(x) > 0 {
mappings: x
}
}
], 1),
}
])
if len(#mappings) > 0 {
mappings: #mappings
}
}
1 change: 0 additions & 1 deletion statchart/schemas/migrate/tests/basic/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"decimalPlaces": 2,
"unit": "bytes/sec"
},
"metricLabel": "",
"sparkline": {},
"thresholds": {
"steps": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"kind": "StatChart",
"spec": {
"metricLabel": "version",
"format": {
"unit": "decimal"
},
"calculation": "mean"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"id": 9,
"type": "stat",
"title": "Exporter version [$application - $environment - $nodename]",
"description": " - {{version}}: Node Exporter version used",
"gridPos": {
"x": 6,
"y": 5,
"h": 3,
"w": 3
},
"fieldConfig": {
"defaults": {
"mappings": [],
"color": {
"mode": "thresholds"
},
"links": [],
"unit": "none"
},
"overrides": []
},
"pluginVersion": "11.6.3",
"targets": [
{
"exemplar": false,
"expr": "node_exporter_build_info{osname=~\".*Linux.*\", application=~\"$application\", environment=~\"$environment\", nodename=~\"$nodename\", instance=~\".*:50700\"}",
"format": "time_series",
"hide": false,
"instant": true,
"interval": "$sampling",
"intervalFactor": 1,
"legendFormat": "{{version}}",
"refId": "version_linux",
"datasource": {
"uid": "$datasource"
},
"editorMode": "code"
}
],
"datasource": {
"uid": "$datasource"
},
"options": {
"reduceOptions": {
"values": false,
"calcs": [
"mean"
],
"fields": ""
},
"orientation": "auto",
"textMode": "name",
"wideLayout": true,
"colorMode": "background",
"graphMode": "none",
"justifyMode": "auto",
"showPercentChange": false,
"percentChangeColorMode": "standard"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"kind": "StatChart",
"spec": {
"metricLabel": "version",
"format": {
"unit": "decimal"
},
"calculation": "last-number"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"id": 9,
"type": "stat",
"title": "Exporter version [$application - $environment - $nodename]",
"description": " - {{version}}: Node Exporter version used",
"gridPos": {
"x": 6,
"y": 5,
"h": 3,
"w": 3
},
"fieldConfig": {
"defaults": {
"mappings": [],
"color": {
"mode": "thresholds"
},
"links": [],
"unit": "none"
},
"overrides": []
},
"pluginVersion": "11.6.3",
"targets": [
{
"exemplar": false,
"expr": "node_exporter_build_info{osname=~\".*Linux.*\", application=~\"$application\", environment=~\"$environment\", nodename=~\"$nodename\", instance=~\".*:50700\"}",
"format": "table",
"hide": false,
"instant": true,
"interval": "$sampling",
"intervalFactor": 1,
"legendFormat": "DO NOT USE ME",
"refId": "version_linux",
"datasource": {
"uid": "$datasource"
},
"editorMode": "code"
}
],
"datasource": {
"uid": "$datasource"
},
"options": {
"reduceOptions": {
"values": false,
"calcs": [
"lastNotNull"
],
"fields": "/^version$/"
},
"orientation": "auto",
"textMode": "auto",
"wideLayout": true,
"colorMode": "background",
"graphMode": "none",
"justifyMode": "auto",
"showPercentChange": false,
"percentChangeColorMode": "standard"
}
}
2 changes: 1 addition & 1 deletion statchart/schemas/migrate/tests/mappings/input.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"datasource": "$datasource",
"description": "Just displaying some metrics with Stat panel",
"description": "Test the migration of Grafana mappings to Perses mappings",
"fieldConfig": {
"defaults": {
"mappings": [
Expand Down
Loading