Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / quotas.py @ 1028e568

History | View | Annotate | Download (7.1 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

    
39
qh = QuotaholderDjangoDBCallpoint()
40

    
41

    
42
def from_holding(holding):
43
    limit, imported_min, imported_max = holding
44
    body = {'limit':       limit,
45
            'used':        imported_min,
46
            'available':   max(0, limit-imported_max),
47
            }
48
    return body
49

    
50

    
51
def limits_only(holding):
52
    limit, imported_min, imported_max = holding
53
    return limit
54

    
55

    
56
def transform_data(holdings, func=None):
57
    if func is None:
58
        func = from_holding
59

    
60
    quota = {}
61
    for (holder, source, resource), value in holdings.iteritems():
62
        holder_quota = quota.get(holder, {})
63
        source_quota = holder_quota.get(source, {})
64
        body = func(value)
65
        source_quota[resource] = body
66
        holder_quota[source] = source_quota
67
        quota[holder] = holder_quota
68
    return quota
69

    
70

    
71
def get_counters(users,  resources=None, sources=None):
72
    uuids = [user.uuid for user in users]
73

    
74
    counters = qh.get_holder_quota(holders=uuids,
75
                                   resources=resources,
76
                                   sources=sources)
77
    return counters
78

    
79

    
80
def get_users_quotas(users, resources=None, sources=None):
81
    counters = get_counters(users, resources, sources)
82
    quotas = transform_data(counters)
83
    return quotas
84

    
85

    
86
def get_users_quotas_and_limits(users, resources=None, sources=None):
87
    counters = get_counters(users, resources, sources)
88
    quotas = transform_data(counters)
89
    limits = transform_data(counters, limits_only)
90
    return quotas, limits
91

    
92

    
93
def get_user_quotas(user, resources=None, sources=None):
94
    quotas = get_users_quotas([user], resources, sources)
95
    return quotas[user.uuid]
96

    
97

    
98
def set_user_quota(quotas):
99
    qh.set_holder_quota(quotas)
100

    
101

    
102
def get_resources(resources=None, services=None):
103
    if resources is None:
104
        rs = Resource.objects.all()
105
    else:
106
        rs = Resource.objects.filter(name__in=resources)
107

    
108
    if services is not None:
109
        rs = rs.filter(service__in=services)
110

    
111
    resource_dict = {}
112
    for r in rs:
113
        resource_dict[r.full_name()] = r.get_info()
114

    
115
    return resource_dict
116

    
117

    
118
def get_default_quota():
119
    _DEFAULT_QUOTA = {}
120
    resources = Resource.objects.select_related('service').all()
121
    for resource in resources:
122
        capacity = resource.uplimit
123
        _DEFAULT_QUOTA[resource.full_name()] = capacity
124

    
125
    return _DEFAULT_QUOTA
126

    
127

    
128
SYSTEM = 'system'
129

    
130

    
131
def initial_quotas(users):
132
    initial = {}
133
    default_quotas = get_default_quota()
134

    
135
    for user in users:
136
        uuid = user.uuid
137
        source_quota = {SYSTEM: dict(default_quotas)}
138
        initial[uuid] = source_quota
139

    
140
    objs = AstakosUserQuota.objects.select_related()
141
    orig_quotas = objs.filter(user__in=users)
142
    for user_quota in orig_quotas:
143
        uuid = user_quota.user.uuid
144
        user_init = initial.get(uuid, {})
145
        resource = user_quota.resource.full_name()
146
        user_init[resource] = user_quota.capacity
147
        initial[uuid] = user_init
148

    
149
    return initial
150

    
151

    
152
def get_grant_source(grant):
153
    return SYSTEM
154

    
155

    
156
def users_quotas(users, initial=None):
157
    if initial is None:
158
        quotas = initial_quotas(users)
159
    else:
160
        quotas = copy.deepcopy(initial)
161

    
162
    ACTUALLY_ACCEPTED = ProjectMembership.ACTUALLY_ACCEPTED
163
    objs = ProjectMembership.objects.select_related('project', 'person')
164
    memberships = objs.filter(person__in=users,
165
                              state__in=ACTUALLY_ACCEPTED,
166
                              project__state=Project.APPROVED)
167

    
168
    project_ids = set(m.project_id for m in memberships)
169
    objs = ProjectApplication.objects.select_related('project')
170
    apps = objs.filter(project__in=project_ids)
171

    
172
    project_dict = {}
173
    for app in apps:
174
        project_dict[app.project] = app
175

    
176
    objs = ProjectResourceGrant.objects.select_related()
177
    grants = objs.filter(project_application__in=apps)
178

    
179
    for membership in memberships:
180
        uuid = membership.person.uuid
181
        userquotas = quotas.get(uuid, {})
182

    
183
        application = project_dict[membership.project]
184

    
185
        for grant in grants:
186
            if grant.project_application_id != application.id:
187
                continue
188

    
189
            source = get_grant_source(grant)
190
            source_quotas = userquotas.get(source, {})
191

    
192
            resource = grant.resource.full_name()
193
            prev = source_quotas.get(resource, 0)
194
            new = prev + grant.member_capacity
195
            source_quotas[resource] = new
196
            userquotas[source] = source_quotas
197
        quotas[uuid] = userquotas
198

    
199
    return quotas
200

    
201

    
202
def user_quotas(user):
203
    quotas = users_quotas([user])
204
    try:
205
        return quotas[user.uuid]
206
    except KeyError:
207
        raise ValueError("could not compute quotas")
208

    
209

    
210
def sync_users(users, sync=True):
211
    def _sync_users(users, sync):
212

    
213
        info = {}
214
        for user in users:
215
            info[user.uuid] = user.email
216

    
217
        qh_quotas, qh_limits = get_users_quotas_and_limits(users)
218
        astakos_initial = initial_quotas(users)
219
        astakos_quotas = users_quotas(users)
220

    
221
        diff_quotas = {}
222
        for holder, local in astakos_quotas.iteritems():
223
            registered = qh_limits.get(holder, None)
224
            if local != registered:
225
                diff_quotas[holder] = dict(local)
226

    
227
        if sync:
228
            r = set_user_quota(diff_quotas)
229

    
230
        return (qh_limits, qh_quotas,
231
                astakos_initial, diff_quotas, info)
232

    
233
    return _sync_users(users, sync)
234

    
235

    
236
def sync_all_users(sync=True):
237
    users = AstakosUser.objects.verified()
238
    return sync_users(users, sync)