Skip to content
Snippets Groups Projects
routes.py 5.59 KiB
Newer Older
Tamas Gal's avatar
Tamas Gal committed
from glob import glob
Tamas Gal's avatar
Tamas Gal committed
from os.path import basename, join, exists, splitext, getsize
from functools import wraps
Tamas Gal's avatar
Tamas Gal committed
from collections import OrderedDict
import toml
from flask import render_template, send_from_directory, request, Response
Tamas Gal's avatar
Tamas Gal committed
from app import app

CONFIG_PATH = "pipeline.toml"
Tamas Gal's avatar
Tamas Gal committed
PLOTS_PATH = "../plots"
Tamas Gal's avatar
Tamas Gal committed
LOGS_PATH = "../logs"
USERNAME = None
PASSWORD = None
Tamas Gal's avatar
Tamas Gal committed
app.config['FREEZER_DESTINATION'] = '../km3web'
Tamas Gal's avatar
Tamas Gal committed

Tamas Gal's avatar
Tamas Gal committed
PLOTS = [['dom_activity', 'dom_rates'], 'pmt_rates_du*', 'pmt_hrv_du*',
Tamas Gal's avatar
Tamas Gal committed
         ['trigger_rates'], ['ztplot', 'triggermap']]
Tamas Gal's avatar
Tamas Gal committed

AHRS_PLOTS = ['yaw_calib_du*', 'pitch_calib_du*', 'roll_calib_du*']
Tamas Gal's avatar
Tamas Gal committed
TRIGGER_PLOTS = [['trigger_rates'], ['trigger_rates_lin']]
Tamas Gal's avatar
Tamas Gal committed
K40_PLOTS = [['intradom'], ['angular_k40rate_distribution']]
Tamas Gal's avatar
Tamas Gal committed
RTTC_PLOTS = [['rttc']]
Tamas Gal's avatar
Tamas Gal committed
RECO_PLOTS = [['track_reco', 'ztplot_roy'], ['time_residuals']]
Tamas Gal's avatar
Tamas Gal committed
COMPACT_PLOTS = [['dom_activity', 'dom_rates', 'pmt_rates', 'pmt_hrv'],
                 ['trigger_rates', 'trigger_rates_lin'],
Tamas Gal's avatar
Tamas Gal committed
                 ['ztplot', 'ztplot_roy', 'triggermap']]
SN_PLOTS = [['sn_bg_histogram', 'sn_pk_history']]
RASP_PLOTS = [['dom_rates', 'ztplot', 'triggermap'],
              [
                  'pmt_rates_du2', 'pmt_rates_du3', 'pmt_rates_du4',
                  'pmt_rates_du5'
              ], ['trigger_rates', 'trigger_rates_lin']]
Tamas Gal's avatar
Tamas Gal committed

if exists(CONFIG_PATH):
    config = toml.load(CONFIG_PATH)
    if "WebServer" in config:
        print("Reading authentication information from '%s'" % CONFIG_PATH)
        USERNAME = config["WebServer"]["username"]
        PASSWORD = config["WebServer"]["password"]


def expand_wildcards(plot_layout):
    """Replace wildcard entries with list of files"""
    plots = []
Tamas Gal's avatar
Tamas Gal committed
    for row in plot_layout:
        if not isinstance(row, list) and '*' in row:
            plots.append(
                sorted([
                    splitext(basename(p))[0]
                    for p in glob(join(app.root_path, PLOTS_PATH, row))
                ]))
        else:
            plots.append(row)
    return plots


def check_auth(username, password):
    """This function is called to check if a username /
    password combination is valid.
    """
    if USERNAME is not None and PASSWORD is not None:
        return username == USERNAME and password == PASSWORD
    else:
        return True


def authenticate():
    """Sends a 401 response that enables basic auth"""
    return Response(
        'Could not verify your access level for that URL.\n'
        'You have to login with proper credentials', 401,
        {'WWW-Authenticate': 'Basic realm="Login Required"'})


def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            return authenticate()
        return f(*args, **kwargs)

    return decorated

Tamas Gal's avatar
Tamas Gal committed

@app.after_request
def add_header(r):
    """
    Disable caches.
    """
    r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    r.headers["Pragma"] = "no-cache"
    r.headers["Expires"] = "0"
    r.headers["Cache-Control"] = "public, max-age=0"
    return r
Tamas Gal's avatar
Tamas Gal committed

Tamas Gal's avatar
Tamas Gal committed

@app.route('/')
Tamas Gal's avatar
Tamas Gal committed
@app.route('/index.html')
@requires_auth
Tamas Gal's avatar
Tamas Gal committed
def index():
    return render_template('plots.html', plots=expand_wildcards(PLOTS))
Tamas Gal's avatar
Tamas Gal committed

Tamas Gal's avatar
Tamas Gal committed
@app.route('/ahrs.html')
@requires_auth
Tamas Gal's avatar
Tamas Gal committed
def ahrs():
    return render_template('plots.html', plots=expand_wildcards(AHRS_PLOTS))
Tamas Gal's avatar
Tamas Gal committed

Tamas Gal's avatar
Tamas Gal committed

Tamas Gal's avatar
Tamas Gal committed
@app.route('/reco.html')
@requires_auth
def reco():
    return render_template('plots.html', plots=expand_wildcards(RECO_PLOTS))
Tamas Gal's avatar
Tamas Gal committed

Tamas Gal's avatar
Tamas Gal committed
@app.route('/sn.html')
@requires_auth
def supernova():
    return render_template('plots.html', plots=expand_wildcards(SN_PLOTS))
Tamas Gal's avatar
Tamas Gal committed

Tamas Gal's avatar
Tamas Gal committed
@app.route('/compact.html')
@requires_auth
def compact():
    return render_template('plots.html', plots=expand_wildcards(COMPACT_PLOTS))
Tamas Gal's avatar
Tamas Gal committed
@app.route('/rttc.html')
@requires_auth
Tamas Gal's avatar
Tamas Gal committed
def rttc():
    return render_template(
        'plots.html',
        plots=expand_wildcards(RTTC_PLOTS),
Tamas Gal's avatar
Tamas Gal committed
        info=
        "Cable Round Trip Time calculated from realtime data provided by the "
Tamas Gal's avatar
Tamas Gal committed
        "Detector Manager. The red lines shows the median and the STD "
Tamas Gal's avatar
Tamas Gal committed
        "from the past 24 hours. "
Tamas Gal's avatar
Tamas Gal committed
        "RTTC = Cable_RTT - (TX_Slave + RX_Slave + TX_Master + RX_Master)")


# @app.route('/k40.html')
# @requires_auth
# def k40():
#     return render_template(
#         'plots.html',
#         plots=expand_wildcards(K40_PLOTS),
#         info="The first plot shows the intra-DOM calibration. "
#         "y-axis: delta_t [ns], x-axis: cosine of angles. "
#         "The second plot the angular distribution of K40 rates. "
#         "y-axis: rate [Hz], x-axis: cosine of angles. "
#         "blue=before, red=after")
Tamas Gal's avatar
Tamas Gal committed


Tamas Gal's avatar
Tamas Gal committed
@app.route('/trigger.html')
@requires_auth
Tamas Gal's avatar
Tamas Gal committed
def trigger():
    return render_template('plots.html', plots=expand_wildcards(TRIGGER_PLOTS))
Tamas Gal's avatar
Tamas Gal committed
@app.route('/logs.html')
@requires_auth
def logs():
Tamas Gal's avatar
Tamas Gal committed
    files = OrderedDict()
    for filename in sorted(glob(join(app.root_path, LOGS_PATH, "MSG*.log"))):
        files[basename(filename)] = getsize(filename)
    print(files)
    return render_template('logs.html', files=files)
Tamas Gal's avatar
Tamas Gal committed


@app.route('/logs/<path:filename>')
@requires_auth
def custom_static_logfile(filename):
    filepath = join(app.root_path, LOGS_PATH)
    print("Serving: {}/{}".format(filepath, filename))
    return send_from_directory(join(app.root_path, LOGS_PATH), filename)
Tamas Gal's avatar
Tamas Gal committed
@app.route('/plots/<path:filename>')
@requires_auth
Tamas Gal's avatar
Tamas Gal committed
def custom_static(filename):
Tamas Gal's avatar
Tamas Gal committed
    filepath = join(app.root_path, PLOTS_PATH)
Tamas Gal's avatar
Tamas Gal committed
    print("Serving: {}/{}".format(filepath, filename))
Tamas Gal's avatar
Tamas Gal committed
    return send_from_directory(join(app.root_path, PLOTS_PATH), filename)


@app.route('/rasp.html')
@requires_auth
def rasp():
    return render_template('plots.html', plots=expand_wildcards(RASP_PLOTS))