113 lines
3.7 KiB
Python
113 lines
3.7 KiB
Python
"""
|
|
Custom Admin Dashboard with Key Metrics
|
|
"""
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
|
from django.shortcuts import render
|
|
from django.db.models import Count, Sum, Q
|
|
from django.utils import timezone
|
|
from datetime import timedelta
|
|
|
|
|
|
@staff_member_required
|
|
def admin_dashboard(request):
|
|
"""Custom admin dashboard with operational metrics"""
|
|
|
|
# Date ranges
|
|
today = timezone.now().date()
|
|
week_ago = today - timedelta(days=7)
|
|
month_ago = today - timedelta(days=30)
|
|
|
|
# Account metrics
|
|
from igny8_core.auth.models import Account
|
|
total_accounts = Account.objects.count()
|
|
active_accounts = Account.objects.filter(status='active').count()
|
|
low_credit_accounts = Account.objects.filter(
|
|
status='active',
|
|
credits__lt=100
|
|
).count()
|
|
|
|
# Content metrics
|
|
from igny8_core.modules.writer.models import Content, Tasks
|
|
content_this_week = Content.objects.filter(created_at__gte=week_ago).count()
|
|
content_this_month = Content.objects.filter(created_at__gte=month_ago).count()
|
|
tasks_pending = Tasks.objects.filter(status='pending').count()
|
|
tasks_in_progress = Tasks.objects.filter(status='in_progress').count()
|
|
|
|
# Billing metrics
|
|
from igny8_core.business.billing.models import Payment, CreditTransaction
|
|
pending_payments = Payment.objects.filter(status='pending_approval').count()
|
|
payments_this_month = Payment.objects.filter(
|
|
created_at__gte=month_ago,
|
|
status='succeeded'
|
|
).aggregate(total=Sum('amount'))['total'] or 0
|
|
|
|
credit_usage_this_month = CreditTransaction.objects.filter(
|
|
created_at__gte=month_ago,
|
|
transaction_type='deduction'
|
|
).aggregate(total=Sum('amount'))['total'] or 0
|
|
|
|
# Automation metrics
|
|
from igny8_core.business.automation.models import AutomationRun
|
|
automation_running = AutomationRun.objects.filter(status='running').count()
|
|
automation_failed = AutomationRun.objects.filter(
|
|
status='failed',
|
|
started_at__gte=week_ago
|
|
).count()
|
|
|
|
# WordPress sync metrics
|
|
from igny8_core.business.integration.models import SyncEvent
|
|
sync_failed_today = SyncEvent.objects.filter(
|
|
success=False,
|
|
created_at__date=today
|
|
).count()
|
|
|
|
# Celery task metrics
|
|
try:
|
|
from django_celery_results.models import TaskResult
|
|
celery_failed = TaskResult.objects.filter(
|
|
status='FAILURE',
|
|
date_created__date=today
|
|
).count()
|
|
celery_pending = TaskResult.objects.filter(status='PENDING').count()
|
|
except:
|
|
celery_failed = 0
|
|
celery_pending = 0
|
|
|
|
# Get alerts
|
|
from .alerts import AdminAlerts
|
|
alerts = AdminAlerts.get_alerts()
|
|
|
|
context = {
|
|
'title': 'IGNY8 Dashboard',
|
|
'accounts': {
|
|
'total': total_accounts,
|
|
'active': active_accounts,
|
|
'low_credit': low_credit_accounts,
|
|
},
|
|
'content': {
|
|
'this_week': content_this_week,
|
|
'this_month': content_this_month,
|
|
'tasks_pending': tasks_pending,
|
|
'tasks_in_progress': tasks_in_progress,
|
|
},
|
|
'billing': {
|
|
'pending_payments': pending_payments,
|
|
'payments_this_month': float(payments_this_month),
|
|
'credit_usage_this_month': abs(credit_usage_this_month),
|
|
},
|
|
'automation': {
|
|
'running': automation_running,
|
|
'failed_this_week': automation_failed,
|
|
},
|
|
'integration': {
|
|
'sync_failed_today': sync_failed_today,
|
|
},
|
|
'celery': {
|
|
'failed_today': celery_failed,
|
|
'pending': celery_pending,
|
|
},
|
|
'alerts': alerts,
|
|
}
|
|
|
|
return render(request, 'admin/dashboard.html', context)
|