root / snf-cyclades-app / synnefo / admin / views.py @ 1295f78a
History | View | Annotate | Download (3.9 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 |
|
34 |
import logging |
35 |
from django import http |
36 |
from django.utils import simplejson as json |
37 |
from synnefo.db.models import VirtualMachine, Network |
38 |
from django.db.models import Count, Sum |
39 |
from snf_django.lib import api |
40 |
from copy import copy |
41 |
|
42 |
|
43 |
log = logging.getLogger(__name__) |
44 |
|
45 |
|
46 |
@api.api_method(http_method='GET', user_required=False, token_required=False, |
47 |
logger=log) |
48 |
@api.allow_jsonp()
|
49 |
def get_stats(request): |
50 |
stats = get_statistics() |
51 |
data = json.dumps(stats) |
52 |
return http.HttpResponse(data, status=200, content_type='application/json') |
53 |
|
54 |
|
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
|