Revision 8b41683a snf-cyclades-app/synnefo/quotas/util.py

b/snf-cyclades-app/synnefo/quotas/util.py
33 33

  
34 34
from django.db.models import Sum, Count, Q
35 35

  
36
from synnefo.db.models import VirtualMachine, Network
36
from synnefo.db.models import VirtualMachine, Network, FloatingIP
37 37
from synnefo.quotas import Quotaholder, ASTAKOS_TOKEN
38 38

  
39 39

  
......
43 43

  
44 44
    vms = VirtualMachine.objects.filter(deleted=False)
45 45
    networks = Network.objects.filter(deleted=False)
46
    floating_ips = FloatingIP.objects.filter(deleted=False)
46 47

  
47 48
    if user is not None:
48 49
        vms = vms.filter(userid=user)
49 50
        networks = networks.filter(userid=user)
51
        floating_ips = floating_ips.filter(userid=user)
50 52

  
51 53
    # Get resources related with VMs
52 54
    vm_resources = vms.values("userid").annotate(num=Count("id"),
......
60 62
           .annotate(active_ram=Sum("flavor__ram"),
61 63
                     active_cpu=Sum("flavor__cpu"))
62 64

  
63
    for vm_res in vm_resources:
65
    for vm_res in vm_resources.iterator():
64 66
        user = vm_res['userid']
65 67
        res = {"cyclades.vm": vm_res["num"],
66 68
               "cyclades.cpu": vm_res["cpu"],
......
68 70
               "cyclades.ram": 1048576 * vm_res["ram"]}
69 71
        holdings[user] = res
70 72

  
71
    for vm_res in vm_active_resources:
73
    for vm_res in vm_active_resources.iterator():
72 74
        user = vm_res['userid']
73 75
        holdings[user]["cyclades.active_cpu"] = vm_res["active_cpu"]
74 76
        holdings[user]["cyclades.active_ram"] = 1048576 * vm_res["active_ram"]
......
76 78
    # Get resources related with networks
77 79
    net_resources = networks.values("userid")\
78 80
                            .annotate(num=Count("id"))
79
    for net_res in net_resources:
81
    for net_res in net_resources.iterator():
80 82
        user = net_res['userid']
81
        if user not in holdings:
82
            holdings[user] = {}
83
        holdings.setdefault(user, {})
83 84
        holdings[user]["cyclades.network.private"] = net_res["num"]
84 85

  
86
    floating_ips_resources = floating_ips.values("userid")\
87
                                         .annotate(num=Count("id"))
88
    for floating_ip_res in floating_ips_resources.iterator():
89
        user = floating_ip_res["userid"]
90
        holdings.setdefault(user, {})
91
        holdings[user]["cyclades.floating_ip"] = floating_ip_res["num"]
92

  
85 93
    return holdings
86 94

  
87 95

  

Also available in: Unified diff