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, blank=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, blank=True)
28 dev_pod = models.BooleanField(default=False)
30 def get_booking_utilization(self, weeks):
32 Return a dictionary containing the count of booked and free seconds for a resource in the
33 range [now,now + weeks] if weeks is positive,
34 or [now-weeks, now] if weeks is negative
37 length = timedelta(weeks=abs(weeks))
46 bookings = self.booking_set.filter(start__lt=start + length, end__gt=start)
49 for booking in bookings:
50 booking_start = booking.start
51 booking_end = booking.end
52 if booking_start < start:
55 booking_end = start + length
56 total = booking_end - booking_start
57 booked_seconds += total.total_seconds()
59 return {'booked_seconds': booked_seconds,
60 '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)
83 class ResourceStatus(models.Model):
84 id = models.AutoField(primary_key=True)
85 resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
86 timestamp = models.DateTimeField(auto_now_add=True)
87 type = models.CharField(max_length=20)
88 title = models.CharField(max_length=50)
89 content = models.CharField(max_length=5000)
92 db_table = 'resource_status'
95 return self.resource.name + ': ' + self.title + ' ' + str(self.timestamp)