maxSeries function

Asked by Pete Emerson

In addition to the existing sumSeries function, I would like to have minSeries and MaxSeries functions as well. Is there any sneaky way to achieve this with the existing code base, or is this a feature request?

Pete

Question information

Language:
English Edit question
Status:
Solved
For:
Graphite Edit question
Assignee:
No assignee Edit question
Solved by:
Pete Emerson
Solved:
Last query:
Last reply:
Revision history for this message
Pete Emerson (pete-appnexus) said :
#1

Ah, I believe I see the core code for sumSeries:

def sumSeries(*seriesLists):
  (seriesList,start,end,step) = normalize(seriesLists)
  #name = "sumSeries(%s)" % ','.join((s.name for s in seriesList))
  name = "sumSeries(%s)" % ','.join(set([s.pathExpression for s in seriesList]))
  values = ( safeSum(row) for row in izip(*seriesList) ) #XXX izip
  series = TimeSeries(name,start,end,step,values)
  series.pathExpression = name
  return [series]

I'm not fluent in python so I'll need to wrap my brain around it, but am I correct that if I can write maxSeries based on this code I'd be good to go?

Pete

Revision history for this message
chrismd (chrismd) said :
#2

Adding new functions is relatively easy (assuming your comfortable with python & a tiny bit of javascript).

First you need to define your functions in $GRAPHITE_ROOT/webapp/web/render/functions.py you can probably just copy and modify an existing function (like sumSeries) to get what you're looking for.

Next you need to add your function to the composer UI's function menu which is defined in $GRAPHITE_ROOT/webapp/content/js/composer_widgets.js. Just search for the string 'showContextMenu:' and you will see the list of defined functions. Just add yours. You will probably want to use the 'sumSeries' entry as a template.

Hope that helps, and feel free to send a patch if you get it working. Thanks!

Revision history for this message
chrismd (chrismd) said :
#3

That's funny you found the answer while I was still typing my reply. :)

Revision history for this message
Pete Emerson (pete-appnexus) said :
#4

I believe I've got it! This is from graphite-0.9.4. Rip out my comments and restructure, whatever you see fit.

$ diff functions.py functions.py.new
23a24,35
> # Pete adding safeMax
> def safeMax(values):
> safeValues = [v for v in values if v is not None]
> if not safeValues: return None
> return max(safeValues)
>
> # Pete adding safeMin
> def safeMin(values):
> safeValues = [v for v in values if v is not None]
> if not safeValues: return None
> return min(safeValues)
>
77a90,108
> # Pete adding maxSeries
> def maxSeries(*seriesLists):
> (seriesList,start,end,step) = normalize(seriesLists)
> #name = "maxSeries(%s)" % ','.join((s.name for s in seriesList))
> name = "maxSeries(%s)" % ','.join(set([s.pathExpression for s in seriesList]))
> values = ( safeMax(row) for row in izip(*seriesList) ) #XXX izip
> series = TimeSeries(name,start,end,step,values)
> series.pathExpression = name
> return [series]
>
> def minSeries(*seriesLists):
> (seriesList,start,end,step) = normalize(seriesLists)
> #name = "minSeries(%s)" % ','.join((s.name for s in seriesList))
> name = "minSeries(%s)" % ','.join(set([s.pathExpression for s in seriesList]))
> values = ( safeMin(row) for row in izip(*seriesList) ) #XXX izip
> series = TimeSeries(name,start,end,step,values)
> series.pathExpression = name
> return [series]
>
207a239
> # Pete adding maxSeries
208a241,242
> 'minSeries' : minSeries,
> 'maxSeries' : maxSeries,

$ diff composer_widgets.js composer_widgets.js.new
390a391,392
> /* Pete adding Max Series 5 lines down */
>
394a397,398
> {text: 'Min Series', handler: this.applyFuncToAll('minSeries')},
> {text: 'Max Series', handler: this.applyFuncToAll('maxSeries')},

Revision history for this message
Pete Emerson (pete-appnexus) said :
#5

This ticket can be closed, as long as you have whatever you need from me. Thanks.

Pete Emerson