1 from datetime import timedelta
3 from django.shortcuts import get_object_or_404
4 from django.utils import timezone
5 from django.views.generic import TemplateView
7 from booking.models import Booking
8 from dashboard.models import Resource
9 from jenkins.models import JenkinsSlave
12 class JenkinsSlavesView(TemplateView):
13 template_name = "dashboard/jenkins_slaves.html"
15 def get_context_data(self, **kwargs):
16 slaves = JenkinsSlave.objects.all()
17 context = super(JenkinsSlavesView, self).get_context_data(**kwargs)
18 context.update({'title': "Jenkins Slaves", 'slaves': slaves})
22 class CIPodsView(TemplateView):
23 template_name = "dashboard/ci_pods.html"
25 def get_context_data(self, **kwargs):
26 ci_pods = Resource.objects.filter(slave__ci_slave=True)
27 context = super(CIPodsView, self).get_context_data(**kwargs)
28 context.update({'title': "CI Pods", 'ci_pods': ci_pods})
32 class DevelopmentPodsView(TemplateView):
33 template_name = "dashboard/dev_pods.html"
35 def get_context_data(self, **kwargs):
36 resources = Resource.objects.filter(slave__dev_pod=True)
38 bookings = Booking.objects.filter(start__lte=timezone.now())
39 bookings = bookings.filter(end__gt=timezone.now())
42 for resource in resources:
43 booking_utilization = resource.get_booking_utilization(weeks=4)
44 total = booking_utilization['booked_seconds'] + booking_utilization['available_seconds']
46 utilization_percentage = "%d%%" % (float(booking_utilization['booked_seconds']) /
48 except (ValueError, ZeroDivisionError):
51 dev_pod = (resource, None, utilization_percentage)
52 for booking in bookings:
53 if booking.resource == resource:
54 dev_pod = (resource, booking, utilization_percentage)
55 dev_pods.append(dev_pod)
57 context = super(DevelopmentPodsView, self).get_context_data(**kwargs)
58 context.update({'title': "Development Pods", 'dev_pods': dev_pods})
62 class ResourceView(TemplateView):
63 template_name = "dashboard/resource.html"
65 def get_context_data(self, **kwargs):
66 resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
67 utilization = resource.slave.get_utilization(timedelta(days=7))
68 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
69 context = super(ResourceView, self).get_context_data(**kwargs)
70 context.update({'title': str(resource), 'resource': resource, 'utilization': utilization,
71 'bookings': bookings})
75 class LabOwnerView(TemplateView):
76 template_name = "dashboard/resource_all.html"
78 def get_context_data(self, **kwargs):
79 resources = Resource.objects.filter(slave__dev_pod=True)
81 for resource in resources:
82 utilization = resource.slave.get_utilization(timedelta(days=7))
83 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
84 pods.append((resource, utilization, bookings))
85 context = super(LabOwnerView, self).get_context_data(**kwargs)
86 context.update({'title': "Overview", 'pods': pods})
90 class BookingUtilizationJSON(View):
91 def get(self, request, *args, **kwargs):
92 resource = get_object_or_404(Resource, id=kwargs['resource_id'])
93 utilization = resource.get_booking_utilization(int(kwargs['weeks']))
97 'data': utilization['booked_seconds'],
101 'label': 'Available',
102 'data': utilization['available_seconds'],
106 return JsonResponse({'data': utilization})