<commit_before>from __future__ import absolute_import
from __future__ import with_statement

from django import forms
from django.conf import settings
from django.contrib import admin
from django.contrib.admin import helpers
from django.contrib.admin.views import main as main_views
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.encoding import force_unicode
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _

from celery import current_app
from celery import states
from celery import registry
from celery.task.control import broadcast, revoke, rate_limit
from celery.utils import abbrtask

from djcelery import loaders
from djcelery.admin_utils import action, display_field, fixedwidth
from djcelery.models import TaskState, WorkerState
from djcelery.models import PeriodicTask, IntervalSchedule, CrontabSchedule
from djcelery.utils import naturaldate


TASK_STATE_COLORS = {states.SUCCESS: "green",
                     states.FAILURE: "red",
                     states.REVOKED: "magenta",
                     states.STARTED: "yellow",
                     states.RETRY: "orange",
                     "RECEIVED": "blue"}
NODE_STATE_COLORS = {"ONLINE": "green",
                     "OFFLINE": "gray"}


class MonitorList(main_views.ChangeList):

    def __init__(self, *args, **kwargs):
        super(MonitorList, self).__init__(*args, **kwargs)
        self.title = self.model_admin.list_page_title


@display_field(_("state"), "state")
def colored_state(task):
    state = escape(task.state)
    color = TASK_STATE_COLORS.get(task.state, "black")
    return """<b><span style="color: %s;">%s</span></b>""" % (color, state)


@display_field(_("state"), "last_heartbeat")
def node_state(node):
    state = node.is_alive() and "ONLINE" or "OFFLINE"
    color = NODE_STATE_COLORS[state]
    return """<b><span style="color: %s;">%s</span></b>""" % (color, state)


@display_field(_("ETA"), "eta")
def eta(task):
    if not task.eta:
        return """<span style="color: gray;">none</span>"""
    return escape(task.eta)


@display_field(_("when"), "tstamp")
def tstamp(task):
    return """<div title="%s">%s</div>""" % (escape(str(task.tstamp)),
                                             escape(naturaldate(task.tstamp)))


@display_field(_("name"), "name")
def name(task):
    short_name = abbrtask(task.name, 16)
    return """<div title="%s"><b>%s</b></div>""" % (escape(task.name),
                                                    escape(short_name))


class ModelMonitor(admin.ModelAdmin):
    can_add = False
    can_delete = False

    def get_changelist(self, request, **kwargs):
        return MonitorList

    def change_view(self, request, object_id, extra_context=None):
        extra_context = extra_context or {}
        extra_context.setdefault("title", self.detail_title)
        return super(ModelMonitor, self).change_view(request, object_id,
                                                     extra_context)

    def has_delete_permission(self, request, obj=None):
        if not self.can_delete:
            return False
        return super(ModelMonitor, self).has_delete_permission(request, obj)

    def has_add_permission(self, request):
        if not self.can_add:
            return False
        return super(ModelMonitor, self).has_add_permission(request)


class TaskMonitor(ModelMonitor):
    detail_title = _("Task detail")
    list_page_title = _("Tasks")
    rate_limit_confirmation_template = "djcelery/confirm_rate_limit.html"
    date_hierarchy = "tstamp"
    fieldsets = (
            (None, {
                "fields": ("state", "task_id", "name", "args", "kwargs",
                           "eta", "runtime", "worker", "tstamp"),
                "classes": ("extrapretty", ),
            }),
            ("Details", {
                "classes": ("collapse", "extrapretty"),
                "fields": ("result", "traceback", "expires"),
            }),
    )
    list_display = (fixedwidth("task_id", name=_("UUID"), pt=8),
                    colored_state,
                    name,
                    fixedwidth("args", pretty=True),
                    fixedwidth("kwargs", pretty=True),
                    eta,
                    tstamp,
                    "worker")
    readonly_fields = ("state", "task_id", "name", "args", "kwargs",
                       "eta", "runtime", "worker", "result", "traceback",
                       "expires", "tstamp")
    list_filter = ("state", "name", "tstamp", "eta", "worker")
    search_fields = ("name", "task_id", "args", "kwargs", "worker__hostname")
    actions = ["revoke_tasks",
               "terminate_tasks",
               "kill_tasks",
               "rate_limit_tasks"]

    @action(_("Revoke selected tasks"))
    def revoke_tasks(self, request, queryset):
        with current_app.default_connection() as connection:
            for state in queryset:
                revoke(state.task_id, connection=connection)

    @action(_("Terminate selected tasks"))
    def terminate_tasks(self, request, queryset):
        with current_app.default_connection() as connection:
            for state in queryset:
                revoke(state.task_id, connection=connection, terminate=True)

    @action(_("Kill selected tasks"))
    def kill_tasks(self, request, queryset):
        with current_app.default_connection() as connection:
            for state in queryset:
                revoke(state.task_id, connection=connection,
                       terminate=True, signal="KILL")

    @action(_("Rate limit selected tasks"))
    def rate_limit_tasks(self, request, queryset):
        tasks = set([task.name for task in queryset])
        opts = self.model._meta
        app_label = opts.app_label
        if request.POST.get("post"):
            rate = request.POST["rate_limit"]
            with current_app.default_connection() as connection:
                for task_name in tasks:
                    rate_limit(task_name, rate, connection=connection)
            return None

        context = {
            "title": _("Rate limit selection"),
            "queryset": queryset,
            "object_name": force_unicode(opts.verbose_name),
            "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
            "opts": opts,
            "root_path": self.admin_site.root_path,
            "app_label": app_label,
        }

        return render_to_response(self.rate_limit_confirmation_template,
                context, context_instance=RequestContext(request))<commit_msg>Removed unused variable (deprecated in django?)<commit_after>from __future__ import absolute_import
from __future__ import with_statement

from django import forms
from django.conf import settings
from django.contrib import admin
from django.contrib.admin import helpers
from django.contrib.admin.views import main as main_views
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.encoding import force_unicode
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _

from celery import current_app
from celery import states
from celery import registry
from celery.task.control import broadcast, revoke, rate_limit
from celery.utils import abbrtask

from djcelery import loaders
from djcelery.admin_utils import action, display_field, fixedwidth
from djcelery.models import TaskState, WorkerState
from djcelery.models import PeriodicTask, IntervalSchedule, CrontabSchedule
from djcelery.utils import naturaldate


TASK_STATE_COLORS = {states.SUCCESS: "green",
                     states.FAILURE: "red",
                     states.REVOKED: "magenta",
                     states.STARTED: "yellow",
                     states.RETRY: "orange",
                     "RECEIVED": "blue"}
NODE_STATE_COLORS = {"ONLINE": "green",
                     "OFFLINE": "gray"}


class MonitorList(main_views.ChangeList):

    def __init__(self, *args, **kwargs):
        super(MonitorList, self).__init__(*args, **kwargs)
        self.title = self.model_admin.list_page_title


@display_field(_("state"), "state")
def colored_state(task):
    state = escape(task.state)
    color = TASK_STATE_COLORS.get(task.state, "black")
    return """<b><span style="color: %s;">%s</span></b>""" % (color, state)


@display_field(_("state"), "last_heartbeat")
def node_state(node):
    state = node.is_alive() and "ONLINE" or "OFFLINE"
    color = NODE_STATE_COLORS[state]
    return """<b><span style="color: %s;">%s</span></b>""" % (color, state)


@display_field(_("ETA"), "eta")
def eta(task):
    if not task.eta:
        return """<span style="color: gray;">none</span>"""
    return escape(task.eta)


@display_field(_("when"), "tstamp")
def tstamp(task):
    return """<div title="%s">%s</div>""" % (escape(str(task.tstamp)),
                                             escape(naturaldate(task.tstamp)))


@display_field(_("name"), "name")
def name(task):
    short_name = abbrtask(task.name, 16)
    return """<div title="%s"><b>%s</b></div>""" % (escape(task.name),
                                                    escape(short_name))


class ModelMonitor(admin.ModelAdmin):
    can_add = False
    can_delete = False

    def get_changelist(self, request, **kwargs):
        return MonitorList

    def change_view(self, request, object_id, extra_context=None):
        extra_context = extra_context or {}
        extra_context.setdefault("title", self.detail_title)
        return super(ModelMonitor, self).change_view(request, object_id,
                                                     extra_context)

    def has_delete_permission(self, request, obj=None):
        if not self.can_delete:
            return False
        return super(ModelMonitor, self).has_delete_permission(request, obj)

    def has_add_permission(self, request):
        if not self.can_add:
            return False
        return super(ModelMonitor, self).has_add_permission(request)


class TaskMonitor(ModelMonitor):
    detail_title = _("Task detail")
    list_page_title = _("Tasks")
    rate_limit_confirmation_template = "djcelery/confirm_rate_limit.html"
    date_hierarchy = "tstamp"
    fieldsets = (
            (None, {
                "fields": ("state", "task_id", "name", "args", "kwargs",
                           "eta", "runtime", "worker", "tstamp"),
                "classes": ("extrapretty", ),
            }),
            ("Details", {
                "classes": ("collapse", "extrapretty"),
                "fields": ("result", "traceback", "expires"),
            }),
    )
    list_display = (fixedwidth("task_id", name=_("UUID"), pt=8),
                    colored_state,
                    name,
                    fixedwidth("args", pretty=True),
                    fixedwidth("kwargs", pretty=True),
                    eta,
                    tstamp,
                    "worker")
    readonly_fields = ("state", "task_id", "name", "args", "kwargs",
                       "eta", "runtime", "worker", "result", "traceback",
                       "expires", "tstamp")
    list_filter = ("state", "name", "tstamp", "eta", "worker")
    search_fields = ("name", "task_id", "args", "kwargs", "worker__hostname")
    actions = ["revoke_tasks",
               "terminate_tasks",
               "kill_tasks",
               "rate_limit_tasks"]

    @action(_("Revoke selected tasks"))
    def revoke_tasks(self, request, queryset):
        with current_app.default_connection() as connection:
            for state in queryset:
                revoke(state.task_id, connection=connection)

    @action(_("Terminate selected tasks"))
    def terminate_tasks(self, request, queryset):
        with current_app.default_connection() as connection:
            for state in queryset:
                revoke(state.task_id, connection=connection, terminate=True)

    @action(_("Kill selected tasks"))
    def kill_tasks(self, request, queryset):
        with current_app.default_connection() as connection:
            for state in queryset:
                revoke(state.task_id, connection=connection,
                       terminate=True, signal="KILL")

    @action(_("Rate limit selected tasks"))
    def rate_limit_tasks(self, request, queryset):
        tasks = set([task.name for task in queryset])
        opts = self.model._meta
        app_label = opts.app_label
        if request.POST.get("post"):
            rate = request.POST["rate_limit"]
            with current_app.default_connection() as connection:
                for task_name in tasks:
                    rate_limit(task_name, rate, connection=connection)
            return None

        context = {
            "title": _("Rate limit selection"),
            "queryset": queryset,
            "object_name": force_unicode(opts.verbose_name),
            "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
            "opts": opts,
            "app_label": app_label,
        }

        return render_to_response(self.rate_limit_confirmation_template,
                context, context_instance=RequestContext(request))
{'fraction_non_alphanumeric': 0.11997877823252993, 'fraction_numerical': 0.00045475216007276033, 'mean_word_length': 2.5817046688382193, 'pattern_counts': {'":': 27, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6565893', 'n_tokens_mistral': 3762, 'n_tokens_neox': 3644, 'n_words': 864}