Skip to content

Commit

Permalink
Fix a analytics no indexandose en ES
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslavandeira committed Mar 7, 2019
1 parent 12935fa commit ff15184
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion series_tiempo_ar_api/apps/analytics/elasticsearch/index.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)):
Expand Down
2 changes: 2 additions & 0 deletions series_tiempo_ar_api/apps/analytics/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion series_tiempo_ar_api/apps/analytics/tests/import_tests.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand Down

0 comments on commit ff15184

Please sign in to comment.