26 from django.db
import models
27 from django.db.models
import Sum
29 from django.conf
import settings
30 from cm.utils.exception
import CMException
31 from cm.utils
import log
32 from common.states
import vm_states, image_states
62 memory = models.IntegerField()
63 cpu = models.IntegerField()
64 storage = models.IntegerField()
65 public_ip = models.IntegerField()
66 points = models.IntegerField()
75 def get_quota(self, fields=None, used=True, total=True):
77 fields = fields
or [
"memory",
"cpu",
"storage",
"public_ip",
"points"]
79 if used
and field !=
"public_ip":
80 quota[
"%s_used" % field] = getattr(self,
"used_%s" % field)
82 quota[field] = getattr(self, field)
83 quota[
'user_id'] = self.id
100 d[
'user_id'] = self.id
128 d[
'user_id'] = self.id
137 d[
'used_public_ip'] = self.public_ips.count()
156 u.memory = settings.USER_QUOTA[
'memory']
157 u.cpu = settings.USER_QUOTA[
'cpu']
158 u.storage = settings.USER_QUOTA[
'storage']
159 u.public_ip = settings.USER_QUOTA[
'public_ip']
160 u.points = settings.USER_QUOTA[
'points']
172 c = self.vm_set.filter(state__in=[vm_states[
'running'], vm_states[
'running ctx'], vm_states[
'init']]).aggregate(cpu_sum=Sum(
'template__cpu'))
174 return c[
'cpu_sum']
or 0
184 m = self.vm_set.filter(state__in=[vm_states[
'running'], vm_states[
'running ctx'], vm_states[
'init']]).aggregate(memory_sum=Sum(
'template__memory'))
186 return m[
'memory_sum']
or 0
198 return self.image_set.exclude(state__exact=image_states[
'locked']).aggregate(Sum(
'size'))[
'size__sum']
or 0
216 dt_now = datetime.datetime.now()
217 start_month = datetime.datetime(dt_now.year, dt_now.month, 1)
226 vms = self.vm_set.exclude(state__in=[vm_states[
'failed'], vm_states[
'saving failed'], vm_states[
'erased']]).exclude(stop_time__lte=start_month)
231 if vm.start_time > start:
232 start = vm.start_time
233 t = (vm.stop_time
or dt_now) - start
234 if t.total_seconds() < 0:
235 t = datetime.timedelta(0)
236 p += vm.template.points * (t.days * 24 + t.seconds / 3600.0)
259 dt_now = datetime.datetime.now()
260 start_month = datetime.datetime(dt_now.year, dt_now.month, 1)
261 start_time = calendar.timegm(start_month.timetuple())
262 now = calendar.timegm(dt_now.timetuple())
266 vms = self.vm_set.exclude(state__in=[vm_states[
'failed'],
267 vm_states[
'saving failed'],
268 vm_states[
'erased']]).exclude(stop_time__lte=start_month)
272 if vm.start_time > start:
273 start = vm.start_time
274 stop = vm.stop_time
or dt_now
276 pq.append([vmn, calendar.timegm(start.timetuple()), vm.template.points,
"%s started" % (vm.name)])
277 pq.append([vmn, calendar.timegm(stop.timetuple()), vm.template.points,
"%s stopped" % (vm.name)])
280 pq = sorted(pq, key=
lambda d: d[1])
282 pt.append([start_time, p,
"beginning of the month"])
286 p += (w[1] - pts[v][0]) / 3600.0 * pts[v][1]
287 pts[v] = [w[1], pts[v][1]]
291 pts.update({w[0]: [w[1], w[2]]})
292 if not (w[1]
in [ts[0]
for ts
in pt]
or w[1] == now):
293 pt.append([w[1],
"%.4f" % p,
"%s [%.0f]" % (w[3], p)])
295 pt.append([now,
"%.4f" % p,
"now [%.0f]" % p])
296 pt = sorted(pt, key=
lambda d: d[0])
297 return {
'points': pt,
318 for template, count
in template_count:
319 cpu_sum += template.cpu * count
320 mem_sum += template.memory * count
322 raise CMException(
'user_cpu_limit')
324 raise CMException(
'user_memory_limit')
326 raise CMException(
'user_storage_limit')
339 raise CMException(
'user_storage_limit')
348 raise CMException(
'user_points_limit')
363 user = User.objects.get(pk=user_id)
364 except User.DoesNotExist:
365 log.exception(user_id,
'Cannot get user')
366 raise CMException(
'user_get')