Search: Handle special datasource types (#52446)

This commit is contained in:
Artur Wierzbicki 2022-07-23 05:10:53 +04:00 committed by GitHub
parent bcfa4fabf6
commit 17ea5f4f3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 1173 additions and 19 deletions

View File

@ -261,6 +261,8 @@ func ReadDashboard(stream io.Reader, lookup DatasourceLookup) (*DashboardInfo, e
}
replaceDatasourceVariables(dash, datasourceVariablesLookup)
fillDefaultDatasources(dash, lookup)
filterOutSpecialDatasources(dash)
targets := newTargetInfo(lookup)
for _, panel := range dash.Panels {
@ -271,6 +273,45 @@ func ReadDashboard(stream io.Reader, lookup DatasourceLookup) (*DashboardInfo, e
return dash, iter.Error
}
func panelRequiresDatasource(panel PanelInfo) bool {
return panel.Type != "row"
}
func fillDefaultDatasources(dash *DashboardInfo, lookup DatasourceLookup) {
for i, panel := range dash.Panels {
if len(panel.Datasource) != 0 || !panelRequiresDatasource(panel) {
continue
}
defaultDs := lookup.ByRef(nil)
if defaultDs != nil {
dash.Panels[i].Datasource = []DataSourceRef{*defaultDs}
}
}
}
func filterOutSpecialDatasources(dash *DashboardInfo) {
for i, panel := range dash.Panels {
var dsRefs []DataSourceRef
// partition into actual datasource references and variables
for _, ds := range panel.Datasource {
switch ds.UID {
case "-- Mixed --":
// The actual datasources used as targets will remain
continue
case "-- Dashboard --":
// The `Dashboard` datasource refers to the results of the query used in another panel
continue
default:
dsRefs = append(dsRefs, ds)
}
}
dash.Panels[i].Datasource = dsRefs
}
}
func replaceDatasourceVariables(dash *DashboardInfo, datasourceVariablesLookup *datasourceVariableLookup) {
for i, panel := range dash.Panels {
var dsVariableRefs []DataSourceRef

View File

@ -59,6 +59,9 @@ func TestReadDashboard(t *testing.T) {
"all-selected-multi-datasource-variable",
"all-selected-single-datasource-variable",
"repeated-datasource-variables-with-default",
"mixed-datasource-with-variable",
"special-datasource-types",
"panels-without-datasources",
}
devdash := "../../../../devenv/dev-dashboards/"

View File

@ -10,18 +10,36 @@
"query1",
"text"
],
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
],
"panels": [
{
"id": 34,
"title": "",
"type": "text",
"pluginVersion": "8.1.0-pre"
"pluginVersion": "8.1.0-pre",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 35,
"title": "",
"type": "text",
"pluginVersion": "8.1.0-pre"
"pluginVersion": "8.1.0-pre",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 32,
@ -31,87 +49,183 @@
{
"id": 41,
"title": "State timeline",
"type": "state-timeline"
"type": "state-timeline",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 62,
"title": "Size, color mapped to different fields + share view",
"type": "geomap"
"type": "geomap",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 4,
"title": "Histogram",
"type": "histogram"
"type": "histogram",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 28,
"title": "Logs",
"type": "logs"
"type": "logs",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 8,
"title": "Dashboard list",
"type": "dashlist",
"pluginVersion": "8.1.0-pre"
"pluginVersion": "8.1.0-pre",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 6,
"title": "Alert list",
"type": "alertlist"
"type": "alertlist",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 26,
"title": "Heatmap",
"type": "heatmap"
"type": "heatmap",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 20,
"title": "Bar gauge",
"type": "bargauge",
"pluginVersion": "8.1.0-pre"
"pluginVersion": "8.1.0-pre",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 24,
"title": "Pie chart",
"type": "piechart"
"type": "piechart",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 18,
"title": "Gauge",
"type": "gauge",
"pluginVersion": "8.1.0-pre"
"pluginVersion": "8.1.0-pre",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 22,
"title": "Tabel",
"type": "table",
"pluginVersion": "8.1.0-pre"
"pluginVersion": "8.1.0-pre",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 10,
"title": "Annotation list",
"type": "annolist"
"type": "annolist",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 16,
"title": "Stat",
"type": "stat",
"pluginVersion": "8.1.0-pre"
"pluginVersion": "8.1.0-pre",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 2,
"title": "Graph NG",
"type": "timeseries"
"type": "timeseries",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 14,
"title": "Bar chart",
"type": "barchart"
"type": "barchart",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 12,
"title": "News panel",
"type": "news"
"type": "news",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
}
],
"schemaVersion": 33,

View File

@ -5,12 +5,24 @@
"templateVars": [
"sqllite"
],
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
],
"panels": [
{
"id": 1,
"title": "usersss!",
"type": "table",
"pluginVersion": "9.1.0-pre"
"pluginVersion": "9.1.0-pre",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
}
],
"schemaVersion": 36,

View File

@ -0,0 +1,49 @@
{
"id": 220,
"title": "new-dashboard-mixed",
"tags": null,
"templateVars": [
"dsVariable"
],
"datasource": [
{
"uid": "gdev-testdata",
"type": "testdata"
},
{
"uid": "default.uid",
"type": "default.type"
},
{
"uid": "P8045C56BDA891CB2",
"type": "cloudwatch"
}
],
"panels": [
{
"id": 2,
"title": "Panel Title",
"type": "table",
"pluginVersion": "9.1.0-pre",
"datasource": [
{
"uid": "gdev-testdata",
"type": "testdata"
},
{
"uid": "default.uid",
"type": "default.type"
},
{
"uid": "P8045C56BDA891CB2",
"type": "cloudwatch"
}
]
}
],
"schemaVersion": 36,
"linkCount": 0,
"timeFrom": "now-6h",
"timeTo": "now",
"timezone": ""
}

View File

@ -0,0 +1,139 @@
{
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 220,
"iteration": 1658225888559,
"links": [],
"liveNow": false,
"panels": [
{
"datasource": {
"type": "datasource",
"uid": "-- Mixed --"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"custom": {
"align": "auto",
"displayMode": "auto",
"inspect": false
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 16,
"w": 12,
"x": 0,
"y": 0
},
"id": 2,
"options": {
"footer": {
"fields": "",
"reducer": [
"sum"
],
"show": false
},
"showHeader": true
},
"pluginVersion": "9.1.0-pre",
"repeat": "dsVariable",
"repeatDirection": "h",
"targets": [
{
"datasource": {
"type": "testdata",
"uid": "${dsVariable}"
},
"refId": "A"
},
{
"datasource": {
"type": "cloudwatch",
"uid": "P8045C56BDA891CB2"
},
"dimensions": {},
"expression": "",
"hide": false,
"id": "",
"label": "",
"matchExact": true,
"metricEditorMode": 0,
"metricName": "",
"metricQueryType": 0,
"namespace": "",
"period": "",
"queryMode": "Metrics",
"refId": "B",
"region": "default",
"sqlExpression": "",
"statistic": "Average"
}
],
"title": "Panel Title",
"type": "table"
}
],
"refresh": "",
"schemaVersion": 36,
"style": "dark",
"tags": [],
"templating": {
"list": [
{
"current": {
"selected": true,
"text": [
"gdev-testdata",
"default"
],
"value": [
"gdev-testdata",
"default"
]
},
"hide": 0,
"includeAll": false,
"multi": false,
"name": "dsVariable",
"options": [],
"query": "testdata",
"queryValue": "",
"refresh": 1,
"regex": "",
"skipUrlSync": false,
"type": "datasource"
}
]
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "new-dashboard-mixed",
"uid": "NV5PypgVz",
"version": 2,
"weekStart": ""
}

View File

@ -0,0 +1,54 @@
{
"title": "Datasource tests - Shared Queries",
"tags": [
"gdev",
"datasource-test"
],
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
],
"panels": [
{
"id": 2,
"title": "Raw Data Graph",
"type": "graph",
"datasource": [
{
"uid": "default.uid",
"type": "default.type"
}
]
},
{
"id": 4,
"title": "Last non-null",
"type": "gauge",
"pluginVersion": "6.4.0-pre"
},
{
"id": 6,
"title": "min",
"type": "gauge",
"pluginVersion": "6.4.0-pre"
},
{
"id": 5,
"title": "Max",
"type": "bargauge",
"pluginVersion": "6.4.0-pre"
},
{
"id": 8,
"title": "Panel Title",
"type": "table"
}
],
"schemaVersion": 19,
"linkCount": 0,
"timeFrom": "now-6h",
"timeTo": "now",
"timezone": ""
}

View File

@ -0,0 +1,346 @@
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations \u0026 Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"links": [],
"panels": [
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"fill": 0,
"fillGradient": 6,
"gridPos": {
"h": 15,
"w": 12,
"x": 0,
"y": 0
},
"id": 2,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"dataLinks": []
},
"percentage": false,
"pointradius": 2,
"points": true,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"refId": "A",
"scenarioId": "csv_metric_values",
"stringInput": "1,20,90,30,5,0,100"
},
{
"refId": "B",
"scenarioId": "csv_metric_values",
"stringInput": "1,20,90,30,5,-100,200"
},
{
"refId": "C",
"scenarioId": "csv_metric_values",
"stringInput": "2.5,3.5,4.5,10.5,20.5,21.5,19.5"
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Raw Data Graph",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"datasource": "-- Dashboard --",
"gridPos": {
"h": 5,
"w": 12,
"x": 12,
"y": 0
},
"id": 4,
"options": {
"fieldOptions": {
"calcs": [
"lastNotNull"
],
"defaults": {
"mappings": [],
"max": 100,
"min": 0,
"thresholds": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"override": {},
"values": false
},
"orientation": "auto",
"showThresholdLabels": false,
"showThresholdMarkers": true
},
"pluginVersion": "6.4.0-pre",
"targets": [
{
"panelId": 2,
"refId": "A"
}
],
"timeFrom": null,
"timeShift": null,
"title": "Last non-null",
"type": "gauge"
},
{
"datasource": "-- Dashboard --",
"gridPos": {
"h": 5,
"w": 12,
"x": 12,
"y": 5
},
"id": 6,
"options": {
"fieldOptions": {
"calcs": [
"min"
],
"defaults": {
"mappings": [],
"max": 100,
"min": 0,
"thresholds": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"override": {},
"values": false
},
"orientation": "auto",
"showThresholdLabels": false,
"showThresholdMarkers": true
},
"pluginVersion": "6.4.0-pre",
"targets": [
{
"panelId": 2,
"refId": "A"
}
],
"timeFrom": null,
"timeShift": null,
"title": "min",
"type": "gauge"
},
{
"datasource": "-- Dashboard --",
"gridPos": {
"h": 5,
"w": 12,
"x": 12,
"y": 10
},
"id": 5,
"options": {
"displayMode": "basic",
"fieldOptions": {
"calcs": [
"max"
],
"defaults": {
"mappings": [],
"max": 200,
"min": 0,
"thresholds": [
{
"color": "green",
"value": null
},
{
"color": "blue",
"value": 40
},
{
"color": "red",
"value": 120
}
]
},
"override": {},
"values": false
},
"orientation": "vertical"
},
"pluginVersion": "6.4.0-pre",
"targets": [
{
"panelId": 2,
"refId": "A"
}
],
"timeFrom": null,
"timeShift": null,
"title": "Max",
"type": "bargauge"
},
{
"columns": [],
"datasource": "-- Dashboard --",
"fontSize": "100%",
"gridPos": {
"h": 10,
"w": 24,
"x": 0,
"y": 15
},
"id": 8,
"options": {},
"pageSize": null,
"showHeader": true,
"sort": {
"col": 0,
"desc": true
},
"styles": [
{
"alias": "Time",
"dateFormat": "YYYY-MM-DD HH:mm:ss",
"pattern": "Time",
"type": "date"
},
{
"alias": "",
"colorMode": null,
"colors": [
"rgba(245, 54, 54, 0.9)",
"rgba(237, 129, 40, 0.89)",
"rgba(50, 172, 45, 0.97)"
],
"decimals": 2,
"pattern": "/.*/",
"thresholds": [],
"type": "number",
"unit": "short"
}
],
"targets": [
{
"panelId": 2,
"refId": "A"
}
],
"timeFrom": null,
"timeShift": null,
"title": "Panel Title",
"transform": "timeseries_to_columns",
"type": "table"
}
],
"schemaVersion": 19,
"style": "dark",
"tags": [
"gdev",
"datasource-test"
],
"templating": {
"list": []
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
]
},
"timezone": "",
"title": "Datasource tests - Shared Queries",
"uid": "ZqZnVvFZz",
"version": 1
}

View File

@ -0,0 +1,77 @@
{
"id": 221,
"title": "special ds",
"tags": null,
"datasource": [
{
"uid": "grafana",
"type": "datasource"
},
{
"uid": "dgd92lq7k",
"type": "frser-sqlite-datasource"
},
{
"uid": "PD8C576611E62080A",
"type": "testdata"
}
],
"panels": [
{
"id": 10,
"title": "mixed ds with grafana ds",
"type": "timeseries",
"datasource": [
{
"uid": "grafana",
"type": "datasource"
},
{
"uid": "dgd92lq7k",
"type": "frser-sqlite-datasource"
}
]
},
{
"id": 8,
"title": "Row title",
"type": "row"
},
{
"id": 4,
"title": "dashboard ds",
"type": "timeseries"
},
{
"id": 6,
"title": "grafana ds",
"type": "timeseries",
"datasource": [
{
"uid": "grafana",
"type": "datasource"
}
]
},
{
"id": 2,
"title": "mixed ds",
"type": "timeseries",
"datasource": [
{
"uid": "dgd92lq7k",
"type": "frser-sqlite-datasource"
},
{
"uid": "PD8C576611E62080A",
"type": "testdata"
}
]
}
],
"schemaVersion": 36,
"linkCount": 0,
"timeFrom": "now-6h",
"timeTo": "now",
"timezone": ""
}

View File

@ -0,0 +1,310 @@
{
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 221,
"links": [],
"liveNow": false,
"panels": [
{
"datasource": {
"type": "datasource",
"uid": "-- Mixed --"
},
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 0
},
"id": 10,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "datasource",
"uid": "grafana"
},
"refId": "A"
},
{
"datasource": {
"type": "frser-sqlite-datasource",
"uid": "dgd92lq7k"
},
"hide": false,
"queryText": "\n SELECT CAST(strftime('%s', 'now', '-1 minute') as INTEGER) as time, 4 as value\n WHERE time >= 1234 and time < 134567\n ",
"queryType": "table",
"rawQueryText": "SELECT CAST(strftime('%s', 'now', '-1 minute') as INTEGER) as time, 4 as value \nWHERE time >= $__from / 1000 and time < $__to / 1000",
"refId": "B",
"timeColumns": [
"time",
"ts"
]
}
],
"title": "mixed ds with grafana ds",
"type": "timeseries"
},
{
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 8
},
"id": 8,
"title": "Row title",
"type": "row"
},
{
"datasource": {
"type": "datasource",
"uid": "-- Dashboard --"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 9
},
"id": 4,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "datasource",
"uid": "-- Dashboard --"
},
"panelId": 2,
"refId": "A"
}
],
"title": "dashboard ds",
"type": "timeseries"
},
{
"datasource": {
"type": "grafana",
"uid": "grafana"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"y": 9
},
"id": 6,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "datasource",
"uid": "grafana"
},
"refId": "A"
}
],
"title": "grafana ds",
"type": "timeseries"
},
{
"datasource": {
"type": "datasource",
"uid": "-- Mixed --"
},
"gridPos": {
"h": 9,
"w": 12,
"x": 0,
"y": 17
},
"id": 2,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "testdata",
"uid": "PD8C576611E62080A"
},
"refId": "A"
},
{
"datasource": {
"type": "frser-sqlite-datasource",
"uid": "dgd92lq7k"
},
"hide": false,
"queryText": "\n SELECT CAST(strftime('%s', 'now', '-1 minute') as INTEGER) as time, 4 as value\n WHERE time >= 1234 and time < 134567\n ",
"queryType": "table",
"rawQueryText": "SELECT CAST(strftime('%s', 'now', '-1 minute') as INTEGER) as time, 4 as value \nWHERE time >= $__from / 1000 and time < $__to / 1000",
"refId": "B",
"timeColumns": [
"time",
"ts"
]
}
],
"title": "mixed ds",
"type": "timeseries"
}
],
"schemaVersion": 36,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "special ds",
"uid": "mocpwtR4k",
"version": 1,
"weekStart": ""
}

View File

@ -844,6 +844,15 @@ func createDatasourceLookup(rows []*datasourceQueryResult) extract.DatasourceLoo
byType[row.Type] = append(byType[row.Type], *ref)
}
if defaultDS == nil {
// fallback replicated from /pkg/api/frontendsettings.go
// https://github.com/grafana/grafana/blob/7ef21662f9ad74b80d832b9f2aa9db2fb4192741/pkg/api/frontendsettings.go#L51-L56
defaultDS = &extract.DataSourceRef{
UID: "grafana",
Type: "datasource",
}
}
return &dsLookup{
byName: byName,
byUID: byUID,