Restructure dashboard project for docker deploying
[pharos.git] / tools / pharos-dashboard / src / dashboard / models.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
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 ##############################################################################
9
10
11 from datetime import timedelta
12
13 from django.utils import timezone
14 from django.contrib.auth.models import User
15 from django.db import models
16
17 from jenkins.models import JenkinsSlave
18
19
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)
28
29     def get_booking_utilization(self, weeks):
30         """
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
34         """
35
36         length = timedelta(weeks=abs(weeks))
37         now = timezone.now()
38
39         start = now
40         end = now + length
41         if weeks < 0:
42             start = now - length
43             end = now
44
45         bookings = self.booking_set.filter(start__lt=start + length, end__gt=start)
46
47         booked_seconds = 0
48         for booking in bookings:
49             booking_start = booking.start
50             booking_end = booking.end
51             if booking_start < start:
52                 booking_start = start
53             if booking_end > end:
54                 booking_end = start + length
55             total = booking_end - booking_start
56             booked_seconds += total.total_seconds()
57
58         return {'booked_seconds': booked_seconds,
59                 'available_seconds': length.total_seconds() - booked_seconds}
60
61     class Meta:
62         db_table = 'resource'
63
64     def __str__(self):
65         return self.name
66
67
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)
76
77     class Meta:
78         db_table = 'server'
79
80     def __str__(self):
81         return self.name