Skip to content

Commit 157ab00

Browse files
Merge pull request #12 from Tenzer/templated-dashboards
Add support for templated dashboards
2 parents cbebda6 + 4ac7500 commit 157ab00

3 files changed

Lines changed: 32 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ hubot>> Graphite Carbon Metrics: http://play.grafana.org/render/dashboard/solo/g
4646
- `hubot graf db graphite-carbon-metrics now-12hr` - Get a dashboard with a window of 12 hours ago to now
4747
- `hubot graf db graphite-carbon-metrics now-24hr now-12hr` - Get a dashboard with a window of 24 hours ago to 12 hours ago
4848
- `hubot graf db graphite-carbon-metrics:3 now-8d now-1d` - Get only the third panel of a particular dashboard with a window of 8 days ago to yesterday
49+
- `hubot graf db graphite-carbon-metrics host=carbon-a` - Get a templated dashboard with the `$host` parameter set to `carbon-a`
4950
- `hubot graf list` - Lists the available dashboards

src/grafana.coffee

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,42 @@ module.exports = (robot) ->
2121
grafana_host = process.env.HUBOT_GRAFANA_HOST
2222
grafana_api_key = process.env.HUBOT_GRAFANA_API_KEY
2323

24-
robot.respond /(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(| [A-Za-z0-9\-\+]+)?(| [A-Za-z0-9\-\+]+)?/i, (msg) ->
24+
robot.respond /(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(.*)?/i, (msg) ->
2525

2626
slug = msg.match[1].trim()
27-
from = msg.match[2] || 'now-6h'
28-
to = msg.match[3] || 'now'
29-
pid = false
27+
remainder = msg.match[2]
28+
timespan = {
29+
from: 'now-6h'
30+
to: 'now'
31+
}
32+
variables = ""
33+
pid = false
3034

3135
# Parse out a specific panel
3236
if /\:/.test slug
3337
parts = slug.split(':')
3438
slug = parts[0]
3539
pid = parseInt parts[1], 10
3640

41+
# Check if we have any extra fields
42+
if remainder
43+
# The order we apply non-variables in
44+
timeFields = ["from", "to"]
45+
46+
for part in remainder.trim().split " "
47+
48+
# Check if it's a variable or part of the timespan
49+
if part.indexOf("=") >= 0
50+
variables = "#{variables}&var-#{part}"
51+
52+
# Only add to the timespan if we haven't already filled out from and to
53+
else if timeFields.length > 0
54+
timespan[timeFields.shift()] = part.trim()
55+
3756
robot.logger.debug msg.match
3857
robot.logger.debug slug
39-
robot.logger.debug from
40-
robot.logger.debug to
58+
robot.logger.debug timespan
59+
robot.logger.debug variables
4160
robot.logger.debug pid
4261

4362
# Call the API to get information about this dashboard
@@ -77,12 +96,8 @@ module.exports = (robot) ->
7796
if pid && pid != panel.id
7897
continue
7998

80-
# Clean up attributes
81-
from = from.trim()
82-
to = to.trim()
83-
84-
imageUrl = "#{grafana_host}/render/#{apiEndpoint}/db/#{slug}/?panelId=#{panel.id}&width=1000&height=500&from=#{from}&to=#{to}"
85-
link = "#{grafana_host}/dashboard/db/#{slug}/?panelId=#{panel.id}&fullscreen&from=#{from}&to=#{to}"
99+
imageUrl = "#{grafana_host}/render/#{apiEndpoint}/db/#{slug}/?panelId=#{panel.id}&width=1000&height=500&from=#{timespan.from}&to=#{timespan.to}#{variables}"
100+
link = "#{grafana_host}/dashboard/db/#{slug}/?panelId=#{panel.id}&fullscreen&from=#{timespan.from}&to=#{timespan.to}#{variables}"
86101
msg.send "#{formatTitleWithTemplate(panel.title, template_map)}: #{imageUrl} - #{link}"
87102

88103
robot.respond /(?:grafana|graph|graf) list/i, (msg) ->

test/grafana-test.coffee

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ describe 'grafana', ->
1313
require('../src/grafana')(@robot)
1414

1515
it 'registers a dashboard listener', ->
16-
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(| [A-Za-z0-9\-\+]+)?(| [A-Za-z0-9\-\+]+)?/i)
16+
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(.*)?/i)
17+
18+
it 'registers a list listener', ->
19+
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) list/i)

0 commit comments

Comments
 (0)