Commit 53e76847 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

Replace set_path with defaultdict in quota handling

parent cd3d6b1b
......@@ -42,7 +42,6 @@ from django.contrib.auth import login as auth_login, logout as auth_logout
from django.db.models import Q
from synnefo_branding.utils import render_to_string
from synnefo.util.keypath import set_path
from synnefo.lib import join_urls
from astakos.im.models import AstakosUser, Invitation, ProjectMembership, \
......@@ -1179,12 +1178,11 @@ def count_pending_app(users):
owner__in=users)
apps_d = _partition_by(lambda a: a.owner.uuid, apps)
usage = {}
usage = quotas.QuotaDict()
for user in users:
uuid = user.uuid
set_path(usage,
[uuid, user.base_project.uuid, quotas.PENDING_APP_RESOURCE],
len(apps_d.get(uuid, [])), createpath=True)
usage[uuid][user.base_project.uuid][quotas.PENDING_APP_RESOURCE] = \
len(apps_d.get(uuid, []))
return usage
......
......@@ -37,9 +37,11 @@ from astakos.im.models import (
import astakos.quotaholder_app.callpoint as qh
from astakos.quotaholder_app.exception import NoCapacityError
from django.db.models import Q
from synnefo.util.keypath import set_path
from collections import defaultdict
QuotaDict = lambda: defaultdict(lambda: defaultdict(dict))
PROJECT_TAG = "project:"
USER_TAG = "user:"
......@@ -104,13 +106,12 @@ def get_related_sources(counters):
def mk_quota_dict(users_counters, project_counters):
quota = {}
quota = QuotaDict()
for (holder, source, resource), u_value in users_counters.iteritems():
p_value = project_counters[(source, None, resource)]
values_dict = from_holding(u_value)
values_dict.update(from_holding(p_value, is_project=True))
set_path(quota, [holder, source, resource], values_dict,
createpath=True)
quota[holder][source][resource] = values_dict
return quota
......@@ -144,19 +145,17 @@ def service_get_quotas(component, users=None):
def mk_limits_dict(counters):
quota = {}
for key, (limit, _, _) in counters.iteritems():
path = list(key)
set_path(quota, path, limit, createpath=True)
quota = QuotaDict()
for (holder, source, resource), (limit, _, _) in counters.iteritems():
quota[holder][source][resource] = limit
return quota
def mk_project_quota_dict(project_counters):
quota = {}
quota = QuotaDict()
for (holder, _, resource), p_value in project_counters.iteritems():
values_dict = from_holding(p_value, is_project=True)
set_path(quota, [holder, resource], values_dict,
createpath=True)
quota[holder][resource] = values_dict
return quota
......@@ -263,8 +262,8 @@ def astakos_project_quotas(projects, resource=None):
"person", "project")
memberships_d = _partition_by(lambda m: m.project_id, memberships)
user_quota = {}
project_quota = {}
user_quota = QuotaDict()
project_quota = QuotaDict()
for project in projects:
pr_ref = get_project_ref(project)
......@@ -276,14 +275,12 @@ def astakos_project_quotas(projects, resource=None):
project_memberships = memberships_d.get(project.id, [])
for grant in project_grants:
resource = grant.resource.name
path = [pr_ref, None, resource]
val = grant.project_capacity if state == Project.NORMAL else 0
set_path(project_quota, path, val, createpath=True)
project_quota[pr_ref][None][resource] = val
for membership in project_memberships:
u_ref = get_user_ref(membership.person)
path = [u_ref, pr_ref, resource]
val = grant.member_capacity if membership.is_active() else 0
set_path(user_quota, path, val, createpath=True)
user_quota[u_ref][pr_ref][resource] = val
return project_quota, user_quota
......@@ -309,13 +306,12 @@ def membership_quota(membership):
u_ref = get_user_ref(membership.person)
objs = ProjectResourceQuota.objects.select_related()
grants = objs.filter(project=project)
user_quota = {}
user_quota = QuotaDict()
is_active = membership.is_active()
for grant in grants:
resource = grant.resource.name
path = [u_ref, pr_ref, resource]
value = grant.member_capacity if is_active else 0
set_path(user_quota, path, value, createpath=True)
user_quota[u_ref][pr_ref][resource] = value
return user_quota
......
......@@ -35,8 +35,9 @@ from django.db.models import Sum, Count, Q
from synnefo.db.models import VirtualMachine, Network, IPAddress
from synnefo.quotas import Quotaholder
from synnefo.util.keypath import set_path
from collections import defaultdict
QuotaDict = lambda: defaultdict(lambda: defaultdict(dict))
MiB = 2 ** 20
GiB = 2 ** 30
......@@ -44,7 +45,7 @@ GiB = 2 ** 30
def get_db_holdings(user=None, project=None):
"""Get holdings from Cyclades DB."""
holdings = {}
holdings = QuotaDict()
vms = VirtualMachine.objects.filter(deleted=False)
networks = Network.objects.filter(deleted=False)
......@@ -74,7 +75,7 @@ def get_db_holdings(user=None, project=None):
"cyclades.total_cpu": vm_res["total_cpu"],
"cyclades.disk": vm_res["disk"] * GiB,
"cyclades.total_ram": vm_res["total_ram"] * MiB}
set_path(holdings, [user, project], res, createpath=True)
holdings[user][project] = res
vm_active_resources = vms.values("userid", "project")\
.filter(Q(operstate="STARTED") | Q(operstate="BUILD") |
......@@ -85,10 +86,8 @@ def get_db_holdings(user=None, project=None):
for vm_res in vm_active_resources.iterator():
user = vm_res['userid']
project = vm_res['project']
set_path(holdings, [user, project, "cyclades.cpu"], vm_res["cpu"],
createpath=True)
set_path(holdings, [user, project, "cyclades.ram"],
vm_res["ram"] * MiB, createpath=True)
holdings[user][project]["cyclades.cpu"] = vm_res["cpu"]
holdings[user][project]["cyclades.ram"] = vm_res["ram"] * MiB
# Get resources related with networks
net_resources = networks.values("userid", "project")\
......@@ -99,8 +98,7 @@ def get_db_holdings(user=None, project=None):
if user is None:
continue
project = net_res['project']
set_path(holdings, [user, project, "cyclades.network.private"],
net_res["num"], createpath=True)
holdings[user][project]["cyclades.network.private"] = net_res["num"]
floating_ips_resources = floating_ips.values("userid", "project")\
.annotate(num=Count("id"))
......@@ -108,15 +106,15 @@ def get_db_holdings(user=None, project=None):
for floating_ip_res in floating_ips_resources.iterator():
user = floating_ip_res["userid"]
project = floating_ip_res["project"]
set_path(holdings, [user, project, "cyclades.floating_ip"],
floating_ip_res["num"], createpath=True)
holdings[user][project]["cyclades.floating_ip"] = \
floating_ip_res["num"]
return holdings
def get_db_project_holdings(project=None):
"""Get holdings from Cyclades DB."""
holdings = {}
holdings = QuotaDict()
vms = VirtualMachine.objects.filter(deleted=False)
networks = Network.objects.filter(deleted=False)
......@@ -140,7 +138,7 @@ def get_db_project_holdings(project=None):
"cyclades.total_cpu": vm_res["total_cpu"],
"cyclades.disk": vm_res["disk"] * GiB,
"cyclades.total_ram": vm_res["total_ram"] * MiB}
set_path(holdings, [project], res, createpath=True)
holdings[project] = res
vm_active_resources = vms.values("project")\
.filter(Q(operstate="STARTED") | Q(operstate="BUILD") |
......@@ -150,10 +148,8 @@ def get_db_project_holdings(project=None):
for vm_res in vm_active_resources.iterator():
project = vm_res['project']
set_path(holdings, [project, "cyclades.cpu"], vm_res["cpu"],
createpath=True)
set_path(holdings, [project, "cyclades.ram"],
vm_res["ram"] * MiB, createpath=True)
holdings[project]["cyclades.cpu"] = vm_res["cpu"]
holdings[project]["cyclades.ram"] = vm_res["ram"] * MiB
# Get resources related with networks
net_resources = networks.values("project").annotate(num=Count("id"))
......@@ -162,16 +158,14 @@ def get_db_project_holdings(project=None):
project = net_res['project']
if project is None:
continue
set_path(holdings, [project, "cyclades.network.private"],
net_res["num"], createpath=True)
holdings[project]["cyclades.network.private"] = net_res["num"]
floating_ips_resources = floating_ips.values("project")\
.annotate(num=Count("id"))
for floating_ip_res in floating_ips_resources.iterator():
project = floating_ip_res["project"]
set_path(holdings, [project, "cyclades.floating_ip"],
floating_ip_res["num"], createpath=True)
holdings[project]["cyclades.floating_ip"] = floating_ip_res["num"]
return holdings
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment