<commit_before>import argparse
import getpass
import json
import os
import sys
from collections import defaultdict
from datetime import date, datetime
from inspect import getsourcefile
from os.path import abspath
from pathlib import Path

from flask import Flask, render_template, request, make_response, redirect, url_for, Blueprint, abort

sys.path.append(str(Path(abspath(getsourcefile(lambda: 0))).parents[1]))

from gocddash import parse_cctray
from gocddash.util.config import create_pipeline_config
from gocddash.analysis.go_client import go_get_pipeline_groups, go_get_cctray, go_get_pipeline_status, create_go_client
from gocddash.console_parsers.git_blame_compare import get_git_comparison
from gocddash.dash_board import failure_tip, pipeline_status
from gocddash.analysis.data_access import get_connection, create_connection
from gocddash.analysis.domain import get_previous_stage, get_current_stage, get_latest_passing_stage, get_first_synced_stage, get_pipeline_heads

group_of_pipeline = defaultdict(str)

# Use a blueprint to allow URLs to be prefixed through
# APPLICATION_ROOT without running the app as a
# sub-mounted WSGI environment. See
# http://stackoverflow.com/questions/18967441/add-a-prefix-to-all-flask-routes
gocddash = Blueprint('gocddash', __name__)


def get_bootstrap_theme():
    theme = request.cookies.get('theme_cookie')
    return theme or 'paper'


def get_footer():
    try:
        return open('footer.txt').read()
    except IOError:
        return '???'


@gocddash.route("/", methods=['GET'])
def dashboard():
    which = request.args.get('which', 'failing')
    project = get_cctray_status()
    progress_bar_data = get_progress_bar_data(project)
    groups = request.cookies.get(
        'checked_pipeline_groups_cookie', '').split(',')
    pipelines = project.select(
        which, groups=groups, group_map=group_of_pipeline)
    for pipeline in pipelines:
        pipeline_name = pipeline.name
        message, whom = pipeline_is_paused(pipeline_name)
        if message:
            pipeline.status = 'Paused'
            pipeline.messages['PausedCause'].add(message)
        if whom:
            pipeline.messages['PausedBy'].add(whom)

    synced_pipelines = dict()
    for pipeline_head in get_pipeline_heads():
        synced_pipelines[pipeline_head.pipeline_name] = pipeline_head

    return render_template('index.html',
                           go_server_url=app.config['PUBLIC_GO_SERVER_URL'],
                           pipelines=pipelines,
                           theme=get_bootstrap_theme(),
                           cols=app.config['PIPELINE_COLUMNS'],
                           now=datetime.now(),
                           footer=get_footer(),
                           synced_pipelines=synced_pipelines,
                           progress_bar_data=progress_bar_data,
                           application_root=app.config['APPLICATION_ROOT'])


def get_progress_bar_data(project):
    no_failing_pipelines = float(len(project.select("failing")))
    no_progress_pipelines = float(len(project.select("progress")))
    total_no_pipelines = float(len(project.select("all")))

    success_percentage = (total_no_pipelines - no_progress_pipelines) / total_no_pipelines * 100
    progress_percentage = (no_progress_pipelines - no_failing_pipelines) / total_no_pipelines * 100
    failing_percentage = no_failing_pipelines / total_no_pipelines * 100

    return [success_percentage, progress_percentage, failing_percentage]


def get_cctray_status():
    xml = go_get_cctray()
    project = parse_cctray.Projects(xml)
    return project


@gocddash.app_errorhandler(500)
def internal_server_error(e):
    return render_template('500.html', statuscode=e, now=datetime.now(), footer=get_footer(), theme=get_bootstrap_theme(), dashboard_link=url_for('gocddash.dashboard')), 500


@gocddash.app_errorhandler(404)
def page_not_found(e):
    return render_template('404.html', statuscode=e, dashboard_link=url_for('gocddash.dashboard'), now=datetime.now(), footer=get_footer(), theme=get_bootstrap_theme()), 404

# Catches all errors - a bit too trigger happy
# @gocddash.app_errorhandler(Exception)
# def all_exception_handler(error):
#     return render_template('500.html', statuscode=error), 500


@gocddash.route("/select/", methods=['GET', 'POST'])
def select():
    all_pipeline_groups = get_all_pipeline_groups()
    # all_pipeline_groups is a list of
    # [ 'name', checked?, [pipelines names...] ]

    if request.method == 'POST':
        checked_pipeline_groups = list(request.form)
        response = redirect(url_for('gocddash.dashboard'), code=302)
        response.set_cookie('checked_pipeline_groups_cookie',
                            value=",".join(checked_pipeline_groups))
    else:
        checked_pipeline_groups = request.cookies.get(
            'checked_pipeline_groups_cookie', '').split(',')
        for i, pipeline_group in enumerate(all_pipeline_groups):
            if pipeline_group[0] in checked_pipeline_groups:
                all_pipeline_groups[i][1] = 'checked'
        template = render_template('select.html',
                                   go_server_url=app.config['PUBLIC_GO_SERVER_URL'],
                                   pipelinegroups=all_pipeline_groups,
                                   now=datetime.now(),
                                   theme=get_bootstrap_theme(),
                                   footer=get_footer())
        response = make_response(template)
    return response


@gocddash.route("/select_theme/", methods=['GET', 'POST'])
def select_theme():
    if request.method == 'POST':
        theme = request.form.get('theme_name')
        response = redirect(url_for('gocddash.select_theme'), code=302)
        response.set_cookie('theme_cookie',
                            value=theme or get_bootstrap_theme())
    else:
        template = render_template('select_theme.html',
                                   go_server_url=app.config['PUBLIC_GO_SERVER_URL'],
                                   now=datetime.now(),
                                   theme=get_bootstrap_theme(),
                                   footer=get_footer())
        response = make_response(template)
    return response


@gocddash.route("/claim/<stage_id>", methods=['POST'])
def claim_stage(stage_id):
    if not get_connection().claim_exists(stage_id):
        responsible = request.form.get('responsible')
        description = request.form.get('description')
        get_connection().insert_stage_claim(stage_id, responsible, description)
        return "OK."
    else:
        abort(409, "Already claimed.")


@gocddash.route("/insights/<pipeline_name>", methods=['GET'])
def insights(pipeline_name):
    current_stage = get_current_stage(pipeline_name)
    if current_stage is None:
        abort(500, "Database error. Have you tried syncing some pipelines using sync_pipelines.py? Current_stage is None.")
    current_status = pipeline_status.create_stage_info(current_stage)
    last_stage = get_previous_stage(current_stage)
    previous_status = pipeline_status.create_stage_info(last_stage)
    latest_passing_stage = get_latest_passing_stage(pipeline_name)
    stage_name_index = (get_connection().get_stage_order(pipeline_name)).index(current_stage.stage_name)

    if current_stage.is_success():
        git_blame_data = []
    elif latest_passing_stage is None:
        latest_passing_stage = get_first_synced_stage(pipeline_name)
        git_blame_data = get_git_comparison(pipeline_name, current_stage.pipeline_counter, latest_passing_stage)
    else:
        git_blame_data = get_git_comparison(pipeline_name, current_stage.pipeline_counter, latest_passing_stage.pipeline_counter)

    base_url = app.config['PUBLIC_GO_SERVER_URL']

    rerun_link = base_url + "pipelines/{}/{}/{}/{}".format(current_stage.pipeline_name,
                                                                          current_stage.pipeline_counter,
                                                                          current_stage.stage_name,
                                                                          current_stage.stage_counter)
    log_link = base_url + "tab/build/detail/{}/{}/{}/{}/{}#tab-tests".format(
        current_stage.pipeline_name, current_stage.pipeline_counter, current_stage.stage_name,
        current_stage.stage_counter, "defaultJob")

    main_pipeline_link = base_url + "tab/pipeline/history/{}".format(current_stage.pipeline_name)

    comparison_link = base_url + "compare/{}/{}/with/{}".format(current_stage.pipeline_name,
                                                                               current_stage.pipeline_counter,
                                                                               latest_passing_stage.pipeline_counter)

    dash_status = get_cctray_status()

    template = render_template(
        'insights.html',  # Defined in the templates folder
        go_server_url=app.config['PUBLIC_GO_SERVER_URL'],<commit_msg>Fixed accessing the wrong field<commit_after>import argparse
import getpass
import json
import os
import sys
from collections import defaultdict
from datetime import date, datetime
from inspect import getsourcefile
from os.path import abspath
from pathlib import Path

from flask import Flask, render_template, request, make_response, redirect, url_for, Blueprint, abort

sys.path.append(str(Path(abspath(getsourcefile(lambda: 0))).parents[1]))

from gocddash import parse_cctray
from gocddash.util.config import create_pipeline_config
from gocddash.analysis.go_client import go_get_pipeline_groups, go_get_cctray, go_get_pipeline_status, create_go_client
from gocddash.console_parsers.git_blame_compare import get_git_comparison
from gocddash.dash_board import failure_tip, pipeline_status
from gocddash.analysis.data_access import get_connection, create_connection
from gocddash.analysis.domain import get_previous_stage, get_current_stage, get_latest_passing_stage, get_first_synced_stage, get_pipeline_heads

group_of_pipeline = defaultdict(str)

# Use a blueprint to allow URLs to be prefixed through
# APPLICATION_ROOT without running the app as a
# sub-mounted WSGI environment. See
# http://stackoverflow.com/questions/18967441/add-a-prefix-to-all-flask-routes
gocddash = Blueprint('gocddash', __name__)


def get_bootstrap_theme():
    theme = request.cookies.get('theme_cookie')
    return theme or 'paper'


def get_footer():
    try:
        return open('footer.txt').read()
    except IOError:
        return '???'


@gocddash.route("/", methods=['GET'])
def dashboard():
    which = request.args.get('which', 'failing')
    project = get_cctray_status()
    progress_bar_data = get_progress_bar_data(project)
    groups = request.cookies.get(
        'checked_pipeline_groups_cookie', '').split(',')
    pipelines = project.select(
        which, groups=groups, group_map=group_of_pipeline)
    for pipeline in pipelines:
        pipeline_name = pipeline.name
        message, whom = pipeline_is_paused(pipeline_name)
        if message:
            pipeline.status = 'Paused'
            pipeline.messages['PausedCause'].add(message)
        if whom:
            pipeline.messages['PausedBy'].add(whom)

    synced_pipelines = dict()
    for pipeline_head in get_pipeline_heads():
        synced_pipelines[pipeline_head.pipeline_name] = pipeline_head

    return render_template('index.html',
                           go_server_url=app.config['PUBLIC_GO_SERVER_URL'],
                           pipelines=pipelines,
                           theme=get_bootstrap_theme(),
                           cols=app.config['PIPELINE_COLUMNS'],
                           now=datetime.now(),
                           footer=get_footer(),
                           synced_pipelines=synced_pipelines,
                           progress_bar_data=progress_bar_data,
                           application_root=app.config['APPLICATION_ROOT'])


def get_progress_bar_data(project):
    no_failing_pipelines = float(len(project.select("failing")))
    no_progress_pipelines = float(len(project.select("progress")))
    total_no_pipelines = float(len(project.select("all")))

    success_percentage = (total_no_pipelines - no_progress_pipelines) / total_no_pipelines * 100
    progress_percentage = (no_progress_pipelines - no_failing_pipelines) / total_no_pipelines * 100
    failing_percentage = no_failing_pipelines / total_no_pipelines * 100

    return [success_percentage, progress_percentage, failing_percentage]


def get_cctray_status():
    xml = go_get_cctray()
    project = parse_cctray.Projects(xml)
    return project


@gocddash.app_errorhandler(500)
def internal_server_error(e):
    return render_template('500.html', statuscode=e, now=datetime.now(), footer=get_footer(), theme=get_bootstrap_theme(), dashboard_link=url_for('gocddash.dashboard')), 500


@gocddash.app_errorhandler(404)
def page_not_found(e):
    return render_template('404.html', statuscode=e, dashboard_link=url_for('gocddash.dashboard'), now=datetime.now(), footer=get_footer(), theme=get_bootstrap_theme()), 404

# Catches all errors - a bit too trigger happy
# @gocddash.app_errorhandler(Exception)
# def all_exception_handler(error):
#     return render_template('500.html', statuscode=error), 500


@gocddash.route("/select/", methods=['GET', 'POST'])
def select():
    all_pipeline_groups = get_all_pipeline_groups()
    # all_pipeline_groups is a list of
    # [ 'name', checked?, [pipelines names...] ]

    if request.method == 'POST':
        checked_pipeline_groups = list(request.form)
        response = redirect(url_for('gocddash.dashboard'), code=302)
        response.set_cookie('checked_pipeline_groups_cookie',
                            value=",".join(checked_pipeline_groups))
    else:
        checked_pipeline_groups = request.cookies.get(
            'checked_pipeline_groups_cookie', '').split(',')
        for i, pipeline_group in enumerate(all_pipeline_groups):
            if pipeline_group[0] in checked_pipeline_groups:
                all_pipeline_groups[i][1] = 'checked'
        template = render_template('select.html',
                                   go_server_url=app.config['PUBLIC_GO_SERVER_URL'],
                                   pipelinegroups=all_pipeline_groups,
                                   now=datetime.now(),
                                   theme=get_bootstrap_theme(),
                                   footer=get_footer())
        response = make_response(template)
    return response


@gocddash.route("/select_theme/", methods=['GET', 'POST'])
def select_theme():
    if request.method == 'POST':
        theme = request.form.get('theme_name')
        response = redirect(url_for('gocddash.select_theme'), code=302)
        response.set_cookie('theme_cookie',
                            value=theme or get_bootstrap_theme())
    else:
        template = render_template('select_theme.html',
                                   go_server_url=app.config['PUBLIC_GO_SERVER_URL'],
                                   now=datetime.now(),
                                   theme=get_bootstrap_theme(),
                                   footer=get_footer())
        response = make_response(template)
    return response


@gocddash.route("/claim/<stage_id>", methods=['POST'])
def claim_stage(stage_id):
    if not get_connection().claim_exists(stage_id):
        responsible = request.form.get('responsible')
        description = request.form.get('description')
        get_connection().insert_stage_claim(stage_id, responsible, description)
        return "OK."
    else:
        abort(409, "Already claimed.")


@gocddash.route("/insights/<pipeline_name>", methods=['GET'])
def insights(pipeline_name):
    current_stage = get_current_stage(pipeline_name)
    if current_stage is None:
        abort(500, "Database error. Have you tried syncing some pipelines using sync_pipelines.py? Current_stage is None.")
    current_status = pipeline_status.create_stage_info(current_stage)
    last_stage = get_previous_stage(current_stage)
    previous_status = pipeline_status.create_stage_info(last_stage)
    latest_passing_stage = get_latest_passing_stage(pipeline_name)
    stage_name_index = (get_connection().get_stage_order(pipeline_name)).index(current_stage.stage_name)

    if current_stage.is_success():
        git_blame_data = []
    elif latest_passing_stage is None:
        latest_passing_stage = get_first_synced_stage(pipeline_name)
        git_blame_data = get_git_comparison(pipeline_name, current_stage.pipeline_counter, latest_passing_stage.pipeline_counter)
    else:
        git_blame_data = get_git_comparison(pipeline_name, current_stage.pipeline_counter, latest_passing_stage.pipeline_counter)

    base_url = app.config['PUBLIC_GO_SERVER_URL']

    rerun_link = base_url + "pipelines/{}/{}/{}/{}".format(current_stage.pipeline_name,
                                                                          current_stage.pipeline_counter,
                                                                          current_stage.stage_name,
                                                                          current_stage.stage_counter)
    log_link = base_url + "tab/build/detail/{}/{}/{}/{}/{}#tab-tests".format(
        current_stage.pipeline_name, current_stage.pipeline_counter, current_stage.stage_name,
        current_stage.stage_counter, "defaultJob")

    main_pipeline_link = base_url + "tab/pipeline/history/{}".format(current_stage.pipeline_name)

    comparison_link = base_url + "compare/{}/{}/with/{}".format(current_stage.pipeline_name,
                                                                               current_stage.pipeline_counter,
                                                                               latest_passing_stage.pipeline_counter)

    dash_status = get_cctray_status()

    template = render_template(
        'insights.html',  # Defined in the templates folder
        go_server_url=app.config['PUBLIC_GO_SERVER_URL'],
{'fraction_non_alphanumeric': 0.09645288030546179, 'fraction_numerical': 0.006308450002766864, 'mean_word_length': 2.9389712292938097, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '456115', 'n_tokens_mistral': 5280, 'n_tokens_neox': 5131, 'n_words': 881}