1 from datetime import timedelta
3 from django.http import JsonResponse
4 from django.shortcuts import get_object_or_404
5 from django.utils import timezone
6 from django.views import View
7 from django.views.generic import TemplateView
9 from booking.models import Booking
10 from dashboard.models import Resource
11 from jenkins.models import JenkinsSlave
14 class JenkinsSlavesView(TemplateView):
15 template_name = "dashboard/jenkins_slaves.html"
17 def get_context_data(self, **kwargs):
18 slaves = JenkinsSlave.objects.all()
19 context = super(JenkinsSlavesView, self).get_context_data(**kwargs)
20 context.update({'title': "Jenkins Slaves", 'slaves': slaves})
24 class CIPodsView(TemplateView):
25 template_name = "dashboard/ci_pods.html"
27 def get_context_data(self, **kwargs):
28 ci_pods = Resource.objects.filter(slave__ci_slave=True)
29 context = super(CIPodsView, self).get_context_data(**kwargs)
30 context.update({'title': "CI Pods", 'ci_pods': ci_pods})
34 class DevelopmentPodsView(TemplateView):
35 template_name = "dashboard/dev_pods.html"
37 def get_context_data(self, **kwargs):
38 resources = Resource.objects.filter(slave__dev_pod=True)
40 bookings = Booking.objects.filter(start__lte=timezone.now())
41 bookings = bookings.filter(end__gt=timezone.now())
44 for resource in resources:
45 booking_utilization = resource.get_booking_utilization(weeks=4)
46 total = booking_utilization['booked_seconds'] + booking_utilization['available_seconds']
48 utilization_percentage = "%d%%" % (float(booking_utilization['booked_seconds']) /
50 except (ValueError, ZeroDivisionError):
53 dev_pod = (resource, None, utilization_percentage)
54 for booking in bookings:
55 if booking.resource == resource:
56 dev_pod = (resource, booking, utilization_percentage)
57 dev_pods.append(dev_pod)
59 context = super(DevelopmentPodsView, self).get_context_data(**kwargs)
60 context.update({'title': "Development Pods", 'dev_pods': dev_pods})
64 class ResourceView(TemplateView):
65 template_name = "dashboard/resource.html"
67 def get_context_data(self, **kwargs):
68 resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
69 utilization = resource.slave.get_utilization(timedelta(days=7))
70 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
71 context = super(ResourceView, self).get_context_data(**kwargs)
72 context.update({'title': str(resource), 'resource': resource, 'utilization': utilization,
73 'bookings': bookings})
77 class LabOwnerView(TemplateView):
78 template_name = "dashboard/resource_all.html"
80 def get_context_data(self, **kwargs):
81 resources = Resource.objects.filter(slave__dev_pod=True)
83 for resource in resources:
84 utilization = resource.slave.get_utilization(timedelta(days=7))
85 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
86 pods.append((resource, utilization, bookings))
87 context = super(LabOwnerView, self).get_context_data(**kwargs)
88 context.update({'title': "Overview", 'pods': pods})
92 class BookingUtilizationJSON(View):
93 def get(self, request, *args, **kwargs):
94 resource = get_object_or_404(Resource, id=kwargs['resource_id'])
95 utilization = resource.get_booking_utilization(int(kwargs['weeks']))
99 'data': utilization['booked_seconds'],
103 'label': 'Available',
104 'data': utilization['available_seconds'],
108 return JsonResponse({'data': utilization})
111 class JenkinsUtilizationJSON(View):
112 def get(self, request, *args, **kwargs):
113 resource = get_object_or_404(Resource, id=kwargs['resource_id'])
114 weeks = int(kwargs['weeks'])
115 utilization = resource.slave.get_utilization(timedelta(weeks=weeks))
119 'data': utilization['offline'],
124 'data': utilization['online'],
129 'data': utilization['idle'],
133 return JsonResponse({'data': utilization})