more impeorventes for kewyrods libreary
This commit is contained in:
@@ -873,6 +873,66 @@ class SeedKeywordViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
|
||||
return queryset
|
||||
|
||||
@action(detail=False, methods=['get'], url_path='stats', url_name='stats')
|
||||
def stats(self, request):
|
||||
"""
|
||||
Get aggregated keyword statistics by industry and country.
|
||||
Returns top industries and countries with keyword counts and total volume.
|
||||
"""
|
||||
from django.db.models import Count, Sum, Q
|
||||
|
||||
try:
|
||||
# Top industries by keyword count
|
||||
industries = Industry.objects.annotate(
|
||||
keyword_count=Count('seed_keywords', filter=Q(seed_keywords__is_active=True)),
|
||||
total_volume=Sum('seed_keywords__volume', filter=Q(seed_keywords__is_active=True))
|
||||
).filter(
|
||||
keyword_count__gt=0
|
||||
).order_by('-keyword_count')[:10]
|
||||
|
||||
industries_data = [{
|
||||
'name': ind.name,
|
||||
'slug': ind.slug,
|
||||
'keyword_count': ind.keyword_count or 0,
|
||||
'total_volume': ind.total_volume or 0,
|
||||
} for ind in industries]
|
||||
|
||||
# Keywords by country
|
||||
countries = SeedKeyword.objects.filter(
|
||||
is_active=True
|
||||
).values('country').annotate(
|
||||
keyword_count=Count('id'),
|
||||
total_volume=Sum('volume')
|
||||
).order_by('-keyword_count')
|
||||
|
||||
countries_data = [{
|
||||
'country': c['country'],
|
||||
'country_display': dict(SeedKeyword.COUNTRY_CHOICES).get(c['country'], c['country']),
|
||||
'keyword_count': c['keyword_count'],
|
||||
'total_volume': c['total_volume'] or 0,
|
||||
} for c in countries]
|
||||
|
||||
# Total stats
|
||||
total_stats = SeedKeyword.objects.filter(is_active=True).aggregate(
|
||||
total_keywords=Count('id'),
|
||||
total_volume=Sum('volume')
|
||||
)
|
||||
|
||||
data = {
|
||||
'industries': industries_data,
|
||||
'countries': countries_data,
|
||||
'total_keywords': total_stats['total_keywords'] or 0,
|
||||
'total_volume': total_stats['total_volume'] or 0,
|
||||
}
|
||||
|
||||
return success_response(data=data, request=request)
|
||||
except Exception as e:
|
||||
return error_response(
|
||||
error=f'Failed to fetch keyword stats: {str(e)}',
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
request=request
|
||||
)
|
||||
|
||||
@action(detail=False, methods=['post'], url_path='import_seed_keywords', url_name='import_seed_keywords')
|
||||
def import_seed_keywords(self, request):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user