files.new_contents: #Copyright 2008 Orbitz WorldWide
  #
  #Licensed under the Apache License, Version 2.0 (the "License");
  #you may not use this file except in compliance with the License.
  #You may obtain a copy of the License at
  #
  #   http://www.apache.org/licenses/LICENSE-2.0
  #
  #Unless required by applicable law or agreed to in writing, software
  #distributed under the License is distributed on an "AS IS" BASIS,
  #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  #See the License for the specific language governing permissions and
  #limitations under the License.
  
  # make / work consistently between python 2.x and 3.x
  # https://www.python.org/dev/peps/pep-0238/
  from __future__ import division
  
  import math
  import random
  import re
  import time
  
  from datetime import datetime, timedelta
  from functools import reduce
  from six.moves import range, zip
  import six
  try:
    from itertools import izip, izip_longest
  except ImportError:
    # Python 3
    from itertools import zip_longest as izip_longest
    izip = zip
  from os import environ
  
  from django.conf import settings
  
  from graphite.errors import NormalizeEmptyResultError
  from graphite.events import models
  from graphite.functions import SeriesFunction, ParamTypes, Param
  from graphite.logger import log
  from graphite.render.attime import getUnitString, parseTimeOffset, parseATTime, SECONDS_STRING, MINUTES_STRING, HOURS_STRING, DAYS_STRING, WEEKS_STRING, MONTHS_STRING, YEARS_STRING
  from graphite.render.evaluator import evaluateTarget
  from graphite.render.grammar import grammar
  from graphite.storage import STORE
  from graphite.util import epoch, epoch_to_dt, timestamp, deltaseconds
  
  # XXX format_units() should go somewhere else
  if environ.get('READTHEDOCS'):
    format_units = lambda *args, **kwargs: (0,'')
  else:
    from graphite.render.glyph import format_units
    from graphite.render.datalib import TimeSeries
  
  NAN = float('NaN')
  INF = float('inf')
  DAY = 86400
  HOUR = 3600
  MINUTE = 60
  
  #Utility functions
  
  
  def safeSum(values):
    safeValues = [v for v in values if v is not None]
    if safeValues:
      return sum(safeValues)
  
  
  def safeDiff(values):
    safeValues = [v for v in values if v is not None]
    if safeValues:
      values = list(map(lambda x: x*-1, safeValues[1:]))
      values.insert(0, safeValues[0])
      return sum(values)
  
  
  def safeLen(values):
    return len([v for v in values if v is not None])
  
  
  def safeDiv(a, b):
    if a is None: return None
    if b in (0,None): return None
    return a / b
  
  
  def safePow(a, b):
    if a is None: return None
    if b is None: return None
    try:
      result = math.pow(a, b)
    except (ValueError, OverflowError):
      return None
    return result
  
  
  def safeMul(*factors):
    if None in factors:
      return None
  
    factors = [float(x) for x in factors]
    product = reduce(lambda x,y: x*y, factors)
    return product
  
  
  def safeSubtract(a,b):
      if a is None or b is None: return None
      return float(a) - float(b)
  
  
  def safeAvg(values):
    safeValues = [v for v in values if v is not None]
    if safeValues:
      return sum(safeValues) / len(safeValues)
  
  
  def safeMedian(values):
    safeValues = [v for v in values if v is not None]
    if safeValues:
      sortedVals = sorted(safeValues)
      mid = len(sortedVals) // 2
      if len(sortedVals) % 2 == 0:
        return float(sortedVals[mid-1] + sortedVals[mid]) / 2
      else:
        return sortedVals[mid]
  
  
  def safeStdDev(a):
    sm = safeSum(a)
    ln = safeLen(a)
    avg = safeDiv(sm,ln)
    if avg is None: return None
    sum = 0
    safeValues = [v for v in a if v is not None]
    for val in safeValues:
       sum = sum + (val - avg) * (val - avg)
    return math.sqrt
  
  [... the rest is omitted ...]
files.new_path: webapp/graphite/render/functions.py
files.old_contents: #Copyright 2008 Orbitz WorldWide
  #
  #Licensed under the Apache License, Version 2.0 (the "License");
  #you may not use this file except in compliance with the License.
  #You may obtain a copy of the License at
  #
  #   http://www.apache.org/licenses/LICENSE-2.0
  #
  #Unless required by applicable law or agreed to in writing, software
  #distributed under the License is distributed on an "AS IS" BASIS,
  #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  #See the License for the specific language governing permissions and
  #limitations under the License.
  
  # make / work consistently between python 2.x and 3.x
  # https://www.python.org/dev/peps/pep-0238/
  from __future__ import division
  
  import math
  import random
  import re
  import time
  
  from datetime import datetime, timedelta
  from functools import reduce
  from six.moves import range, zip
  import six
  try:
    from itertools import izip, izip_longest
  except ImportError:
    # Python 3
    from itertools import zip_longest as izip_longest
    izip = zip
  from os import environ
  
  from django.conf import settings
  
  from graphite.errors import NormalizeEmptyResultError
  from graphite.events import models
  from graphite.functions import SeriesFunction, ParamTypes, Param
  from graphite.logger import log
  from graphite.render.attime import getUnitString, parseTimeOffset, parseATTime, SECONDS_STRING, MINUTES_STRING, HOURS_STRING, DAYS_STRING, WEEKS_STRING, MONTHS_STRING, YEARS_STRING
  from graphite.render.evaluator import evaluateTarget
  from graphite.render.grammar import grammar
  from graphite.storage import STORE
  from graphite.util import epoch, epoch_to_dt, timestamp, deltaseconds
  
  # XXX format_units() should go somewhere else
  if environ.get('READTHEDOCS'):
    format_units = lambda *args, **kwargs: (0,'')
  else:
    from graphite.render.glyph import format_units
    from graphite.render.datalib import TimeSeries
  
  NAN = float('NaN')
  INF = float('inf')
  DAY = 86400
  HOUR = 3600
  MINUTE = 60
  
  #Utility functions
  
  
  def safeSum(values):
    safeValues = [v for v in values if v is not None]
    if safeValues:
      return sum(safeValues)
  
  
  def safeDiff(values):
    safeValues = [v for v in values if v is not None]
    if safeValues:
      values = list(map(lambda x: x*-1, safeValues[1:]))
      values.insert(0, safeValues[0])
      return sum(values)
  
  
  def safeLen(values):
    return len([v for v in values if v is not None])
  
  
  def safeDiv(a, b):
    if a is None: return None
    if b in (0,None): return None
    return a / b
  
  
  def safePow(a, b):
    if a is None: return None
    if b is None: return None
    try:
      result = math.pow(a, b)
    except (ValueError, OverflowError):
      return None
    return result
  
  
  def safeMul(*factors):
    if None in factors:
      return None
  
    factors = [float(x) for x in factors]
    product = reduce(lambda x,y: x*y, factors)
    return product
  
  
  def safeSubtract(a,b):
      if a is None or b is None: return None
      return float(a) - float(b)
  
  
  def safeAvg(values):
    safeValues = [v for v in values if v is not None]
    if safeValues:
      return sum(safeValues) / len(safeValues)
  
  
  def safeMedian(values):
    safeValues = [v for v in values if v is not None]
    if safeValues:
      sortedVals = sorted(safeValues)
      mid = len(sortedVals) // 2
      if len(sortedVals) % 2 == 0:
        return float(sortedVals[mid-1] + sortedVals[mid]) / 2
      else:
        return sortedVals[mid]
  
  
  def safeStdDev(a):
    sm = safeSum(a)
    ln = safeLen(a)
    avg = safeDiv(sm,ln)
    if avg is None: return None
    sum = 0
    safeValues = [v for v in a if v is not None]
    for val in safeValues:
       sum = sum + (val - avg) * (val - avg)
    return math.sqrt
  
  [... the rest is omitted ...]
files.old_path: webapp/graphite/render/functions.py
files.new_contents: import copy
  import inspect
  import math
  import pytz
  import six
  
  from datetime import datetime
  from fnmatch import fnmatch
  from mock import patch, call, MagicMock
  from os.path import dirname, join
  from six.moves import range
  
  from .base import TestCase
  from django.conf import settings
  
  try:
    from django.urls import reverse
  except ImportError:  # Django < 1.10
    from django.core.urlresolvers import reverse
  
  from graphite.errors import NormalizeEmptyResultError
  from graphite.functions import _SeriesFunctions, loadFunctions
  from graphite.render.datalib import TimeSeries
  from graphite.render import functions
  from graphite.render.evaluator import evaluateTarget
  from graphite.render.grammar import grammar
  from graphite.tags.utils import TaggedSeries
  from graphite.util import json
  
  
  def return_greater(series, value):
      return [i for i in series if i is not None and i > value]
  
  
  def return_less(series, value):
      return [i for i in series if i is not None and i < value]
  
  
  class FunctionsTest(TestCase):
  
      def setUp(self):
        super(FunctionsTest, self).setUp()
        # Display more diff.
        self.maxDiff = 1024
  
      #
      # Test safeSum()
      #
  
      def test_safeSum_None(self):
          with self.assertRaises(TypeError):
              functions.safeSum(None)
  
      def test_safeSum_empty_list(self):
          self.assertEqual(functions.safeSum([]), None)
  
      def test_safeSum_all_numbers(self):
          self.assertEqual(functions.safeSum([1,2,3,4]), 10)
  
      def test_safeSum_all_None(self):
          self.assertEqual(functions.safeSum([None,None,None,None]), None)
  
      def test_safeSum_mixed(self):
          self.assertEqual(functions.safeSum([10,None,5,None]), 15)
  
      #
      # Test safeDiff()
      #
  
      def test_safeDiff_None(self):
          with self.assertRaises(TypeError):
              functions.safeDiff(None)
  
      def test_safeDiff_empty_list(self):
          self.assertEqual(functions.safeDiff([]), None)
  
      def test_safeDiff_all_numbers(self):
          self.assertEqual(functions.safeDiff([1,2,3,4]), -8)
  
      def test_safeDiff_all_None(self):
          self.assertEqual(functions.safeDiff([None,None,None,None]), None)
  
      def test_safeDiff_mixed(self):
          self.assertEqual(functions.safeDiff([10,None,5,None]), 5)
  
      #
      # Test safeLen()
      #
  
      def test_safeLen_None(self):
          with self.assertRaises(TypeError):
              functions.safeLen(None)
  
      def test_safeLen_empty_list(self):
          self.assertEqual(functions.safeLen([]), 0)
  
      def test_safeLen_all_numbers(self):
          self.assertEqual(functions.safeLen([1,2,3,4]), 4)
  
      def test_safeLen_all_None(self):
          self.assertEqual(functions.safeLen([None,None,None,None]), 0)
  
      def test_safeLen_mixed(self):
          self.assertEqual(functions.safeLen([10,None,5,None]), 2)
  
      #
      # Test safeDiv()
      #
  
      def test_safeDiv_None_None(self):
          self.assertEqual(functions.safeDiv(None, None), None)
  
      def test_safeDiv_5_None(self):
          self.assertEqual(functions.safeDiv(5, None), None)
  
      def test_safeDiv_5_0(self):
          self.assertEqual(functions.safeDiv(5, 0), None)
  
      def test_safeDiv_0_10(self):
          self.assertEqual(functions.safeDiv(0,10), 0)
  
      def test_safeDiv_10_5(self):
          self.assertEqual(functions.safeDiv(10,5), 2)
  
      #
      # Test safePow()
      #
  
      def test_safePow_None_None(self):
          self.assertEqual(functions.safePow(None, None), None)
  
      def test_safePow_5_None(self):
          self.assertEqual(functions.safePow(5, None), None)
  
      def test_safePow_5_0(self):
          self.assertEqual(functions.safePow(5, 0), 1.0)
  
      def test_safePow_0_10(self):
          self.assertEqual(functions.safePow(0,10), 0)
  
      def test_safePow_10_5(self):
          self
  
  [... the rest is omitted ...]
files.new_path: webapp/tests/test_functions.py
files.old_contents: import copy
  import inspect
  import math
  import pytz
  import six
  
  from datetime import datetime
  from fnmatch import fnmatch
  from mock import patch, call, MagicMock
  from os.path import dirname, join
  from six.moves import range
  
  from .base import TestCase
  from django.conf import settings
  
  try:
    from django.urls import reverse
  except ImportError:  # Django < 1.10
    from django.core.urlresolvers import reverse
  
  from graphite.errors import NormalizeEmptyResultError
  from graphite.functions import _SeriesFunctions, loadFunctions
  from graphite.render.datalib import TimeSeries
  from graphite.render import functions
  from graphite.render.evaluator import evaluateTarget
  from graphite.render.grammar import grammar
  from graphite.tags.utils import TaggedSeries
  from graphite.util import json
  
  
  def return_greater(series, value):
      return [i for i in series if i is not None and i > value]
  
  
  def return_less(series, value):
      return [i for i in series if i is not None and i < value]
  
  
  class FunctionsTest(TestCase):
  
      def setUp(self):
        super(FunctionsTest, self).setUp()
        # Display more diff.
        self.maxDiff = 1024
  
      #
      # Test safeSum()
      #
  
      def test_safeSum_None(self):
          with self.assertRaises(TypeError):
              functions.safeSum(None)
  
      def test_safeSum_empty_list(self):
          self.assertEqual(functions.safeSum([]), None)
  
      def test_safeSum_all_numbers(self):
          self.assertEqual(functions.safeSum([1,2,3,4]), 10)
  
      def test_safeSum_all_None(self):
          self.assertEqual(functions.safeSum([None,None,None,None]), None)
  
      def test_safeSum_mixed(self):
          self.assertEqual(functions.safeSum([10,None,5,None]), 15)
  
      #
      # Test safeDiff()
      #
  
      def test_safeDiff_None(self):
          with self.assertRaises(TypeError):
              functions.safeDiff(None)
  
      def test_safeDiff_empty_list(self):
          self.assertEqual(functions.safeDiff([]), None)
  
      def test_safeDiff_all_numbers(self):
          self.assertEqual(functions.safeDiff([1,2,3,4]), -8)
  
      def test_safeDiff_all_None(self):
          self.assertEqual(functions.safeDiff([None,None,None,None]), None)
  
      def test_safeDiff_mixed(self):
          self.assertEqual(functions.safeDiff([10,None,5,None]), 5)
  
      #
      # Test safeLen()
      #
  
      def test_safeLen_None(self):
          with self.assertRaises(TypeError):
              functions.safeLen(None)
  
      def test_safeLen_empty_list(self):
          self.assertEqual(functions.safeLen([]), 0)
  
      def test_safeLen_all_numbers(self):
          self.assertEqual(functions.safeLen([1,2,3,4]), 4)
  
      def test_safeLen_all_None(self):
          self.assertEqual(functions.safeLen([None,None,None,None]), 0)
  
      def test_safeLen_mixed(self):
          self.assertEqual(functions.safeLen([10,None,5,None]), 2)
  
      #
      # Test safeDiv()
      #
  
      def test_safeDiv_None_None(self):
          self.assertEqual(functions.safeDiv(None, None), None)
  
      def test_safeDiv_5_None(self):
          self.assertEqual(functions.safeDiv(5, None), None)
  
      def test_safeDiv_5_0(self):
          self.assertEqual(functions.safeDiv(5, 0), None)
  
      def test_safeDiv_0_10(self):
          self.assertEqual(functions.safeDiv(0,10), 0)
  
      def test_safeDiv_10_5(self):
          self.assertEqual(functions.safeDiv(10,5), 2)
  
      #
      # Test safePow()
      #
  
      def test_safePow_None_None(self):
          self.assertEqual(functions.safePow(None, None), None)
  
      def test_safePow_5_None(self):
          self.assertEqual(functions.safePow(5, None), None)
  
      def test_safePow_5_0(self):
          self.assertEqual(functions.safePow(5, 0), 1.0)
  
      def test_safePow_0_10(self):
          self.assertEqual(functions.safePow(0,10), 0)
  
      def test_safePow_10_5(self):
          self
  
  [... the rest is omitted ...]
files.old_path: webapp/tests/test_functions.py