Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / admin / views.py @ 06ffd11f

History | View | Annotate | Download (3.9 kB)

1 4a8667dc Christos Stavrakakis
# Copyright 2013 GRNET S.A. All rights reserved.
2 4a8667dc Christos Stavrakakis
#
3 4a8667dc Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
4 4a8667dc Christos Stavrakakis
# without modification, are permitted provided that the following
5 4a8667dc Christos Stavrakakis
# conditions are met:
6 4a8667dc Christos Stavrakakis
#
7 4a8667dc Christos Stavrakakis
#   1. Redistributions of source code must retain the above
8 4a8667dc Christos Stavrakakis
#      copyright notice, this list of conditions and the following
9 4a8667dc Christos Stavrakakis
#      disclaimer.
10 4a8667dc Christos Stavrakakis
#
11 4a8667dc Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
12 4a8667dc Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 4a8667dc Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
14 4a8667dc Christos Stavrakakis
#      provided with the distribution.
15 4a8667dc Christos Stavrakakis
#
16 4a8667dc Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 4a8667dc Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 4a8667dc Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 4a8667dc Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 4a8667dc Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 4a8667dc Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 4a8667dc Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 4a8667dc Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 4a8667dc Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 4a8667dc Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 4a8667dc Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 4a8667dc Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
28 4a8667dc Christos Stavrakakis
#
29 4a8667dc Christos Stavrakakis
# The views and conclusions contained in the software and
30 4a8667dc Christos Stavrakakis
# documentation are those of the authors and should not be
31 4a8667dc Christos Stavrakakis
# interpreted as representing official policies, either expressed
32 4a8667dc Christos Stavrakakis
# or implied, of GRNET S.A.
33 4a8667dc Christos Stavrakakis
34 4a8667dc Christos Stavrakakis
import logging
35 4a8667dc Christos Stavrakakis
from django import http
36 4a8667dc Christos Stavrakakis
from django.utils import simplejson as json
37 4a8667dc Christos Stavrakakis
from synnefo.db.models import VirtualMachine, Network
38 4a8667dc Christos Stavrakakis
from django.db.models import Count, Sum
39 4a8667dc Christos Stavrakakis
from snf_django.lib import api
40 4a8667dc Christos Stavrakakis
from copy import copy
41 4a8667dc Christos Stavrakakis
42 4a8667dc Christos Stavrakakis
43 4a8667dc Christos Stavrakakis
log = logging.getLogger(__name__)
44 4a8667dc Christos Stavrakakis
45 4a8667dc Christos Stavrakakis
46 4a8667dc Christos Stavrakakis
@api.api_method(http_method='GET', user_required=False, token_required=False,
47 d4e33598 Kostas Papadimitriou
                logger=log, serializations=['json'])
48 1295f78a Kostas Papadimitriou
@api.allow_jsonp()
49 4a8667dc Christos Stavrakakis
def get_stats(request):
50 4a8667dc Christos Stavrakakis
    stats = get_statistics()
51 4a8667dc Christos Stavrakakis
    data = json.dumps(stats)
52 1295f78a Kostas Papadimitriou
    return http.HttpResponse(data, status=200, content_type='application/json')
53 4a8667dc Christos Stavrakakis
54 4a8667dc Christos Stavrakakis
55 4a8667dc Christos Stavrakakis
def get_statistics():
56 4a8667dc Christos Stavrakakis
    # VirtualMachines
57 4a8667dc Christos Stavrakakis
    vm_objects = VirtualMachine.objects
58 4a8667dc Christos Stavrakakis
    servers = vm_objects.values("deleted", "operstate")\
59 4a8667dc Christos Stavrakakis
                        .annotate(count=Count("id"),
60 4a8667dc Christos Stavrakakis
                                  cpu=Sum("flavor__cpu"),
61 4a8667dc Christos Stavrakakis
                                  ram=Sum("flavor__ram"),
62 4a8667dc Christos Stavrakakis
                                  disk=Sum("flavor__disk"))
63 4a8667dc Christos Stavrakakis
    zero_stats = {"count": 0, "cpu": 0, "ram": 0, "disk": 0}
64 4a8667dc Christos Stavrakakis
    server_stats = {}
65 4a8667dc Christos Stavrakakis
    for state in VirtualMachine.RSAPI_STATE_FROM_OPER_STATE.values():
66 4a8667dc Christos Stavrakakis
        server_stats[state] = copy(zero_stats)
67 4a8667dc Christos Stavrakakis
68 4a8667dc Christos Stavrakakis
    for stats in servers:
69 4a8667dc Christos Stavrakakis
        deleted = stats.pop("deleted")
70 4a8667dc Christos Stavrakakis
        operstate = stats.pop("operstate")
71 4a8667dc Christos Stavrakakis
        state = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE.get(operstate)
72 4a8667dc Christos Stavrakakis
        if deleted:
73 4a8667dc Christos Stavrakakis
            for key in zero_stats.keys():
74 4a8667dc Christos Stavrakakis
                server_stats["DELETED"][key] += stats.get(key, 0)
75 4a8667dc Christos Stavrakakis
        elif state:
76 4a8667dc Christos Stavrakakis
            for key in zero_stats.keys():
77 4a8667dc Christos Stavrakakis
                server_stats[state][key] += stats.get(key, 0)
78 4a8667dc Christos Stavrakakis
79 4a8667dc Christos Stavrakakis
    #Networks
80 4a8667dc Christos Stavrakakis
    net_objects = Network.objects
81 4a8667dc Christos Stavrakakis
    networks = net_objects.values("deleted", "state")\
82 4a8667dc Christos Stavrakakis
                          .annotate(count=Count("id"))
83 4a8667dc Christos Stavrakakis
    zero_stats = {"count": 0}
84 4a8667dc Christos Stavrakakis
    network_stats = {}
85 4a8667dc Christos Stavrakakis
    for state in Network.RSAPI_STATE_FROM_OPER_STATE.values():
86 4a8667dc Christos Stavrakakis
        network_stats[state] = copy(zero_stats)
87 4a8667dc Christos Stavrakakis
88 4a8667dc Christos Stavrakakis
    for stats in networks:
89 4a8667dc Christos Stavrakakis
        deleted = stats.pop("deleted")
90 4a8667dc Christos Stavrakakis
        state = stats.pop("state")
91 4a8667dc Christos Stavrakakis
        state = Network.RSAPI_STATE_FROM_OPER_STATE.get(state)
92 4a8667dc Christos Stavrakakis
        if deleted:
93 4a8667dc Christos Stavrakakis
            for key in zero_stats.keys():
94 4a8667dc Christos Stavrakakis
                network_stats["DELETED"][key] += stats.get(key, 0)
95 4a8667dc Christos Stavrakakis
        elif state:
96 4a8667dc Christos Stavrakakis
            for key in zero_stats.keys():
97 4a8667dc Christos Stavrakakis
                network_stats[state][key] += stats.get(key, 0)
98 4a8667dc Christos Stavrakakis
99 4a8667dc Christos Stavrakakis
    statistics = {"servers": server_stats,
100 4a8667dc Christos Stavrakakis
                  "networks": network_stats}
101 4a8667dc Christos Stavrakakis
    return statistics