From b2cccd5a34b2a435d63e3647c29bd8b46446039b Mon Sep 17 00:00:00 2001 From: Lucas Lavandeira <19612265+lucaslavandeira@users.noreply.github.com> Date: Mon, 25 Jun 2018 12:26:48 -0300 Subject: [PATCH 1/3] No devuelvo los nulls iniciales de una serie Ocurre cuando se aplica un representation_mode a una serie, los cambios de los primeros valores son nulos --- .../api/query/es_query/response_formatter.py | 3 +++ .../apps/api/tests/es_query_tests.py | 16 ++++++++++++++-- .../apps/api/tests/pipeline_tests/ids_tests.py | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py b/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py index 012d47cc..d3ae32f0 100644 --- a/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py +++ b/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py @@ -35,6 +35,9 @@ def format_response(self): else: for hit in response: data = hit[rep_mode] if rep_mode in hit else None + if data is None: + continue + timestamp_dict = self.data_dict.setdefault(hit.timestamp, {}) timestamp_dict[self._data_dict_series_key(self.series[i])] = data diff --git a/series_tiempo_ar_api/apps/api/tests/es_query_tests.py b/series_tiempo_ar_api/apps/api/tests/es_query_tests.py index ece2564e..65aa4396 100644 --- a/series_tiempo_ar_api/apps/api/tests/es_query_tests.py +++ b/series_tiempo_ar_api/apps/api/tests/es_query_tests.py @@ -342,8 +342,8 @@ def test_end_of_period_with_rep_mode(self): orig_eop.sort('asc') end_of_period = orig_eop.run() - for i, row in enumerate(data[1:], 1): # El primero es nulo en pct change - value = end_of_period[i][1] / end_of_period[i - 1][1] - 1 + for i, row in enumerate(data): # El primero es nulo en pct change + value = end_of_period[i + 1][1] / end_of_period[i][1] - 1 self.assertAlmostEqual(value, row[1]) @@ -462,3 +462,15 @@ def test_min_max_collapse_agg_with_rep_mode(self): min_val = max([row[1] for row in data]) self.assertAlmostEqual(min_val, other_data[0][1], places=5) + + def test_rep_mode_has_no_leading_nulls(self): + self.query.add_series(self.single_series, 'percent_change', self.series_periodicity) + data = self.query.run() + + self.assertIsNotNone(data[0][1]) + + def test_year_rep_mode_has_no_leading_nulls(self): + self.query.add_series(self.single_series, 'percent_change_a_year_ago', self.series_periodicity) + data = self.query.run() + + self.assertIsNotNone(data[0][1]) diff --git a/series_tiempo_ar_api/apps/api/tests/pipeline_tests/ids_tests.py b/series_tiempo_ar_api/apps/api/tests/pipeline_tests/ids_tests.py index b4ec2628..75259922 100644 --- a/series_tiempo_ar_api/apps/api/tests/pipeline_tests/ids_tests.py +++ b/series_tiempo_ar_api/apps/api/tests/pipeline_tests/ids_tests.py @@ -57,8 +57,8 @@ def test_global_rep_mode(self): other_query.sort('asc') other_data = other_query.run()['data'] - for index, row in enumerate(other_data[1:], start=1): - change = data[index][1] - data[index - 1][1] + for index, row in enumerate(other_data): + change = data[index + 1][1] - data[index][1] # La resta anterior trae pérdida de precisión si los números de 'data' son grandes self.assertAlmostEqual(row[1], change, places=5) From 7eb80c4d5e2bd0db3e8caad2766589511fefa6df Mon Sep 17 00:00:00 2001 From: Lucas Lavandeira <19612265+lucaslavandeira@users.noreply.github.com> Date: Mon, 25 Jun 2018 14:08:19 -0300 Subject: [PATCH 2/3] Reescribo el armado de respuesta --- .../apps/api/query/es_query/response_formatter.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py b/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py index d3ae32f0..f71762a9 100644 --- a/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py +++ b/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py @@ -33,13 +33,11 @@ def format_response(self): timestamp_dict = self.data_dict.setdefault(hit['key_as_string'], {}) timestamp_dict[self._data_dict_series_key(self.series[i])] = data else: + response = filter(lambda hit: rep_mode in hit, response) for hit in response: - data = hit[rep_mode] if rep_mode in hit else None - if data is None: - continue - timestamp_dict = self.data_dict.setdefault(hit.timestamp, {}) - timestamp_dict[self._data_dict_series_key(self.series[i])] = data + series = self._data_dict_series_key(self.series[i]) + timestamp_dict[series] = hit[rep_mode] if not self.data_dict: # No hay datos return [] From f602ee4d63eb6e8c6bc5b14b940186d22de30d5b Mon Sep 17 00:00:00 2001 From: Lucas Lavandeira <19612265+lucaslavandeira@users.noreply.github.com> Date: Mon, 25 Jun 2018 14:21:21 -0300 Subject: [PATCH 3/3] Fix pylint --- .../apps/api/query/es_query/response_formatter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py b/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py index f71762a9..5bd0d943 100644 --- a/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py +++ b/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py @@ -33,7 +33,7 @@ def format_response(self): timestamp_dict = self.data_dict.setdefault(hit['key_as_string'], {}) timestamp_dict[self._data_dict_series_key(self.series[i])] = data else: - response = filter(lambda hit: rep_mode in hit, response) + response = filter(lambda hit, mode=rep_mode: mode in hit, response) for hit in response: timestamp_dict = self.data_dict.setdefault(hit.timestamp, {}) series = self._data_dict_series_key(self.series[i])