<commit_before>import sys
import os
from json import dumps
import socket
import argparse
from konfig import Config

import bottle
from bottle import (route, SimpleTemplate, request,
                    app as _app, TEMPLATE_PATH, static_file,
                    abort, redirect)

import gevent
from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketHandler, WebSocketError

from loadsweb.controller import Controller
from loads.transport.client import TimeoutError


_TMPL = os.path.join(os.path.dirname(__file__), 'templates')
_MEDIA = os.path.join(os.path.dirname(__file__), 'media')

TEMPLATE_PATH.append(_TMPL)


def render(name, **options):
    with open(os.path.join(_TMPL, name + '.tmpl')) as f:

        return SimpleTemplate(f.read(), lookup=[_TMPL]).render(**options)


@route('/')
def handle_index():
    if not app.controller.ping_db():
        # the DB is down.
        # XXX status code ?
        # XXX redirect w/
        app.controller.reconnect()
        return render('error', message='The DB seems down')

    try:
        info = app.controller.get_broker_info()
    except TimeoutError:
        # the broker is down.
        # XXX status code ?
        # XXX redirect w/
        app.controller.reconnect()
        return render('error', message='The Broker seems down')

    runs, inactives = _get_runs(size=10)
    return render('index', runs=runs, inactives=inactives,
                  controller=app.controller,
                  broker_info=info,
                  wsserver=app.config['wsserver'],
                  wsport=app.config['wsport'])


def _get_runs(size=10):
    """Returns the last :param size: runs.

    Return a list of active and inactive runs.
    """
    def _dated(run_id):
        info = app.controller.get_run_info(run_id, data=False)
        started = info['metadata'].get('started', 0)
        fqn = info['metadata']['fqn']
        return started, fqn, run_id, info

    runs = [_dated(run) for run in app.controller.get_runs(active=True,
                                                           data=False)]
    runs.sort()

    inactives = [_dated(run) for run in app.controller.get_runs(stopped=True,
                                                                data=False)]
    inactives.sort()
    inactives.reverse()
    return runs[:size], inactives[:size]


@route('/run/<run_id>')
def handle_run(run_id=None):
    info = app.controller.get_run_info(run_id)

    return render('run', run_id=run_id,
                  info=info, active=info['metadata'].get('active', False),
                  controller=app.controller,
                  wsserver=app.config['wsserver'],<commit_msg>display a default value for fqn<commit_after>import sys
import os
from json import dumps
import socket
import argparse
from konfig import Config

import bottle
from bottle import (route, SimpleTemplate, request,
                    app as _app, TEMPLATE_PATH, static_file,
                    abort, redirect)

import gevent
from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketHandler, WebSocketError

from loadsweb.controller import Controller
from loads.transport.client import TimeoutError


_TMPL = os.path.join(os.path.dirname(__file__), 'templates')
_MEDIA = os.path.join(os.path.dirname(__file__), 'media')

TEMPLATE_PATH.append(_TMPL)


def render(name, **options):
    with open(os.path.join(_TMPL, name + '.tmpl')) as f:

        return SimpleTemplate(f.read(), lookup=[_TMPL]).render(**options)


@route('/')
def handle_index():
    if not app.controller.ping_db():
        # the DB is down.
        # XXX status code ?
        # XXX redirect w/
        app.controller.reconnect()
        return render('error', message='The DB seems down')

    try:
        info = app.controller.get_broker_info()
    except TimeoutError:
        # the broker is down.
        # XXX status code ?
        # XXX redirect w/
        app.controller.reconnect()
        return render('error', message='The Broker seems down')

    runs, inactives = _get_runs(size=10)
    return render('index', runs=runs, inactives=inactives,
                  controller=app.controller,
                  broker_info=info,
                  wsserver=app.config['wsserver'],
                  wsport=app.config['wsport'])


def _get_runs(size=10):
    """Returns the last :param size: runs.

    Return a list of active and inactive runs.
    """
    def _dated(run_id):
        info = app.controller.get_run_info(run_id, data=False)
        started = info['metadata'].get('started', 0)
        fqn = info['metadata'].get('fqn', '?')
        return started, fqn, run_id, info

    runs = [_dated(run) for run in app.controller.get_runs(active=True,
                                                           data=False)]
    runs.sort()

    inactives = [_dated(run) for run in app.controller.get_runs(stopped=True,
                                                                data=False)]
    inactives.sort()
    inactives.reverse()
    return runs[:size], inactives[:size]


@route('/run/<run_id>')
def handle_run(run_id=None):
    info = app.controller.get_run_info(run_id)

    return render('run', run_id=run_id,
                  info=info, active=info['metadata'].get('active', False),
                  controller=app.controller,
                  wsserver=app.config['wsserver'],
{'fraction_non_alphanumeric': 0.11302073603586774, 'fraction_numerical': 0.0018681113394358303, 'mean_word_length': 2.4541935483870967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1305758', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1545, 'n_words': 382}