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