Skip to content

Commit

Permalink
add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dethi committed Jun 18, 2015
1 parent ed6bee6 commit 5f1bd91
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion AlgoliaSearch/management/commands/algolia_buildindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ def handle(self, *args, **options):
if options['model'] and not (model.__name__ in options['model']):
continue

counts = adapter.reindex_all(batch_size=options['batchsize'])
counts = adapter.index_all(batch_size=options['batchsize'])
self.stdout.write('\t* {} --> {}'.format(model.__name__, counts))
20 changes: 19 additions & 1 deletion AlgoliaSearch/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from __future__ import unicode_literals

import logging

from django.conf import settings
from django.db import models

from algoliasearch import algoliasearch


logger = logging.getLogger(__name__)


class AlgoliaIndexError(Exception):
'''Something went wrong with an Algolia Index.'''

Expand Down Expand Up @@ -117,25 +122,31 @@ def __build_object(self, instance):
if callable(attr):
attr = attr()
tmp['_geoloc'] = {'lat': attr[0], 'lng': attr[1]}
logger.debug('BUILD %s FROM %s', tmp['objectID'], self.model)
return tmp

def update_obj_index(self, instance):
'''Update the object.'''
obj = self.__build_object(instance)
self.__index.save_object(obj)
logger.debug('UPDATE %s FROM %s', obj['objectID'], self.model)

def delete_obj_index(self, instance):
'''Delete the object.'''
self.__index.delete_object(instance.pk)
objectID = self.__get_objectID(instance)
self.__index.delete_object(objectID)
logger.debug('DELETE %s FROM %s', objectID, self.model)

def set_settings(self):
'''Apply the settings to the index.'''
if self.settings:
self.__index.set_settings(self.settings)
logger.debug('APPLY SETTINGS ON %s', self.index_name)

def clear_index(self):
'''Clear the index.'''
self.__index.clear_index()
logger.debug('CLEAR INDEX %s', self.index_name)

def index_all(self, batch_size=1000):
'''
Expand All @@ -152,10 +163,12 @@ def index_all(self, batch_size=1000):
batch.append(self.__build_object(instance))
if len(batch) >= batch_size:
self.__index.save_objects(batch)
logger.info('SAVE %d OBJECTS TO %s', len(batch), self.index_name)
batch = []
counts += 1
if len(batch) > 0:
self.__index.save_objects(batch)
logger.info('SAVE %d OBJECTS TO %s', len(batch), self.index_name)
return counts

def reindex_all(self, batch_size=1000):
Expand All @@ -167,7 +180,9 @@ def reindex_all(self, batch_size=1000):
'''
if self.settings:
self.__tmp_index.set_settings(self.settings)
logger.debug('APPLY SETTINGS ON %s_tmp', self.index_name)
self.__tmp_index.clear_index()
logger.debug('CLEAR INDEX %s_tmp', self.index_name)

result = None
counts = 0
Expand All @@ -176,11 +191,14 @@ def reindex_all(self, batch_size=1000):
batch.append(self.__build_object(instance))
if len(batch) >= batch_size:
result = self.__tmp_index.save_objects(batch)
logger.info('SAVE %d OBJECTS TO %s_tmp', len(batch), self.index_name)
batch = []
counts += 1
if len(batch) > 0:
result = self.__tmp_index.save_objects(batch)
logger.info('SAVE %d OBJECTS TO %s_tmp', len(batch), self.index_name)
if result:
self.__tmp_index.wait_task(result['taskID'])
self.__client.move_index(self.index_name + '_tmp', self.index_name)
logger.info('MOVE INDEX %s_tmp TO %s', self.index_name, self.index_name)
return counts
11 changes: 10 additions & 1 deletion AlgoliaSearch/registration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

import logging

from django.conf import settings
from django.db import models
from django.db.models.signals import post_save, pre_delete
Expand All @@ -9,6 +11,8 @@

from algoliasearch import algoliasearch

logger = logging.getLogger(__name__)


class AlgoliaEngineError(Exception):
'''Something went wrong with Algolia engine.'''
Expand All @@ -25,7 +29,8 @@ def __init__(self,
'''Initializes Algolia engine.'''
self.__registered_models = {}
self.client = algoliasearch.Client(app_id, api_key)
self.client.set_extra_header('User-Agent', 'Algolia for Django {}'.format(VERSION))
self.client.set_extra_header('User-Agent',
'Algolia for Django {}'.format(VERSION))

def is_registered(self, model):
'''Checks whether the given models is registered with Algolia engine.'''
Expand All @@ -48,6 +53,7 @@ def register(self, model, index_cls=AlgoliaIndex):
# Connect to the signalling framework.
post_save.connect(self.__post_save_receiver, model)
pre_delete.connect(self.__pre_delete_receiver, model)
logger.info('REGISTER %s', model)

def unregister(self, model):
'''
Expand All @@ -64,6 +70,7 @@ def unregister(self, model):
# Disconnect fron the signalling framework.
post_save.disconnect(self.__post_save_receiver, model)
pre_delete.disconnect(self.__pre_delete_receiver, model)
logger.info('UNREGISTER %s', model)

def get_registered_models(self):
'''Returns a sequence of models that have been registered with Algolia engine.'''
Expand Down Expand Up @@ -92,10 +99,12 @@ def delete_obj_index(self, obj):

def __post_save_receiver(self, instance, **kwargs):
'''Signal handler for when a registered model has been saved.'''
logger.debug('RECEIVE post_save FOR %s', instance.__class__)
self.update_obj_index(instance)

def __pre_delete_receiver(self, instance, **kwargs):
'''Signal handler for when a registered model has been deleted.'''
logger.debug('RECEIVE pre_delete FOR %s', instance.__class__)
self.delete_obj_index(instance)

# Algolia engine
Expand Down

0 comments on commit 5f1bd91

Please sign in to comment.