示例#1
0
 def init(self):
     """Init DAS web server, connect to DAS Core"""
     try:
         self.dasmgr = DASCore(multitask=False)
         self.dbs_instances = self.dasmgr.mapping.dbs_instances()
         self.dbs_global = self.dasmgr.mapping.dbs_global_instance()
         if KeywordSearchHandler:
             self.kws = KeywordSearchHandler(self.dasmgr)
     except ConnectionFailure:
         tstamp = dastimestamp('')
         mythr = threading.current_thread()
         print("### MongoDB connection failure thread=%s, id=%s, time=%s" \
               % (mythr.name, mythr.ident, tstamp))
     except Exception as exc:
         print_exc(exc)
         self.dasmgr = None
         self.kws = None
示例#2
0
文件: kws_web_srv.py 项目: ktf/DAS
    def dasmap_reload_handler(self):
        """ reload KWS after DASMaps reloaded """
        print dastimestamp('KWS reloading on DASMaps reload')

        try:
            self.dbs_instances = self.dasmgr.mapping.dbs_instances()
            self.dbs_global = self.dasmgr.mapping.dbs_global_instance()
            self.kws = KeywordSearchHandler(self.dasmgr)
        except ConnectionFailure:
            tstamp = dastimestamp('')
            mythr = threading.current_thread()
            print "### MongoDB connection failure thread=%s, id=%s, time=%s" \
                  % (mythr.name, mythr.ident, tstamp)
        except Exception as exc:
            print_exc(exc)
            self.kws = None
示例#3
0
文件: kws_web_srv.py 项目: dmwm/DAS
 def init(self):
     """Init DAS web server, connect to DAS Core"""
     try:
         self.dasmgr = DASCore(multitask=False)
         self.dbs_instances = self.dasmgr.mapping.dbs_instances()
         self.dbs_global = self.dasmgr.mapping.dbs_global_instance()
         if  KeywordSearchHandler:
             self.kws = KeywordSearchHandler(self.dasmgr)
     except ConnectionFailure:
         tstamp = dastimestamp('')
         mythr = threading.current_thread()
         print("### MongoDB connection failure thread=%s, id=%s, time=%s" \
               % (mythr.name, mythr.ident, tstamp))
     except Exception as exc:
         print_exc(exc)
         self.dasmgr = None
         self.kws = None
示例#4
0
文件: kws_web_srv.py 项目: dmwm/DAS
class KWSWebService(DASWebManager):
    """
    DAS web service interface.
    """

    def __init__(self, dasconfig):
        DASWebManager.__init__(self, dasconfig)

        self.dasconfig = dasconfig
        self.dburi = self.dasconfig['mongodb']['dburi']
        self.dasmgr = None  # defined at run-time via self.init()
        self.kws = None  # defined at run-time via self.init()
        self.init()

        # Monitoring thread which performs auto-reconnection
        thname = 'dascore_monitor_kws'
        start_new_thread(thname, dascore_monitor, ({'das': self.dasmgr,
                                                    'uri': self.dburi},
                                                   self.init, 5))

    def init(self):
        """Init DAS web server, connect to DAS Core"""
        try:
            self.dasmgr = DASCore(multitask=False)
            self.dbs_instances = self.dasmgr.mapping.dbs_instances()
            self.dbs_global = self.dasmgr.mapping.dbs_global_instance()
            if  KeywordSearchHandler:
                self.kws = KeywordSearchHandler(self.dasmgr)
        except ConnectionFailure:
            tstamp = dastimestamp('')
            mythr = threading.current_thread()
            print("### MongoDB connection failure thread=%s, id=%s, time=%s" \
                  % (mythr.name, mythr.ident, tstamp))
        except Exception as exc:
            print_exc(exc)
            self.dasmgr = None
            self.kws = None

    @expose
    @tools.secmodv2()
    def index(self, *args, **kwargs):
        """Main page"""
        page = "DAS KWS Server"
        return self.page(page)

    def top(self):
        """
        Provide masthead for all web pages
        """
        return ''

    def bottom(self, div=""):
        """
        Provide footer for all web pages
        """
        return ''

    def _get_dbs_inst(self, inst):
        """
        returns dbsmngr for given instance
        """
        if inst in self.dbs_instances:
            return inst
        else:
            return self.dbs_global

    @expose
    @enable_cross_origin
    @checkargs(DAS_WEB_INPUTS)
    @tools.secmodv2()
    def kws_async(self, **kwargs):
        """
        Returns KeywordSearch results for AJAX call (for now as html snippet)
        """
        set_no_cache_flags()

        uinput = kwargs.get('input', '').strip()
        inst = kwargs.get('instance', '').strip()
        if not inst:
            return 'You must provide DBS instance name'

        # it is simplest to run KWS on cherrypy web threads, if more requests
        # come than size of thread pool; they'll have to wait. that looks OK...

        if not uinput:
            return 'The query must be not empty'

        if not self.kws:
            return 'Query suggestions are unavailable right now...'

        timeout = self.dasconfig['keyword_search']['timeout']
        show_scores = self.dasconfig['keyword_search'].get('show_scores', False)
        inst = self._get_dbs_inst(inst)  # validate dbs inst name or use default

        return self.kws.handle_search(self,
                                      query=uinput,
                                      dbs_inst=inst,
                                      is_ajax=True,
                                      timeout=timeout,
                                      show_score=show_scores)
示例#5
0
class KWSWebService(DASWebManager):
    """
    DAS web service interface.
    """
    def __init__(self, dasconfig):
        DASWebManager.__init__(self, dasconfig)

        self.dasconfig = dasconfig
        self.dburi = self.dasconfig['mongodb']['dburi']
        self.dasmgr = None  # defined at run-time via self.init()
        self.kws = None  # defined at run-time via self.init()
        self.init()

        # Monitoring thread which performs auto-reconnection
        thname = 'dascore_monitor_kws'
        start_new_thread(thname, dascore_monitor, ({
            'das': self.dasmgr,
            'uri': self.dburi
        }, self.init, 5))

    def init(self):
        """Init DAS web server, connect to DAS Core"""
        try:
            self.dasmgr = DASCore(multitask=False)
            self.dbs_instances = self.dasmgr.mapping.dbs_instances()
            self.dbs_global = self.dasmgr.mapping.dbs_global_instance()
            if KeywordSearchHandler:
                self.kws = KeywordSearchHandler(self.dasmgr)
        except ConnectionFailure:
            tstamp = dastimestamp('')
            mythr = threading.current_thread()
            print("### MongoDB connection failure thread=%s, id=%s, time=%s" \
                  % (mythr.name, mythr.ident, tstamp))
        except Exception as exc:
            print_exc(exc)
            self.dasmgr = None
            self.kws = None

    @expose
    @tools.secmodv2()
    def index(self, *args, **kwargs):
        """Main page"""
        page = "DAS KWS Server"
        return self.page(page)

    def top(self):
        """
        Provide masthead for all web pages
        """
        return ''

    def bottom(self, div=""):
        """
        Provide footer for all web pages
        """
        return ''

    def _get_dbs_inst(self, inst):
        """
        returns dbsmngr for given instance
        """
        if inst in self.dbs_instances:
            return inst
        else:
            return self.dbs_global

    @expose
    @enable_cross_origin
    @checkargs(DAS_WEB_INPUTS)
    @tools.secmodv2()
    def kws_async(self, **kwargs):
        """
        Returns KeywordSearch results for AJAX call (for now as html snippet)
        """
        set_no_cache_flags()

        uinput = kwargs.get('input', '').strip()
        inst = kwargs.get('instance', '').strip()
        if not inst:
            return 'You must provide DBS instance name'

        # it is simplest to run KWS on cherrypy web threads, if more requests
        # come than size of thread pool; they'll have to wait. that looks OK...

        if not uinput:
            return 'The query must be not empty'

        if not self.kws:
            return 'Query suggestions are unavailable right now...'

        timeout = self.dasconfig['keyword_search']['timeout']
        show_scores = self.dasconfig['keyword_search'].get(
            'show_scores', False)
        inst = self._get_dbs_inst(
            inst)  # validate dbs inst name or use default

        return self.kws.handle_search(self,
                                      query=uinput,
                                      dbs_inst=inst,
                                      is_ajax=True,
                                      timeout=timeout,
                                      show_score=show_scores)