furry/products/analytics.py

111 lines
4.0 KiB
Python

from django.db.models import Count, Sum, Avg, Q
from django.utils import timezone
from datetime import timedelta
from .models import Product, Order, Review, CustomOrder, UserProfile
class ShopAnalytics:
"""Analytics-Klasse für Shop-Statistiken"""
@staticmethod
def get_sales_statistics(days=30):
"""Verkaufsstatistiken der letzten X Tage"""
end_date = timezone.now()
start_date = end_date - timedelta(days=days)
orders = Order.objects.filter(
created__range=(start_date, end_date),
payment_status='paid'
)
return {
'total_orders': orders.count(),
'total_revenue': orders.aggregate(Sum('total_amount'))['total_amount__sum'] or 0,
'average_order_value': orders.aggregate(Avg('total_amount'))['total_amount__avg'] or 0,
'top_products': orders.values('items__product__name').annotate(
count=Count('items__product')
).order_by('-count')[:5]
}
@staticmethod
def get_product_analytics():
"""Produkt-spezifische Analytics"""
products = Product.objects.all()
return {
'total_products': products.count(),
'in_stock': products.filter(stock__gt=0).count(),
'low_stock': products.filter(stock__lte=5, stock__gt=0).count(),
'out_of_stock': products.filter(stock=0).count(),
'featured_products': products.filter(is_featured=True).count(),
'custom_orders': products.filter(is_custom_order=True).count(),
'top_rated': products.annotate(
avg_rating=Avg('reviews__rating')
).filter(avg_rating__gte=4.0).order_by('-avg_rating')[:5]
}
@staticmethod
def get_user_analytics():
"""Benutzer-spezifische Analytics"""
from django.contrib.auth.models import User
users = User.objects.all()
profiles = UserProfile.objects.all()
return {
'total_users': users.count(),
'active_users': users.filter(last_login__gte=timezone.now() - timedelta(days=30)).count(),
'newsletter_subscribers': profiles.filter(newsletter=True).count(),
'top_customers': Order.objects.values('user__username').annotate(
total_spent=Sum('total_amount')
).order_by('-total_spent')[:5]
}
@staticmethod
def get_custom_order_analytics():
"""Custom Order Analytics"""
custom_orders = CustomOrder.objects.all()
return {
'total_custom_orders': custom_orders.count(),
'pending_orders': custom_orders.filter(status='pending').count(),
'in_progress': custom_orders.filter(status='in_progress').count(),
'completed': custom_orders.filter(status='completed').count(),
'average_completion_time': custom_orders.filter(
status='completed'
).aggregate(
avg_time=Avg('updated' - 'created')
)['avg_time']
}
@staticmethod
def get_review_analytics():
"""Review Analytics"""
reviews = Review.objects.all()
return {
'total_reviews': reviews.count(),
'average_rating': reviews.aggregate(Avg('rating'))['rating__avg'] or 0,
'rating_distribution': reviews.values('rating').annotate(
count=Count('rating')
).order_by('rating'),
'recent_reviews': reviews.order_by('-created')[:10]
}
class UserBehaviorTracker:
"""Tracking für Benutzerverhalten"""
@staticmethod
def track_product_view(product, user=None):
"""Produktansicht tracken"""
# Hier könnte man Redis oder eine separate Tracking-Tabelle verwenden
pass
@staticmethod
def track_search_query(query, user=None):
"""Suchanfragen tracken"""
pass
@staticmethod
def track_cart_addition(product, user=None):
"""Warenkorb-Hinzufügung tracken"""
pass