Skip to content

Commit

Permalink
Merge pull request #973 from kdebski/master
Browse files Browse the repository at this point in the history
Add function to remove empty Series.
  • Loading branch information
obfuscurity committed Jan 22, 2015
2 parents 59ee9ee + 6001cb7 commit 58e8f2c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion webapp/content/js/composer_widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,8 @@ function createFunctionsMenu() {
{text: 'sortByMinima', handler: applyFuncToEach('sortByMinima')},
{text: 'limit', handler: applyFuncToEachWithInput('limit', 'Limit to first ___ of a list of metrics')},
{text: 'Exclude', handler: applyFuncToEachWithInput('exclude', 'Exclude metrics that match a regular expression')},
{text: 'Grep', handler: applyFuncToEachWithInput('grep', 'Exclude metrics that don\'t match a regular expression')}
{text: 'Grep', handler: applyFuncToEachWithInput('grep', 'Exclude metrics that don\'t match a regular expression')},
{text: 'Remove Empty Series', handler: applyFuncToEachWithInput('removeEmptySeries', 'Removes series with no data from graph')}
]
}, {
text: 'Special',
Expand Down
21 changes: 21 additions & 0 deletions webapp/graphite/render/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def safeAbs(value):
if value is None: return None
return abs(value)

def safeIsNotEmpty(values):
safeValues = [v for v in values if v is not None]
return len(safeValues) > 0

# Greatest common divisor
def gcd(a, b):
if b == 0:
Expand Down Expand Up @@ -3180,6 +3184,22 @@ def sinFunction(requestContext, name, amplitude=1, step=60):
int(epoch(requestContext["endTime"])),
step, values)]

def removeEmptySeries(requestContext, seriesList):
"""
Takes one metric or a wildcard seriesList.
Out of all metrics passed, draws only the metrics with not empty data
Example:
.. code-block:: none
&target=removeEmptySeries(server*.instance*.threads.busy)
Draws only live servers with not empty data.
"""
return [ series for series in seriesList if safeIsNotEmpty(series) ]

def randomWalkFunction(requestContext, name, step=60):
"""
Short Alias: randomWalk()
Expand Down Expand Up @@ -3353,6 +3373,7 @@ def pieMinimum(requestContext, series):
'useSeriesAbove': useSeriesAbove,
'exclude' : exclude,
'grep' : grep,
'removeEmptySeries' : removeEmptySeries,

# Data Filter functions
'removeAbovePercentile' : removeAbovePercentile,
Expand Down
13 changes: 13 additions & 0 deletions webapp/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ def _generate_series_list(self):
seriesList.append(TimeSeries(name, 0, 1, 1, c))
return seriesList

def test_check_empty_lists(self):
seriesList = []
config = [[1000, 100, 10, 0], []]
for i, c in enumerate(config):
seriesList.append(TimeSeries('Test(%d)' % i, 0, 0, 0, c))

self.assertTrue(functions.safeIsNotEmpty(seriesList[0]))
self.assertFalse(functions.safeIsNotEmpty(seriesList[1]))

result = functions.removeEmptySeries({}, seriesList)

self.assertEqual(1, len(result))

def test_remove_above_percentile(self):
seriesList = self._generate_series_list()
percent = 50
Expand Down

0 comments on commit 58e8f2c

Please sign in to comment.