<commit_before>from python_dashing.server.react import ReactServer
from python_dashing.scheduler import Scheduler

from tornado.httpserver import HTTPServer
from tornado.wsgi import WSGIContainer
from tornado.ioloop import IOLoop

from flask import send_from_directory, render_template, abort
from werkzeug.routing import PathConverter
from flask import Flask

from textwrap import dedent
import pkg_resources
import threading
import tempfile
import logging
import shutil
import flask
import time
import os

log = logging.getLogger("python_dashing.server")

here = os.path.dirname(__file__)

class Server(object):
    def __init__(self, host, port, debug, dashboards, modules, module_options, datastore, dynamic_dashboard_js, compiled_static_prep, compiled_static_folder, without_checks):
        self.thread_stopper = {"finished": False}

        self.host = host
        self.port = port
        self.modules = modules
        self.datastore = datastore
        self.dashboards = dashboards
        self.module_options = module_options
        self.without_checks = without_checks
        self.dynamic_dashboard_js = dynamic_dashboard_js

        self.compiled_static_prep = compiled_static_prep
        self.compiled_static_folder = compiled_static_folder

        self.react_server = ReactServer()

    def serve(self):
        http_server = HTTPServer(WSGIContainer(self.app))
        http_server.listen(self.port, self.host)
        log.info("Starting server on http://%s:%s", self.host, self.port)

        try:
            IOLoop.instance().start()
        finally:
            self.thread_stopper["finished"] = True

    def start_checks(self, scheduler, thread_stopper):
        first_run = True
        while True:
            if thread_stopper['finished']:
                break

            try:
                scheduler.run(self.datastore, force=first_run)
            except Exception:
                log.exception("Failed to run scheduler")

            first_run = False
            time.sleep(5)

    @property
    def app(self):
        if getattr(self, "_app", None) is None:
            self._app = Flask("python_dashing.server")

            # Remove auto generated static route
            while self._app.url_map._rules:
                self._app.url_map._rules.pop()
            for key in list(self._app.url_map._rules_by_endpoint):
                self._app.url_map._rules_by_endpoint.pop(key)
            self._app.url_map.update()
            self._app.view_functions.clear()

            self.servers = {}
            for name, module in self.modules.items():
                self.servers[name] = (module, module.make_server(self.module_options[name].server_options))

            scheduler = Scheduler()
            if not self.without_checks:
                for name, (module, server) in self.servers.items():
                    scheduler.register(module, server, name)

            checks_thread = threading.Thread(target=self.start_checks, args=(scheduler, self.thread_stopper, ))
            checks_thread.daemon = True
            checks_thread.start()

            # Register our own routes
            self.register_routes(self._app)

            if self.dynamic_dashboard_js:
                # Prepare the docker image for translating jsx into javascript
                deps = {}
                for _, module in sorted(self.modules.items()):
                    deps.update(module.npm_deps())
                self.react_server.prepare(deps, self.compiled_static_folder)
        return self._app

    def register_routes(self, app):
        class EverythingConverter(PathConverter):
            regex = '.*?'
        app.url_map.converters['everything'] = EverythingConverter

        make_view = lambda func: lambda *args, **kwargs : func(self.datastore, *args, **kwargs)
        for name, (_, server) in self.servers.items():
            for route_name, func in server.routes:
                view = make_view(func)
                view.__name__ = "server_route_{0}_{1}".format(name, route_name)
                app.route("/data/{0}/{1}".format(name, route_name))(view)

        @app.route("/diagnostic/status/heartbeat", methods=['GET'])<commit_msg>Allowing functions to return dictionary for jsonify<commit_after>from python_dashing.server.react import ReactServer
from python_dashing.scheduler import Scheduler

from tornado.httpserver import HTTPServer
from tornado.wsgi import WSGIContainer
from tornado.ioloop import IOLoop

from flask import send_from_directory, render_template, abort
from werkzeug.routing import PathConverter
from flask import Flask

from functools import wraps
from textwrap import dedent
import pkg_resources
import threading
import tempfile
import logging
import shutil
import flask
import time
import os

log = logging.getLogger("python_dashing.server")

here = os.path.dirname(__file__)

class Server(object):
    def __init__(self, host, port, debug, dashboards, modules, module_options, datastore, dynamic_dashboard_js, compiled_static_prep, compiled_static_folder, without_checks):
        self.thread_stopper = {"finished": False}

        self.host = host
        self.port = port
        self.modules = modules
        self.datastore = datastore
        self.dashboards = dashboards
        self.module_options = module_options
        self.without_checks = without_checks
        self.dynamic_dashboard_js = dynamic_dashboard_js

        self.compiled_static_prep = compiled_static_prep
        self.compiled_static_folder = compiled_static_folder

        self.react_server = ReactServer()

    def serve(self):
        http_server = HTTPServer(WSGIContainer(self.app))
        http_server.listen(self.port, self.host)
        log.info("Starting server on http://%s:%s", self.host, self.port)

        try:
            IOLoop.instance().start()
        finally:
            self.thread_stopper["finished"] = True

    def start_checks(self, scheduler, thread_stopper):
        first_run = True
        while True:
            if thread_stopper['finished']:
                break

            try:
                scheduler.run(self.datastore, force=first_run)
            except Exception:
                log.exception("Failed to run scheduler")

            first_run = False
            time.sleep(5)

    @property
    def app(self):
        if getattr(self, "_app", None) is None:
            self._app = Flask("python_dashing.server")

            # Remove auto generated static route
            while self._app.url_map._rules:
                self._app.url_map._rules.pop()
            for key in list(self._app.url_map._rules_by_endpoint):
                self._app.url_map._rules_by_endpoint.pop(key)
            self._app.url_map.update()
            self._app.view_functions.clear()

            self.servers = {}
            for name, module in self.modules.items():
                self.servers[name] = (module, module.make_server(self.module_options[name].server_options))

            scheduler = Scheduler()
            if not self.without_checks:
                for name, (module, server) in self.servers.items():
                    scheduler.register(module, server, name)

            checks_thread = threading.Thread(target=self.start_checks, args=(scheduler, self.thread_stopper, ))
            checks_thread.daemon = True
            checks_thread.start()

            # Register our own routes
            self.register_routes(self._app)

            if self.dynamic_dashboard_js:
                # Prepare the docker image for translating jsx into javascript
                deps = {}
                for _, module in sorted(self.modules.items()):
                    deps.update(module.npm_deps())
                self.react_server.prepare(deps, self.compiled_static_folder)
        return self._app

    def register_routes(self, app):
        class EverythingConverter(PathConverter):
            regex = '.*?'
        app.url_map.converters['everything'] = EverythingConverter

        def make_view(func):
            @wraps(func)
            def wrapper(*args, **kwargs):
                res = func(self.datastore, *args, **kwargs)
                if type(res) is dict:
                    return flask.jsonify(res)
                else:
                    return res
            return wrapper

        for name, (_, server) in self.servers.items():
            for route_name, func in server.routes:
                view = make_view(func)
                view.__name__ = "server_route_{0}_{1}".format(name, route_name)
                app.route("/data/{0}/{1}".format(name, route_name))(view)

        @app.route("/diagnostic/status/heartbeat", methods=['GET'])
{'fraction_non_alphanumeric': 0.09006103881147069, 'fraction_numerical': 0.0011516756881262238, 'mean_word_length': 2.352895752895753, 'pattern_counts': {'":': 2, '<': 3, '<?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': '3605202', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2345, 'n_words': 533}