8954f6c3880431f5ad560f3af09ba7bb053a9d4f
[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             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)
48
49         context = super(DevelopmentPodsView, self).get_context_data(**kwargs)
50         context.update({'title': "Development Pods", 'dev_pods': dev_pods})
51         return context
52
53
54 class ResourceView(TemplateView):
55     template_name = "dashboard/resource.html"
56
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})
64         return context
65
66
67 class LabOwnerView(TemplateView):
68     template_name = "dashboard/resource_all.html"
69
70     def get_context_data(self, **kwargs):
71         resources = Resource.objects.filter(slave__dev_pod=True)
72         pods = []
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})
79         return context
80
81
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']))
86         utilization = [
87             {
88                 'label': 'Booked',
89                 'data': utilization['booked_seconds'],
90                 'color': '#d9534f'
91             },
92             {
93                 'label': 'Available',
94                 'data': utilization['available_seconds'],
95                 'color': '#5cb85c'
96             },
97         ]
98         return JsonResponse({'data': utilization})