Skip to content

Commit 0683f92

Browse files
[ENHANCEMENT] TimeSeriesChart: migration: support lineStyle & opacity override (#387)
* [ENHANCEMENT] TimeSeriesChart: migration: support lineStyle & opacity overrides Signed-off-by: Antoine THEBAUD <antoine.thebaud@yahoo.fr> * migration logic for lineStyle Signed-off-by: Antoine THEBAUD <antoine.thebaud@yahoo.fr> * fix for lineStyle override migration Signed-off-by: Antoine THEBAUD <antoine.thebaud@yahoo.fr> * fix for opacity override Signed-off-by: Antoine THEBAUD <antoine.thebaud@yahoo.fr> --------- Signed-off-by: Antoine THEBAUD <antoine.thebaud@yahoo.fr>
1 parent 36220cb commit 0683f92

8 files changed

Lines changed: 68 additions & 26 deletions

File tree

timeserieschart/schemas/migrate/migrate.cue

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ import (
2121
#grafanaType: "timeseries" | "graph"
2222
#panel: _
2323

24+
// key: grafana line style, value: perses line style
25+
#lineStyleMapping: {
26+
solid: "solid"
27+
dash: "dashed"
28+
dot: "dotted"
29+
}
30+
2431
kind: "TimeSeriesChart"
2532
spec: {
2633
// legend
@@ -134,6 +141,11 @@ spec: {
134141
}
135142
}
136143

144+
#lineStyle: *#panel.fieldConfig.defaults.custom.lineStyle.fill | null
145+
if #lineStyle != null {
146+
visual: lineStyle: #lineStyleMapping[#lineStyle]
147+
}
148+
137149
#fillOpacity: *#panel.fieldConfig.defaults.custom.fillOpacity | null
138150
if #fillOpacity != null {
139151
visual: areaOpacity: #fillOpacity / 100
@@ -160,18 +172,28 @@ spec: {
160172

161173
// migrate fixedColor overrides to querySettings when applicable
162174
#querySettings: [
163-
for override in (*#panel.fieldConfig.overrides | [])
164-
if (override.matcher.id == "byName" || override.matcher.id == "byRegexp") && override.matcher.options != _|_
165-
for property in override.properties
166-
if (*property.value.fixedColor | null) != null
167-
for i, target in (*#panel.targets | [])
168-
if target.legendFormat == override.matcher.options || target.legendFormat =~ strings.Trim(override.matcher.options, "/") {
175+
for i, target in (*#panel.targets | []) {
169176
queryIndex: i
170-
colorMode: "fixed"
171-
colorValue: property.value.fixedColor
177+
for override in (*#panel.fieldConfig.overrides | [])
178+
if (override.matcher.id == "byName" || override.matcher.id == "byRegexp") && override.matcher.options != _|_
179+
for property in override.properties
180+
if target.legendFormat == override.matcher.options || target.legendFormat =~ strings.Trim(override.matcher.options, "/") {
181+
if property.id == "color" if (*property.value.fixedColor | null) != null {
182+
colorMode: "fixed"
183+
colorValue: property.value.fixedColor
184+
}
185+
if property.id == "custom.lineStyle" if (*property.value.fill | null) != null {
186+
lineStyle: #lineStyleMapping[property.value.fill]
187+
}
188+
if property.id == "custom.fillOpacity" {
189+
areaOpacity: property.value / 100
190+
}
191+
}
172192
}
173193
]
174-
if len(#querySettings) != 0 {
175-
querySettings: #querySettings
194+
// don't keep elements that just define the queryIndex
195+
#querySettingsFiltered: [for qs in #querySettings if len(qs) > 1 { qs }]
196+
if len(#querySettingsFiltered) != 0 {
197+
querySettings: #querySettingsFiltered
176198
}
177199
}

timeserieschart/schemas/migrate/tests/basic-3/expected.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"areaOpacity": 0.1,
1111
"connectNulls": false,
1212
"display": "line",
13-
"lineWidth": 1
13+
"lineWidth": 1,
14+
"lineStyle": "dotted"
1415
},
1516
"yAxis": {
1617
"format": {

timeserieschart/schemas/migrate/tests/basic-3/input.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
},
4242
"thresholdsStyle": {
4343
"mode": "off"
44+
},
45+
"lineStyle": {
46+
"fill": "dot",
47+
"dash": [
48+
0,
49+
10
50+
]
4451
}
4552
},
4653
"color": {

timeserieschart/schemas/migrate/tests/basic/expected.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"areaOpacity": 0.1,
1111
"connectNulls": false,
1212
"display": "line",
13-
"lineWidth": 1
13+
"lineWidth": 1,
14+
"lineStyle": "solid"
1415
},
1516
"yAxis": {
1617
"format": {

timeserieschart/schemas/migrate/tests/basic/input.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"insertNulls": false,
2525
"lineInterpolation": "linear",
2626
"lineWidth": 1,
27+
"lineStyle": {
28+
"fill": "solid"
29+
},
2730
"pointSize": 5,
2831
"scaleDistribution": {
2932
"type": "linear"

timeserieschart/schemas/migrate/tests/byregexp-colors/expected.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@
2525
},
2626
"querySettings": [
2727
{
28-
"queryIndex": 2,
28+
"queryIndex": 1,
2929
"colorMode": "fixed",
30-
"colorValue": "#F2495C"
30+
"colorValue": "#5794F2",
31+
"lineStyle": "dashed",
32+
"areaOpacity": 0
3133
},
3234
{
33-
"queryIndex": 1,
35+
"queryIndex": 2,
3436
"colorMode": "fixed",
35-
"colorValue": "#5794F2"
37+
"colorValue": "#F2495C",
38+
"areaOpacity": 0
3639
}
3740
]
3841
}

timeserieschart/schemas/migrate/tests/color-based-on-legend-text/expected.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77
"values": []
88
},
99
"querySettings": [
10-
{
11-
"queryIndex": 2,
12-
"colorMode": "fixed",
13-
"colorValue": "#890F02"
14-
},
15-
{
16-
"queryIndex": 5,
17-
"colorMode": "fixed",
18-
"colorValue": "#052B51"
19-
},
2010
{
2111
"queryIndex": 0,
2212
"colorMode": "fixed",
@@ -27,10 +17,21 @@
2717
"colorMode": "fixed",
2818
"colorValue": "#0A437C"
2919
},
20+
{
21+
"queryIndex": 2,
22+
"colorMode": "fixed",
23+
"colorValue": "#890F02",
24+
"areaOpacity": 1
25+
},
3026
{
3127
"queryIndex": 4,
3228
"colorMode": "fixed",
3329
"colorValue": "#6D1F62"
30+
},
31+
{
32+
"queryIndex": 5,
33+
"colorMode": "fixed",
34+
"colorValue": "#052B51"
3435
}
3536
],
3637
"visual": {

timeserieschart/schemas/migrate/tests/color-based-on-legend-text/input.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
"fixedColor": "#890F02",
7575
"mode": "fixed"
7676
}
77+
},
78+
{
79+
"id": "custom.fillOpacity",
80+
"value": 100
7781
}
7882
]
7983
},

0 commit comments

Comments
 (0)