django phase 3 and 4
This commit is contained in:
@@ -66,8 +66,8 @@ class Igny8AdminConfig(AdminConfig):
|
||||
def _setup_celery_admin(self):
|
||||
"""Setup enhanced Celery admin with proper unregister/register"""
|
||||
try:
|
||||
from django_celery_results.models import TaskResult
|
||||
from igny8_core.admin.celery_admin import CeleryTaskResultAdmin
|
||||
from django_celery_results.models import TaskResult, GroupResult
|
||||
from igny8_core.admin.celery_admin import CeleryTaskResultAdmin, CeleryGroupResultAdmin
|
||||
|
||||
# Unregister the default TaskResult admin
|
||||
try:
|
||||
@@ -75,8 +75,15 @@ class Igny8AdminConfig(AdminConfig):
|
||||
except admin.sites.NotRegistered:
|
||||
pass
|
||||
|
||||
# Register our enhanced version
|
||||
# Unregister the default GroupResult admin
|
||||
try:
|
||||
admin.site.unregister(GroupResult)
|
||||
except admin.sites.NotRegistered:
|
||||
pass
|
||||
|
||||
# Register our enhanced versions
|
||||
admin.site.register(TaskResult, CeleryTaskResultAdmin)
|
||||
admin.site.register(GroupResult, CeleryGroupResultAdmin)
|
||||
except Exception as e:
|
||||
# Log the error but don't crash the app
|
||||
import logging
|
||||
|
||||
@@ -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
|
||||
|
||||
253
backend/igny8_core/admin/reports.py
Normal file
253
backend/igny8_core/admin/reports.py
Normal file
@@ -0,0 +1,253 @@
|
||||
"""
|
||||
Analytics & Reporting Views for IGNY8 Admin
|
||||
"""
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from django.shortcuts import render
|
||||
from django.db.models import Count, Sum, Avg, Q
|
||||
from django.utils import timezone
|
||||
from datetime import timedelta
|
||||
import json
|
||||
|
||||
|
||||
@staff_member_required
|
||||
def revenue_report(request):
|
||||
"""Revenue and billing analytics"""
|
||||
from igny8_core.business.billing.models import Payment
|
||||
from igny8_core.auth.models import Plan
|
||||
|
||||
# Date ranges
|
||||
today = timezone.now()
|
||||
months = []
|
||||
monthly_revenue = []
|
||||
|
||||
for i in range(6):
|
||||
month_start = today.replace(day=1) - timedelta(days=30*i)
|
||||
month_end = month_start.replace(day=28) + timedelta(days=4)
|
||||
|
||||
revenue = Payment.objects.filter(
|
||||
status='succeeded',
|
||||
processed_at__gte=month_start,
|
||||
processed_at__lt=month_end
|
||||
).aggregate(total=Sum('amount'))['total'] or 0
|
||||
|
||||
months.insert(0, month_start.strftime('%b %Y'))
|
||||
monthly_revenue.insert(0, float(revenue))
|
||||
|
||||
# Plan distribution
|
||||
plan_distribution = Plan.objects.annotate(
|
||||
account_count=Count('account')
|
||||
).values('name', 'account_count')
|
||||
|
||||
# Payment method breakdown
|
||||
payment_methods = Payment.objects.filter(
|
||||
status='succeeded'
|
||||
).values('payment_method').annotate(
|
||||
count=Count('id'),
|
||||
total=Sum('amount')
|
||||
).order_by('-total')
|
||||
|
||||
# Total revenue all time
|
||||
total_revenue = Payment.objects.filter(
|
||||
status='succeeded'
|
||||
).aggregate(total=Sum('amount'))['total'] or 0
|
||||
|
||||
context = {
|
||||
'title': 'Revenue Report',
|
||||
'months': json.dumps(months),
|
||||
'monthly_revenue': json.dumps(monthly_revenue),
|
||||
'plan_distribution': list(plan_distribution),
|
||||
'payment_methods': list(payment_methods),
|
||||
'total_revenue': float(total_revenue),
|
||||
}
|
||||
|
||||
# Merge with admin context
|
||||
from igny8_core.admin.site import admin_site
|
||||
admin_context = admin_site.each_context(request)
|
||||
context.update(admin_context)
|
||||
|
||||
return render(request, 'admin/reports/revenue.html', context)
|
||||
|
||||
|
||||
@staff_member_required
|
||||
def usage_report(request):
|
||||
"""Credit usage and AI operations analytics"""
|
||||
from igny8_core.business.billing.models import CreditUsageLog
|
||||
|
||||
# Usage by operation type
|
||||
usage_by_operation = CreditUsageLog.objects.values(
|
||||
'operation_type'
|
||||
).annotate(
|
||||
total_credits=Sum('credits_used'),
|
||||
total_cost=Sum('cost_usd'),
|
||||
operation_count=Count('id')
|
||||
).order_by('-total_credits')
|
||||
|
||||
# Top credit consumers
|
||||
top_consumers = CreditUsageLog.objects.values(
|
||||
'account__name'
|
||||
).annotate(
|
||||
total_credits=Sum('credits_used'),
|
||||
operation_count=Count('id')
|
||||
).order_by('-total_credits')[:10]
|
||||
|
||||
# Model usage distribution
|
||||
model_usage = CreditUsageLog.objects.values(
|
||||
'model_used'
|
||||
).annotate(
|
||||
usage_count=Count('id')
|
||||
).order_by('-usage_count')
|
||||
|
||||
# Total credits used
|
||||
total_credits = CreditUsageLog.objects.aggregate(
|
||||
total=Sum('credits_used')
|
||||
)['total'] or 0
|
||||
|
||||
context = {
|
||||
'title': 'Usage Report',
|
||||
'usage_by_operation': list(usage_by_operation),
|
||||
'top_consumers': list(top_consumers),
|
||||
'model_usage': list(model_usage),
|
||||
'total_credits': int(total_credits),
|
||||
}
|
||||
|
||||
# Merge with admin context
|
||||
from igny8_core.admin.site import admin_site
|
||||
admin_context = admin_site.each_context(request)
|
||||
context.update(admin_context)
|
||||
|
||||
return render(request, 'admin/reports/usage.html', context)
|
||||
|
||||
|
||||
@staff_member_required
|
||||
def content_report(request):
|
||||
"""Content production analytics"""
|
||||
from igny8_core.modules.writer.models import Content, Tasks
|
||||
|
||||
# Content by type
|
||||
content_by_type = Content.objects.values(
|
||||
'content_type'
|
||||
).annotate(count=Count('id')).order_by('-count')
|
||||
|
||||
# Production timeline (last 30 days)
|
||||
days = []
|
||||
daily_counts = []
|
||||
for i in range(30):
|
||||
day = timezone.now().date() - timedelta(days=i)
|
||||
count = Content.objects.filter(created_at__date=day).count()
|
||||
days.insert(0, day.strftime('%m/%d'))
|
||||
daily_counts.insert(0, count)
|
||||
|
||||
# Average word count by content type
|
||||
avg_words = Content.objects.values('content_type').annotate(
|
||||
avg_words=Avg('word_count')
|
||||
).order_by('-avg_words')
|
||||
|
||||
# Task completion rate
|
||||
total_tasks = Tasks.objects.count()
|
||||
completed_tasks = Tasks.objects.filter(status='completed').count()
|
||||
completion_rate = (completed_tasks / total_tasks * 100) if total_tasks > 0 else 0
|
||||
|
||||
# Total content produced
|
||||
total_content = Content.objects.count()
|
||||
|
||||
context = {
|
||||
'title': 'Content Production Report',
|
||||
'content_by_type': list(content_by_type),
|
||||
'days': json.dumps(days),
|
||||
'daily_counts': json.dumps(daily_counts),
|
||||
'avg_words': list(avg_words),
|
||||
'completion_rate': round(completion_rate, 1),
|
||||
'total_content': total_content,
|
||||
'total_tasks': total_tasks,
|
||||
'completed_tasks': completed_tasks,
|
||||
}
|
||||
|
||||
# Merge with admin context
|
||||
from igny8_core.admin.site import admin_site
|
||||
admin_context = admin_site.each_context(request)
|
||||
context.update(admin_context)
|
||||
|
||||
return render(request, 'admin/reports/content.html', context)
|
||||
|
||||
|
||||
@staff_member_required
|
||||
def data_quality_report(request):
|
||||
"""Check data quality and integrity"""
|
||||
issues = []
|
||||
|
||||
# Orphaned content (no site)
|
||||
from igny8_core.modules.writer.models import Content
|
||||
orphaned_content = Content.objects.filter(site__isnull=True).count()
|
||||
if orphaned_content > 0:
|
||||
issues.append({
|
||||
'severity': 'warning',
|
||||
'type': 'Orphaned Records',
|
||||
'count': orphaned_content,
|
||||
'description': 'Content items without assigned site',
|
||||
'action_url': '/admin/writer/content/?site__isnull=True'
|
||||
})
|
||||
|
||||
# Tasks without clusters
|
||||
from igny8_core.modules.writer.models import Tasks
|
||||
tasks_no_cluster = Tasks.objects.filter(cluster__isnull=True).count()
|
||||
if tasks_no_cluster > 0:
|
||||
issues.append({
|
||||
'severity': 'info',
|
||||
'type': 'Missing Relationships',
|
||||
'count': tasks_no_cluster,
|
||||
'description': 'Tasks without assigned cluster',
|
||||
'action_url': '/admin/writer/tasks/?cluster__isnull=True'
|
||||
})
|
||||
|
||||
# Accounts with negative credits
|
||||
from igny8_core.auth.models import Account
|
||||
negative_credits = Account.objects.filter(credits__lt=0).count()
|
||||
if negative_credits > 0:
|
||||
issues.append({
|
||||
'severity': 'error',
|
||||
'type': 'Data Integrity',
|
||||
'count': negative_credits,
|
||||
'description': 'Accounts with negative credit balance',
|
||||
'action_url': '/admin/igny8_core_auth/account/?credits__lt=0'
|
||||
})
|
||||
|
||||
# Duplicate keywords
|
||||
from igny8_core.modules.planner.models import Keywords
|
||||
duplicates = Keywords.objects.values('keyword', 'site', 'sector').annotate(
|
||||
count=Count('id')
|
||||
).filter(count__gt=1).count()
|
||||
if duplicates > 0:
|
||||
issues.append({
|
||||
'severity': 'warning',
|
||||
'type': 'Duplicates',
|
||||
'count': duplicates,
|
||||
'description': 'Duplicate keywords for same site/sector',
|
||||
'action_url': '/admin/planner/keywords/'
|
||||
})
|
||||
|
||||
# Content without SEO data
|
||||
no_seo = Content.objects.filter(
|
||||
Q(meta_title__isnull=True) | Q(meta_title='') |
|
||||
Q(meta_description__isnull=True) | Q(meta_description='')
|
||||
).count()
|
||||
if no_seo > 0:
|
||||
issues.append({
|
||||
'severity': 'info',
|
||||
'type': 'Incomplete Data',
|
||||
'count': no_seo,
|
||||
'description': 'Content missing SEO metadata',
|
||||
'action_url': '/admin/writer/content/'
|
||||
})
|
||||
|
||||
context = {
|
||||
'title': 'Data Quality Report',
|
||||
'issues': issues,
|
||||
'total_issues': len(issues),
|
||||
}
|
||||
|
||||
# Merge with admin context
|
||||
from igny8_core.admin.site import admin_site
|
||||
admin_context = admin_site.each_context(request)
|
||||
context.update(admin_context)
|
||||
|
||||
return render(request, 'admin/reports/data_quality.html', context)
|
||||
@@ -21,16 +21,26 @@ class Igny8AdminSite(UnfoldAdminSite):
|
||||
index_title = 'IGNY8 Administration'
|
||||
|
||||
def get_urls(self):
|
||||
"""Get admin URLs with dashboard available at /admin/dashboard/"""
|
||||
"""Get admin URLs with dashboard and reports available"""
|
||||
from django.urls import path
|
||||
from .dashboard import admin_dashboard
|
||||
from .reports import revenue_report, usage_report, content_report, data_quality_report
|
||||
|
||||
urls = super().get_urls()
|
||||
custom_urls = [
|
||||
path('dashboard/', self.admin_view(admin_dashboard), name='dashboard'),
|
||||
path('reports/revenue/', self.admin_view(revenue_report), name='report_revenue'),
|
||||
path('reports/usage/', self.admin_view(usage_report), name='report_usage'),
|
||||
path('reports/content/', self.admin_view(content_report), name='report_content'),
|
||||
path('reports/data-quality/', self.admin_view(data_quality_report), name='report_data_quality'),
|
||||
]
|
||||
return custom_urls + urls
|
||||
|
||||
def index(self, request, extra_context=None):
|
||||
"""Redirect to custom dashboard"""
|
||||
from django.shortcuts import redirect
|
||||
return redirect('admin:dashboard')
|
||||
|
||||
def get_sidebar_list(self, request):
|
||||
"""
|
||||
Override Unfold's get_sidebar_list to return our custom app groups
|
||||
|
||||
Reference in New Issue
Block a user