Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / stats-astakos.py @ 2a0dfa0a

History | View | Annotate | Download (3.6 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33
import json
34
import string
35
#from optparse import make_option
36

    
37
from snf_django.management.commands import SynnefoCommand, CommandError
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
42

    
43

    
44
class Command(SynnefoCommand):
45
    help = "Get available statistics of Astakos service"
46
    can_import_settings = True
47

    
48
    option_list = SynnefoCommand.option_list + (
49
    )
50

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

    
54
        output_format = options["output_format"]
55
        if output_format == "json":
56
            self.stdout.write(json.dumps(stats, indent=4) + "\n")
57
        elif output_format == "pretty":
58
            pretty_print_stats(stats, self.stdout)
59
        else:
60
            raise CommandError("Output format '%s' not supported." %
61
                               output_format)
62

    
63

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

    
67

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

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

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

    
77
    all_providers = users.pop("total")
78
    if all_providers is not None:
79
        fields = ["total", "verified", "active"]
80
        table = columns_from_fields(fields, all_providers)
81
        usage = all_providers.get("usage", {})
82
        for name, val in sorted(usage.items()):
83
            table.append((name, val))
84
        pprint_table(stdout, table, None,
85
                     title="Statistics for All Providers")
86
        newline()
87

    
88
    for provider_name, provider_info in sorted(users.items()):
89
        fields = ["total", "verified", "active"]
90
        table = columns_from_fields(fields, provider_info)
91
        usage = provider_info.get("usage", {})
92
        for name, val in sorted(usage.items()):
93
            table.append((name, val))
94
        pprint_table(stdout, table, None,
95
                     title="Statistics for Provider '%s'" % provider_name)
96
        newline()