<commit_before>from django.conf import settings

from rest_framework import viewsets, response, permissions

from metaci.api.views.testmethod_perf import BuildFlowFilterSet, DEFAULTS

from metaci.api.views.testmethod_perf import TestMethodPerfFilterSet
from metaci.api.views.testresults import TestMethodResultFilterSet


class TestMethodPerfUIApiView(viewsets.ViewSet):
    permission_classes = (permissions.AllowAny,)

    defaults = [
        (item, value)
        for (item, value) in DEFAULTS.__dict__.items()
        if not item.startswith("_")
    ]

    def list(self, request, format=None):
        buildflow_filters = self.collect_filter_defs(BuildFlowFilterSet, [])
        testmethodperf_filters = self.collect_filter_defs(
            TestMethodPerfFilterSet, buildflow_filters.keys()
        )
        testmethodresult_filters = self.collect_filter_defs(
            TestMethodResultFilterSet, buildflow_filters.keys()
        )

        json = {
            "buildflow_filters": buildflow_filters.values(),
            "testmethod_perf": {
                "includable_fields": testmethodperf_filters["include_fields"][
                    "choices"
                ],
                "filters": testmethodperf_filters.values(),
                "defaults": {**dict(self.defaults)},
            },
            "testmethod_results": {
                "includable_fields": testmethodresult_filters["include_fields"][
                    "choices"
                ],
                "filters": testmethodresult_filters.values(),
                "defaults": {**dict(self.defaults)},
            },
            "debug": settings.DEBUG,
        }

        return response.Response(json)

    def collect_filter_defs(self, filterSet, exclusion_names):
        json_filter_defs = {}
        defaults = dict(self.defaults)
        for name, filter in filterSet.get_filters().items():
            if name in exclusion_names:
                continue
            obj = {
                "name": name,
                "label": filter._label,
                "field_type": filter.field_class.__name__,
                "field_module": filter.field_class.__module__,
                "lookup_expr": filter.lookup_expr,
            }
            choices = filter.extra.get("choices")
            if choices:
                obj["choices"] = choices

            initial = filter.extra.get("initial")
            if initial:
                obj["initial"] = initial
            elif name in defaults:
                obj["initial"] = defaults[name]

            if "ui" in filter.extra:
                for key, value in filter.extra["ui"].items():
                    obj[key] = value

            json_filter_defs[name] = obj

        return json_filter_defs<commit_msg>Minor cleanups.<commit_after>from django.conf import settings

from rest_framework import viewsets, response, permissions

from metaci.api.views.testmethod_perf import (
    BuildFlowFilterSet,
    TestMethodPerfFilterSet,
    DEFAULTS,
)
from metaci.api.views.testresults import TestMethodResultFilterSet


class TestMethodPerfUIApiView(viewsets.ViewSet):
    permission_classes = (permissions.AllowAny,)

    defaults = [
        (item, value)
        for (item, value) in DEFAULTS.__dict__.items()
        if not item.startswith("_")
    ]

    def list(self, request, format=None):
        buildflow_filters = self.collect_filter_defs(BuildFlowFilterSet, [])
        testmethodperf_filters = self.collect_filter_defs(
            TestMethodPerfFilterSet, buildflow_filters.keys()
        )
        testmethodresult_filters = self.collect_filter_defs(
            TestMethodResultFilterSet, buildflow_filters.keys()
        )

        json = {
            "buildflow_filters": buildflow_filters.values(),
            "testmethod_perf": {
                "includable_fields": testmethodperf_filters["include_fields"][
                    "choices"
                ],
                "filters": testmethodperf_filters.values(),
                "defaults": {**dict(self.defaults)},
            },
            "testmethod_results": {
                "includable_fields": testmethodresult_filters["include_fields"][
                    "choices"
                ],
                "filters": testmethodresult_filters.values(),
                "defaults": {**dict(self.defaults)},
            },
            "debug": settings.DEBUG,
        }

        return response.Response(json)

    def collect_filter_defs(self, filterSet, exclusion_names):
        json_filter_defs = {}
        defaults = dict(self.defaults)
        for name, filter in filterSet.get_filters().items():
            if name in exclusion_names:
                continue
            obj = {
                "name": name,
                "label": filter._label,
                "field_type": filter.field_class.__name__,
                "field_module": filter.field_class.__module__,
                "lookup_expr": filter.lookup_expr,
            }
            choices = filter.extra.get("choices")
            if choices:
                obj["choices"] = choices

            initial = filter.extra.get("initial")
            if initial:
                obj["initial"] = initial
            elif name in defaults:
                obj["initial"] = defaults[name]

            json_filter_defs[name] = obj

        return json_filter_defs
{'fraction_non_alphanumeric': 0.09625768013405325, 'fraction_numerical': 0.0, 'mean_word_length': 1.9811320754716981, 'pattern_counts': {'":': 30, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '916455', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1377, 'n_words': 258}