##// END OF EJS Templates
Sort domains by their usage in statistics
neko259 -
r1782:71d3a47e default
parent child Browse files
Show More
@@ -1,34 +1,36 b''
1 from django.core.management import BaseCommand
1 from django.core.management import BaseCommand
2 from django.db.models import Count
2 from django.db.models import Count
3
3
4 from boards.models import Attachment
4 from boards.models import Attachment
5 from boards.utils import get_domain
5 from boards.utils import get_domain
6
6
7
7
8 class Command(BaseCommand):
8 class Command(BaseCommand):
9 help = 'Gather board statistics'
9 help = 'Gather board statistics'
10
10
11 def handle(self, *args, **options):
11 def handle(self, *args, **options):
12 print('* Domains and their usage')
12 print('* Domains and their usage')
13 domains = {}
13 domains = {}
14 for attachment in Attachment.objects.exclude(url=''):
14 for attachment in Attachment.objects.exclude(url=''):
15 domain = get_domain(attachment.url)
15 domain = get_domain(attachment.url)
16 if domain in domains:
16 if domain in domains:
17 domains[domain] += 1
17 domains[domain] += 1
18 else:
18 else:
19 domains[domain] = 1
19 domains[domain] = 1
20
20
21 for domain in domains:
21 domain_list = [(item, domains[item]) for item in domains.keys()]
22 print('{}: {}'.format(domain, domains[domain]))
22 domain_list = sorted(domain_list, key=lambda domain: -domain[1])
23 for domain in domain_list:
24 print('{}: {}'.format(domain[0], domain[1]))
23
25
24 print('* Overall numbers')
26 print('* Overall numbers')
25 print('{} attachments in the system, {} of them as URLs'.format(
27 print('{} attachments in the system, {} of them as URLs'.format(
26 Attachment.objects.count(),
28 Attachment.objects.count(),
27 Attachment.objects.exclude(url='').count()))
29 Attachment.objects.exclude(url='').count()))
28
30
29 print('* File types')
31 print('* File types')
30 mimetypes = Attachment.objects.filter(url='')\
32 mimetypes = Attachment.objects.filter(url='')\
31 .values('mimetype').annotate(count=Count('id'))\
33 .values('mimetype').annotate(count=Count('id'))\
32 .order_by('-count')
34 .order_by('-count')
33 for mimetype in mimetypes:
35 for mimetype in mimetypes:
34 print('{}: {}'.format(mimetype['mimetype'], mimetype['count']))
36 print('{}: {}'.format(mimetype['mimetype'], mimetype['count']))
General Comments 0
You need to be logged in to leave comments. Login now