Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / endpoints / qh.py @ ee45eb81

History | View | Annotate | Download (9.7 kB)

1 fc1e2f02 Sofia Papagiannaki
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 5ce3ce4f Sofia Papagiannaki
#
3 fc1e2f02 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 fc1e2f02 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 fc1e2f02 Sofia Papagiannaki
# conditions are met:
6 5ce3ce4f Sofia Papagiannaki
#
7 fc1e2f02 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 fc1e2f02 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 fc1e2f02 Sofia Papagiannaki
#      disclaimer.
10 5ce3ce4f Sofia Papagiannaki
#
11 fc1e2f02 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 fc1e2f02 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 fc1e2f02 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 fc1e2f02 Sofia Papagiannaki
#      provided with the distribution.
15 5ce3ce4f Sofia Papagiannaki
#
16 fc1e2f02 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 fc1e2f02 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 fc1e2f02 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 fc1e2f02 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 fc1e2f02 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 fc1e2f02 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 fc1e2f02 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 fc1e2f02 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 fc1e2f02 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 fc1e2f02 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 fc1e2f02 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 fc1e2f02 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 5ce3ce4f Sofia Papagiannaki
#
29 fc1e2f02 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 fc1e2f02 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 fc1e2f02 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 fc1e2f02 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 fc1e2f02 Sofia Papagiannaki
34 fc1e2f02 Sofia Papagiannaki
import logging
35 bd4f356c Sofia Papagiannaki
import itertools
36 bd4f356c Sofia Papagiannaki
37 bd4f356c Sofia Papagiannaki
from functools import wraps
38 4fc7d569 Sofia Papagiannaki
from collections import namedtuple
39 fc1e2f02 Sofia Papagiannaki
40 fc1e2f02 Sofia Papagiannaki
from django.utils.translation import ugettext as _
41 fc1e2f02 Sofia Papagiannaki
42 30d92d1e Georgios D. Tsoukalas
from astakos.im.settings import (
43 0ea1f32b Georgios D. Tsoukalas
        QUOTAHOLDER_URL, QUOTAHOLDER_TOKEN, LOGGING_LEVEL)
44 fc1e2f02 Sofia Papagiannaki
45 30d92d1e Georgios D. Tsoukalas
if QUOTAHOLDER_URL:
46 c836d69f Georgios D. Tsoukalas
    from kamaki.clients.quotaholder import QuotaholderClient
47 8b59b8ea Sofia Papagiannaki
48 fc1e2f02 Sofia Papagiannaki
ENTITY_KEY = '1'
49 fc1e2f02 Sofia Papagiannaki
50 ae497612 Olga Brani
inf = float('inf')
51 ae497612 Olga Brani
52 fc1e2f02 Sofia Papagiannaki
logger = logging.getLogger(__name__)
53 fc1e2f02 Sofia Papagiannaki
54 ae497612 Olga Brani
inf = float('inf')
55 5ce3ce4f Sofia Papagiannaki
56 ee45eb81 Giorgos Korfiatis
clientkey = 'astakos'
57 ee45eb81 Giorgos Korfiatis
58 73fbaec4 Sofia Papagiannaki
_client = None
59 73fbaec4 Sofia Papagiannaki
def get_client():
60 73fbaec4 Sofia Papagiannaki
    global _client
61 73fbaec4 Sofia Papagiannaki
    if _client:
62 73fbaec4 Sofia Papagiannaki
        return _client
63 73fbaec4 Sofia Papagiannaki
    if not QUOTAHOLDER_URL:
64 73fbaec4 Sofia Papagiannaki
        return
65 73fbaec4 Sofia Papagiannaki
    _client = QuotaholderClient(QUOTAHOLDER_URL, token=QUOTAHOLDER_TOKEN)
66 52116521 Sofia Papagiannaki
    return _client
67 73fbaec4 Sofia Papagiannaki
68 bd4f356c Sofia Papagiannaki
def call(func_name):
69 b6852727 Olga Brani
    """Decorator function for Quotaholder client calls."""
70 bd4f356c Sofia Papagiannaki
    def decorator(payload_func):
71 bd4f356c Sofia Papagiannaki
        @wraps(payload_func)
72 73fbaec4 Sofia Papagiannaki
        def wrapper(entities=(), **kwargs):
73 bd4f356c Sofia Papagiannaki
            if not entities:
74 73fbaec4 Sofia Papagiannaki
                return ()
75 9a06d96f Olga Brani
76 30d92d1e Georgios D. Tsoukalas
            if not QUOTAHOLDER_URL:
77 73fbaec4 Sofia Papagiannaki
                return ()
78 9a06d96f Olga Brani
79 73fbaec4 Sofia Papagiannaki
            c = get_client() 
80 bd4f356c Sofia Papagiannaki
            func = c.__dict__.get(func_name)
81 bd4f356c Sofia Papagiannaki
            if not func:
82 73fbaec4 Sofia Papagiannaki
                return ()
83 9a06d96f Olga Brani
84 52116521 Sofia Papagiannaki
            data = payload_func(entities, c, **kwargs)
85 bd4f356c Sofia Papagiannaki
            if not data:
86 73fbaec4 Sofia Papagiannaki
                return data
87 9a06d96f Olga Brani
88 bd4f356c Sofia Papagiannaki
            funcname = func.__name__
89 bd4f356c Sofia Papagiannaki
            kwargs = {'context': {}, funcname: data}
90 bd4f356c Sofia Papagiannaki
            rejected = func(**kwargs)
91 bd4f356c Sofia Papagiannaki
            msg = _('%s: %s - Rejected: %s' % (funcname, data, rejected,))
92 bd4f356c Sofia Papagiannaki
            logger.log(LOGGING_LEVEL, msg)
93 73fbaec4 Sofia Papagiannaki
            return rejected
94 bd4f356c Sofia Papagiannaki
        return wrapper
95 bd4f356c Sofia Papagiannaki
    return decorator
96 bd4f356c Sofia Papagiannaki
97 9a06d96f Olga Brani
98 bd4f356c Sofia Papagiannaki
@call('set_quota')
99 bd4f356c Sofia Papagiannaki
def send_quota(users, client=None):
100 fc1e2f02 Sofia Papagiannaki
    data = []
101 fc1e2f02 Sofia Papagiannaki
    append = data.append
102 fc1e2f02 Sofia Papagiannaki
    for user in users:
103 670de92a Sofia Papagiannaki
        for resource, uplimit in user.quota.iteritems():
104 bd4f356c Sofia Papagiannaki
            key = ENTITY_KEY
105 bd4f356c Sofia Papagiannaki
            quantity = None
106 ae497612 Olga Brani
            capacity = uplimit if uplimit != inf else None
107 bd4f356c Sofia Papagiannaki
            import_limit = None
108 bd4f356c Sofia Papagiannaki
            export_limit = None
109 bd4f356c Sofia Papagiannaki
            flags = 0
110 9a06d96f Olga Brani
            args = (
111 9a06d96f Olga Brani
                user.email, resource, key, quantity, capacity, import_limit,
112 9a06d96f Olga Brani
                export_limit, flags)
113 fc1e2f02 Sofia Papagiannaki
            append(args)
114 bd4f356c Sofia Papagiannaki
    return data
115 5ce3ce4f Sofia Papagiannaki
116 2a97d93b Giorgos Korfiatis
QuotaLimits = namedtuple('QuotaLimits', ('holder',
117 2a97d93b Giorgos Korfiatis
                                         'capacity',
118 2a97d93b Giorgos Korfiatis
                                         'import_limit',
119 2a97d93b Giorgos Korfiatis
                                         'export_limit'))
120 2a97d93b Giorgos Korfiatis
121 ee45eb81 Giorgos Korfiatis
def qh_add_quota(serial, quotalimits_list):
122 ee45eb81 Giorgos Korfiatis
    if not QUOTAHOLDER_URL:
123 ee45eb81 Giorgos Korfiatis
        return ()
124 ee45eb81 Giorgos Korfiatis
125 ee45eb81 Giorgos Korfiatis
    context = {}
126 ee45eb81 Giorgos Korfiatis
    c = get_client()
127 ee45eb81 Giorgos Korfiatis
128 2a97d93b Giorgos Korfiatis
    data = []
129 2a97d93b Giorgos Korfiatis
    append = data.append
130 2a97d93b Giorgos Korfiatis
    for ql in quotalimits_list:
131 2a97d93b Giorgos Korfiatis
        args = (ql.holder, ql.resource, ENTITY_KEY,
132 2a97d93b Giorgos Korfiatis
                0, ql.capacity, ql.import_limit, ql.export_limit)
133 2a97d93b Giorgos Korfiatis
        append(args)
134 2a97d93b Giorgos Korfiatis
135 ee45eb81 Giorgos Korfiatis
    result = c.add_quota(context=context,
136 ee45eb81 Giorgos Korfiatis
                         clientkey=clientkey,
137 ee45eb81 Giorgos Korfiatis
                         serial=serial,
138 ee45eb81 Giorgos Korfiatis
                         add_quota=data)
139 ee45eb81 Giorgos Korfiatis
140 ee45eb81 Giorgos Korfiatis
    return result
141 ee45eb81 Giorgos Korfiatis
142 ee45eb81 Giorgos Korfiatis
def qh_query_serials(serials):
143 ee45eb81 Giorgos Korfiatis
    if not QUOTAHOLDER_URL:
144 ee45eb81 Giorgos Korfiatis
        return ()
145 ee45eb81 Giorgos Korfiatis
146 ee45eb81 Giorgos Korfiatis
    context = {}
147 ee45eb81 Giorgos Korfiatis
    c = get_client()
148 ee45eb81 Giorgos Korfiatis
    result = c.query_serials(context=context,
149 ee45eb81 Giorgos Korfiatis
                             clientkey=clientkey,
150 ee45eb81 Giorgos Korfiatis
                             serials=serials)
151 ee45eb81 Giorgos Korfiatis
    return result
152 ee45eb81 Giorgos Korfiatis
153 ee45eb81 Giorgos Korfiatis
def qh_ack_serials(serials):
154 ee45eb81 Giorgos Korfiatis
    if not QUOTAHOLDER_URL:
155 ee45eb81 Giorgos Korfiatis
        return ()
156 ee45eb81 Giorgos Korfiatis
157 ee45eb81 Giorgos Korfiatis
    context = {}
158 ee45eb81 Giorgos Korfiatis
    c = get_client()
159 ee45eb81 Giorgos Korfiatis
    result = c.ack_serials(context=context,
160 ee45eb81 Giorgos Korfiatis
                           clientkey=clientkey,
161 ee45eb81 Giorgos Korfiatis
                           serials=serials)
162 ee45eb81 Giorgos Korfiatis
    return
163 5ce3ce4f Sofia Papagiannaki
164 bd4f356c Sofia Papagiannaki
@call('set_quota')
165 bd4f356c Sofia Papagiannaki
def send_resource_quantities(resources, client=None):
166 bd4f356c Sofia Papagiannaki
    data = []
167 bd4f356c Sofia Papagiannaki
    append = data.append
168 bd4f356c Sofia Papagiannaki
    for resource in resources:
169 bd4f356c Sofia Papagiannaki
        key = ENTITY_KEY
170 bd4f356c Sofia Papagiannaki
        quantity = resource.meta.filter(key='quantity') or None
171 bd4f356c Sofia Papagiannaki
        capacity = None
172 bd4f356c Sofia Papagiannaki
        import_limit = None
173 bd4f356c Sofia Papagiannaki
        export_limit = None
174 bd4f356c Sofia Papagiannaki
        flags = 0
175 bd4f356c Sofia Papagiannaki
        args = (resource.service, resource, key, quantity, capacity,
176 bd4f356c Sofia Papagiannaki
                import_limit, export_limit, flags)
177 bd4f356c Sofia Papagiannaki
        append(args)
178 bd4f356c Sofia Papagiannaki
    return data
179 bd4f356c Sofia Papagiannaki
180 bd4f356c Sofia Papagiannaki
181 bd4f356c Sofia Papagiannaki
@call('get_quota')
182 bd4f356c Sofia Papagiannaki
def get_quota(users, client=None):
183 fc1e2f02 Sofia Papagiannaki
    data = []
184 fc1e2f02 Sofia Papagiannaki
    append = data.append
185 fc1e2f02 Sofia Papagiannaki
    for user in users:
186 fc1e2f02 Sofia Papagiannaki
        try:
187 fc1e2f02 Sofia Papagiannaki
            entity = user.email
188 fc1e2f02 Sofia Papagiannaki
        except AttributeError:
189 fc1e2f02 Sofia Papagiannaki
            continue
190 fc1e2f02 Sofia Papagiannaki
        else:
191 bd4f356c Sofia Papagiannaki
            for r in user.quota.keys():
192 bd4f356c Sofia Papagiannaki
                args = entity, r, ENTITY_KEY
193 fc1e2f02 Sofia Papagiannaki
                append(args)
194 bd4f356c Sofia Papagiannaki
    return data
195 5ce3ce4f Sofia Papagiannaki
196 5ce3ce4f Sofia Papagiannaki
197 bd4f356c Sofia Papagiannaki
@call('create_entity')
198 bd4f356c Sofia Papagiannaki
def create_entities(entities, client=None, field=''):
199 fc1e2f02 Sofia Papagiannaki
    data = []
200 fc1e2f02 Sofia Papagiannaki
    append = data.append
201 bd4f356c Sofia Papagiannaki
    for entity in entities:
202 fc1e2f02 Sofia Papagiannaki
        try:
203 bd4f356c Sofia Papagiannaki
            entity = entity.__getattribute__(field)
204 fc1e2f02 Sofia Papagiannaki
        except AttributeError:
205 fc1e2f02 Sofia Papagiannaki
            continue
206 bd4f356c Sofia Papagiannaki
        owner = 'system'
207 bd4f356c Sofia Papagiannaki
        key = ENTITY_KEY
208 bd4f356c Sofia Papagiannaki
        ownerkey = ''
209 bd4f356c Sofia Papagiannaki
        args = entity, owner, key, ownerkey
210 bd4f356c Sofia Papagiannaki
        append(args)
211 bd4f356c Sofia Papagiannaki
    return data
212 5ce3ce4f Sofia Papagiannaki
213 5ce3ce4f Sofia Papagiannaki
214 bd4f356c Sofia Papagiannaki
def register_users(users, client=None):
215 bd4f356c Sofia Papagiannaki
    users, copy = itertools.tee(users)
216 73fbaec4 Sofia Papagiannaki
    rejected = create_entities(entities=users,
217 bd4f356c Sofia Papagiannaki
                                       client=client,
218 bd4f356c Sofia Papagiannaki
                                       field='email')
219 bd4f356c Sofia Papagiannaki
    created = (e for e in copy if unicode(e) not in rejected)
220 73fbaec4 Sofia Papagiannaki
    return send_quota(created)
221 bd4f356c Sofia Papagiannaki
222 bd4f356c Sofia Papagiannaki
223 bd4f356c Sofia Papagiannaki
def register_resources(resources, client=None):
224 bd4f356c Sofia Papagiannaki
    resources, copy = itertools.tee(resources)
225 73fbaec4 Sofia Papagiannaki
    rejected = create_entities(entities=resources,
226 bd4f356c Sofia Papagiannaki
                                       client=client,
227 bd4f356c Sofia Papagiannaki
                                       field='service')
228 bd4f356c Sofia Papagiannaki
    created = (e for e in copy if unicode(e) not in rejected)
229 73fbaec4 Sofia Papagiannaki
    return send_resource_quantities(created)
230 2925e285 root
231 2925e285 root
232 2925e285 root
from datetime import datetime
233 2925e285 root
234 2925e285 root
strptime = datetime.strptime
235 2925e285 root
timefmt = '%Y-%m-%dT%H:%M:%S.%f'
236 2925e285 root
237 476fdba1 root
SECOND_RESOLUTION = 1
238 476fdba1 root
239 9a06d96f Olga Brani
240 2925e285 root
def total_seconds(timedelta_object):
241 2925e285 root
    return timedelta_object.seconds + timedelta_object.days * 86400
242 2925e285 root
243 9a06d96f Olga Brani
244 476fdba1 root
def iter_timeline(timeline, before):
245 476fdba1 root
    if not timeline:
246 476fdba1 root
        return
247 476fdba1 root
248 476fdba1 root
    for t in timeline:
249 476fdba1 root
        yield t
250 476fdba1 root
251 476fdba1 root
    t = dict(t)
252 476fdba1 root
    t['issue_time'] = before
253 476fdba1 root
    yield t
254 476fdba1 root
255 9a06d96f Olga Brani
256 476fdba1 root
def _usage_units(timeline, after, before, details=0):
257 476fdba1 root
258 2925e285 root
    t_total = 0
259 476fdba1 root
    uu_total = 0
260 476fdba1 root
    t_after = strptime(after, timefmt)
261 476fdba1 root
    t_before = strptime(before, timefmt)
262 476fdba1 root
    t0 = t_after
263 476fdba1 root
    u0 = 0
264 2925e285 root
265 476fdba1 root
    for point in iter_timeline(timeline, before):
266 2925e285 root
        issue_time = point['issue_time']
267 2925e285 root
268 476fdba1 root
        if issue_time <= after:
269 476fdba1 root
            u0 = point['target_allocated_through']
270 2925e285 root
            continue
271 2925e285 root
272 476fdba1 root
        t = strptime(issue_time, timefmt) if issue_time <= before else t_before
273 476fdba1 root
        t_diff = int(total_seconds(t - t0) * SECOND_RESOLUTION)
274 2925e285 root
        t_total += t_diff
275 2925e285 root
        uu_cost = u0 * t_diff
276 2925e285 root
        uu_total += uu_cost
277 476fdba1 root
        t0 = t
278 476fdba1 root
        u0 = point['target_allocated_through']
279 2925e285 root
280 476fdba1 root
        target = point['target']
281 2925e285 root
        if details:
282 2925e285 root
            yield  (target,
283 2925e285 root
                    point['resource'],
284 2925e285 root
                    point['name'],
285 2925e285 root
                    issue_time,
286 2925e285 root
                    uu_cost,
287 2925e285 root
                    uu_total)
288 2925e285 root
289 2925e285 root
    if not t_total:
290 2925e285 root
        return
291 2925e285 root
292 2925e285 root
    yield  (target,
293 2925e285 root
            'total',
294 2925e285 root
            point['resource'],
295 2925e285 root
            issue_time,
296 9a06d96f Olga Brani
            uu_total / t_total,
297 2925e285 root
            uu_total)
298 2925e285 root
299 9a06d96f Olga Brani
300 476fdba1 root
def usage_units(timeline, after, before, details=0):
301 476fdba1 root
    return list(_usage_units(timeline, after, before, details=details))
302 2925e285 root
303 9a06d96f Olga Brani
304 476fdba1 root
def traffic_units(timeline, after, before, details=0):
305 82b05401 root
    tu_total = 0
306 82b05401 root
    target = None
307 82b05401 root
    issue_time = None
308 82b05401 root
309 82b05401 root
    for point in timeline:
310 82b05401 root
        issue_time = point['issue_time']
311 476fdba1 root
        if issue_time <= after:
312 476fdba1 root
            continue
313 476fdba1 root
        if issue_time > before:
314 476fdba1 root
            break
315 476fdba1 root
316 476fdba1 root
        target = point['target']
317 82b05401 root
        tu = point['target_allocated_through']
318 82b05401 root
        tu_total += tu
319 82b05401 root
320 82b05401 root
        if details:
321 82b05401 root
            yield  (target,
322 82b05401 root
                    point['resource'],
323 82b05401 root
                    point['name'],
324 82b05401 root
                    issue_time,
325 82b05401 root
                    tu,
326 82b05401 root
                    tu_total)
327 82b05401 root
328 82b05401 root
    if not tu_total:
329 82b05401 root
        return
330 82b05401 root
331 82b05401 root
    yield  (target,
332 82b05401 root
            'total',
333 82b05401 root
            point['resource'],
334 82b05401 root
            issue_time,
335 9a06d96f Olga Brani
            tu_total // len(timeline),
336 82b05401 root
            tu_total)
337 2925e285 root
338 9a06d96f Olga Brani
339 2925e285 root
def timeline_charge(entity, resource, after, before, details, charge_type):
340 2925e285 root
    key = '1'
341 2925e285 root
    if charge_type == 'charge_usage':
342 2925e285 root
        charge_units = usage_units
343 2925e285 root
    elif charge_type == 'charge_traffic':
344 2925e285 root
        charge_units = traffic_units
345 2925e285 root
    else:
346 2925e285 root
        m = 'charge type %s not supported' % charge_type
347 2925e285 root
        raise ValueError(m)
348 2925e285 root
349 30d92d1e Georgios D. Tsoukalas
    quotaholder = QuotaholderClient(QUOTAHOLDER_URL, token=QUOTAHOLDER_TOKEN)
350 2925e285 root
    timeline = quotaholder.get_timeline(
351 9a06d96f Olga Brani
        context={},
352 9a06d96f Olga Brani
        after=after,
353 9a06d96f Olga Brani
        before=before,
354 9a06d96f Olga Brani
        get_timeline=[[entity, resource, key]])
355 476fdba1 root
    cu = charge_units(timeline, after, before, details=details)
356 82b05401 root
    return cu