<commit_before>import numpy
from braces.views import CsrfExemptMixin
from braces.views import JsonRequestResponseMixin
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views import View
from django.views.generic import TemplateView

from core.managers.panda_manager import PandaManager, TransferFilter


@method_decorator(login_required, name='dispatch')
class TransfersDetailChartView(CsrfExemptMixin, JsonRequestResponseMixin, View):

    def get(self, request):
        group_by = request.GET.get('group_by', default='Strength')

        ages = _to_int_list(request.GET.get('ages', default=None))
        strengths = _to_int_list(request.GET.get('strengths', default=None))
        positions = _to_list(request.GET.get('positions', default=None))
        seasons = _to_int_list(request.GET.get('seasons', default=None))
        matchdays = _to_int_list(request.GET.get('matchdays', default=None))
        min_price = _to_int(request.GET.get('min_price', default=None))
        max_price = _to_int(request.GET.get('max_price', default=None))

        if positions == 'All':
            positions = None

        panda_manager = PandaManager()

        ungrouped_dataframe = panda_manager.filter_transfers(TransferFilter(ages=ages,
                                                                            strengths=strengths,
                                                                            positions=positions,
                                                                            seasons=seasons,
                                                                            matchdays=matchdays,
                                                                            min_price=min_price,
                                                                            max_price=max_price,))

        prices = panda_manager.get_grouped_prices(group_by,
                                                  ages=ages,
                                                  strengths=strengths,
                                                  positions=positions,
                                                  seasons=seasons,
                                                  matchdays=matchdays,
                                                  min_price=min_price,
                                                  max_price=max_price,
                                                  )

        chart_json = {
            "series": [
                {
                    "name": 'Preise',
                    "data": self._get_data_from_dataframe(prices)
                },
            ],
            "categories": self.convert_to_json_serializable_list(prices),
            "ages": list(map(int, list(ungrouped_dataframe.groupby('Age').Age.nunique().index))),
            "strengths": list(map(int, list(ungrouped_dataframe.groupby('Strength').Strength.nunique().index))),
            "positions": list(ungrouped_dataframe.groupby('Position').Position.nunique().index),
            "seasons": list(map(int, list(ungrouped_dataframe.groupby('Season').Season.nunique().index))),
            "matchdays": list(map(int, list(ungrouped_dataframe.groupby('Matchday').Matchday.nunique().index))),
        }

        return self.render_json_response(chart_json)

    @staticmethod
    def convert_to_json_serializable_list(prices):
        try:
            int(numpy.array(prices.mean().index[0]))
            return list(map(int, numpy.array(prices.mean().index)))
        except TypeError:
            return list(map(str, numpy.array(prices.mean().index)))

    @staticmethod
    def _get_data_from_dataframe(prices):
        mins = prices.min()
        quantiles = prices.quantile([0.25, 0.75])
        medians = prices.median()
        maxs = prices.max()
        data = []
        for x_index in numpy.array(prices.mean().index):
            data.append([float(mins[x_index]),
                         float(quantiles[x_index][0.25]),
                         float(medians[x_index]),
                         float(quantiles[x_index][0.75]),
                         float(maxs[x_index])
                         ])
        return data


@method_decorator(login_required, name='dispatch')<commit_msg>#19 don't throw 500 error if empty result set<commit_after>import numpy
from braces.views import CsrfExemptMixin
from braces.views import JsonRequestResponseMixin
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views import View
from django.views.generic import TemplateView

from core.managers.panda_manager import PandaManager, TransferFilter


@method_decorator(login_required, name='dispatch')
class TransfersDetailChartView(CsrfExemptMixin, JsonRequestResponseMixin, View):

    def get(self, request):
        group_by = request.GET.get('group_by', default='Strength')

        ages = _to_int_list(request.GET.get('ages', default=None))
        strengths = _to_int_list(request.GET.get('strengths', default=None))
        positions = _to_list(request.GET.get('positions', default=None))
        seasons = _to_int_list(request.GET.get('seasons', default=None))
        matchdays = _to_int_list(request.GET.get('matchdays', default=None))
        min_price = _to_int(request.GET.get('min_price', default=None))
        max_price = _to_int(request.GET.get('max_price', default=None))

        if positions == 'All':
            positions = None

        panda_manager = PandaManager()

        ungrouped_dataframe = panda_manager.filter_transfers(TransferFilter(ages=ages,
                                                                            strengths=strengths,
                                                                            positions=positions,
                                                                            seasons=seasons,
                                                                            matchdays=matchdays,
                                                                            min_price=min_price,
                                                                            max_price=max_price,))

        prices = panda_manager.get_grouped_prices(group_by,
                                                  ages=ages,
                                                  strengths=strengths,
                                                  positions=positions,
                                                  seasons=seasons,
                                                  matchdays=matchdays,
                                                  min_price=min_price,
                                                  max_price=max_price,
                                                  )

        chart_json = {
            "series": [
                {
                    "name": 'Preise',
                    "data": self._get_data_from_dataframe(prices)
                },
            ],
            "categories": self.convert_to_json_serializable_list(prices),
            "ages": list(map(int, list(ungrouped_dataframe.groupby('Age').Age.nunique().index))),
            "strengths": list(map(int, list(ungrouped_dataframe.groupby('Strength').Strength.nunique().index))),
            "positions": list(ungrouped_dataframe.groupby('Position').Position.nunique().index),
            "seasons": list(map(int, list(ungrouped_dataframe.groupby('Season').Season.nunique().index))),
            "matchdays": list(map(int, list(ungrouped_dataframe.groupby('Matchday').Matchday.nunique().index))),
        }

        return self.render_json_response(chart_json)

    @staticmethod
    def convert_to_json_serializable_list(prices):
        try:
            int(numpy.array(prices.mean().index[0]))
            return list(map(int, numpy.array(prices.mean().index)))
        except (TypeError, IndexError):
            return list(map(str, numpy.array(prices.mean().index)))

    @staticmethod
    def _get_data_from_dataframe(prices):
        mins = prices.min()
        quantiles = prices.quantile([0.25, 0.75])
        medians = prices.median()
        maxs = prices.max()
        data = []
        for x_index in numpy.array(prices.mean().index):
            data.append([float(mins[x_index]),
                         float(quantiles[x_index][0.25]),
                         float(medians[x_index]),
                         float(quantiles[x_index][0.75]),
                         float(maxs[x_index])
                         ])
        return data


@method_decorator(login_required, name='dispatch')
{'fraction_non_alphanumeric': 0.09800971997222865, 'fraction_numerical': 0.0035871326081925482, 'mean_word_length': 1.5525694034258712, 'pattern_counts': {'":': 18, '<': 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': 68, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6592110', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2074, 'n_words': 310}