django phase 3 and 4
This commit is contained in:
@@ -4,9 +4,10 @@ Celery Task Monitoring Admin - Unfold Style
|
||||
from django.contrib import admin
|
||||
from django.utils.html import format_html
|
||||
from django.contrib import messages
|
||||
from django_celery_results.models import TaskResult
|
||||
from django_celery_results.models import TaskResult, GroupResult
|
||||
from unfold.admin import ModelAdmin
|
||||
from unfold.contrib.filters.admin import RangeDateFilter
|
||||
from celery import current_app
|
||||
|
||||
|
||||
class CeleryTaskResultAdmin(ModelAdmin):
|
||||
@@ -79,12 +80,15 @@ class CeleryTaskResultAdmin(ModelAdmin):
|
||||
seconds = duration.total_seconds()
|
||||
|
||||
if seconds < 1:
|
||||
return format_html('<span style="color: #0bbf87;">{:.2f}ms</span>', seconds * 1000)
|
||||
time_str = f'{seconds * 1000:.2f}ms'
|
||||
return format_html('<span style="color: #0bbf87;">{}</span>', time_str)
|
||||
elif seconds < 60:
|
||||
return format_html('<span style="color: #0693e3;">{:.2f}s</span>', seconds)
|
||||
time_str = f'{seconds:.2f}s'
|
||||
return format_html('<span style="color: #0693e3;">{}</span>', time_str)
|
||||
else:
|
||||
minutes = seconds / 60
|
||||
return format_html('<span style="color: #ff7a00;">{:.1f}m</span>', minutes)
|
||||
time_str = f'{minutes:.1f}m'
|
||||
return format_html('<span style="color: #ff7a00;">{}</span>', time_str)
|
||||
return '-'
|
||||
execution_time.short_description = 'Duration'
|
||||
|
||||
@@ -143,9 +147,9 @@ class CeleryTaskResultAdmin(ModelAdmin):
|
||||
count = old_tasks.count()
|
||||
old_tasks.delete()
|
||||
|
||||
self.message_user(request, f'🗑️ Cleared {count} old task(s)', messages.SUCCESS)
|
||||
self.message_user(request, f'Cleared {count} old task(s)', messages.SUCCESS)
|
||||
|
||||
clear_old_tasks.short_description = '🗑️ Clear Old Tasks (30+ days)'
|
||||
clear_old_tasks.short_description = 'Clear Old Tasks (30+ days)'
|
||||
|
||||
def has_add_permission(self, request):
|
||||
"""Disable manual task creation"""
|
||||
@@ -154,3 +158,56 @@ class CeleryTaskResultAdmin(ModelAdmin):
|
||||
def has_change_permission(self, request, obj=None):
|
||||
"""Make read-only"""
|
||||
return False
|
||||
|
||||
|
||||
class CeleryGroupResultAdmin(ModelAdmin):
|
||||
"""Admin interface for monitoring Celery group results with Unfold styling"""
|
||||
|
||||
list_display = [
|
||||
'group_id',
|
||||
'date_created',
|
||||
'date_done',
|
||||
'result_count',
|
||||
]
|
||||
list_filter = [
|
||||
('date_created', RangeDateFilter),
|
||||
('date_done', RangeDateFilter),
|
||||
]
|
||||
search_fields = ['group_id', 'result']
|
||||
readonly_fields = [
|
||||
'group_id', 'date_created', 'date_done', 'content_type',
|
||||
'content_encoding', 'result'
|
||||
]
|
||||
date_hierarchy = 'date_created'
|
||||
ordering = ['-date_created']
|
||||
|
||||
fieldsets = (
|
||||
('Group Information', {
|
||||
'fields': ('group_id', 'date_created', 'date_done')
|
||||
}),
|
||||
('Result Details', {
|
||||
'fields': ('content_type', 'content_encoding', 'result'),
|
||||
'classes': ('collapse',)
|
||||
}),
|
||||
)
|
||||
|
||||
def result_count(self, obj):
|
||||
"""Count tasks in the group"""
|
||||
if obj.result:
|
||||
try:
|
||||
import json
|
||||
result_data = json.loads(obj.result) if isinstance(obj.result, str) else obj.result
|
||||
if isinstance(result_data, list):
|
||||
return len(result_data)
|
||||
except:
|
||||
pass
|
||||
return '-'
|
||||
result_count.short_description = 'Task Count'
|
||||
|
||||
def has_add_permission(self, request):
|
||||
"""Disable manual group result creation"""
|
||||
return False
|
||||
|
||||
def has_change_permission(self, request, obj=None):
|
||||
"""Make read-only"""
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user