Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Dashboard search #22

Merged
merged 2 commits into from
Oct 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
.node-version
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ hubot>> Graphite Carbon Metrics: http://play.grafana.org/render/dashboard/solo/g
- `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
- `hubot graf db graphite-carbon-metrics host=carbon-a` - Get a templated dashboard with the `$host` parameter set to `carbon-a`
- `hubot graf list` - Lists the available dashboards
- `hubot graf list production` - Lists all dashboards tagged `production`
- `hubot graf search elb` - Search for dashboards that match `elb`
62 changes: 42 additions & 20 deletions src/grafana.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#
# Commands:
# hubot graf db <dashboard slug>[:<panel id>][ <template variables>][ <from clause>][ <to clause>] - Show grafana dashboard graphs
# hubot graf list - Lists all dashboards available
# hubot graf list <tag> - Lists all dashboards available (optional: <tag>)
# hubot graf search <keyword> - Search available dashboards by <keyword>
#

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

# Get a list of available dashboards
robot.respond /(?:grafana|graph|graf) list$/i, (msg) ->
callGrafana 'search', (dashboards) ->
robot.respond /(?:grafana|graph|graf) list\s?(.+)?/i, (msg) ->
if msg.match[1]
tag = msg.match[1].trim()
callGrafana "search?tag=#{tag}", (dashboards) ->
robot.logger.debug dashboards
response = "Dashboards tagged `#{tag}`:\n"
sendDashboardList dashboards, response, msg
else
callGrafana 'search', (dashboards) ->
robot.logger.debug dashboards
response = "Available dashboards:\n"
sendDashboardList dashboards, response, msg

# Search dashboards
robot.respond /(?:grafana|graph|graf) search (.+)/i, (msg) ->
query = msg.match[2].trim()
robot.logger.debug query
callGrafana "search?query=#{query}", (dashboards) ->
robot.logger.debug dashboards
response = "Available dashboards:\n"
response = "Dashboards matching `#{query}`:\n"
sendDashboardList dashboards, response, msg

# Send Dashboard list
sendDashboardList = (dashboards, response, msg) ->
# Handle refactor done for version 2.0.2+
if dashboards.dashboards
list = dashboards.dashboards
else
list = dashboards

robot.logger.debug list
unless list.length > 0
return

for dashboard in list
# Handle refactor done for version 2.0.2+
if dashboards.dashboards
list = dashboards.dashboards
if dashboard.uri
slug = dashboard.uri.replace /^db\//, ''
else
list = dashboards

robot.logger.debug list

for dashboard in list
# Handle refactor done for version 2.0.2+
if dashboard.uri
slug = dashboard.uri.replace /^db\//, ''
else
slug = dashboard.slug
response = response + "- #{slug}: #{dashboard.title}\n"
slug = dashboard.slug
response = response + "- #{slug}: #{dashboard.title}\n"

# Remove trailing newline
response.trim()
# Remove trailing newline
response.trim()

msg.send response
msg.send response

# Handle generic errors
sendError = (message, msg) ->
Expand Down
5 changes: 4 additions & 1 deletion test/grafana-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ describe 'grafana', ->
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(.*)?/i)

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

it 'registers a search listener', ->
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) search (.+)/i)