From ff1518480244f8154c596b917494fdd905f630a9 Mon Sep 17 00:00:00 2001 From: Lucas Lavandeira <19612265+lucaslavandeira@users.noreply.github.com> Date: Thu, 7 Mar 2019 12:48:17 -0300 Subject: [PATCH] Fix a analytics no indexandose en ES --- series_tiempo_ar_api/apps/analytics/elasticsearch/index.py | 3 ++- series_tiempo_ar_api/apps/analytics/importer.py | 2 ++ series_tiempo_ar_api/apps/analytics/tests/import_tests.py | 5 ++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/series_tiempo_ar_api/apps/analytics/elasticsearch/index.py b/series_tiempo_ar_api/apps/analytics/elasticsearch/index.py index 30ce7942..e713f783 100644 --- a/series_tiempo_ar_api/apps/analytics/elasticsearch/index.py +++ b/series_tiempo_ar_api/apps/analytics/elasticsearch/index.py @@ -1,6 +1,7 @@ import json from json import JSONDecodeError +from django.db.models import QuerySet from elasticsearch.helpers import streaming_bulk, parallel_bulk from elasticsearch_dsl import Index from elasticsearch_dsl.connections import connections @@ -19,7 +20,7 @@ def __init__(self, index: str = SERIES_QUERY_INDEX_NAME): self.es_index.doc_type(SeriesQuery) self.es_connection = connections.get_connection() - def index(self, queryset): + def index(self, queryset: QuerySet): self._init_index() for success, info in parallel_bulk(self.es_connection, generate_es_query(queryset)): diff --git a/series_tiempo_ar_api/apps/analytics/importer.py b/series_tiempo_ar_api/apps/analytics/importer.py index 5acd4784..3c910b14 100644 --- a/series_tiempo_ar_api/apps/analytics/importer.py +++ b/series_tiempo_ar_api/apps/analytics/importer.py @@ -32,6 +32,7 @@ def run(self, import_all=False): AnalyticsImportTask.info(self.task, "Todo OK") except Exception as e: AnalyticsImportTask.info(self.task, "Error importando analytics: {}".format(e)) + raise e self.task.refresh_from_db() self.task.status = AnalyticsImportTask.FINISHED @@ -66,6 +67,7 @@ def _load_queries(self, response): Query.objects.bulk_create(queries) # a ES if self.index_to_es: + queries = Query.objects.filter(api_mgmt_id__in=[q.api_mgmt_id for q in queries]) AnalyticsIndexer().index(queries) def exec_request(self, url=None, **params): diff --git a/series_tiempo_ar_api/apps/analytics/tests/import_tests.py b/series_tiempo_ar_api/apps/analytics/tests/import_tests.py index 306ffe81..fcc9ed3b 100644 --- a/series_tiempo_ar_api/apps/analytics/tests/import_tests.py +++ b/series_tiempo_ar_api/apps/analytics/tests/import_tests.py @@ -1,8 +1,11 @@ #!coding=utf8 from decimal import Decimal +from django.core.exceptions import FieldError from faker import Faker from django.test import TestCase +from nose.tools import raises + from series_tiempo_ar_api.apps.analytics.tasks import enqueue_new_import_analytics_task from series_tiempo_ar_api.apps.analytics.models import ImportConfig, Query, AnalyticsImportTask @@ -11,9 +14,9 @@ class UninitializedImportConfigTests(TestCase): + @raises(FieldError) def test_not_initialized_model(self): enqueue_new_import_analytics_task(index_to_es=False) - self.assertTrue('Error' in AnalyticsImportTask.objects.last().logs) class FakeRequests: