Skip to content

Instantly share code, notes, and snippets.

@zwily
Last active December 15, 2015 20:59
Show Gist options
  • Save zwily/5322615 to your computer and use it in GitHub Desktop.
Save zwily/5322615 to your computer and use it in GitHub Desktop.
class GraphiteUrl
constructor: (base, attrs = {}) ->
@base = base
@attrs = attrs
@targets = []
attr: (k, v) ->
@attrs[k] = v
target: (target) ->
@targets.push if typeof(target) == "function" then target() else target
f: (name, args...) -> GraphiteUrl.func(name, args...)
render: ->
@base + '?' +
("#{k}=#{v}" for k, v of @attrs).join('&') + '&' +
("target=#{t}" for t in @targets).join('&')
# Builds graphite function calls with either blocks or strings,
# whichever makes you feel more special.
@func: (name, args...) ->
name +
"(" +
((if typeof(arg) == "function" then arg() else arg) for arg in args).join(',') +
")"
# Add graphite functions to the prototype, for convenience' sake.
functions = [
'alias'
'aliasByNode'
'aliasSub'
'alpha'
'areaBetween'
'asPercent'
'averageAbove'
'averageBelow'
'averageSeries'
'averageSeriesWithWildcards'
'cactiStyle'
'color'
'constantLine'
'cumulative'
'currentAbove'
'currentBelow'
'dashed'
'derivative'
'diffSeries'
'divideSeries'
'drawAsInfinite'
'events'
'exclude'
'groupByNode'
'highestAverage'
'highestCurrent'
'highestMax'
'hitcount'
'holtWintersAberration'
'holtWintersConfidenceBands'
'holtWintersForecast'
'integral'
'keepLastValue'
'legendValue'
'limit'
'lineWidth'
'logarithm'
'lowestAverage'
'lowestCurrent'
'maxSeries'
'maximumAbove'
'maximumBelow'
'minSeries'
'minimumAbove'
'mostDeviant'
'movingAverage'
'movingMedian'
'multiplySeries'
'nPercentile'
'nonNegativeDerivative'
'offset'
'randomWalkFunction'
'rangeOfSeries'
'removeAbovePercentile'
'removeAboveValue'
'removeBelowPercentile'
'removeBelowValue'
'scale'
'secondYAxis'
'sinFunction'
'smartSummarize'
'sortByMaxima'
'sortByMinima'
'stacked'
'stdev'
'substr'
'sumSeries'
'sumSeriesWithWildcards'
'summarize'
'threshold'
'timeFunction'
'timeShift'
]
for fname in functions
do (fname) ->
GraphiteUrl::[fname] = (args...) ->
GraphiteUrl.func(fname, args...)
g = new GraphiteUrl "https://informer.instructure.com/render/",
title: 'Cluster Overprovisioning'
width: 1000
from: '-1day'
until: 'now'
bgcolor: 'FFFFFF'
fgcolor: '000000'
yMin: 0
tz: 'America/Denver'
height: 400
clusters = [ '1', '1_3', '2_14', '3', '4', '5', '6', '7_11', '8', '9_10', '12_13' ]
_load =
g.sumSeries ->
(g.multiplySeries("clusters.canvas.app_cluster.#{c}.cpu", "clusters.canvas.app_cluster.#{c}.instances") for c in clusters)
_capacity =
g.sumSeries "clusters.canvas.app_cluster.*.instances"
g.target ->
g.cactiStyle ->
g.alias ->
g.divideSeries ->
g.diffSeries _capacity, _load
, _capacity
, '"overprovisioning %"'
$("#graphite").attr('src', g.render())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment