diff --git a/ccplugin.py b/ccplugin.py index da3ef51e1b4e4e3c12c5c1378e07b350555cb253_Y2NwbHVnaW4ucHk=..3d097513f4dd18e7be03dd4d6e5793cb78441d39_Y2NwbHVnaW4ucHk= 100644 --- a/ccplugin.py +++ b/ccplugin.py @@ -11,8 +11,8 @@ from elasticsearch.helpers import parallel_bulk -from cubicweb.cwctl import CWCTL -from cubicweb.utils import admincnx +from cubicweb.cwctl import CWCTL, init_cmdline_log_threshold +from cubicweb.cwconfig import CubicWebConfiguration as cwcfg from cubicweb.toolsutils import Command from cubes.elasticsearch.es import indexable_types, fulltext_indexable_rql @@ -34,6 +34,7 @@ arguments = '<instance id>' options = [ ('dry-run', - {'type': 'yn', 'default': False, + {'action': 'store_true', 'default': False, + 'short': 'N', 'help': 'set to True if you want to skip the insertion in ES'}), ('debug', @@ -38,8 +39,10 @@ 'help': 'set to True if you want to skip the insertion in ES'}), ('debug', - {'type': 'yn', 'default': False, - 'help': ('set to True if you want to print ' - 'out debug info and progress')}), + {'action': 'store_true', 'default': False, 'short': 'D', + 'help': ('shortcut for --loglevel=debug')}), + ('loglevel', + {'short': 'l', 'type': 'choice', 'metavar': '<log level>', + 'default': None, 'choices': ('debug', 'info', 'warning', 'error')}), ('etypes', {'type': 'csv', 'default': '', 'help': 'only index given etypes [default:all indexable types]'}), @@ -56,9 +59,14 @@ def run(self, args): """run the command with its specific arguments""" appid = args.pop(0) - with admincnx(appid) as cnx: + if self['debug']: + self['loglevel'] = 'debug' + config = cwcfg.config_for(appid, debugmode=self['loglevel']) + if self['loglevel']: + init_cmdline_log_threshold(config, self['loglevel']) + with config.repository().internal_cnx() as cnx: schema = cnx.vreg.schema indexer = cnx.vreg['es'].select('indexer', cnx) es = indexer.get_connection() indexer.create_index() if self.config.index_name: @@ -60,8 +68,9 @@ schema = cnx.vreg.schema indexer = cnx.vreg['es'].select('indexer', cnx) es = indexer.get_connection() indexer.create_index() if self.config.index_name: + cnx.info('create ES index {}'.format(self.config.index_name)) indexer.create_index(index_name=self.config.index_name) if es: if self.config.etypes: @@ -70,8 +79,8 @@ etypes = indexable_types( schema, custom_skip_list=self.config.except_etypes) assert self.config.except_etypes not in etypes - if self.config.debug and not self.config.etypes: - print(u'found indexable_types {}'.format( + if not self.config.etypes: + cnx.debug(u'found indexable types: {}'.format( ','.join(etypes))) for _ in parallel_bulk( es, self.bulk_actions(etypes, cnx, @@ -80,4 +89,5 @@ raise_on_error=False, raise_on_exception=False): pass + print(_) else: @@ -83,4 +93,3 @@ else: - if self.config.debug: - print(u'no elasticsearch configuration found, skipping') + cnx.info(u'no elasticsearch configuration found, skipping') @@ -86,6 +95,7 @@ - def bulk_actions(self, etypes, cnx, - index_name=None, dry_run=False): + def bulk_actions(self, etypes, cnx, index_name=None, dry_run=False): + if index_name is None: + index_name = cnx.vreg.config['index-name'] for etype in etypes: rql = fulltext_indexable_rql(etype, cnx.vreg.schema) rset = cnx.execute(rql) @@ -89,9 +99,9 @@ for etype in etypes: rql = fulltext_indexable_rql(etype, cnx.vreg.schema) rset = cnx.execute(rql) - if self.config.debug: - print(u'indexing {} {}'.format(etype, len(rset))) - print(u'RQL : {}'.format(rql)) + cnx.info(u'[{}] indexing {} {} entities'.format(index_name, len(rset), etype)) + cnx.debug(u'RQL: {}'.format(rql)) + for entity in rset.entities(): serializer = entity.cw_adapt_to('IFullTextIndexSerializable') json = serializer.serialize()