Revision c8a38c0e snf-astakos-app/astakos/im/management/commands/stats-astakos.py

b/snf-astakos-app/astakos/im/management/commands/stats-astakos.py
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33

  
34

  
35
import datetime
36 33
import json
37 34
import string
38

  
39 35
#from optparse import make_option
40 36

  
41
from django.conf import settings
42
from snf_django.management.utils import pprint_table
43

  
44 37
from snf_django.management.commands import SynnefoCommand, CommandError
45
from astakos.im.models import AstakosUser, Resource
46
from astakos.quotaholder_app.models import Holding
47
from django.db.models import Sum
38
from snf_django.management.utils import pprint_table
39
#from astakos.im.models import AstakosUser, Resource
40
#from astakos.quotaholder_app.models import Holding
41
from astakos.admin import stats as statistics
48 42

  
49 43

  
50 44
class Command(SynnefoCommand):
......
55 49
    )
56 50

  
57 51
    def handle(self, *args, **options):
58
        stats = get_astakos_stats()
52
        stats = statistics.get_astakos_stats()
59 53

  
60 54
        output_format = options["output_format"]
61 55
        if output_format == "json":
......
67 61
                               output_format)
68 62

  
69 63

  
70
def get_astakos_stats():
71
    stats = {"datetime": datetime.datetime.now().strftime("%c")}
72

  
73
    resources = Resource.objects.values_list("name", flat=True)
74

  
75
    users = AstakosUser.objects.all()
76
    verified = users.filter(email_verified=True)
77
    active = users.filter(is_active=True)
78

  
79
    user_stats = {}
80
    user_stats["total"] = {"total": users.count(),
81
                           "verified": verified.count(),
82
                           "active": active.count(),
83
                           "usage": {}}
84

  
85
    for resource in resources:
86
        usage = Holding.objects.filter(resource=resource)\
87
                               .aggregate(summ=Sum("usage_max"))
88
        user_stats["total"]["usage"][resource] = int(usage["summ"])
89

  
90
    for provider in settings.ASTAKOS_IM_MODULES:
91

  
92
        users = AstakosUser.objects.filter(auth_providers__module=provider)
93
        verified = users.filter(email_verified=True)
94
        active = users.filter(is_active=True)
95

  
96
        user_stats[provider] = {"total": users.count(),
97
                                "verified": verified.count(),
98
                                "active": active.count(),
99
                                "usage": {}}
100

  
101
        users_uuids = users.values_list("uuid", flat=True)
102
        for resource in resources:
103
            usage = Holding.objects\
104
                           .filter(holder__in=users_uuids, resource=resource)\
105
                           .aggregate(summ=Sum("usage_max"))
106
            user_stats[provider]["usage"][resource] = int(usage["summ"])
107

  
108
    stats["users"] = user_stats
109

  
110
    return stats
111

  
112

  
113 64
def columns_from_fields(fields, values):
114 65
    return zip(map(string.lower, fields), [values.get(f, 0) for f in fields])
115 66

  
......
117 68
def pretty_print_stats(stats, stdout):
118 69
    newline = lambda: stdout.write("\n")
119 70

  
120
    datetime = stats.get("datetime")
121
    stdout.write("datetime: %s\n" % datetime)
71
    _datetime = stats.get("datetime")
72
    stdout.write("datetime: %s\n" % _datetime)
122 73
    newline()
123 74

  
124 75
    users = stats.get("users", {})

Also available in: Unified diff