Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / quotas.py @ 764d99c4

History | View | Annotate | Download (9.3 kB)

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