Permalink
Browse files

Merge pull request #1318 from spajus/graphite_update

Make graphite.coffee work and add `graphite list`
  • Loading branch information...
2 parents c6e9608 + c20922a commit 5f1bdee75fe33165576f058a6752d6dee41f3803 @technicalpickles technicalpickles committed Feb 10, 2014
Showing with 28 additions and 10 deletions.
  1. +28 −10 src/scripts/graphite.coffee
@@ -10,25 +10,32 @@
# GRAPHITE_AUTH (e.g. user:password for Basic Auth)
#
# Commands:
-# hubot graphite search <string> - search for graph by name
-# hubot graphite show <graph.name> - output graph
+# graphite list - list all available graphs
+# graphite search <string> - search for graph by name
+# graphite show <graph.name> - output graph
#
-# Author:
+# Authors:
# obfuscurity
+# spajus
module.exports = (robot) ->
+
+ robot.hear /graphite list/i, (msg) ->
+ treeversal msg, (data) ->
+ output = ""
+ output += "#{human_id(metric)}\n" for metric in data
+ msg.send output
robot.hear /graphite search (\w+)/i, (msg) ->
treeversal msg, (data) ->
output = ""
- output += metric.id + "\n" for metric in data
+ output += "#{human_id(metric)}\n" for metric in data
msg.send output
robot.hear /graphite show (\S+)/i, (msg) ->
treeversal msg, (data) ->
construct_url msg, data[0].graphUrl, (url) ->
msg.send url
construct_url = (msg, graphUrl, cb) ->
- cb(graphUrl) unless process.env.GRAPHITE_AUTH
graphRegex = /(\bhttps?:\/\/)(\S+)(\/render\/\S+)$/
serverRegex = /(\bhttps?:\/\/)(\S+)$/
uri = graphUrl.match(graphRegex)[3]
@@ -37,21 +44,29 @@ construct_url = (msg, graphUrl, cb) ->
port = construct_port()
timestamp = '#' + new Date().getTime()
suffix = '&png'
- newUrl = proto + process.env.GRAPHITE_AUTH + '@' + server + port + uri + timestamp + suffix
+ if process.env.GRAPHITE_AUTH
+ newUrl = proto + process.env.GRAPHITE_AUTH + '@' + server + port + uri + timestamp + suffix
+ else
+ newUrl = proto + server + port + uri + timestamp + suffix
cb(newUrl)
treeversal = (msg, cb, node="") ->
data = []
if node == ""
prefix = "*"
else
- prefix = node + ".*"
+ if node.slice(-1) == '.'
+ prefix = node + "*"
+ else
+ prefix = node + ".*"
port = construct_port()
- uri = "/browser/usergraph/?query=#{prefix}&format=treejson&contexts=1&path=#{node}&user=#{node}&node=#{node}"
+ uri = "/browser/usergraph/?query=#{prefix}&format=treejson&contexts=1&path=#{node}&node=#{node}"
auth = 'Basic ' + new Buffer(process.env.GRAPHITE_AUTH).toString('base64') if process.env.GRAPHITE_AUTH
+ headers = { Accept: "application/json", 'Content-type': 'application/json' }
+ headers['Authorization'] = auth if auth
msg
.http(process.env.GRAPHITE_URL + port + uri)
- .headers(Authorization: auth if auth, Accept: "application/json", 'Content-type': 'application/json')
+ .headers(headers)
.get() (err, res, body) ->
unless res.statusCode is 200
console.log(res)
@@ -62,12 +77,15 @@ treeversal = (msg, cb, node="") ->
treeversal(msg, cb, nodes[i].id)
else
regex = new RegExp(msg.match[1], "gi")
- unless nodes[i].id.search(regex) == -1
+ unless human_id(nodes[i]).search(regex) == -1
unless nodes[i].id == "no-click"
data[data.length] = nodes[i]
i++
cb(data) if data.length > 0
+human_id = (node) ->
+ node.id.replace /\.[a-z0-9]+$/, ".#{node.text}"
+
construct_port = () ->
port = ':'
if process.env.GRAPHITE_PORT

0 comments on commit 5f1bdee

Please sign in to comment.