Skip to content

Instantly share code, notes, and snippets.

@joerayme
Created July 5, 2013 13:33
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joerayme/5934555 to your computer and use it in GitHub Desktop.
Save joerayme/5934555 to your computer and use it in GitHub Desktop.
Very simple Graphite widget for Dashing

##Description

Coull.com's very simple Dashing widget to display a Graphite graph with the correct dimensions and auto-refresh it every so often.

##Usage

Copy graphite.scss, graphite.html and graphite.coffee into widgets/graphite.

In your dashboard, add a snippet such as this:

<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
  <div data-view="Graphite" data-image="http://my.graphite.instance.com/render?from=-3hours&amp;width=600&amp;height=400&amp;target=my.graphite.metric&amp;_uniq=0.11581272282637656" data-interval="60000"></div>
</li>

The data-image attribute should be set to a graphite image URL. Don't worry about the width, height and uniq paramters - they'll be replaced by the javascript based on your dashboard settings. data-interval may be set to indicate the number of milliseconds between refreshes of the graph. If omitted, it will default to 60,000 (1 minute).

class Dashing.Graphite extends Dashing.Widget
ready: ->
if @get('interval')
interval = parseInt(@get('interval'))
else
interval = 60000
self = this
setInterval ->
self.updateGraph()
, interval
@updateGraph()
updateGraph: ->
url = @get('image')
$n = $(@node)
width = $n.width()
height = $n.height()
url = @updateUrl url, 'width', width
url = @updateUrl url, 'height', height
url = @updateUrl url, '_uniq', new Date().getTime()
$n.find('img').attr 'src', url
updateUrl: (url, param, value) ->
if url.indexOf(param) >= 0
regexp = new RegExp '([\?&])' + param + '(=[^&]*)?&?'
url = url.replace regexp, '$1'
repl = param + '=' + value
if url.indexOf('?') < 0
url + '?' + repl
else
url + '&' + repl
<img data-bind-width="width" data-bind-height="height" />
.widget.widget-graphite {
padding: 0;
}
Copyright (c) 2013 Couller Ltd. and Joseph Ray
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@ykyuen
Copy link

ykyuen commented Dec 2, 2014

I made a small change on the .coffee such that it will read and override the from parameter on the url and display the graphite graph with dynamic range.
ex. http://localhost:3030/my_dashboard?from=-12hours

graphite.coffee

class Dashing.Graphite extends Dashing.Widget

  ready: ->
    if @get('interval')
      interval = parseInt(@get('interval'))
    else
      interval = 60000
    self = this
    setInterval ->
      self.updateGraph()
    , interval
    @updateGraph()

  updateGraph: ->
    url = @get('image')
    $n = $(@node)
    width = $n.width()
    height = $n.height()

    url = @updateUrl url, 'width', width
    url = @updateUrl url, 'height', height
    url = @updateUrl url, '_uniq', new Date().getTime()

    from = getQueryVariable('from')
    if from
      url = @updateUrl url, 'from', from

    $n.find('img').attr 'src', url

  updateUrl: (url, param, value) ->
    if url.indexOf(param) >= 0
      regexp = new RegExp '([\?&])' + param + '(=[^&]*)?&?'
      url = url.replace regexp, '$1'

    repl = param + '=' + value

    if url.indexOf('?') < 0
      url + '?' + repl
    else
      url + '&' + repl

  getQueryVariable = (variable) ->
    query = window.location.search.substring(1)
    vars = query.split("&")
    i = 0

    while i < vars.length
      pair = vars[i].split("=")
      return pair[1]  if pair[0] is variable
      i++
    false

@charlesrg
Copy link

Paste a picture please.

@willejs
Copy link

willejs commented Aug 24, 2015

+1

@kondalonline
Copy link

+1
Would love to see a picture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment