1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

# Copyright 2013 The Distro Tracker Developers 

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

# at https://deb.li/DTAuthors 

# 

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

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

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

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

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

""" 

Implements the command which outputs statistics. 

""" 

import json 

 

from django.core.management.base import BaseCommand 

from django.utils import timezone 

 

from distro_tracker.core.models import ( 

SourcePackageName, 

Subscription, 

UserEmail 

) 

 

 

class Command(BaseCommand): 

""" 

A Django management command which outputs some statistics. 

""" 

 

help = ( # noqa 

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

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

"- Total number of subscriptions\n" 

"- Total number of unique emails\n" 

) 

 

def add_arguments(self, parser): 

parser.add_argument( 

'--json', 

action='store_true', 

default=False, 

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

) 

 

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

 

from collections import OrderedDict 

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

stats = OrderedDict(( 

('package_number', 

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

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

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

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

)) 

 

if kwargs['json']: 

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

else: 

# Legacy output format 

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

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