1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
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 ##############################################################################
11 from datetime import timedelta
13 from django.http import JsonResponse
14 from django.shortcuts import get_object_or_404
15 from django.utils import timezone
16 from django.views import View
17 from django.views.generic import TemplateView
19 from booking.models import Booking
20 from dashboard.models import Resource
21 from jenkins.models import JenkinsSlave
24 class JenkinsSlavesView(TemplateView):
25 template_name = "dashboard/jenkins_slaves.html"
27 def get_context_data(self, **kwargs):
28 slaves = JenkinsSlave.objects.all()
29 context = super(JenkinsSlavesView, self).get_context_data(**kwargs)
30 context.update({'title': "Jenkins Slaves", 'slaves': slaves})
34 class CIPodsView(TemplateView):
35 template_name = "dashboard/ci_pods.html"
37 def get_context_data(self, **kwargs):
38 ci_pods = Resource.objects.filter(slave__ci_slave=True)
39 context = super(CIPodsView, self).get_context_data(**kwargs)
40 context.update({'title': "CI Pods", 'ci_pods': ci_pods})
44 class DevelopmentPodsView(TemplateView):
45 template_name = "dashboard/dev_pods.html"
47 def get_context_data(self, **kwargs):
48 resources = Resource.objects.filter(slave__dev_pod=True)
50 bookings = Booking.objects.filter(start__lte=timezone.now())
51 bookings = bookings.filter(end__gt=timezone.now())
54 for resource in resources:
55 booking_utilization = resource.get_booking_utilization(weeks=4)
56 total = booking_utilization['booked_seconds'] + booking_utilization['available_seconds']
58 utilization_percentage = "%d%%" % (float(booking_utilization['booked_seconds']) /
60 except (ValueError, ZeroDivisionError):
63 dev_pod = (resource, None, utilization_percentage)
64 for booking in bookings:
65 if booking.resource == resource:
66 dev_pod = (resource, booking, utilization_percentage)
67 dev_pods.append(dev_pod)
69 context = super(DevelopmentPodsView, self).get_context_data(**kwargs)
70 context.update({'title': "Development Pods", 'dev_pods': dev_pods})
74 class ResourceView(TemplateView):
75 template_name = "dashboard/resource.html"
77 def get_context_data(self, **kwargs):
78 resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
79 utilization = resource.slave.get_utilization(timedelta(days=7))
80 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
81 context = super(ResourceView, self).get_context_data(**kwargs)
82 context.update({'title': str(resource), 'resource': resource, 'utilization': utilization,
83 'bookings': bookings})
87 class LabOwnerView(TemplateView):
88 template_name = "dashboard/resource_all.html"
90 def get_context_data(self, **kwargs):
91 resources = Resource.objects.filter(slave__dev_pod=True)
93 for resource in resources:
94 utilization = resource.slave.get_utilization(timedelta(days=7))
95 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
96 pods.append((resource, utilization, bookings))
97 context = super(LabOwnerView, self).get_context_data(**kwargs)
98 context.update({'title': "Overview", 'pods': pods})
102 class BookingUtilizationJSON(View):
103 def get(self, request, *args, **kwargs):
104 resource = get_object_or_404(Resource, id=kwargs['resource_id'])
105 utilization = resource.get_booking_utilization(int(kwargs['weeks']))
109 'data': utilization['booked_seconds'],
113 'label': 'Available',
114 'data': utilization['available_seconds'],
118 return JsonResponse({'data': utilization})
121 class JenkinsUtilizationJSON(View):
122 def get(self, request, *args, **kwargs):
123 resource = get_object_or_404(Resource, id=kwargs['resource_id'])
124 weeks = int(kwargs['weeks'])
125 utilization = resource.slave.get_utilization(timedelta(weeks=weeks))
129 'data': utilization['offline'],
134 'data': utilization['online'],
139 'data': utilization['idle'],
143 return JsonResponse({'data': utilization})