root / snf-astakos-app / astakos / im / quotas.py @ 9b126f13
History | View | Annotate | Download (8 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from astakos.im.models import ( |
35 |
Resource, AstakosUserQuota, AstakosUser, |
36 |
Project, ProjectMembership, ProjectResourceGrant, ProjectApplication) |
37 |
from astakos.quotaholder.callpoint import QuotaholderDjangoDBCallpoint |
38 |
from django.db.models import Q |
39 |
|
40 |
qh = QuotaholderDjangoDBCallpoint() |
41 |
|
42 |
|
43 |
def from_holding(holding): |
44 |
limit, imported_min, imported_max = holding |
45 |
body = {'limit': limit,
|
46 |
'usage': imported_max,
|
47 |
'pending': imported_max-imported_min,
|
48 |
} |
49 |
return body
|
50 |
|
51 |
|
52 |
def limits_only(holding): |
53 |
limit, imported_min, imported_max = holding |
54 |
return limit
|
55 |
|
56 |
|
57 |
def transform_data(holdings, func=None): |
58 |
if func is None: |
59 |
func = from_holding |
60 |
|
61 |
quota = {} |
62 |
for (holder, source, resource), value in holdings.iteritems(): |
63 |
holder_quota = quota.get(holder, {}) |
64 |
source_quota = holder_quota.get(source, {}) |
65 |
body = func(value) |
66 |
source_quota[resource] = body |
67 |
holder_quota[source] = source_quota |
68 |
quota[holder] = holder_quota |
69 |
return quota
|
70 |
|
71 |
|
72 |
def get_counters(users, resources=None, sources=None): |
73 |
uuids = [user.uuid for user in users] |
74 |
|
75 |
counters = qh.get_quota(holders=uuids, |
76 |
resources=resources, |
77 |
sources=sources) |
78 |
return counters
|
79 |
|
80 |
|
81 |
def get_users_quotas(users, resources=None, sources=None): |
82 |
counters = get_counters(users, resources, sources) |
83 |
quotas = transform_data(counters) |
84 |
return quotas
|
85 |
|
86 |
|
87 |
def get_users_quotas_and_limits(users, resources=None, sources=None): |
88 |
counters = get_counters(users, resources, sources) |
89 |
quotas = transform_data(counters) |
90 |
limits = transform_data(counters, limits_only) |
91 |
return quotas, limits
|
92 |
|
93 |
|
94 |
def get_user_quotas(user, resources=None, sources=None): |
95 |
quotas = get_users_quotas([user], resources, sources) |
96 |
return quotas[user.uuid]
|
97 |
|
98 |
|
99 |
def get_service_quotas(service, users=None): |
100 |
resources = Resource.objects.filter(service=service.name) |
101 |
resource_names = [r.name for r in resources] |
102 |
counters = qh.get_quota(holders=users, resources=resource_names) |
103 |
return transform_data(counters)
|
104 |
|
105 |
|
106 |
def _level_quota_dict(quotas): |
107 |
lst = [] |
108 |
for holder, holder_quota in quotas.iteritems(): |
109 |
for source, source_quota in holder_quota.iteritems(): |
110 |
for resource, limit in source_quota.iteritems(): |
111 |
key = (holder, source, resource) |
112 |
lst.append((key, limit)) |
113 |
return lst
|
114 |
|
115 |
|
116 |
def set_user_quota(quotas): |
117 |
q = _level_quota_dict(quotas) |
118 |
qh.set_quota(q) |
119 |
|
120 |
|
121 |
def get_default_quota(): |
122 |
_DEFAULT_QUOTA = {} |
123 |
resources = Resource.objects.select_related('service').all()
|
124 |
for resource in resources: |
125 |
capacity = resource.uplimit |
126 |
_DEFAULT_QUOTA[resource.full_name()] = capacity |
127 |
|
128 |
return _DEFAULT_QUOTA
|
129 |
|
130 |
|
131 |
SYSTEM = 'system'
|
132 |
|
133 |
|
134 |
def initial_quotas(users): |
135 |
initial = {} |
136 |
default_quotas = get_default_quota() |
137 |
|
138 |
for user in users: |
139 |
uuid = user.uuid |
140 |
source_quota = {SYSTEM: dict(default_quotas)}
|
141 |
initial[uuid] = source_quota |
142 |
|
143 |
objs = AstakosUserQuota.objects.select_related() |
144 |
orig_quotas = objs.filter(user__in=users) |
145 |
for user_quota in orig_quotas: |
146 |
uuid = user_quota.user.uuid |
147 |
user_init = initial.get(uuid, {}) |
148 |
source_quota = user_init.get(SYSTEM, {}) |
149 |
resource = user_quota.resource.full_name() |
150 |
source_quota[resource] = user_quota.capacity |
151 |
user_init[SYSTEM] = source_quota |
152 |
initial[uuid] = user_init |
153 |
|
154 |
return initial
|
155 |
|
156 |
|
157 |
def get_grant_source(grant): |
158 |
return SYSTEM
|
159 |
|
160 |
|
161 |
def astakos_users_quotas(users, initial=None): |
162 |
if initial is None: |
163 |
quotas = initial_quotas(users) |
164 |
else:
|
165 |
quotas = copy.deepcopy(initial) |
166 |
|
167 |
ACTUALLY_ACCEPTED = ProjectMembership.ACTUALLY_ACCEPTED |
168 |
objs = ProjectMembership.objects.select_related('project', 'person') |
169 |
memberships = objs.filter(person__in=users, |
170 |
state__in=ACTUALLY_ACCEPTED, |
171 |
project__state=Project.APPROVED) |
172 |
|
173 |
project_ids = set(m.project_id for m in memberships) |
174 |
objs = ProjectApplication.objects.select_related('project')
|
175 |
apps = objs.filter(project__in=project_ids) |
176 |
|
177 |
project_dict = {} |
178 |
for app in apps: |
179 |
project_dict[app.project] = app |
180 |
|
181 |
objs = ProjectResourceGrant.objects.select_related() |
182 |
grants = objs.filter(project_application__in=apps) |
183 |
|
184 |
for membership in memberships: |
185 |
uuid = membership.person.uuid |
186 |
userquotas = quotas.get(uuid, {}) |
187 |
|
188 |
application = project_dict[membership.project] |
189 |
|
190 |
for grant in grants: |
191 |
if grant.project_application_id != application.id:
|
192 |
continue
|
193 |
|
194 |
source = get_grant_source(grant) |
195 |
source_quotas = userquotas.get(source, {}) |
196 |
|
197 |
resource = grant.resource.full_name() |
198 |
prev = source_quotas.get(resource, 0)
|
199 |
new = prev + grant.member_capacity |
200 |
source_quotas[resource] = new |
201 |
userquotas[source] = source_quotas |
202 |
quotas[uuid] = userquotas |
203 |
|
204 |
return quotas
|
205 |
|
206 |
|
207 |
def astakos_user_quotas(user): |
208 |
quotas = astakos_users_quotas([user]) |
209 |
try:
|
210 |
return quotas[user.uuid]
|
211 |
except KeyError: |
212 |
raise ValueError("could not compute quotas") |
213 |
|
214 |
|
215 |
def list_user_quotas(users): |
216 |
qh_quotas, qh_limits = get_users_quotas_and_limits(users) |
217 |
astakos_initial = initial_quotas(users) |
218 |
astakos_quotas = astakos_users_quotas(users) |
219 |
|
220 |
diff_quotas = {} |
221 |
for holder, local in astakos_quotas.iteritems(): |
222 |
registered = qh_limits.get(holder, None)
|
223 |
if local != registered:
|
224 |
diff_quotas[holder] = dict(local)
|
225 |
|
226 |
return (qh_limits, qh_quotas,
|
227 |
astakos_initial, diff_quotas) |
228 |
|
229 |
|
230 |
def qh_add_resource_limit(resource, diff): |
231 |
objs = AstakosUser.forupdate.filter(Q(email_verified=True) &
|
232 |
~Q(policy=resource)) |
233 |
users = objs.select_for_update() |
234 |
uuids = [u.uuid for u in users] |
235 |
qh.add_resource_limit(holders=uuids, sources=[SYSTEM], |
236 |
resources=[resource.name], diff=diff) |
237 |
|
238 |
|
239 |
def qh_sync_new_resource(resource, limit): |
240 |
users = AstakosUser.forupdate.filter( |
241 |
email_verified=True).select_for_update()
|
242 |
|
243 |
resource_name = resource.name |
244 |
data = [] |
245 |
for user in users: |
246 |
uuid = user.uuid |
247 |
key = uuid, SYSTEM, resource_name |
248 |
data.append((key, limit)) |
249 |
|
250 |
qh.set_quota(data) |
251 |
|
252 |
|
253 |
def qh_sync_users(user_ids): |
254 |
users = AstakosUser.forupdate.filter(id__in=user_ids).select_for_update() |
255 |
astakos_quotas = astakos_users_quotas(list(users))
|
256 |
set_user_quota(astakos_quotas) |
257 |
|
258 |
|
259 |
def qh_sync_user(user_id): |
260 |
qh_sync_users([user_id]) |