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.filter(active=True)
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, slave__active=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(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 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
80 context = super(ResourceView, self).get_context_data(**kwargs)
81 context.update({'title': str(resource), 'resource': resource, 'bookings': bookings})
85 class LabOwnerView(TemplateView):
86 template_name = "dashboard/resource_all.html"
88 def get_context_data(self, **kwargs):
89 resources = Resource.objects.filter(slave__dev_pod=True, slave__active=True)
91 for resource in resources:
92 utilization = resource.slave.get_utilization(timedelta(days=7))
93 bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
94 pods.append((resource, utilization, bookings))
95 context = super(LabOwnerView, self).get_context_data(**kwargs)
96 context.update({'title': "Overview", 'pods': pods})
100 class BookingUtilizationJSON(View):
101 def get(self, request, *args, **kwargs):
102 resource = get_object_or_404(Resource, id=kwargs['resource_id'])
103 utilization = resource.get_booking_utilization(int(kwargs['weeks']))
107 'data': utilization['booked_seconds'],
111 'label': 'Available',
112 'data': utilization['available_seconds'],
116 return JsonResponse({'data': utilization})
119 class JenkinsUtilizationJSON(View):
120 def get(self, request, *args, **kwargs):
121 resource = get_object_or_404(Resource, id=kwargs['resource_id'])
122 weeks = int(kwargs['weeks'])
124 utilization = resource.slave.get_utilization(timedelta(weeks=weeks))
128 'data': utilization['offline'],
133 'data': utilization['online'],
138 'data': utilization['idle'],
142 jutilization = JsonResponse({'data': utilization})
143 except AttributeError:
144 return JsonResponse({'data': ''})