Merge "Adding Labels"
[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.contrib.auth.models import User
14 from django.db import models
15 from django.utils import timezone
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, 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)
29
30     def get_booking_utilization(self, weeks):
31         """
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
35         """
36
37         length = timedelta(weeks=abs(weeks))
38         now = timezone.now()
39
40         start = now
41         end = now + length
42         if weeks < 0:
43             start = now - length
44             end = now
45
46         bookings = self.booking_set.filter(start__lt=start + length, end__gt=start)
47
48         booked_seconds = 0
49         for booking in bookings:
50             booking_start = booking.start
51             booking_end = booking.end
52             if booking_start < start:
53                 booking_start = start
54             if booking_end > end:
55                 booking_end = start + length
56             total = booking_end - booking_start
57             booked_seconds += total.total_seconds()
58
59         return {'booked_seconds': booked_seconds,
60                 'available_seconds': length.total_seconds() - booked_seconds}
61
62     class Meta:
63         db_table = 'resource'
64
65     def __str__(self):
66         return self.name
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
82
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)
90
91     class Meta:
92         db_table = 'resource_status'
93
94     def __str__(self):
95         return self.resource.name + ': ' + self.title + ' ' + str(self.timestamp)