<commit_before>import os
import os.path

import sockjs.tornado

import tornado.auth
import tornado.escape
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.options

import zmq
import pymongo

import multiprocessing

import panoptes.admin.web.uimodules as uimodules
import panoptes.admin.web.handlers.base as handlers
import panoptes.admin.web.handlers.messaging_connection as mc
import panoptes.admin.web.handlers.login as login

from panoptes.utils import config, database, logger


@logger.has_logger
@config.has_config
class BaseApp(tornado.web.Application):

    """ The main Application entry for our PANOPTES admin interface """

    def __init__(self, debug=False):

        db = database.PanMongo()

        # Setup up our communication socket to listen to Observatory broker
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REQ)
        self.socket.connect("tcp://localhost:5559")

        AdminRouter = sockjs.tornado.SockJSRouter(
            mc.MessagingConnection,
            '/messaging_conn',
            user_settings=dict(db=db, socket=self.socket),
        )

        app_handlers = [
            (r"/", handlers.MainHandler),
            (r"/login", login.LoginHandler),
            (r"/logout", login.LogoutHandler),
        ] + AdminRouter.urls

        settings = dict(
            cookie_secret="PANOPTES_SUPER_DOOPER_SECRET",
            login_url="/login",
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            xsrf_cookies=True,
            db=db,
            debug=debug,
            site_title="PANOPTES",
            ui_modules=uimodules,
        )

        super().__init__(app_handlers, **settings)

@logger.has_logger
@config.has_config
class Application(BaseApp):

    """ Class that controls the web admin interface.

    Responsible for putting the web interface into a separate
    process and controlling how it stops/starts.
    """

    def __init__(self):
        self._processes = list()

        webapp_process = multiprocessing.Process(target=self.loop_capture, args=[])
        webapp_process.daemon = True
        webapp_process.name = 'web_admin_process'
        self._processes.append(webapp_process)

    def loop_capture(self):
        """ Starts the loop capture """
        port = self.config.get('admin').get('web').get('port', 8888)
        debug = self.config.get('admin').get('web').get('debug', False)

        http_server = tornado.httpserver.HTTPServer(BaseApp(debug=debug))
        http_server.listen(port)
        tornado.ioloop.IOLoop.instance().start()

    def start_app(self):
        """ Starts the web admin app """

        for process in self._processes:
            self.logger.info("Staring admin interface process {}".format(process.name))
            process.start()

    def stop_app(self):
        """ Stops the web app """
        for process in self._processes:
            self.logger.info("Stopping admin interface {}".format(process.name))
            process.terminate()
            process.join()

if __name__ == '__main__':
    web = Application()
    web.start_app()<commit_msg>Adding log handlers for web; about to split<commit_after>import os
import os.path

import sockjs.tornado

import tornado.auth
import tornado.escape
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.options

import zmq
import pymongo

import multiprocessing

import panoptes.admin.web.uimodules as uimodules
import panoptes.admin.web.handlers.base as handlers
import panoptes.admin.web.handlers.messaging_connection as mc
import panoptes.admin.web.handlers.login as login

from panoptes.utils import config, database, logger


@logger.has_logger
@config.has_config
class BaseApp(tornado.web.Application):

    """ The main Application entry for our PANOPTES admin interface """

    def __init__(self, debug=False):

        db = database.PanMongo()

        # Setup up our communication socket to listen to Observatory broker
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REQ)
        self.socket.connect("tcp://localhost:5559")

        AdminRouter = sockjs.tornado.SockJSRouter(
            mc.MessagingConnection,
            '/messaging_conn',
            user_settings=dict(db=db, socket=self.socket),
        )

        app_handlers = [
            (r"/", handlers.MainHandler),
            (r"/login", login.LoginHandler),
            (r"/logout", login.LogoutHandler),
        ] + AdminRouter.urls

        settings = dict(
            cookie_secret="PANOPTES_SUPER_DOOPER_SECRET",
            login_url="/login",
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            xsrf_cookies=True,
            db=db,
            debug=debug,
            site_title="PANOPTES",
            ui_modules=uimodules,
            compress_response=True,
            autoreload = True,
        )

        super().__init__(app_handlers, **settings)

    def log_request(self, handler):
        """ Override the log handler so we can write the log to panoptes log """
        request_time = 1000.0 * handler.request.request_time()

        if handler.get_status() < 400:
            log_method = self.logger.debug
        elif handler.get_status() < 500:
            log_method = self.logger.warning
        else:
            log_method = self.logger.error

        request_time = 1000.0 * handler.request.request_time()

        log_method("{} {} {:.2f}ms".format(
            handler.get_status(), handler._request_summary(), request_time
        ))


@logger.has_logger
@config.has_config
class Application(BaseApp):

    """ Class that controls the web admin interface.

    Responsible for putting the web interface into a separate
    process and controlling how it stops/starts.
    """

    def __init__(self):
        self._processes = list()

        webapp_process = multiprocessing.Process(target=self.loop_capture, args=[])
        webapp_process.daemon = True
        webapp_process.name = 'web_admin_process'
        self._processes.append(webapp_process)

    def loop_capture(self):
        """ Starts the loop capture """
        port = self.config.get('admin').get('web').get('port', 8888)
        debug = self.config.get('admin').get('web').get('debug', False)

        http_server = tornado.httpserver.HTTPServer(BaseApp(debug=debug))
        http_server.listen(port)
        tornado.ioloop.IOLoop.instance().start()

    def start_app(self):
        """ Starts the web admin app """

        for process in self._processes:
            self.logger.info("Staring admin interface process {}".format(process.name))
            process.start()

    def stop_app(self):
        """ Stops the web app """
        for process in self._processes:
            self.logger.info("Stopping admin interface {}".format(process.name))
            process.terminate()
            process.join()

{'fraction_non_alphanumeric': 0.10842356911826748, 'fraction_numerical': 0.004640697510898608, 'mean_word_length': 2.667870036101083, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3681354', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2024, 'n_words': 445}