Revision c8cb5d53

b/snf-astakos-app/astakos/im/functions.py
42 42
from django.db.models import Q
43 43

  
44 44
from synnefo_branding.utils import render_to_string
45
from synnefo.util.keypath import set_path
46 45

  
47 46
from synnefo.lib import join_urls
48 47
from astakos.im.models import AstakosUser, Invitation, ProjectMembership, \
......
1179 1178
                                             owner__in=users)
1180 1179
    apps_d = _partition_by(lambda a: a.owner.uuid, apps)
1181 1180

  
1182
    usage = {}
1181
    usage = quotas.QuotaDict()
1183 1182
    for user in users:
1184 1183
        uuid = user.uuid
1185
        set_path(usage,
1186
                 [uuid, user.base_project.uuid, quotas.PENDING_APP_RESOURCE],
1187
                 len(apps_d.get(uuid, [])), createpath=True)
1184
        usage[uuid][user.base_project.uuid][quotas.PENDING_APP_RESOURCE] = \
1185
            len(apps_d.get(uuid, []))
1188 1186
    return usage
1189 1187

  
1190 1188

  
b/snf-astakos-app/astakos/im/quotas.py
37 37
import astakos.quotaholder_app.callpoint as qh
38 38
from astakos.quotaholder_app.exception import NoCapacityError
39 39
from django.db.models import Q
40
from synnefo.util.keypath import set_path
40
from collections import defaultdict
41 41

  
42 42

  
43
QuotaDict = lambda: defaultdict(lambda: defaultdict(dict))
44

  
43 45
PROJECT_TAG = "project:"
44 46
USER_TAG = "user:"
45 47

  
......
104 106

  
105 107

  
106 108
def mk_quota_dict(users_counters, project_counters):
107
    quota = {}
109
    quota = QuotaDict()
108 110
    for (holder, source, resource), u_value in users_counters.iteritems():
109 111
        p_value = project_counters[(source, None, resource)]
110 112
        values_dict = from_holding(u_value)
111 113
        values_dict.update(from_holding(p_value, is_project=True))
112
        set_path(quota, [holder, source, resource], values_dict,
113
                 createpath=True)
114
        quota[holder][source][resource] = values_dict
114 115
    return quota
115 116

  
116 117

  
......
144 145

  
145 146

  
146 147
def mk_limits_dict(counters):
147
    quota = {}
148
    for key, (limit, _, _) in counters.iteritems():
149
        path = list(key)
150
        set_path(quota, path, limit, createpath=True)
148
    quota = QuotaDict()
149
    for (holder, source, resource), (limit, _, _) in counters.iteritems():
150
        quota[holder][source][resource] = limit
151 151
    return quota
152 152

  
153 153

  
154 154
def mk_project_quota_dict(project_counters):
155
    quota = {}
155
    quota = QuotaDict()
156 156
    for (holder, _, resource), p_value in project_counters.iteritems():
157 157
        values_dict = from_holding(p_value, is_project=True)
158
        set_path(quota, [holder, resource], values_dict,
159
                 createpath=True)
158
        quota[holder][resource] = values_dict
160 159
    return quota
161 160

  
162 161

  
......
263 262
        "person", "project")
264 263
    memberships_d = _partition_by(lambda m: m.project_id, memberships)
265 264

  
266
    user_quota = {}
267
    project_quota = {}
265
    user_quota = QuotaDict()
266
    project_quota = QuotaDict()
268 267

  
269 268
    for project in projects:
270 269
        pr_ref = get_project_ref(project)
......
276 275
        project_memberships = memberships_d.get(project.id, [])
277 276
        for grant in project_grants:
278 277
            resource = grant.resource.name
279
            path = [pr_ref, None, resource]
280 278
            val = grant.project_capacity if state == Project.NORMAL else 0
281
            set_path(project_quota, path, val, createpath=True)
279
            project_quota[pr_ref][None][resource] = val
282 280
            for membership in project_memberships:
283 281
                u_ref = get_user_ref(membership.person)
284
                path = [u_ref, pr_ref, resource]
285 282
                val = grant.member_capacity if membership.is_active() else 0
286
                set_path(user_quota, path, val, createpath=True)
283
                user_quota[u_ref][pr_ref][resource] = val
287 284

  
288 285
    return project_quota, user_quota
289 286

  
......
309 306
    u_ref = get_user_ref(membership.person)
310 307
    objs = ProjectResourceQuota.objects.select_related()
311 308
    grants = objs.filter(project=project)
312
    user_quota = {}
309
    user_quota = QuotaDict()
313 310
    is_active = membership.is_active()
314 311
    for grant in grants:
315 312
        resource = grant.resource.name
316
        path = [u_ref, pr_ref, resource]
317 313
        value = grant.member_capacity if is_active else 0
318
        set_path(user_quota, path, value, createpath=True)
314
        user_quota[u_ref][pr_ref][resource] = value
319 315
    return user_quota
320 316

  
321 317

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

  
36 36
from synnefo.db.models import VirtualMachine, Network, IPAddress
37 37
from synnefo.quotas import Quotaholder
38
from synnefo.util.keypath import set_path
38
from collections import defaultdict
39 39

  
40
QuotaDict = lambda: defaultdict(lambda: defaultdict(dict))
40 41

  
41 42
MiB = 2 ** 20
42 43
GiB = 2 ** 30
......
44 45

  
45 46
def get_db_holdings(user=None, project=None):
46 47
    """Get holdings from Cyclades DB."""
47
    holdings = {}
48
    holdings = QuotaDict()
48 49

  
49 50
    vms = VirtualMachine.objects.filter(deleted=False)
50 51
    networks = Network.objects.filter(deleted=False)
......
74 75
               "cyclades.total_cpu": vm_res["total_cpu"],
75 76
               "cyclades.disk": vm_res["disk"] * GiB,
76 77
               "cyclades.total_ram": vm_res["total_ram"] * MiB}
77
        set_path(holdings, [user, project], res, createpath=True)
78
        holdings[user][project] = res
78 79

  
79 80
    vm_active_resources = vms.values("userid", "project")\
80 81
        .filter(Q(operstate="STARTED") | Q(operstate="BUILD") |
......
85 86
    for vm_res in vm_active_resources.iterator():
86 87
        user = vm_res['userid']
87 88
        project = vm_res['project']
88
        set_path(holdings, [user, project, "cyclades.cpu"], vm_res["cpu"],
89
                 createpath=True)
90
        set_path(holdings, [user, project, "cyclades.ram"],
91
                 vm_res["ram"] * MiB, createpath=True)
89
        holdings[user][project]["cyclades.cpu"] = vm_res["cpu"]
90
        holdings[user][project]["cyclades.ram"] = vm_res["ram"] * MiB
92 91

  
93 92
    # Get resources related with networks
94 93
    net_resources = networks.values("userid", "project")\
......
99 98
        if user is None:
100 99
            continue
101 100
        project = net_res['project']
102
        set_path(holdings, [user, project, "cyclades.network.private"],
103
                 net_res["num"], createpath=True)
101
        holdings[user][project]["cyclades.network.private"] = net_res["num"]
104 102

  
105 103
    floating_ips_resources = floating_ips.values("userid", "project")\
106 104
                                         .annotate(num=Count("id"))
......
108 106
    for floating_ip_res in floating_ips_resources.iterator():
109 107
        user = floating_ip_res["userid"]
110 108
        project = floating_ip_res["project"]
111
        set_path(holdings, [user, project, "cyclades.floating_ip"],
112
                 floating_ip_res["num"], createpath=True)
109
        holdings[user][project]["cyclades.floating_ip"] = \
110
            floating_ip_res["num"]
113 111

  
114 112
    return holdings
115 113

  
116 114

  
117 115
def get_db_project_holdings(project=None):
118 116
    """Get holdings from Cyclades DB."""
119
    holdings = {}
117
    holdings = QuotaDict()
120 118

  
121 119
    vms = VirtualMachine.objects.filter(deleted=False)
122 120
    networks = Network.objects.filter(deleted=False)
......
140 138
               "cyclades.total_cpu": vm_res["total_cpu"],
141 139
               "cyclades.disk": vm_res["disk"] * GiB,
142 140
               "cyclades.total_ram": vm_res["total_ram"] * MiB}
143
        set_path(holdings, [project], res, createpath=True)
141
        holdings[project] = res
144 142

  
145 143
    vm_active_resources = vms.values("project")\
146 144
        .filter(Q(operstate="STARTED") | Q(operstate="BUILD") |
......
150 148

  
151 149
    for vm_res in vm_active_resources.iterator():
152 150
        project = vm_res['project']
153
        set_path(holdings, [project, "cyclades.cpu"], vm_res["cpu"],
154
                 createpath=True)
155
        set_path(holdings, [project, "cyclades.ram"],
156
                 vm_res["ram"] * MiB, createpath=True)
151
        holdings[project]["cyclades.cpu"] = vm_res["cpu"]
152
        holdings[project]["cyclades.ram"] = vm_res["ram"] * MiB
157 153

  
158 154
    # Get resources related with networks
159 155
    net_resources = networks.values("project").annotate(num=Count("id"))
......
162 158
        project = net_res['project']
163 159
        if project is None:
164 160
            continue
165
        set_path(holdings, [project, "cyclades.network.private"],
166
                 net_res["num"], createpath=True)
161
        holdings[project]["cyclades.network.private"] = net_res["num"]
167 162

  
168 163
    floating_ips_resources = floating_ips.values("project")\
169 164
        .annotate(num=Count("id"))
170 165

  
171 166
    for floating_ip_res in floating_ips_resources.iterator():
172 167
        project = floating_ip_res["project"]
173
        set_path(holdings, [project, "cyclades.floating_ip"],
174
                 floating_ip_res["num"], createpath=True)
168
        holdings[project]["cyclades.floating_ip"] = floating_ip_res["num"]
175 169

  
176 170
    return holdings
177 171

  

Also available in: Unified diff