Skip to content

Commit

Permalink
Merge pull request #349 from datosgobar/323-limit-offset-bug
Browse files Browse the repository at this point in the history
Corrijo offsets inválidos cuando se aplica rep_mode
  • Loading branch information
lucaslavandeira authored Aug 13, 2018
2 parents e881f97 + 7d70900 commit fe20e28
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
17 changes: 17 additions & 0 deletions series_tiempo_ar_api/apps/api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,20 @@ def get_periodicity_human_format_es(periodicity):
}

return periodicities[periodicity]


def extra_offset(periodicity):
"""Cantidad de valores extra a pedir en el offset de ES cuando se aplica
la periodicidad pasada. Es necesario para que al aplicar transformaciones
de variación, se devuelva la cantidad exacta esperada por el usuario.
Ejemplo: limit=100, rep_mode=change_a_year_ago, devolvería 99 resultados
en una serie anual, necesitamos sumar el valor devuelto por esta función
"""
offsets = {
'year': 1,
'semester': 2,
'quarter': 4,
'month': 12,
'day': 365,
}
return offsets.get(periodicity, 0)
2 changes: 1 addition & 1 deletion series_tiempo_ar_api/apps/api/query/es_query/es_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def add_pagination(self, start, limit):
raise QueryError(strings.EMPTY_QUERY_ERROR)

for serie in self.series:
serie.search = serie.search[start:limit]
serie.add_pagination(start, limit)

# Guardo estos parámetros, necesarios en el evento de hacer un collapse
self.args[constants.PARAM_START] = start
Expand Down
8 changes: 8 additions & 0 deletions series_tiempo_ar_api/apps/api/query/es_query/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.conf import settings
from elasticsearch_dsl import Search, Q, A

from series_tiempo_ar_api.apps.api.helpers import extra_offset
from series_tiempo_ar_api.apps.api.query import constants
from series_tiempo_ar_api.libs.indexing.elastic import ElasticInstance

Expand Down Expand Up @@ -53,3 +54,10 @@ def add_collapse(self, periodicity):
else: # Ignoramos la in memory ag
self.collapse_agg = constants.AGG_DEFAULT
self.search = self.search.filter('bool', must=[Q('match', interval=periodicity)])

self.args[constants.PARAM_PERIODICITY] = periodicity

def add_pagination(self, start, limit):
# ☢️☢️☢️
es_offset = limit + extra_offset(self.args[constants.PARAM_PERIODICITY])
self.search = self.search[start:es_offset]
10 changes: 10 additions & 0 deletions series_tiempo_ar_api/apps/api/tests/query_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,13 @@ def test_query_count(self):

# Longitud de la serie pedida. Ver support/generate_data.py
self.assertEqual(resp['count'], 1000)

def test_day_series_length_with_limit_and_rep_mode(self):
day_series_name = settings.TEST_SERIES_NAME.format('day')
field = Field.objects.get(identifier=day_series_name)

self.query.add_series(day_series_name, field, rep_mode='percent_change_a_year_ago')
self.query.add_pagination(start=0, limit=2)
result = self.query.run()

self.assertEqual(len(result['data']), 2)

0 comments on commit fe20e28

Please sign in to comment.