<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import copy

import pygal
from django.db.models import Avg, Min, Count
from django.utils import timezone

from djangotrellostats.apps.boards.models import Card
from djangotrellostats.apps.charts.models import CachedChart
from djangotrellostats.apps.dev_times.models import DailySpentTime



from djangotrellostats.apps.base.auth import user_is_member, user_is_visitor, get_user_boards
from datetime import date
from djangotrellostats.utils.week import number_of_weeks_of_year, get_iso_week_of_year


# Average spent times
def avg_spent_times(request, board=None):

    # Caching
    chart_uuid = "labels.avg_spent_times-{0}".format(board.id if board else "None")
    try:
        chart = CachedChart.get(board=board, uuid=chart_uuid)
        return chart.render_django_response()
    except CachedChart.DoesNotExist:
        pass

    chart_title = u"Average task spent time as of {0}".format(timezone.now())
    if board:
        chart_title += u" for board {0}".format(board.name)

    avg_times_chart = pygal.HorizontalBar(title=chart_title, legend_at_bottom=True, print_values=True,
                                          print_zeroes=False, human_readable=True)

    if board:
        cards = board.cards.all()
        avg_spent_time = cards.aggregate(Avg("spent_time"))["spent_time__avg"]
        avg_times_chart.add(u"Average spent time", avg_spent_time)
    else:
        boards = get_user_boards(request.user)
        cards = Card.objects.filter(board__in=boards)
        avg_spent_time = cards.aggregate(Avg("spent_time"))["spent_time__avg"]
        avg_times_chart.add(u"All boards", avg_spent_time)
        for board in boards:
            board_avg_spent_time = board.cards.aggregate(Avg("spent_time"))["spent_time__avg"]
            avg_times_chart.add(u"{0}".format(board.name), board_avg_spent_time)

    if board:
        labels = board.labels.all()

        for label in labels:
            if label.name:
                avg_times_chart.add(u"{0} average spent time".format(label.name), label.avg_spent_time())

    chart = CachedChart.make(board=board, uuid=chart_uuid, svg=avg_times_chart.render(is_unicode=True))
    return chart.render_django_response()


# Average estimated times
def avg_estimated_times(request, board=None):

    # Caching
    chart_uuid = "labels.avg_estimated_times-{0}".format(board.id if board else "None")
    try:
        chart = CachedChart.get(board=board, uuid=chart_uuid)
        return chart.render_django_response()
    except CachedChart.DoesNotExist:
        pass

    chart_title = u"Average task estimated time as of {0}".format(timezone.now())
    if board:
        chart_title += u" for board {0}".format(board.name)

    avg_times_chart = pygal.HorizontalBar(title=chart_title, legend_at_bottom=True, print_values=True,
                                          print_zeroes=False, human_readable=True)

    if board:
        cards = board.cards.all()
        total_avg_estimated_time = cards.aggregate(Avg("estimated_time"))["estimated_time__avg"]
        avg_times_chart.add(u"Average estimated time", total_avg_estimated_time)
    else:
        boards = get_user_boards(request.user)
        cards = Card.objects.filter(board__in=boards)
        total_avg_estimated_time = cards.aggregate(Avg("estimated_time"))["estimated_time__avg"]
        avg_times_chart.add(u"All boards", total_avg_estimated_time)
        for board in boards:
            board_avg_estimated_time = board.cards.aggregate(Avg("estimated_time"))["estimated_time__avg"]
            avg_times_chart.add(u"{0}".format(board.name), board_avg_estimated_time)

    if board:
        labels = board.labels.all()

        for label in labels:
            if label.name:
                avg_times_chart.add(u"{0} average estimated time".format(label.name), label.avg_estimated_time())

    chart = CachedChart.make(board=board, uuid=chart_uuid, svg=avg_times_chart.render(is_unicode=True))
    return chart.render_django_response()


# Average spent time by month
def avg_estimated_time_by_month(board=None):
    return _daily_spent_times_by_period(board, "estimated_time")


# Average estimated time by month
def avg_spent_time_by_month(board=None):
    return _daily_spent_times_by_period(board, "spent_time")


# Number of cards worked on by month
def number_of_cards_worked_on_by_month(board=None):
    return _daily_spent_times_by_period(board, "spent_time", operation="Count")


# Number of cards worked on by week
def number_of_cards_worked_on_by_week(board=None):
    return _daily_spent_times_by_period(board, "spent_time", operation="Count", period="week")


# Average spent/estimated time by week/month
def _daily_spent_times_by_period(board=None, time_measurement="spent_time", operation="Avg", period="month"):

    # Caching
    chart_uuid = "labels._daily_spent_times_by_period-{0}-{1}-{2}-{3}".format(board.id if board else "None", time_measurement, operation, period)
    try:
        chart = CachedChart.get(board=board, uuid=chart_uuid)
        return chart.render_django_response()
    except CachedChart.DoesNotExist:
        pass

    daily_spent_time_filter = {"{0}__gt".format(time_measurement): 0}
    last_activity_datetime = timezone.now()
    if board:
        last_activity_datetime = board.last_activity_datetime
        daily_spent_time_filter["board"] = board

    if operation == "Avg":
        chart_title = u"Task average {1} as of {0}".format(last_activity_datetime, time_measurement.replace("_", " "))
        if board:
            chart_title += u" for board {0} (fetched on {1})".format(board.name, board.get_human_fetch_datetime())
    elif operation == "Count":
        chart_title = u"Tasks worked on as of {0}".format(last_activity_datetime)
        if board:
            chart_title += u" for board {0} (fetched on {1})".format(board.name, board.get_human_fetch_datetime())
    else:
        ValueError(u"Operation not valid only Avg and Count values are valid")

    period_measurement_chart = pygal.StackedBar(title=chart_title, legend_at_bottom=True, print_values=True,
                                                print_zeroes=False,
                                                human_readable=True)
    labels = []
    if board:
        labels = board.labels.all()

    date_i = copy.deepcopy(
        DailySpentTime.objects.filter(**daily_spent_time_filter).aggregate(min_date=Min("date"))["min_date"]
    )
    if date_i is None:
        return period_measurement_chart.render_django_response()

    month_i = date_i.month
    week_i = get_iso_week_of_year(date_i)
    year_i = date_i.year

    if operation == "Avg":
        aggregation = Avg
    elif operation == "Count":
        aggregation = Count
    else:
        ValueError(u"Operation not valid only Avg and Count values are valid")

    measurement_titles = []
    measurement_values = []

    label_measurement_titles = {label.id: [] for label in labels}
    label_measurement_values = {label.id: [] for label in labels}

    first_loop = True<commit_msg>Rotating 45 degrees X axis labels<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import copy

import pygal
from django.db.models import Avg, Min, Count
from django.utils import timezone

from djangotrellostats.apps.boards.models import Card
from djangotrellostats.apps.charts.models import CachedChart
from djangotrellostats.apps.dev_times.models import DailySpentTime



from djangotrellostats.apps.base.auth import user_is_member, user_is_visitor, get_user_boards
from datetime import date
from djangotrellostats.utils.week import number_of_weeks_of_year, get_iso_week_of_year


# Average spent times
def avg_spent_times(request, board=None):

    # Caching
    chart_uuid = "labels.avg_spent_times-{0}".format(board.id if board else "None")
    try:
        chart = CachedChart.get(board=board, uuid=chart_uuid)
        return chart.render_django_response()
    except CachedChart.DoesNotExist:
        pass

    chart_title = u"Average task spent time as of {0}".format(timezone.now())
    if board:
        chart_title += u" for board {0}".format(board.name)

    avg_times_chart = pygal.HorizontalBar(title=chart_title, legend_at_bottom=True, print_values=True,
                                          print_zeroes=False, human_readable=True)

    if board:
        cards = board.cards.all()
        avg_spent_time = cards.aggregate(Avg("spent_time"))["spent_time__avg"]
        avg_times_chart.add(u"Average spent time", avg_spent_time)
    else:
        boards = get_user_boards(request.user)
        cards = Card.objects.filter(board__in=boards)
        avg_spent_time = cards.aggregate(Avg("spent_time"))["spent_time__avg"]
        avg_times_chart.add(u"All boards", avg_spent_time)
        for board in boards:
            board_avg_spent_time = board.cards.aggregate(Avg("spent_time"))["spent_time__avg"]
            avg_times_chart.add(u"{0}".format(board.name), board_avg_spent_time)

    if board:
        labels = board.labels.all()

        for label in labels:
            if label.name:
                avg_times_chart.add(u"{0} average spent time".format(label.name), label.avg_spent_time())

    chart = CachedChart.make(board=board, uuid=chart_uuid, svg=avg_times_chart.render(is_unicode=True))
    return chart.render_django_response()


# Average estimated times
def avg_estimated_times(request, board=None):

    # Caching
    chart_uuid = "labels.avg_estimated_times-{0}".format(board.id if board else "None")
    try:
        chart = CachedChart.get(board=board, uuid=chart_uuid)
        return chart.render_django_response()
    except CachedChart.DoesNotExist:
        pass

    chart_title = u"Average task estimated time as of {0}".format(timezone.now())
    if board:
        chart_title += u" for board {0}".format(board.name)

    avg_times_chart = pygal.HorizontalBar(title=chart_title, legend_at_bottom=True, print_values=True,
                                          print_zeroes=False, human_readable=True)

    if board:
        cards = board.cards.all()
        total_avg_estimated_time = cards.aggregate(Avg("estimated_time"))["estimated_time__avg"]
        avg_times_chart.add(u"Average estimated time", total_avg_estimated_time)
    else:
        boards = get_user_boards(request.user)
        cards = Card.objects.filter(board__in=boards)
        total_avg_estimated_time = cards.aggregate(Avg("estimated_time"))["estimated_time__avg"]
        avg_times_chart.add(u"All boards", total_avg_estimated_time)
        for board in boards:
            board_avg_estimated_time = board.cards.aggregate(Avg("estimated_time"))["estimated_time__avg"]
            avg_times_chart.add(u"{0}".format(board.name), board_avg_estimated_time)

    if board:
        labels = board.labels.all()

        for label in labels:
            if label.name:
                avg_times_chart.add(u"{0} average estimated time".format(label.name), label.avg_estimated_time())

    chart = CachedChart.make(board=board, uuid=chart_uuid, svg=avg_times_chart.render(is_unicode=True))
    return chart.render_django_response()


# Average spent time by month
def avg_estimated_time_by_month(board=None):
    return _daily_spent_times_by_period(board, "estimated_time")


# Average estimated time by month
def avg_spent_time_by_month(board=None):
    return _daily_spent_times_by_period(board, "spent_time")


# Number of cards worked on by month
def number_of_cards_worked_on_by_month(board=None):
    return _daily_spent_times_by_period(board, "spent_time", operation="Count")


# Number of cards worked on by week
def number_of_cards_worked_on_by_week(board=None):
    return _daily_spent_times_by_period(board, "spent_time", operation="Count", period="week")


# Average spent/estimated time by week/month
def _daily_spent_times_by_period(board=None, time_measurement="spent_time", operation="Avg", period="month"):

    # Caching
    chart_uuid = "labels._daily_spent_times_by_period-{0}-{1}-{2}-{3}".format(board.id if board else "None", time_measurement, operation, period)
    try:
        chart = CachedChart.get(board=board, uuid=chart_uuid)
        return chart.render_django_response()
    except CachedChart.DoesNotExist:
        pass

    daily_spent_time_filter = {"{0}__gt".format(time_measurement): 0}
    last_activity_datetime = timezone.now()
    if board:
        last_activity_datetime = board.last_activity_datetime
        daily_spent_time_filter["board"] = board

    if operation == "Avg":
        chart_title = u"Task average {1} as of {0}".format(last_activity_datetime, time_measurement.replace("_", " "))
        if board:
            chart_title += u" for board {0} (fetched on {1})".format(board.name, board.get_human_fetch_datetime())
    elif operation == "Count":
        chart_title = u"Tasks worked on as of {0}".format(last_activity_datetime)
        if board:
            chart_title += u" for board {0} (fetched on {1})".format(board.name, board.get_human_fetch_datetime())
    else:
        raise ValueError(u"Operation not valid only Avg and Count values are valid")

    period_measurement_chart = pygal.StackedBar(title=chart_title, legend_at_bottom=True, print_values=True,
                                                print_zeroes=False, x_label_rotation=45,
                                                human_readable=True)
    labels = []
    if board:
        labels = board.labels.all()

    date_i = copy.deepcopy(
        DailySpentTime.objects.filter(**daily_spent_time_filter).aggregate(min_date=Min("date"))["min_date"]
    )
    if date_i is None:
        return period_measurement_chart.render_django_response()

    month_i = date_i.month
    week_i = get_iso_week_of_year(date_i)
    year_i = date_i.year

    if operation == "Avg":
        aggregation = Avg
    elif operation == "Count":
        aggregation = Count
    else:
        ValueError(u"Operation not valid only Avg and Count values are valid")

    measurement_titles = []
    measurement_values = []

    label_measurement_titles = {label.id: [] for label in labels}
    label_measurement_values = {label.id: [] for label in labels}

    first_loop = True
{'fraction_non_alphanumeric': 0.10059795990151249, 'fraction_numerical': 0.003658107632782272, 'mean_word_length': 3.5331632653061225, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4032071', 'n_tokens_mistral': 4627, 'n_tokens_neox': 4479, 'n_words': 878}