Skip to content

Commit

Permalink
Fix bug with template values not showing up in the title. Improved te…
Browse files Browse the repository at this point in the history
…st coverage. The test template dashboard now has a parameterized title for the Graph panel (Graph for $server).
  • Loading branch information
KeesCBakker committed Apr 24, 2024
1 parent bc88859 commit 46fc436
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 16 deletions.
10 changes: 3 additions & 7 deletions src/service/GrafanaService.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,9 @@ class GrafanaService {
if (!template.current) {
continue;
}
for (const p of req.template_map) {
if (template.name === p.name) {
templateMap[`$${template.name}`] = p.value;
} else {
templateMap[`$${template.name}`] = template.current.text;
}
}

const _param = req.template_params.find((param) => param.name === template.name);
templateMap[`$${template.name}`] = _param ? _param.value : template.current.text;
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/service/query/GrafanaDashboardRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,9 @@ class GrafanaDashboardRequest {
this.query = new GrafanaDashboardQuery();

/**
* @type {Array<string>}
* @type {Array<{name: string, value: string}>}
*/
this.template_params = [];

/**
* @type {Array<string>}
*/
this.template_map = [];
}
}

Expand Down
283 changes: 282 additions & 1 deletion test/fixtures/v8/dashboard-templating.json
Original file line number Diff line number Diff line change
@@ -1 +1,282 @@
{"meta":{"type":"db","canSave":false,"canEdit":true,"canAdmin":false,"canStar":false,"slug":"templating-showcase","url":"/d/000000091/templating-showcase","expires":"0001-01-01T00:00:00Z","created":"2017-01-20T06:38:55Z","updated":"2020-08-18T19:57:10Z","updatedBy":"diana.payton@grafana.com","createdBy":"carl@raintank.io","version":22,"hasAcl":false,"isFolder":false,"folderId":608,"folderUid":"PGJ1Fr4Zz","folderTitle":"Demos","folderUrl":"/dashboards/f/PGJ1Fr4Zz/demos","provisioned":false,"provisionedExternalId":""},"dashboard":{"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,"id":91,"iteration":1597780597713,"links":[],"panels":[{"aliasColors":{},"bars":false,"dashLength":10,"dashes":false,"datasource":"$db","fieldConfig":{"defaults":{"custom":{}},"overrides":[]},"fill":1,"fillGradient":0,"gridPos":{"h":7,"w":24,"x":0,"y":0},"hiddenSeries":false,"id":1,"legend":{"avg":false,"current":false,"max":false,"min":false,"show":true,"total":false,"values":false},"lines":true,"linewidth":1,"links":[],"nullPointMode":"connected","percentage":false,"pluginVersion":"7.1.3","pointradius":5,"points":false,"renderer":"flot","seriesOverrides":[],"spaceLength":10,"stack":false,"steppedLine":false,"targets":[{"refId":"A","target":"aliasByNode(apps.backend.$server.counters.requests.count, 2)","textEditor":false}],"thresholds":[],"timeFrom":null,"timeRegions":[],"timeShift":null,"title":"Graph","tooltip":{"msResolution":false,"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}},{"columns":[{"text":"Avg","value":"avg"},{"text":"Total","value":"total"}],"datasource":"$db","fieldConfig":{"defaults":{"custom":{}},"overrides":[]},"fontSize":"100%","gridPos":{"h":7,"w":16,"x":0,"y":7},"id":2,"links":[],"pageSize":null,"scroll":true,"showHeader":true,"sort":{"col":0,"desc":true},"styles":[{"align":"auto","dateFormat":"YYYY-MM-DD HH:mm:ss","pattern":"Time","type":"date"},{"align":"auto","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":[{"refId":"A","target":"aliasByNode(apps.backend.$server.counters.requests.count, 2)","textEditor":false}],"title":"Table","transform":"timeseries_aggregations","type":"table-old"},{"cacheTimeout":null,"datasource":"$db","fieldConfig":{"defaults":{"custom":{},"mappings":[{"id":0,"op":"=","text":"N/A","type":1,"value":"null"}],"nullValueMode":"connected","thresholds":{"mode":"absolute","steps":[{"color":"green","value":null},{"color":"red","value":80}]},"unit":"none"},"overrides":[]},"gridPos":{"h":7,"w":8,"x":16,"y":7},"id":3,"interval":null,"links":[],"maxDataPoints":100,"options":{"colorMode":"value","graphMode":"none","justifyMode":"auto","orientation":"horizontal","reduceOptions":{"calcs":["mean"],"fields":"/^sumSeries(apps.backend.{backend_01,backend_02,backend_03,backend_04}.counters.requests.count)$/","values":false},"textMode":"auto"},"pluginVersion":"7.1.3","targets":[{"refId":"A","target":"sumSeries(apps.backend.$server.counters.requests.count)"}],"title":"Stat","type":"stat"}],"schemaVersion":26,"style":"dark","tags":["graphite","templating"],"templating":{"list":[{"allValue":null,"current":{"text":"server1","value":"server1"},"hide":0,"includeAll":false,"label":null,"multi":false,"name":"custom","options":[{"selected":true,"text":"server1","value":"server1"},{"selected":false,"text":"server2","value":"server2"}],"query":"server1,server2","skipUrlSync":false,"type":"custom"},{"allValue":null,"current":{"tags":[],"text":"All","value":["$__all"]},"datasource":"graphite","definition":"","hide":0,"includeAll":true,"label":null,"multi":true,"name":"server","options":[{"selected":true,"text":"All","value":"$__all"},{"selected":false,"text":"backend_01","value":"backend_01"},{"selected":false,"text":"backend_02","value":"backend_02"},{"selected":false,"text":"backend_03","value":"backend_03"},{"selected":false,"text":"backend_04","value":"backend_04"}],"query":"apps.backend.*","refresh":0,"regex":"","skipUrlSync":false,"sort":0,"tagValuesQuery":null,"tags":[],"tagsQuery":null,"type":"query","useTags":false},{"allValue":null,"current":{"text":"All","value":"$__all"},"datasource":"graphite","definition":"","hide":0,"includeAll":true,"label":null,"multi":true,"name":"dependent","options":[{"selected":true,"text":"All","value":"$__all"},{"selected":false,"text":"counters","value":"counters"}],"query":"apps.backend.$server.*","refresh":0,"regex":"","skipUrlSync":false,"sort":0,"tagValuesQuery":null,"tags":[],"tagsQuery":null,"type":"query","useTags":false},{"current":{"tags":[],"text":"graphite","value":"graphite"},"hide":0,"includeAll":false,"label":null,"multi":false,"name":"db","options":[],"query":"graphite","refresh":1,"regex":"","skipUrlSync":false,"type":"datasource"}]},"time":{"from":"now-6h","to":"now"},"timepicker":{"refresh_intervals":["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"],"time_options":["5m","15m","1h","6h","12h","24h","2d","7d","30d"]},"timezone":"browser","title":"Templating showcase","uid":"000000091","version":22}}
{
"meta": {
"type": "db",
"canSave": false,
"canEdit": true,
"canAdmin": false,
"canStar": false,
"slug": "templating-showcase",
"url": "/d/000000091/templating-showcase",
"expires": "0001-01-01T00:00:00Z",
"created": "2017-01-20T06:38:55Z",
"updated": "2020-08-18T19:57:10Z",
"updatedBy": "diana.payton@grafana.com",
"createdBy": "carl@raintank.io",
"version": 22,
"hasAcl": false,
"isFolder": false,
"folderId": 608,
"folderUid": "PGJ1Fr4Zz",
"folderTitle": "Demos",
"folderUrl": "/dashboards/f/PGJ1Fr4Zz/demos",
"provisioned": false,
"provisionedExternalId": ""
},
"dashboard": {
"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,
"id": 91,
"iteration": 1597780597713,
"links": [],
"panels": [
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "$db",
"fieldConfig": { "defaults": { "custom": {} }, "overrides": [] },
"fill": 1,
"fillGradient": 0,
"gridPos": { "h": 7, "w": 24, "x": 0, "y": 0 },
"hiddenSeries": false,
"id": 1,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "connected",
"percentage": false,
"pluginVersion": "7.1.3",
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"refId": "A",
"target": "aliasByNode(apps.backend.$server.counters.requests.count, 2)",
"textEditor": false
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Graph for $server",
"tooltip": { "msResolution": false, "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 }
},
{
"columns": [
{ "text": "Avg", "value": "avg" },
{ "text": "Total", "value": "total" }
],
"datasource": "$db",
"fieldConfig": { "defaults": { "custom": {} }, "overrides": [] },
"fontSize": "100%",
"gridPos": { "h": 7, "w": 16, "x": 0, "y": 7 },
"id": 2,
"links": [],
"pageSize": null,
"scroll": true,
"showHeader": true,
"sort": { "col": 0, "desc": true },
"styles": [
{ "align": "auto", "dateFormat": "YYYY-MM-DD HH:mm:ss", "pattern": "Time", "type": "date" },
{
"align": "auto",
"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": [
{
"refId": "A",
"target": "aliasByNode(apps.backend.$server.counters.requests.count, 2)",
"textEditor": false
}
],
"title": "Table",
"transform": "timeseries_aggregations",
"type": "table-old"
},
{
"cacheTimeout": null,
"datasource": "$db",
"fieldConfig": {
"defaults": {
"custom": {},
"mappings": [{ "id": 0, "op": "=", "text": "N/A", "type": 1, "value": "null" }],
"nullValueMode": "connected",
"thresholds": {
"mode": "absolute",
"steps": [
{ "color": "green", "value": null },
{ "color": "red", "value": 80 }
]
},
"unit": "none"
},
"overrides": []
},
"gridPos": { "h": 7, "w": 8, "x": 16, "y": 7 },
"id": 3,
"interval": null,
"links": [],
"maxDataPoints": 100,
"options": {
"colorMode": "value",
"graphMode": "none",
"justifyMode": "auto",
"orientation": "horizontal",
"reduceOptions": {
"calcs": ["mean"],
"fields": "/^sumSeries(apps.backend.{backend_01,backend_02,backend_03,backend_04}.counters.requests.count)$/",
"values": false
},
"textMode": "auto"
},
"pluginVersion": "7.1.3",
"targets": [{ "refId": "A", "target": "sumSeries(apps.backend.$server.counters.requests.count)" }],
"title": "Stat",
"type": "stat"
}
],
"schemaVersion": 26,
"style": "dark",
"tags": ["graphite", "templating"],
"templating": {
"list": [
{
"allValue": null,
"current": { "text": "server1", "value": "server1" },
"hide": 0,
"includeAll": false,
"label": null,
"multi": false,
"name": "custom",
"options": [
{ "selected": true, "text": "server1", "value": "server1" },
{ "selected": false, "text": "server2", "value": "server2" }
],
"query": "server1,server2",
"skipUrlSync": false,
"type": "custom"
},
{
"allValue": null,
"current": { "tags": [], "text": "All", "value": ["$__all"] },
"datasource": "graphite",
"definition": "",
"hide": 0,
"includeAll": true,
"label": null,
"multi": true,
"name": "server",
"options": [
{ "selected": true, "text": "All", "value": "$__all" },
{ "selected": false, "text": "backend_01", "value": "backend_01" },
{ "selected": false, "text": "backend_02", "value": "backend_02" },
{ "selected": false, "text": "backend_03", "value": "backend_03" },
{ "selected": false, "text": "backend_04", "value": "backend_04" }
],
"query": "apps.backend.*",
"refresh": 0,
"regex": "",
"skipUrlSync": false,
"sort": 0,
"tagValuesQuery": null,
"tags": [],
"tagsQuery": null,
"type": "query",
"useTags": false
},
{
"allValue": null,
"current": { "text": "All", "value": "$__all" },
"datasource": "graphite",
"definition": "",
"hide": 0,
"includeAll": true,
"label": null,
"multi": true,
"name": "dependent",
"options": [
{ "selected": true, "text": "All", "value": "$__all" },
{ "selected": false, "text": "counters", "value": "counters" }
],
"query": "apps.backend.$server.*",
"refresh": 0,
"regex": "",
"skipUrlSync": false,
"sort": 0,
"tagValuesQuery": null,
"tags": [],
"tagsQuery": null,
"type": "query",
"useTags": false
},
{
"current": { "tags": [], "text": "graphite", "value": "graphite" },
"hide": 0,
"includeAll": false,
"label": null,
"multi": false,
"name": "db",
"options": [],
"query": "graphite",
"refresh": 1,
"regex": "",
"skipUrlSync": false,
"type": "datasource"
}
]
},
"time": { "from": "now-6h", "to": "now" },
"timepicker": {
"refresh_intervals": ["5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d"],
"time_options": ["5m", "15m", "1h", "6h", "12h", "24h", "2d", "7d", "30d"]
},
"timezone": "browser",
"title": "Templating showcase",
"uid": "000000091",
"version": 22
}
}
2 changes: 1 addition & 1 deletion test/grafana-v8-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('grafana v8', () => {
it('hubot should respond with a templated graph', async () => {
let response = await ctx.sendAndWaitForResponse('hubot graf db 000000091:graph server=backend_01 now-6h');
expect(response).to.eql(
'Graph: https://play.grafana.org/render/d-solo/000000091/?panelId=1&width=1000&height=500&from=now-6h&to=now&var-server=backend_01 - https://play.grafana.org/d/000000091/?panelId=1&fullscreen&from=now-6h&to=now&var-server=backend_01'
'Graph for backend_01: https://play.grafana.org/render/d-solo/000000091/?panelId=1&width=1000&height=500&from=now-6h&to=now&var-server=backend_01 - https://play.grafana.org/d/000000091/?panelId=1&fullscreen&from=now-6h&to=now&var-server=backend_01'
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/kiosk-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('retrieve dashboard graphs', function () {
it('should respond with multiple graphs without kiosk mode', async () => {
await ctx.sendAndWaitForResponse('hubot graf db 000000091');
expect(ctx.sends).to.eql([
'Graph: https://play.grafana.org/render/d-solo/000000091/?panelId=1&width=1000&height=500&from=now-6h&to=now - https://play.grafana.org/d/000000091/?panelId=1&fullscreen&from=now-6h&to=now',
'Graph for All: https://play.grafana.org/render/d-solo/000000091/?panelId=1&width=1000&height=500&from=now-6h&to=now - https://play.grafana.org/d/000000091/?panelId=1&fullscreen&from=now-6h&to=now',
'Table: https://play.grafana.org/render/d-solo/000000091/?panelId=2&width=1000&height=500&from=now-6h&to=now - https://play.grafana.org/d/000000091/?panelId=2&fullscreen&from=now-6h&to=now',
'Stat: https://play.grafana.org/render/d-solo/000000091/?panelId=3&width=1000&height=500&from=now-6h&to=now - https://play.grafana.org/d/000000091/?panelId=3&fullscreen&from=now-6h&to=now',
]);
Expand Down

0 comments on commit 46fc436

Please sign in to comment.