Skip to content

Commit

Permalink
Merge pull request #2127 from Felixoid/perSecond-and-scaleToSeconds
Browse files Browse the repository at this point in the history
Get rid of OverflowError in perSecond and scaleToSeconds
  • Loading branch information
deniszh committed Nov 27, 2017
2 parents b73cc91 + 8a0c009 commit c68ab72
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 10 additions & 2 deletions webapp/graphite/render/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,11 @@ def scaleToSeconds(requestContext, seriesList, seconds):

for i,value in enumerate(series):
if value is not None:
series[i] = round(value * factor, 6)
# Division long by float cause OverflowError
try:
series[i] = round(value * factor, 6)
except OverflowError:
series[i] = (value // series.step) * seconds

return seriesList

Expand Down Expand Up @@ -1609,7 +1613,11 @@ def perSecond(requestContext, seriesList, maxValue=None):
delta, prev = _nonNegativeDelta(val, prev, maxValue)

if delta is not None:
newValues.append(round(delta / step, 6))
# Division long by float cause OverflowError
try:
newValues.append(round(delta / step, 6))
except OverflowError:
newValues.append(delta // step)
else:
newValues.append(None)

Expand Down
8 changes: 4 additions & 4 deletions webapp/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1581,8 +1581,8 @@ def test_nonNegativeDerivative_max(self):
self.assertEqual(expected, result, 'nonNegativeDerivative result incorrect')

def test_perSecond(self):
seriesList = self._gen_series_list_with_data(key='test',start=0,end=600,step=60,data=[0, 120, 240, 480, 960, 1920, 3840, 7680, 15360, 30720])
expected = [TimeSeries('perSecond(test)', 0, 600, 60, [None, 2, 2, 4, 8, 16, 32, 64, 128, 256])]
seriesList = self._gen_series_list_with_data(key='test',start=0,end=600,step=60,data=[0, 120, 240, 480, 960, 1920, 3840, 7680, 15360, 60 ** 256 + 15360 ])
expected = [TimeSeries('perSecond(test)', 0, 600, 60, [None, 2, 2, 4, 8, 16, 32, 64, 128, 60 ** 255])]
result = functions.perSecond({}, seriesList)
self.assertEqual(list(expected[0]), list(result[0]))
self.assertEqual(expected, result)
Expand All @@ -1592,8 +1592,8 @@ def test_perSecond(self):
self.assertEqual(list(expected[0]), list(result[0]))

def test_perSecond_float(self):
seriesList = self._gen_series_list_with_data(key='test',start=0,end=600,step=60,data=[0, 90, 186, 291, 411, 561, 747, 939, 136, 336])
expected = [TimeSeries('perSecond(test)', 0, 600, 60, [None, 1.5, 1.6, 1.75, 2, 2.5, 3.1, 3.2, 3.3, 3.333333])]
seriesList = self._gen_series_list_with_data(key='test',start=0,end=600,step=60,data=[0, 90, 186, 291, 411, 561, 747, 136, 336, 60**256 + 336])
expected = [TimeSeries('perSecond(test)', 0, 600, 60, [None, 1.5, 1.6, 1.75, 2, 2.5, 3.1, 6.5, 3.333333, None])]
result = functions.perSecond({}, seriesList, maxValue=1000)
self.assertEqual(list(expected[0]), list(result[0]))
self.assertEqual(expected, result)
Expand Down

0 comments on commit c68ab72

Please sign in to comment.