Commit 81cadb43 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

Compute and show quotas by source

Adjust astakos code to the new nested dict structure of quotas.
parent d337d476
......@@ -80,13 +80,13 @@ class Command(BaseCommand):
else:
log = self.run(sync)
qh_l, qh_c, astakos_i, diff_q, info = log
qh_limits, qh_quotas, astakos_i, diff_q, info = log
if list_only:
self.list_quotas(qh_l, qh_c, astakos_i, info)
self.list_quotas(qh_quotas, astakos_i, info)
else:
if verify:
self.print_verify(qh_l, diff_q)
self.print_verify(qh_limits, diff_q)
if sync:
self.print_sync(diff_q)
......@@ -125,32 +125,33 @@ class Command(BaseCommand):
logger.exception(e)
raise CommandError("Syncing failed.")
def list_quotas(self, qh_limits, qh_counters, astakos_initial, info):
labels = ('uuid', 'email', 'resource', 'initial', 'total', 'usage')
columns = (36, 30, 24, 12, 12, 12)
def list_quotas(self, qh_quotas, astakos_initial, info):
labels = ('uuid', 'email', 'source', 'resource', 'initial', 'total', 'usage')
columns = (36, 30, 20, 24, 12, 12, 12)
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
self.stdout.write(line + '\n')
sep = '-' * len(line)
self.stdout.write(sep + '\n')
for holder, resources in qh_limits.iteritems():
h_counters = qh_counters[holder]
for holder, holder_quotas in qh_quotas.iteritems():
h_initial = astakos_initial[holder]
email = info[holder]
for resource, limits in resources.iteritems():
initial = str(h_initial[resource])
capacity = str(limits)
used = str(h_counters[resource])
fields = holder, email, resource, initial, capacity, used
output = []
for field, width in zip(fields, columns):
s = field.rjust(width)
output.append(s)
line = ' '.join(output)
self.stdout.write(line + '\n')
for source, source_quotas in holder_quotas.iteritems():
s_initial = h_initial[source]
for resource, values in source_quotas.iteritems():
initial = str(s_initial[resource])
capacity = str(values['limit'])
used = str(values['used'])
fields = holder, email, source, resource, initial, capacity, used
output = []
for field, width in zip(fields, columns):
s = field.rjust(width)
output.append(s)
line = ' '.join(output)
self.stdout.write(line + '\n')
def print_sync(self, diff_quotas):
size = len(diff_quotas)
......
......@@ -84,6 +84,8 @@ from astakos.quotaholder.api import QH_PRACTICALLY_INFINITE
from synnefo.lib.db.intdecimalfield import intDecimalField
from synnefo.util.text import uenc, udec
from astakos.im.quotas import get_users_quotas_and_limits, set_user_quota
logger = logging.getLogger(__name__)
DEFAULT_CONTENT_TYPE = None
......@@ -774,13 +776,16 @@ class AstakosUser(User):
raise ValueError("could not compute quotas")
SYSTEM = 'system'
def initial_quotas(users):
initial = {}
default_quotas = get_default_quota()
for user in users:
uuid = user.uuid
initial[uuid] = dict(default_quotas)
source_quota = {SYSTEM: dict(default_quotas)}
initial[uuid] = source_quota
objs = AstakosUserQuota.objects.select_related()
orig_quotas = objs.filter(user__in=users)
......@@ -794,16 +799,30 @@ def initial_quotas(users):
return initial
def get_grant_source(grant):
return SYSTEM
def users_quotas(users, initial=None):
if initial is None:
quotas = initial_quotas(users)
else:
quotas = copy.deepcopy(initial)
objs = ProjectMembership.objects.select_related('application', 'person')
memberships = objs.filter(person__in=users)
ACTUALLY_ACCEPTED = ProjectMembership.ACTUALLY_ACCEPTED
objs = ProjectMembership.objects.select_related('project', 'person')
memberships = objs.filter(person__in=users,
state__in=ACTUALLY_ACCEPTED,
project__state=Project.APPROVED)
project_ids = set(m.project_id for m in memberships)
objs = ProjectApplication.objects.select_related('project')
apps = objs.filter(project__in=project_ids)
project_dict = {}
for app in apps:
project_dict[app.project] = app
apps = set(m.application for m in memberships if m.application is not None)
objs = ProjectResourceGrant.objects.select_related()
grants = objs.filter(project_application__in=apps)
......@@ -811,17 +830,20 @@ def users_quotas(users, initial=None):
uuid = membership.person.uuid
userquotas = quotas.get(uuid, {})
application = membership.application
if application is None:
continue
application = project_dict[membership.project]
for grant in grants:
if grant.project_application_id != application.id:
continue
source = get_grant_source(grant)
source_quotas = userquotas.get(source, {})
resource = grant.resource.full_name()
prev = userquotas.get(resource, 0)
prev = source_quotas.get(resource, 0)
new = prev + grant.member_capacity
userquotas[resource] = new
source_quotas[resource] = new
userquotas[source] = source_quotas
quotas[uuid] = userquotas
return quotas
......@@ -2305,8 +2327,7 @@ def sync_users(users, sync=True):
for user in users:
info[user.uuid] = user.email
resources = get_resource_names()
qh_limits, qh_counters = qh_get_quotas(users, resources)
qh_quotas, qh_limits = get_users_quotas_and_limits(users)
astakos_initial = initial_quotas(users)
astakos_quotas = users_quotas(users)
......@@ -2317,9 +2338,9 @@ def sync_users(users, sync=True):
diff_quotas[holder] = dict(local)
if sync:
r = send_quotas(diff_quotas)
r = set_user_quota(diff_quotas)
return (qh_limits, qh_counters,
return (qh_limits, qh_quotas,
astakos_initial, diff_quotas, info)
return _sync_users(users, sync)
......
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