1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from datetime import timedelta
13 from django.utils import timezone
14 from django.contrib.auth.models import User
15 from django.db import models
17 from jenkins.models import JenkinsSlave
20 class Resource(models.Model):
21 id = models.AutoField(primary_key=True)
22 name = models.CharField(max_length=100, unique=True)
23 description = models.CharField(max_length=300, blank=True, null=True)
24 url = models.CharField(max_length=100, blank=True, null=True)
25 owner = models.ForeignKey(User, related_name='user_lab_owner', null=True)
26 vpn_users = models.ManyToManyField(User, related_name='user_vpn_users')
27 slave = models.ForeignKey(JenkinsSlave, on_delete=models.DO_NOTHING, null=True)
29 def get_booking_utilization(self, weeks):
31 Return a dictionary containing the count of booked and free seconds for a resource in the
32 range [now,now + weeks] if weeks is positive,
33 or [now-weeks, now] if weeks is negative
36 length = timedelta(weeks=abs(weeks))
45 bookings = self.booking_set.filter(start__lt=start + length, end__gt=start)
48 for booking in bookings:
49 booking_start = booking.start
50 booking_end = booking.end
51 if booking_start < start:
54 booking_end = start + length
55 total = booking_end - booking_start
56 booked_seconds += total.total_seconds()
58 return {'booked_seconds': booked_seconds,
59 'available_seconds': length.total_seconds() - booked_seconds}
68 class Server(models.Model):
69 id = models.AutoField(primary_key=True)
70 resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
71 name = models.CharField(max_length=100, blank=True)
72 model = models.CharField(max_length=100, blank=True)
73 cpu = models.CharField(max_length=100, blank=True)
74 ram = models.CharField(max_length=100, blank=True)
75 storage = models.CharField(max_length=100, blank=True)