<commit_before>from datetime import datetime
from time import time
from flask import flash, redirect, render_template, url_for, jsonify
from flask_login import login_required

from . import monitor

from app.models import Configuration, Alert, AlertType
from app.common import check_alarm_setter, prepare_map_for_monitoring
from app import db
from app.monitor.forms import AlertsFilterForm

#TODO split in two functions: general home + AJAX for alert list refresh (with filters)
@monitor.route('/home', methods = ['GET', 'POST'])
@login_required
def home():
    # Find current configuration
    #FIXME handle situation where there is no current configuration setup yet!
    current_config = Configuration.query.filter_by(current = True).first()
    # Check alerts filter form
    filter_form = AlertsFilterForm(prefix = 'alert_filter_', latest_id = '-1')
    # Set default active tab
    if filter_form.is_submitted():
        active_tab = 'alerts'
    else:
        active_tab = 'maps'
    if filter_form.validate_on_submit():
        alerts = filter_alerts(current_config.id, filter_form)
    else:
        # Limit to last 30 days by default
        limit = datetime.fromtimestamp(time() - 30 * 24 * 3600)
        filter_form.period_from.data = limit
        alerts = Alert.query.filter_by(config_id = current_config.id).filter(Alert.when >= limit).order_by(Alert.when.desc()).all()
    if alerts:
        filter_form.latest_id.data = str(alerts[0].id)
    return render_template('monitor/home.html', 
        configuration = current_config,
        filter_form = filter_form,
        alerts = alerts,
        active_tab = active_tab,
        svg_map = prepare_map_for_monitoring(current_config))

def filter_alerts(config_id, filter_form, limit = False):
    latest_id = int(filter_form.latest_id.data)
    query = Alert.query.filter_by(config_id = config_id)
    if limit:
        query = query.filter(Alert.id > latest_id)
    if filter_form.period_from.data:
        print('filter_alerts(when >= %s)' % str(filter_form.period_from.data))
        query = query.filter(Alert.when >= filter_form.period_from.data)
    if filter_form.period_to.data:
        print('filter_alerts(when <= %s)' % str(filter_form.period_to.data))
        query = query.filter(Alert.when <= filter_form.period_to.data)
    if filter_form.alert_level.data and filter_form.alert_level.data != 0:
        print('filter_alerts(level = %d)' % filter_form.alert_level.data)
        query = query.filter_by(level = filter_form.alert_level.data)
    if filter_form.alert_type.data and filter_form.alert_type.data != 0:
        print('filter_alerts(type = %d)' % filter_form.alert_type.data)
        query = query.filter_by(alert_type = filter_form.alert_type.data)
    return query.order_by(Alert.when.desc()).all()
    
@monitor.route('/refresh_alerts', methods = ['POST'])
@login_required
def refresh_alerts():
    # Find current configuration
    current_config = Configuration.query.filter_by(current = True).first()
    # Check alerts filter form
    filter_form = AlertsFilterForm(prefix = 'alert_filter_')
    alerts = filter_alerts(current_config.id, filter_form, limit = True)
    latest_id = int(filter_form.latest_id.data)
    alerts_display = []
    # Prepare rendering of new alerts so they are ready to integrate into the DOM on JS side
    if alerts:
        latest_id = alerts[0].id
        alerts_display = [render_template('monitor/alerts.html', alert = alert) for alert in alerts]
    return jsonify(alerts = alerts_display, latest_id = latest_id)

@monitor.route('/activate')
@login_required
def activate_config():
    return set_config_active(True)<commit_msg>Removed obsolete traces.<commit_after>from datetime import datetime
from time import time
from flask import flash, redirect, render_template, url_for, jsonify
from flask_login import login_required

from . import monitor

from app.models import Configuration, Alert, AlertType
from app.common import check_alarm_setter, prepare_map_for_monitoring
from app import db
from app.monitor.forms import AlertsFilterForm

#TODO split in two functions: general home + AJAX for alert list refresh (with filters)
@monitor.route('/home', methods = ['GET', 'POST'])
@login_required
def home():
    # Find current configuration
    #FIXME handle situation where there is no current configuration setup yet!
    current_config = Configuration.query.filter_by(current = True).first()
    # Check alerts filter form
    filter_form = AlertsFilterForm(prefix = 'alert_filter_', latest_id = '-1')
    # Set default active tab
    if filter_form.is_submitted():
        active_tab = 'alerts'
    else:
        active_tab = 'maps'
    if filter_form.validate_on_submit():
        alerts = filter_alerts(current_config.id, filter_form)
    else:
        # Limit to last 30 days by default
        limit = datetime.fromtimestamp(time() - 30 * 24 * 3600)
        filter_form.period_from.data = limit
        alerts = Alert.query.filter_by(config_id = current_config.id).filter(Alert.when >= limit).order_by(Alert.when.desc()).all()
    if alerts:
        filter_form.latest_id.data = str(alerts[0].id)
    return render_template('monitor/home.html', 
        configuration = current_config,
        filter_form = filter_form,
        alerts = alerts,
        active_tab = active_tab,
        svg_map = prepare_map_for_monitoring(current_config))

def filter_alerts(config_id, filter_form, limit = False):
    query = Alert.query.filter_by(config_id = config_id)
    if limit:
        latest_id = int(filter_form.latest_id.data)
        query = query.filter(Alert.id > latest_id)
    if filter_form.period_from.data:
        query = query.filter(Alert.when >= filter_form.period_from.data)
    if filter_form.period_to.data:
        query = query.filter(Alert.when <= filter_form.period_to.data)
    if filter_form.alert_level.data and filter_form.alert_level.data != 0:
        query = query.filter_by(level = filter_form.alert_level.data)
    if filter_form.alert_type.data and filter_form.alert_type.data != 0:
        query = query.filter_by(alert_type = filter_form.alert_type.data)
    return query.order_by(Alert.when.desc()).all()
    
@monitor.route('/refresh_alerts', methods = ['POST'])
@login_required
def refresh_alerts():
    # Find current configuration
    current_config = Configuration.query.filter_by(current = True).first()
    # Check alerts filter form
    filter_form = AlertsFilterForm(prefix = 'alert_filter_')
    alerts = filter_alerts(current_config.id, filter_form, limit = True)
    latest_id = int(filter_form.latest_id.data)
    alerts_display = []
    # Prepare rendering of new alerts so they are ready to integrate into the DOM on JS side
    if alerts:
        latest_id = alerts[0].id
        alerts_display = [render_template('monitor/alerts.html', alert = alert) for alert in alerts]
    return jsonify(alerts = alerts_display, latest_id = latest_id)

@monitor.route('/activate')
@login_required
def activate_config():
    return set_config_active(True)
{'fraction_non_alphanumeric': 0.09813416892180601, 'fraction_numerical': 0.004272895598917533, 'mean_word_length': 3.8628808864265927, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, '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': '7465747', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2092, 'n_words': 513}