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.contrib.auth.models import User
14 from django.db import models
15 from django.utils import timezone
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', blank=True)
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}
67 class Server(models.Model):
68 id = models.AutoField(primary_key=True)
69 resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
70 name = models.CharField(max_length=100, blank=True)
71 model = models.CharField(max_length=100, blank=True)
72 cpu = models.CharField(max_length=100, blank=True)
73 ram = models.CharField(max_length=100, blank=True)
74 storage = models.CharField(max_length=100, blank=True)
82 class ResourceStatus(models.Model):
83 id = models.AutoField(primary_key=True)
84 resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
85 timestamp = models.DateTimeField(auto_now_add=True)
86 type = models.CharField(max_length=20)
87 title = models.CharField(max_length=50)
88 content = models.CharField(max_length=5000)
91 db_table = 'resource_status'
94 return self.resource.name + ': ' + self.title + ' ' + str(self.timestamp)