Skip to content

Commit

Permalink
Merge pull request #2270 from deniszh/backport/1.1.x/pr-2259_pr-2262_…
Browse files Browse the repository at this point in the history
…pr-2262_pr-2262_pr-2262_pr-2262_pr-2264_pr-2266

[1.1.x] Adding dummy.txt to storage dirs | Fix test_delay list nesting | Add test for delay func where step > len(series) | Add tests for delay render function with negative step | Simplify logic of delay fun
  • Loading branch information
deniszh authored Apr 4, 2018
2 parents 94d4200 + 1c5892e commit 58cb2f0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
4 changes: 4 additions & 0 deletions docs/config-webapp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ Finally, configure the nginx vhost:
server {
listen 80;
location /static/ {
alias /opt/graphite/webapp/content/;
}
location / {
include uwsgi_params;
uwsgi_pass localhost:8080;
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

storage_dirs = []

for subdir in ('whisper', 'ceres', 'rrd', 'log', 'log/webapp'):
for subdir in ('whisper/dummy.txt', 'ceres/dummy.txt', 'rrd/dummy.txt', 'log/dummy.txt', 'log/webapp/dummy.txt'):
storage_dirs.append( ('storage/%s' % subdir, []) )

webapp_content = defaultdict(list)
Expand Down
31 changes: 13 additions & 18 deletions webapp/graphite/render/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,19 @@ def safeLast(values):
for v in reversed(values):
if v is not None: return v

def safeMin(values):
def safeMin(values, default=None):
safeValues = [v for v in values if v is not None]
if safeValues:
return min(safeValues)
else:
return default

def safeMax(values):
def safeMax(values, default=None):
safeValues = [v for v in values if v is not None]
if safeValues:
return max(safeValues)
else:
return default

def safeMap(function, values):
safeValues = [v for v in values if v is not None]
Expand Down Expand Up @@ -1930,15 +1934,10 @@ def delay(requestContext, seriesList, steps):
"""
results = []
for series in seriesList:
newValues = []
prev = []
for val in series:
if len(prev) < steps:
newValues.append(None)
prev.append(val)
continue
newValues.append(prev.pop(0))
prev.append(val)
if steps < 0:
newValues = series[-steps:] + [None] * min(-steps, len(series))
else:
newValues = [None] * min(steps, len(series)) + series[:-steps]
series.tags['delay'] = steps
newName = "delay(%s,%d)" % (series.name, steps)
newSeries = series.copy(name=newName, values=newValues)
Expand Down Expand Up @@ -3257,7 +3256,7 @@ def sortByMinima(requestContext, seriesList):
&target=sortByMinima(server*.instance*.memory.free)
"""
newSeries = [series for series in seriesList if safeMax(series) > 0]
newSeries = [series for series in seriesList if safeMax(series, default=0) > 0]
newSeries.sort(key=keyFunc(safeMin))
return newSeries

Expand Down Expand Up @@ -5260,12 +5259,8 @@ def minMax(requestContext, seriesList):
for series in seriesList:
series.name = "minMax(%s)" % (series.name)
series.pathExpression = series.name
min_val = safeMin(series)
max_val = safeMax(series)
if min_val is None:
min_val = 0.0
if max_val is None:
max_val = 0.0
min_val = safeMin(series, default=0.0)
max_val = safeMax(series, default=0.0)
for i, val in enumerate(series):
if series[i] is not None:
try:
Expand Down
43 changes: 41 additions & 2 deletions webapp/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,11 +876,50 @@ def test_changed(self):

def test_delay(self):
source = [
TimeSeries('collectd.test-db1.load.value',0,1,1,[list(range(18))] + [None, None]),
TimeSeries('collectd.test-db1.load.value',0,1,1,list(range(18)) + [None, None]),
]
delay = 2
expectedList = [
TimeSeries('delay(collectd.test-db1.load.value,2)',0,1,1,[None, None] + [list(range(18))]),
TimeSeries('delay(collectd.test-db1.load.value,2)',0,1,1,[None, None] + list(range(18))),
]
gotList = functions.delay({}, source, delay)
self.assertEqual(len(gotList), len(expectedList))
for got, expected in zip(gotList, expectedList):
self.assertEqual(got, expected)

def test_delay_too_many_steps(self):
source = [
TimeSeries('collectd.test-db1.load.value',0,1,1,list(range(20))),
]
delay = 25
expectedList = [
TimeSeries('delay(collectd.test-db1.load.value,25)',0,1,1,[None]*20),
]
gotList = functions.delay({}, source, delay)
self.assertEqual(len(gotList), len(expectedList))
for got, expected in zip(gotList, expectedList):
self.assertEqual(got, expected)

def test_delay_negative(self):
source = [
TimeSeries('collectd.test-db1.load.value',0,1,1,[None,None] + list(range(2,20))),
]
delay = -2
expectedList = [
TimeSeries('delay(collectd.test-db1.load.value,-2)',0,1,1,list(range(2,20)) + [None,None]),
]
gotList = functions.delay({}, source, delay)
self.assertEqual(len(gotList), len(expectedList))
for got, expected in zip(gotList, expectedList):
self.assertEqual(got, expected)

def test_delay_negative_too_many_steps(self):
source = [
TimeSeries('collectd.test-db1.load.value',0,1,1,list(range(20))),
]
delay = -25
expectedList = [
TimeSeries('delay(collectd.test-db1.load.value,-25)',0,1,1,[None]*20),
]
gotList = functions.delay({}, source, delay)
self.assertEqual(len(gotList), len(expectedList))
Expand Down

0 comments on commit 58cb2f0

Please sign in to comment.