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 dev_pod = (resource, None)
44 for booking in bookings:
45 if booking.resource == resource:
46 dev_pod = (resource, booking)
47 dev_pods.append(dev_pod)
49 context = super(DevelopmentPodsView, self).get_context_data(**kwargs)
50 context.update({'title': "Development Pods", 'dev_pods': dev_pods})
54 class ResourceView(TemplateView):
55 template_name = "dashboard/resource.html"
57 def get_context_data(self, **kwargs):
58 resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
59 utilization = resource.slave.get_utilization(timedelta(days=7))
60 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
61 context = super(ResourceView, self).get_context_data(**kwargs)
62 context.update({'title': str(resource), 'resource': resource, 'utilization': utilization,
63 'bookings': bookings})
67 class LabOwnerView(TemplateView):
68 template_name = "dashboard/resource_all.html"
70 def get_context_data(self, **kwargs):
71 resources = Resource.objects.filter(slave__dev_pod=True)
73 for resource in resources:
74 utilization = resource.slave.get_utilization(timedelta(days=7))
75 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
76 pods.append((resource, utilization, bookings))
77 context = super(LabOwnerView, self).get_context_data(**kwargs)
78 context.update({'title': "Overview", 'pods': pods})
82 class BookingUtilizationJSON(View):
83 def get(self, request, *args, **kwargs):
84 resource = get_object_or_404(Resource, id=kwargs['resource_id'])
85 utilization = resource.get_booking_utilization(int(kwargs['weeks']))
89 'data': utilization['booked_seconds'],
94 'data': utilization['available_seconds'],
98 return JsonResponse({'data': utilization})