diff --git a/src/InternetStatusReporter.py b/src/InternetStatusReporter.py index 18804d9..ba0fa12 100644 --- a/src/InternetStatusReporter.py +++ b/src/InternetStatusReporter.py @@ -114,13 +114,14 @@ def measure_latency(self): from json import dumps from sys import exc_info from tcp_latency import measure_latency - from src.utils import calculate_standard_deviation + from src.utils import calculate_standard_deviation, remove_none results = {} for latency_address in self.latency_addresses: try: runs = int(getenv('LATENCY_RUNS')) data = measure_latency(host=latency_address, port=80, runs=runs, timeout=2.5) + data = remove_none(data) mean, deviation = calculate_standard_deviation(data, self.logger) except: self.logger.exception(f'Unexpected error: {exc_info()[0]}') diff --git a/src/tests/utils_test.py b/src/tests/utils_test.py index 23ac848..8a18f2f 100644 --- a/src/tests/utils_test.py +++ b/src/tests/utils_test.py @@ -24,16 +24,16 @@ def test_calculate_standard_deviation_when_an_array_of_numbers_is_passed(self, m ) @patch('logging.Logger') - def test_calculate_standard_deviation_when_an_array_of_numbers_and_none_is_passed(self, mock_logger): + def test_calculate_standard_deviation_when_an_array_of_one_number_is_passed(self, mock_logger): self.assertEqual( - calculate_standard_deviation([None,1.0,2,3,None,4.0,5], mock_logger), - (3.0, 1.5811388300841898) + calculate_standard_deviation([1.0], mock_logger), + (1.0, 0) ) @patch('logging.Logger') - def test_calculate_standard_deviation_when_an_array_of_none_is_passed(self, mock_logger): + def test_calculate_standard_deviation_when_an_empty_array_is_passed(self, mock_logger): self.assertEqual( - calculate_standard_deviation([None, None, None, None], mock_logger), + calculate_standard_deviation([], mock_logger), None ) diff --git a/src/utils.py b/src/utils.py index 765987f..7a08c7b 100644 --- a/src/utils.py +++ b/src/utils.py @@ -15,11 +15,12 @@ def calculate_standard_deviation(data, logger): from math import sqrt from sys import exc_info - data = remove_none(data) length = len(data) if length == 0: return + elif length == 1: + return data[0], 0 try: mean = reduce(lambda a,b: a+b, data)/length