<commit_before>__author__ = 'Lesko'
# Documentation is like sex.
# When it's good, it's very good.
# When it's bad, it's better than nothing.
# When it lies to you, it may be a while before you realize something's wrong.
from flask import render_template, redirect, flash, request, jsonify
from flask.ext.basicauth import BasicAuth
import sqlalchemy

from forms import AddEditNodeForm, SettingsForm, BackPeriodForm
from models import PingerData, LastEntry, Nodes, AppSettings
from common import stats
from app import app, db

from StringIO import StringIO
from pprint import pprint
import datetime
import csv
import os

basic_auth = BasicAuth(app)


@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html',
                           page_loc="404"), 404


@app.route('/')
@app.route('/home')
def home():
    return render_template("home.html", page_loc="home")


@app.route('/view_nodes', methods=["GET", "POST"])
def view_nodes():
    last_common_id, update_time = db.session.query(LastEntry.last_common_id, LastEntry.date_time). \
        filter(LastEntry.ready_to_read == True). \
        order_by(LastEntry.date_time.desc()).first()

    data = db.session.query(PingerData.up, Nodes.id, Nodes.name, Nodes.ip, Nodes.interface, Nodes.node_type).join(
        Nodes). \
        filter(PingerData.common_id == last_common_id). \
        filter(Nodes.in_use != False).order_by(Nodes.id.asc()).all()  # TODO: == True?

    return render_template("view_nodes.html",
                           data=data,
                           update_time=update_time,
                           page_loc="nodes - view")


@app.route("/node_details/<node_id>", methods=["GET", "POST"])
def show_node_details(node_id=None):
    # print node_id
    node_name = db.session.query(Nodes.name).filter(Nodes.id == node_id).first()[0]

    last_up_time = db.session.query(PingerData.node_id, PingerData.date_time). \
        filter(PingerData.up == "TRUE").filter(PingerData.node_id == node_id). \
        order_by(PingerData.date_time.desc()).first()
    pprint(last_up_time)

    last_down_time = db.session.query(PingerData.node_id, PingerData.date_time). \
        filter(PingerData.up == "FALSE").filter(PingerData.node_id == node_id). \
        order_by(PingerData.date_time.desc()).first()
    # pprint(last_down_time)

    mean_delay = db.session.query(PingerData.node_id,
                                  (db.func.sum(PingerData.delay) / db.func.count(PingerData.delay)) * 1000). \
        filter(PingerData.delay != None).filter(PingerData.node_id == node_id). \
        group_by(PingerData.node_id).all()
    # pprint(mean_delay)

    try:
        last_up_time = last_up_time[1]
    except IndexError:
        last_up_time = None
    try:
        last_down_time = last_down_time[1]
    except IndexError:
        last_down_time = None
    try:
        mean_delay = mean_delay[0][1]
    except IndexError:
        mean_delay = None
    return render_template("node_details.html",
                           node_id=node_id,
                           node_name=node_name,
                           last_up_time=last_up_time,
                           last_down_time=last_down_time,
                           mean_delay=mean_delay,
                           page_loc="nodes - details")


@app.route("/manage_nodes", methods=["GET", "POST"])
def manage_nodes():<commit_msg>Fixed bug created in previous commit ...  - BUG: database .all() -> .first() caused None as return if no data is available (node was never up/down)  - FIX: Error handling<commit_after>__author__ = 'Lesko'
# Documentation is like sex.
# When it's good, it's very good.
# When it's bad, it's better than nothing.
# When it lies to you, it may be a while before you realize something's wrong.
from flask import render_template, redirect, flash, request, jsonify
from flask.ext.basicauth import BasicAuth
import sqlalchemy

from forms import AddEditNodeForm, SettingsForm, BackPeriodForm
from models import PingerData, LastEntry, Nodes, AppSettings
from common import stats
from app import app, db

from StringIO import StringIO
from pprint import pprint
import datetime
import csv
import os

basic_auth = BasicAuth(app)


@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html',
                           page_loc="404"), 404


@app.route('/')
@app.route('/home')
def home():
    return render_template("home.html", page_loc="home")


@app.route('/view_nodes', methods=["GET", "POST"])
def view_nodes():
    last_common_id, update_time = db.session.query(LastEntry.last_common_id, LastEntry.date_time). \
        filter(LastEntry.ready_to_read == True). \
        order_by(LastEntry.date_time.desc()).first()

    data = db.session.query(PingerData.up, Nodes.id, Nodes.name, Nodes.ip, Nodes.interface, Nodes.node_type).join(
        Nodes). \
        filter(PingerData.common_id == last_common_id). \
        filter(Nodes.in_use != False).order_by(Nodes.id.asc()).all()  # TODO: == True?

    return render_template("view_nodes.html",
                           data=data,
                           update_time=update_time,
                           page_loc="nodes - view")


@app.route("/node_details/<node_id>", methods=["GET", "POST"])
def show_node_details(node_id=None):
    # print node_id
    node_name = db.session.query(Nodes.name).filter(Nodes.id == node_id).first()[0]

    last_up_time = db.session.query(PingerData.node_id, PingerData.date_time). \
        filter(PingerData.up == "TRUE").filter(PingerData.node_id == node_id). \
        order_by(PingerData.date_time.desc()).first()
    pprint(last_up_time)

    last_down_time = db.session.query(PingerData.node_id, PingerData.date_time). \
        filter(PingerData.up == "FALSE").filter(PingerData.node_id == node_id). \
        order_by(PingerData.date_time.desc()).first()
    pprint(last_down_time)

    mean_delay = db.session.query(PingerData.node_id,
                                  (db.func.sum(PingerData.delay) / db.func.count(PingerData.delay)) * 1000). \
        filter(PingerData.delay != None).filter(PingerData.node_id == node_id). \
        group_by(PingerData.node_id).all()
    # pprint(mean_delay)

    try:
        last_up_time = last_up_time[1]
    except (IndexError, TypeError):
        last_up_time = None

    try:
        last_down_time = last_down_time[1]
    except (IndexError, TypeError):
        last_down_time = None
    try:
        mean_delay = mean_delay[0][1]
    except IndexError:
        mean_delay = None
    return render_template("node_details.html",
                           node_id=node_id,
                           node_name=node_name,
                           last_up_time=last_up_time,
                           last_down_time=last_down_time,
                           mean_delay=mean_delay,
                           page_loc="nodes - details")


@app.route("/manage_nodes", methods=["GET", "POST"])
def manage_nodes():
{'fraction_non_alphanumeric': 0.11990235496840897, 'fraction_numerical': 0.0060310166570936245, 'mean_word_length': 2.7915078933043005, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3567065', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2180, 'n_words': 432}