Add utilization column to dev pod tab
[pharos.git] / tools / pharos-dashboard / dashboard / views.py
1 from datetime import timedelta
2
3 from django.shortcuts import get_object_or_404
4 from django.utils import timezone
5 from django.views.generic import TemplateView
6
7 from booking.models import Booking
8 from dashboard.models import Resource
9 from jenkins.models import JenkinsSlave
10
11
12 class JenkinsSlavesView(TemplateView):
13     template_name = "dashboard/jenkins_slaves.html"
14
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})
19         return context
20
21
22 class CIPodsView(TemplateView):
23     template_name = "dashboard/ci_pods.html"
24
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})
29         return context
30
31
32 class DevelopmentPodsView(TemplateView):
33     template_name = "dashboard/dev_pods.html"
34
35     def get_context_data(self, **kwargs):
36         resources = Resource.objects.filter(slave__dev_pod=True)
37
38         bookings = Booking.objects.filter(start__lte=timezone.now())
39         bookings = bookings.filter(end__gt=timezone.now())
40
41         dev_pods = []
42         for resource in resources:
43             booking_utilization = resource.get_booking_utilization(weeks=4)
44             total = booking_utilization['booked_seconds'] + booking_utilization['available_seconds']
45             try:
46                utilization_percentage =  "%d%%" % (float(booking_utilization['booked_seconds']) /
47                                                    total * 100)
48             except (ValueError, ZeroDivisionError):
49                 return ""
50
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)
56
57         context = super(DevelopmentPodsView, self).get_context_data(**kwargs)
58         context.update({'title': "Development Pods", 'dev_pods': dev_pods})
59         return context
60
61
62 class ResourceView(TemplateView):
63     template_name = "dashboard/resource.html"
64
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})
72         return context
73
74
75 class LabOwnerView(TemplateView):
76     template_name = "dashboard/resource_all.html"
77
78     def get_context_data(self, **kwargs):
79         resources = Resource.objects.filter(slave__dev_pod=True)
80         pods = []
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})
87         return context
88
89
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']))
94         utilization = [
95             {
96                 'label': 'Booked',
97                 'data': utilization['booked_seconds'],
98                 'color': '#d9534f'
99             },
100             {
101                 'label': 'Available',
102                 'data': utilization['available_seconds'],
103                 'color': '#5cb85c'
104             },
105         ]
106         return JsonResponse({'data': utilization})