root / snf-astakos-app / astakos / im / endpoints / qh.py @ 6f00ed3e
History | View | Annotate | Download (10.3 kB)
1 |
# Copyright 2011-2012 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 |
import logging |
35 |
import itertools |
36 |
|
37 |
from functools import wraps |
38 |
from collections import namedtuple |
39 |
|
40 |
from django.utils.translation import ugettext as _ |
41 |
|
42 |
from astakos.im.settings import ( |
43 |
QUOTAHOLDER_URL, QUOTAHOLDER_TOKEN, LOGGING_LEVEL) |
44 |
|
45 |
if QUOTAHOLDER_URL:
|
46 |
from kamaki.clients.quotaholder import QuotaholderClient |
47 |
from kamaki.clients.quotaholder import QH_PRACTICALLY_INFINITE |
48 |
|
49 |
ENTITY_KEY = '1'
|
50 |
|
51 |
|
52 |
logger = logging.getLogger(__name__) |
53 |
|
54 |
inf = float('inf') |
55 |
|
56 |
clientkey = 'astakos'
|
57 |
|
58 |
_client = None
|
59 |
def get_client(): |
60 |
global _client
|
61 |
if _client:
|
62 |
return _client
|
63 |
if not QUOTAHOLDER_URL: |
64 |
return
|
65 |
_client = QuotaholderClient(QUOTAHOLDER_URL, token=QUOTAHOLDER_TOKEN) |
66 |
return _client
|
67 |
|
68 |
def set_quota(payload): |
69 |
c = get_client() |
70 |
if not c: |
71 |
return
|
72 |
result = c.set_quota(context={}, clientkey=clientkey, set_quota=payload) |
73 |
logger.info('set_quota: %s rejected: %s' % (payload, result))
|
74 |
return result
|
75 |
|
76 |
def get_quota(user): |
77 |
c = get_client() |
78 |
if not c: |
79 |
return
|
80 |
payload = [] |
81 |
append = payload.append |
82 |
for r in user.quota.keys(): |
83 |
append((user.uuid, r, ENTITY_KEY),) |
84 |
result = c.get_quota(context={}, clientkey=clientkey, get_quota=payload) |
85 |
logger.info('get_quota: %s rejected: %s' % (payload, result))
|
86 |
return result
|
87 |
|
88 |
def create_entity(payload): |
89 |
c = get_client() |
90 |
if not c: |
91 |
return
|
92 |
result = c.create_entity(context={}, clientkey=clientkey, create_entity=payload) |
93 |
logger.info('create_entity: %s rejected: %s' % (payload, result))
|
94 |
return result
|
95 |
|
96 |
SetQuotaPayload = namedtuple('SetQuotaPayload', ('holder', |
97 |
'resource',
|
98 |
'key',
|
99 |
'quantity',
|
100 |
'capacity',
|
101 |
'import_limit',
|
102 |
'export_limit',
|
103 |
'flags'))
|
104 |
|
105 |
GetQuotaPayload = namedtuple('GetQuotaPayload', ('holder', |
106 |
'resource',
|
107 |
'key'))
|
108 |
|
109 |
CreateEntityPayload = namedtuple('CreateEntityPayload', ('entity', |
110 |
'owner',
|
111 |
'key',
|
112 |
'ownerkey'))
|
113 |
QuotaLimits = namedtuple('QuotaLimits', ('holder', |
114 |
'resource',
|
115 |
'capacity',
|
116 |
'import_limit',
|
117 |
'export_limit'))
|
118 |
|
119 |
def register_users(users): |
120 |
payload = list(CreateEntityPayload(
|
121 |
entity=u.uuid, |
122 |
owner='system',
|
123 |
key=ENTITY_KEY, |
124 |
ownerkey='') for u in users) |
125 |
rejected = create_entity(payload) |
126 |
if not rejected: |
127 |
payload = [] |
128 |
append = payload.append |
129 |
for u in users: |
130 |
for resource, uplimit in u.quota.iteritems(): |
131 |
append( SetQuotaPayload( |
132 |
holder=u.uuid, |
133 |
resource=resource, |
134 |
key=ENTITY_KEY, |
135 |
quantity=0,
|
136 |
capacity=uplimit if uplimit != inf else None, |
137 |
import_limit=QH_PRACTICALLY_INFINITE, |
138 |
export_limit=QH_PRACTICALLY_INFINITE, |
139 |
flags=0))
|
140 |
return set_quota(payload)
|
141 |
|
142 |
|
143 |
def register_resources(resources): |
144 |
rdata = ((r.service, r) for r in resources) |
145 |
services = set(r.service for r in resources) |
146 |
payload = list(CreateEntityPayload(
|
147 |
entity=service, |
148 |
owner='system',
|
149 |
key=ENTITY_KEY, |
150 |
ownerkey='') for service in set(services)) |
151 |
rejected = create_entity(payload) |
152 |
if not rejected: |
153 |
payload = list(SetQuotaPayload(
|
154 |
holder=resource.service, |
155 |
resource=resource, |
156 |
key=ENTITY_KEY, |
157 |
quantity=QH_PRACTICALLY_INFINITE, |
158 |
capacity=0,
|
159 |
import_limit=QH_PRACTICALLY_INFINITE, |
160 |
export_limit=QH_PRACTICALLY_INFINITE, |
161 |
flags=0) for resource in resources) |
162 |
return set_quota(payload)
|
163 |
|
164 |
def qh_add_quota(serial, sub_list, add_list): |
165 |
if not QUOTAHOLDER_URL: |
166 |
return ()
|
167 |
|
168 |
context = {} |
169 |
c = get_client() |
170 |
|
171 |
sub_quota = [] |
172 |
sub_append = sub_quota.append |
173 |
add_quota = [] |
174 |
add_append = add_quota.append |
175 |
|
176 |
for ql in sub_list: |
177 |
args = (ql.holder, ql.resource, ENTITY_KEY, |
178 |
0, ql.capacity, ql.import_limit, ql.export_limit)
|
179 |
sub_append(args) |
180 |
|
181 |
for ql in add_list: |
182 |
args = (ql.holder, ql.resource, ENTITY_KEY, |
183 |
0, ql.capacity, ql.import_limit, ql.export_limit)
|
184 |
add_append(args) |
185 |
|
186 |
result = c.add_quota(context=context, |
187 |
clientkey=clientkey, |
188 |
serial=serial, |
189 |
sub_quota=sub_quota, |
190 |
add_quota=add_quota) |
191 |
|
192 |
return result
|
193 |
|
194 |
def qh_query_serials(serials): |
195 |
if not QUOTAHOLDER_URL: |
196 |
return ()
|
197 |
|
198 |
context = {} |
199 |
c = get_client() |
200 |
result = c.query_serials(context=context, |
201 |
clientkey=clientkey, |
202 |
serials=serials) |
203 |
return result
|
204 |
|
205 |
def qh_ack_serials(serials): |
206 |
if not QUOTAHOLDER_URL: |
207 |
return ()
|
208 |
|
209 |
context = {} |
210 |
c = get_client() |
211 |
result = c.ack_serials(context=context, |
212 |
clientkey=clientkey, |
213 |
serials=serials) |
214 |
return
|
215 |
|
216 |
from datetime import datetime |
217 |
|
218 |
strptime = datetime.strptime |
219 |
timefmt = '%Y-%m-%dT%H:%M:%S.%f'
|
220 |
|
221 |
SECOND_RESOLUTION = 1
|
222 |
|
223 |
|
224 |
def total_seconds(timedelta_object): |
225 |
return timedelta_object.seconds + timedelta_object.days * 86400 |
226 |
|
227 |
|
228 |
def iter_timeline(timeline, before): |
229 |
if not timeline: |
230 |
return
|
231 |
|
232 |
for t in timeline: |
233 |
yield t
|
234 |
|
235 |
t = dict(t)
|
236 |
t['issue_time'] = before
|
237 |
yield t
|
238 |
|
239 |
|
240 |
def _usage_units(timeline, after, before, details=0): |
241 |
|
242 |
t_total = 0
|
243 |
uu_total = 0
|
244 |
t_after = strptime(after, timefmt) |
245 |
t_before = strptime(before, timefmt) |
246 |
t0 = t_after |
247 |
u0 = 0
|
248 |
|
249 |
for point in iter_timeline(timeline, before): |
250 |
issue_time = point['issue_time']
|
251 |
|
252 |
if issue_time <= after:
|
253 |
u0 = point['target_allocated_through']
|
254 |
continue
|
255 |
|
256 |
t = strptime(issue_time, timefmt) if issue_time <= before else t_before |
257 |
t_diff = int(total_seconds(t - t0) * SECOND_RESOLUTION)
|
258 |
t_total += t_diff |
259 |
uu_cost = u0 * t_diff |
260 |
uu_total += uu_cost |
261 |
t0 = t |
262 |
u0 = point['target_allocated_through']
|
263 |
|
264 |
target = point['target']
|
265 |
if details:
|
266 |
yield (target,
|
267 |
point['resource'],
|
268 |
point['name'],
|
269 |
issue_time, |
270 |
uu_cost, |
271 |
uu_total) |
272 |
|
273 |
if not t_total: |
274 |
return
|
275 |
|
276 |
yield (target,
|
277 |
'total',
|
278 |
point['resource'],
|
279 |
issue_time, |
280 |
uu_total / t_total, |
281 |
uu_total) |
282 |
|
283 |
|
284 |
def usage_units(timeline, after, before, details=0): |
285 |
return list(_usage_units(timeline, after, before, details=details)) |
286 |
|
287 |
|
288 |
def traffic_units(timeline, after, before, details=0): |
289 |
tu_total = 0
|
290 |
target = None
|
291 |
issue_time = None
|
292 |
|
293 |
for point in timeline: |
294 |
issue_time = point['issue_time']
|
295 |
if issue_time <= after:
|
296 |
continue
|
297 |
if issue_time > before:
|
298 |
break
|
299 |
|
300 |
target = point['target']
|
301 |
tu = point['target_allocated_through']
|
302 |
tu_total += tu |
303 |
|
304 |
if details:
|
305 |
yield (target,
|
306 |
point['resource'],
|
307 |
point['name'],
|
308 |
issue_time, |
309 |
tu, |
310 |
tu_total) |
311 |
|
312 |
if not tu_total: |
313 |
return
|
314 |
|
315 |
yield (target,
|
316 |
'total',
|
317 |
point['resource'],
|
318 |
issue_time, |
319 |
tu_total // len(timeline),
|
320 |
tu_total) |
321 |
|
322 |
|
323 |
def timeline_charge(entity, resource, after, before, details, charge_type): |
324 |
key = '1'
|
325 |
if charge_type == 'charge_usage': |
326 |
charge_units = usage_units |
327 |
elif charge_type == 'charge_traffic': |
328 |
charge_units = traffic_units |
329 |
else:
|
330 |
m = 'charge type %s not supported' % charge_type
|
331 |
raise ValueError(m) |
332 |
|
333 |
quotaholder = QuotaholderClient(QUOTAHOLDER_URL, token=QUOTAHOLDER_TOKEN) |
334 |
timeline = quotaholder.get_timeline( |
335 |
context={}, |
336 |
after=after, |
337 |
before=before, |
338 |
get_timeline=[[entity, resource, key]]) |
339 |
cu = charge_units(timeline, after, before, details=details) |
340 |
return cu
|