summaryrefslogtreecommitdiffstats
path: root/tools/pharos-dashboard/dashboard
diff options
context:
space:
mode:
authormaxbr <maxbr@mi.fu-berlin.de>2016-09-12 11:08:56 +0200
committermaxbr <maxbr@mi.fu-berlin.de>2016-09-12 11:08:56 +0200
commitc684fe13cf6f3b4666a664bfdc638e446b2d123f (patch)
tree35d2e23477976c61a14cff0426e3a343ccf94360 /tools/pharos-dashboard/dashboard
parentd6f164dc96576a93d5472a95878a79b8167b5898 (diff)
Add booking utilization chart
JIRA: PHAROS-263 Change-Id: I3a3a5115a1cf7742f9ac5a3ec753699c36b49815 Signed-off-by: maxbr <maxbr@mi.fu-berlin.de>
Diffstat (limited to 'tools/pharos-dashboard/dashboard')
-rw-r--r--tools/pharos-dashboard/dashboard/models.py38
-rw-r--r--tools/pharos-dashboard/dashboard/templatetags/jira_filters.py2
-rw-r--r--tools/pharos-dashboard/dashboard/urls.py5
-rw-r--r--tools/pharos-dashboard/dashboard/views.py22
4 files changed, 63 insertions, 4 deletions
diff --git a/tools/pharos-dashboard/dashboard/models.py b/tools/pharos-dashboard/dashboard/models.py
index d645cd55..734da386 100644
--- a/tools/pharos-dashboard/dashboard/models.py
+++ b/tools/pharos-dashboard/dashboard/models.py
@@ -1,3 +1,6 @@
+from datetime import timedelta
+
+from django.utils import timezone
from django.contrib.auth.models import User
from django.db import models
@@ -9,9 +12,42 @@ class Resource(models.Model):
name = models.CharField(max_length=100, unique=True)
description = models.CharField(max_length=300, blank=True, null=True)
url = models.CharField(max_length=100, blank=True, null=True)
- owner = models.ForeignKey(User)
+ owner = models.ForeignKey(User, related_name='user_lab_owner', null=True)
+ vpn_users = models.ManyToManyField(User, related_name='user_vpn_users')
slave = models.ForeignKey(JenkinsSlave, on_delete=models.DO_NOTHING, null=True)
+ def get_booking_utilization(self, weeks):
+ """
+ Return a dictionary containing the count of booked and free seconds for a resource in the
+ range [now,now + weeks] if weeks is positive,
+ or [now-weeks, now] if weeks is negative
+ """
+
+ length = timedelta(weeks=abs(weeks))
+ now = timezone.now()
+
+ start = now
+ end = now + length
+ if weeks < 0:
+ start = now - length
+ end = now
+
+ bookings = self.booking_set.filter(start__lt=start + length, end__gt=start)
+
+ booked_seconds = 0
+ for booking in bookings:
+ booking_start = booking.start
+ booking_end = booking.end
+ if booking_start < start:
+ booking_start = start
+ if booking_end > end:
+ booking_end = start + length
+ total = booking_end - booking_start
+ booked_seconds += total.total_seconds()
+
+ return {'booked_seconds': booked_seconds,
+ 'available_seconds': length.total_seconds() - booked_seconds}
+
class Meta:
db_table = 'resource'
diff --git a/tools/pharos-dashboard/dashboard/templatetags/jira_filters.py b/tools/pharos-dashboard/dashboard/templatetags/jira_filters.py
index d9c27612..1be0600b 100644
--- a/tools/pharos-dashboard/dashboard/templatetags/jira_filters.py
+++ b/tools/pharos-dashboard/dashboard/templatetags/jira_filters.py
@@ -1,6 +1,6 @@
from django.template.defaultfilters import register
-from pharos_dashboard import settings
+from django.conf import settings
@register.filter
diff --git a/tools/pharos-dashboard/dashboard/urls.py b/tools/pharos-dashboard/dashboard/urls.py
index 809204c1..baa2d633 100644
--- a/tools/pharos-dashboard/dashboard/urls.py
+++ b/tools/pharos-dashboard/dashboard/urls.py
@@ -23,6 +23,9 @@ urlpatterns = [
url(r'^jenkins_slaves/$', JenkinsSlavesView.as_view(), name='jenkins_slaves'),
url(r'^resource/all/$', LabOwnerView.as_view(), name='resources'),
url(r'^resource/(?P<resource_id>[0-9]+)/$', ResourceView.as_view(), name='resource'),
-
+ url(r'^resource/(?P<resource_id>[0-9]+)/booking_utilization/(?P<weeks>-?\d+)/$',
+ BookingUtilizationJSON.as_view(), name='booking_utilization'),
+ url(r'^resource/(?P<resource_id>[0-9]+)/jenkins_utilization/(?P<weeks>-?\d+)/$',
+ JenkinsUtilizationJSON.as_view(), name='jenkins_utilization'),
url(r'^$', DevelopmentPodsView.as_view(), name="index"),
]
diff --git a/tools/pharos-dashboard/dashboard/views.py b/tools/pharos-dashboard/dashboard/views.py
index ef1845c8..8954f6c3 100644
--- a/tools/pharos-dashboard/dashboard/views.py
+++ b/tools/pharos-dashboard/dashboard/views.py
@@ -59,7 +59,8 @@ class ResourceView(TemplateView):
utilization = resource.slave.get_utilization(timedelta(days=7))
bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
context = super(ResourceView, self).get_context_data(**kwargs)
- context.update({'title': str(resource), 'resource': resource, 'utilization': utilization, 'bookings': bookings})
+ context.update({'title': str(resource), 'resource': resource, 'utilization': utilization,
+ 'bookings': bookings})
return context
@@ -76,3 +77,22 @@ class LabOwnerView(TemplateView):
context = super(LabOwnerView, self).get_context_data(**kwargs)
context.update({'title': "Overview", 'pods': pods})
return context
+
+
+class BookingUtilizationJSON(View):
+ def get(self, request, *args, **kwargs):
+ resource = get_object_or_404(Resource, id=kwargs['resource_id'])
+ utilization = resource.get_booking_utilization(int(kwargs['weeks']))
+ utilization = [
+ {
+ 'label': 'Booked',
+ 'data': utilization['booked_seconds'],
+ 'color': '#d9534f'
+ },
+ {
+ 'label': 'Available',
+ 'data': utilization['available_seconds'],
+ 'color': '#5cb85c'
+ },
+ ]
+ return JsonResponse({'data': utilization})