Skip to content

Commit

Permalink
Merge pull request #2234 from DanCech/aggregateLine
Browse files Browse the repository at this point in the history
add optional keepStep parameter to aggregateLine
  • Loading branch information
DanCech committed Feb 14, 2018
2 parents 0cee578 + 654b950 commit 53b9f97
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
18 changes: 13 additions & 5 deletions webapp/graphite/render/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4078,11 +4078,14 @@ def constantLine(requestContext, value):
Param('value', ParamTypes.float, required=True),
]

def aggregateLine(requestContext, seriesList, func='average'):
def aggregateLine(requestContext, seriesList, func='average', keepStep=False):
"""
Takes a metric or wildcard seriesList and draws a horizontal line
based on the function applied to each series.
If the optional keepStep parameter is set to True, the result will
have the same time period and step as the source series.
Note: By default, the graphite renderer consolidates data points by
averaging data points over time. If you are using the 'min' or 'max'
function for aggregateLine, this can cause an unusual gap in the
Expand Down Expand Up @@ -4110,16 +4113,21 @@ def aggregateLine(requestContext, seriesList, func='average'):
else:
name = 'aggregateLine(%s, None)' % (series.name)

[series] = constantLine(requestContext, value)
series.name = name
series.pathExpression = name
results.append(series)
if keepStep:
aggSeries = series.copy(name=name, values=[value] * len(series))
else:
[aggSeries] = constantLine(requestContext, value)
aggSeries.name = name
aggSeries.pathExpression = name

results.append(aggSeries)
return results

aggregateLine.group = 'Calculate'
aggregateLine.params = [
Param('seriesList', ParamTypes.seriesList, required=True),
Param('func', ParamTypes.aggFunc, default='average', options=aggFuncNames),
Param('keepStep', ParamTypes.boolean, default=False),
]

def verticalLine(requestContext, ts, label=None, color=None):
Expand Down
22 changes: 12 additions & 10 deletions webapp/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3577,18 +3577,19 @@ def test_aggregateLine_avg(self):
)

expectedResult = [
TimeSeries('aggregateLine(collectd.test-db1.load.value, 4)', 3600, 3600, 0, [4.0, 4.0, 4.0]),
TimeSeries('aggregateLine(collectd.test-db2.load.value, None)', 3600, 3600, 0, [None, None, None]),
TimeSeries('aggregateLine(collectd.test-db3.load.value, 1.85714)', 3600, 3600, 0, [1.8571428571428572, 1.8571428571428572, 1.8571428571428572]),
TimeSeries('aggregateLine(collectd.test-db4.load.value, 8.22222)', 3600, 3600, 0, [8.222222222222221, 8.222222222222221, 8.222222222222221]),
TimeSeries('aggregateLine(collectd.test-db1.load.value, 4)', 0, 600, 60, [4.0] * 10),
TimeSeries('aggregateLine(collectd.test-db2.load.value, None)', 0, 600, 60, [None] * 10),
TimeSeries('aggregateLine(collectd.test-db3.load.value, 1.85714)', 0, 600, 60, [1.8571428571428572] * 10),
TimeSeries('aggregateLine(collectd.test-db4.load.value, 8.22222)', 0, 600, 60, [8.222222222222221] * 10),
]
result = functions.aggregateLine(
self._build_requestContext(
startTime=datetime(1970,1,1,1,0,0,0,pytz.timezone(settings.TIME_ZONE)),
endTime=datetime(1970,1,1,1,0,0,0,pytz.timezone(settings.TIME_ZONE))
),
seriesList,
'avg'
'avg',
keepStep=True
)
self.assertEqual(result, expectedResult)

Expand Down Expand Up @@ -3635,18 +3636,19 @@ def test_aggregateLine_max(self):
)

expectedResult = [
TimeSeries('aggregateLine(collectd.test-db1.load.value, 7)', 3600, 3600, 0, [7.0, 7.0, 7.0]),
TimeSeries('aggregateLine(collectd.test-db2.load.value, None)', 3600, 3600, 0, [None, None, None]),
TimeSeries('aggregateLine(collectd.test-db3.load.value, 4)', 3600, 3600, 0, [4.0, 4.0, 4.0]),
TimeSeries('aggregateLine(collectd.test-db4.load.value, 10)', 3600, 3600, 0, [10.0, 10.0, 10.0]),
TimeSeries('aggregateLine(collectd.test-db1.load.value, 7)', 0, 600, 60, [7.0] * 10),
TimeSeries('aggregateLine(collectd.test-db2.load.value, None)', 0, 600, 60, [None] * 10),
TimeSeries('aggregateLine(collectd.test-db3.load.value, 4)', 0, 600, 60, [4.0] * 10),
TimeSeries('aggregateLine(collectd.test-db4.load.value, 10)', 0, 600, 60, [10.0] * 10),
]
result = functions.aggregateLine(
self._build_requestContext(
startTime=datetime(1970,1,1,1,0,0,0,pytz.timezone(settings.TIME_ZONE)),
endTime=datetime(1970,1,1,1,0,0,0,pytz.timezone(settings.TIME_ZONE))
),
seriesList,
'max'
'max',
keepStep=True
)
self.assertEqual(result, expectedResult)

Expand Down

0 comments on commit 53b9f97

Please sign in to comment.