Skip to content

Commit c931571

Browse files
Merge pull request #22 from stephenyeargin/search-dashboards
Feature: Dashboard search
2 parents 268d53d + be6ccb2 commit c931571

4 files changed

Lines changed: 49 additions & 21 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/node_modules
2+
.node-version

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ hubot>> Graphite Carbon Metrics: http://play.grafana.org/render/dashboard/solo/g
5959
- `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
6060
- `hubot graf db graphite-carbon-metrics host=carbon-a` - Get a templated dashboard with the `$host` parameter set to `carbon-a`
6161
- `hubot graf list` - Lists the available dashboards
62+
- `hubot graf list production` - Lists all dashboards tagged `production`
63+
- `hubot graf search elb` - Search for dashboards that match `elb`

src/grafana.coffee

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#
2525
# Commands:
2626
# hubot graf db <dashboard slug>[:<panel id>][ <template variables>][ <from clause>][ <to clause>] - Show grafana dashboard graphs
27-
# hubot graf list - Lists all dashboards available
27+
# hubot graf list <tag> - Lists all dashboards available (optional: <tag>)
28+
# hubot graf search <keyword> - Search available dashboards by <keyword>
2829
#
2930

3031
crypto = require 'crypto'
@@ -142,31 +143,52 @@ module.exports = (robot) ->
142143
sendRobotResponse msg, title, imageUrl, link
143144

144145
# Get a list of available dashboards
145-
robot.respond /(?:grafana|graph|graf) list$/i, (msg) ->
146-
callGrafana 'search', (dashboards) ->
146+
robot.respond /(?:grafana|graph|graf) list\s?(.+)?/i, (msg) ->
147+
if msg.match[1]
148+
tag = msg.match[1].trim()
149+
callGrafana "search?tag=#{tag}", (dashboards) ->
150+
robot.logger.debug dashboards
151+
response = "Dashboards tagged `#{tag}`:\n"
152+
sendDashboardList dashboards, response, msg
153+
else
154+
callGrafana 'search', (dashboards) ->
155+
robot.logger.debug dashboards
156+
response = "Available dashboards:\n"
157+
sendDashboardList dashboards, response, msg
158+
159+
# Search dashboards
160+
robot.respond /(?:grafana|graph|graf) search (.+)/i, (msg) ->
161+
query = msg.match[2].trim()
162+
robot.logger.debug query
163+
callGrafana "search?query=#{query}", (dashboards) ->
147164
robot.logger.debug dashboards
148-
response = "Available dashboards:\n"
165+
response = "Dashboards matching `#{query}`:\n"
166+
sendDashboardList dashboards, response, msg
167+
168+
# Send Dashboard list
169+
sendDashboardList = (dashboards, response, msg) ->
170+
# Handle refactor done for version 2.0.2+
171+
if dashboards.dashboards
172+
list = dashboards.dashboards
173+
else
174+
list = dashboards
175+
176+
robot.logger.debug list
177+
unless list.length > 0
178+
return
149179

180+
for dashboard in list
150181
# Handle refactor done for version 2.0.2+
151-
if dashboards.dashboards
152-
list = dashboards.dashboards
182+
if dashboard.uri
183+
slug = dashboard.uri.replace /^db\//, ''
153184
else
154-
list = dashboards
155-
156-
robot.logger.debug list
157-
158-
for dashboard in list
159-
# Handle refactor done for version 2.0.2+
160-
if dashboard.uri
161-
slug = dashboard.uri.replace /^db\//, ''
162-
else
163-
slug = dashboard.slug
164-
response = response + "- #{slug}: #{dashboard.title}\n"
185+
slug = dashboard.slug
186+
response = response + "- #{slug}: #{dashboard.title}\n"
165187

166-
# Remove trailing newline
167-
response.trim()
188+
# Remove trailing newline
189+
response.trim()
168190

169-
msg.send response
191+
msg.send response
170192

171193
# Handle generic errors
172194
sendError = (message, msg) ->

test/grafana-test.coffee

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ describe 'grafana', ->
1616
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(.*)?/i)
1717

1818
it 'registers a list listener', ->
19-
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) list$/i)
19+
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) list\s?(.+)?/i)
20+
21+
it 'registers a search listener', ->
22+
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) search (.+)/i)

0 commit comments

Comments
 (0)