1# Copyright 2013 The Distro Tracker Developers 

2# See the COPYRIGHT file at the top-level directory of this distribution and 

3# at https://deb.li/DTAuthors 

4# 

5# This file is part of Distro Tracker. It is subject to the license terms 

6# in the LICENSE file found in the top-level directory of this 

7# distribution and at https://deb.li/DTLicense. No part of Distro Tracker, 

8# including this file, may be copied, modified, propagated, or distributed 

9# except according to the terms contained in the LICENSE file. 

10""" 

11Implements the command which outputs statistics. 

12""" 

13import json 

14 

15from django.core.management.base import BaseCommand 

16from django.utils import timezone 

17 

18from distro_tracker.core.models import ( 

19 SourcePackageName, 

20 Subscription, 

21 UserEmail 

22) 

23 

24 

25class Command(BaseCommand): 

26 """ 

27 A Django management command which outputs some statistics. 

28 """ 

29 

30 help = ( # noqa 

31 "Get some statistics about the package tracker:\n" 

32 "- Total number of source packages with at least one subscription\n" 

33 "- Total number of subscriptions\n" 

34 "- Total number of unique emails\n" 

35 ) 

36 

37 def add_arguments(self, parser): 

38 parser.add_argument( 

39 '--json', 

40 action='store_true', 

41 default=False, 

42 help='Output the result encoded as a JSON object' 

43 ) 

44 

45 def handle(self, *args, **kwargs): 

46 

47 from collections import OrderedDict 

48 # Necessary to keep ordering because of the legacy output format. 

49 stats = OrderedDict(( 

50 ('package_number', 

51 SourcePackageName.objects.all_with_subscribers().count()), 

52 ('subscription_number', Subscription.objects.count()), 

53 ('date', timezone.now().strftime('%Y-%m-%d')), 

54 ('unique_emails_number', UserEmail.objects.count()), 

55 )) 

56 

57 if kwargs['json']: 

58 self.stdout.write(json.dumps(stats)) 

59 else: 

60 # Legacy output format 

61 self.stdout.write('Src pkg\tSubscr.\tDate\t\tNb email') 

62 self.stdout.write('\t'.join(map(str, stats.values())))