Revision bda47e03 snf-cyclades-app/synnefo/admin/views.py

b/snf-cyclades-app/synnefo/admin/views.py
34 34
import logging
35 35
from django import http
36 36
from django.utils import simplejson as json
37
from synnefo.db.models import VirtualMachine, Network
38
from django.db.models import Count, Sum
37
from django.conf import settings
39 38
from snf_django.lib import api
40
from copy import copy
41 39

  
40
from synnefo.admin import stats
42 41

  
43
log = logging.getLogger(__name__)
42
logger = logging.getLogger(__name__)
44 43

  
45 44

  
46 45
@api.api_method(http_method='GET', user_required=False, token_required=False,
47
                logger=log, serializations=['json'])
46
                logger=logger, serializations=['json'])
48 47
@api.allow_jsonp()
49
def get_stats(request):
50
    stats = get_statistics()
51
    data = json.dumps(stats)
48
def get_public_stats(request):
49
    _stats = stats.get_public_stats()
50
    data = json.dumps(_stats)
52 51
    return http.HttpResponse(data, status=200, content_type='application/json')
53 52

  
54 53

  
55
def get_statistics():
56
    # VirtualMachines
57
    vm_objects = VirtualMachine.objects
58
    servers = vm_objects.values("deleted", "operstate")\
59
                        .annotate(count=Count("id"),
60
                                  cpu=Sum("flavor__cpu"),
61
                                  ram=Sum("flavor__ram"),
62
                                  disk=Sum("flavor__disk"))
63
    zero_stats = {"count": 0, "cpu": 0, "ram": 0, "disk": 0}
64
    server_stats = {}
65
    for state in VirtualMachine.RSAPI_STATE_FROM_OPER_STATE.values():
66
        server_stats[state] = copy(zero_stats)
67

  
68
    for stats in servers:
69
        deleted = stats.pop("deleted")
70
        operstate = stats.pop("operstate")
71
        state = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE.get(operstate)
72
        if deleted:
73
            for key in zero_stats.keys():
74
                server_stats["DELETED"][key] += stats.get(key, 0)
75
        elif state:
76
            for key in zero_stats.keys():
77
                server_stats[state][key] += stats.get(key, 0)
78

  
79
    #Networks
80
    net_objects = Network.objects
81
    networks = net_objects.values("deleted", "state")\
82
                          .annotate(count=Count("id"))
83
    zero_stats = {"count": 0}
84
    network_stats = {}
85
    for state in Network.RSAPI_STATE_FROM_OPER_STATE.values():
86
        network_stats[state] = copy(zero_stats)
87

  
88
    for stats in networks:
89
        deleted = stats.pop("deleted")
90
        state = stats.pop("state")
91
        state = Network.RSAPI_STATE_FROM_OPER_STATE.get(state)
92
        if deleted:
93
            for key in zero_stats.keys():
94
                network_stats["DELETED"][key] += stats.get(key, 0)
95
        elif state:
96
            for key in zero_stats.keys():
97
                network_stats[state][key] += stats.get(key, 0)
98

  
99
    statistics = {"servers": server_stats,
100
                  "networks": network_stats}
101
    return statistics
54
@api.api_method(http_method='GET', user_required=True, token_required=True,
55
                logger=logger, serializations=['json'])
56
@api.user_in_groups(permitted_groups=settings.ADMIN_STATS_PERMITTED_GROUPS,
57
                    logger=logger)
58
def get_cyclades_stats(request):
59
    _stats = stats.get_cyclades_stats(backend=None, clusters=True,
60
                                      servers=True, resources=True,
61
                                      networks=True, images=True)
62
    data = json.dumps(_stats)
63
    return http.HttpResponse(data, status=200, content_type='application/json')

Also available in: Unified diff