<commit_before># -*- coding: utf-8 -*-
"""
Defines views.
"""

import calendar
import logging
from flask import redirect, abort
from flask.ext.mako import MakoTemplates, render_template
from mako.exceptions import TopLevelLookupException

from presence_analyzer.main import app
from presence_analyzer.utils import (
    jsonify,
    get_data,
    mean,
    group_by_weekday,
    total_group_by_weekday,
    group_by_start_end,
    parse_xml
)

mako = MakoTemplates(app)

log = logging.getLogger(__name__)  # pylint: disable=invalid-name


@app.route('/')
def mainpage():
    """
    Redirects to front page.
    """
    return redirect('/presence_weekday')


@app.route('/<template>')
def viewer(template):
    """
    Redirects to template.
    """
    sites = {
        'mean_time_weekday': 'Presence mean time',
        'presence_weekday': 'Presence by weekday',
        'presence_start_end': 'Presence start-end',
        'presence_total_hour': 'User hours vs total hours'
    }

    try:
        return render_template(template+'.html', selected=template, base=sites)
    except TopLevelLookupException:
        abort(404)


@app.route('/api/v1/users', methods=['GET'])
@jsonify
def users_view():
    """
    Users listing for dropdown.
    """
    data = get_data()
    return [
        {'user_id': i, 'name': 'User {0}'.format(str(i))}
        for i in data.keys()
    ]


@app.route('/api/v2/users', methods=['GET'])
@jsonify
def users_view2():
    """
    Users listing for dropdown.
    """
    data = parse_xml()
    return [
        {
            'user_id': user_id,
            'name': data[user_id]['name'],
            'avatar': data[user_id]['avatar']
        }
        for user_id in data.keys()
    ]


@app.route('/api/v1/mean_time_weekday/<int:user_id>', methods=['GET'])
@jsonify
def mean_time_weekday_view(user_id):
    """
    Returns mean presence time of given user grouped by weekday.
    """
    data = get_data()
    if user_id not in data:
        log.debug('User %s not found!', user_id)
        abort(404)

    weekdays = group_by_weekday(data[user_id])
    result = [
        (calendar.day_abbr[weekday], mean(intervals))
        for weekday, intervals in enumerate(weekdays)
    ]

    return result


@app.route('/api/v1/presence_weekday/<int:user_id>', methods=['GET'])
@jsonify
def presence_weekday_view(user_id):
    """
    Returns total presence time of given user grouped by weekday.<commit_msg>[bug_5892] Fixed sorted by name<commit_after># -*- coding: utf-8 -*-
"""
Defines views.
"""

import calendar
import logging
import locale
from flask import redirect, abort
from flask.ext.mako import MakoTemplates, render_template
from mako.exceptions import TopLevelLookupException

from presence_analyzer.main import app
from presence_analyzer.utils import (
    jsonify,
    get_data,
    mean,
    group_by_weekday,
    total_group_by_weekday,
    group_by_start_end,
    parse_xml
)

mako = MakoTemplates(app)

log = logging.getLogger(__name__)  # pylint: disable=invalid-name


@app.route('/')
def mainpage():
    """
    Redirects to front page.
    """
    return redirect('/presence_weekday')


@app.route('/<template>')
def viewer(template):
    """
    Redirects to template.
    """
    sites = {
        'mean_time_weekday': 'Presence mean time',
        'presence_weekday': 'Presence by weekday',
        'presence_start_end': 'Presence start-end',
        'presence_total_hour': 'User hours vs total hours'
    }

    try:
        return render_template(template+'.html', selected=template, base=sites)
    except TopLevelLookupException:
        abort(404)


@app.route('/api/v1/users', methods=['GET'])
@jsonify
def users_view():
    """
    Users listing for dropdown.
    """
    data = get_data()
    return [
        {'user_id': i, 'name': 'User {0}'.format(str(i))}
        for i in data.keys()
    ]


@app.route('/api/v2/users', methods=['GET'])
@jsonify
def users_view2():
    """
    Users listing for dropdown.
    """
    locale.setlocale(locale.LC_COLLATE, "")
    data = parse_xml()
    values = [
        {
            'user_id': user_id,
            'name': data[user_id]['name'],
            'avatar': data[user_id]['avatar']
        }
        for user_id in data.keys()
    ]
    values.sort(key=lambda x: x['name'], cmp=locale.strcoll)

    return values


@app.route('/api/v1/mean_time_weekday/<int:user_id>', methods=['GET'])
@jsonify
def mean_time_weekday_view(user_id):
    """
    Returns mean presence time of given user grouped by weekday.
    """
    data = get_data()
    if user_id not in data:
        log.debug('User %s not found!', user_id)
        abort(404)

    weekdays = group_by_weekday(data[user_id])
    result = [
        (calendar.day_abbr[weekday], mean(intervals))
        for weekday, intervals in enumerate(weekdays)
    ]

    return result


@app.route('/api/v1/presence_weekday/<int:user_id>', methods=['GET'])
@jsonify
def presence_weekday_view(user_id):
    """
    Returns total presence time of given user grouped by weekday.
{'fraction_non_alphanumeric': 0.13484486873508353, 'fraction_numerical': 0.0059665871121718375, 'mean_word_length': 3.0393574297188755, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '5084764', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1652, 'n_words': 376}