0eddc13040ee1997784ff5334880c25c3c160c8a
[pharos.git] / tools / pharos-dashboard / dashboard / views.py
1 from datetime import timedelta
2
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
8
9 from booking.models import Booking
10 from dashboard.models import Resource
11 from jenkins.models import JenkinsSlave
12
13
14 class JenkinsSlavesView(TemplateView):
15     template_name = "dashboard/jenkins_slaves.html"
16
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})
21         return context
22
23
24 class CIPodsView(TemplateView):
25     template_name = "dashboard/ci_pods.html"
26
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})
31         return context
32
33
34 class DevelopmentPodsView(TemplateView):
35     template_name = "dashboard/dev_pods.html"
36
37     def get_context_data(self, **kwargs):
38         resources = Resource.objects.filter(slave__dev_pod=True)
39
40         bookings = Booking.objects.filter(start__lte=timezone.now())
41         bookings = bookings.filter(end__gt=timezone.now())
42
43         dev_pods = []
44         for resource in resources:
45             booking_utilization = resource.get_booking_utilization(weeks=4)
46             total = booking_utilization['booked_seconds'] + booking_utilization['available_seconds']
47             try:
48                utilization_percentage =  "%d%%" % (float(booking_utilization['booked_seconds']) /
49                                                    total * 100)
50             except (ValueError, ZeroDivisionError):
51                 return ""
52
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)
58
59         context = super(DevelopmentPodsView, self).get_context_data(**kwargs)
60         context.update({'title': "Development Pods", 'dev_pods': dev_pods})
61         return context
62
63
64 class ResourceView(TemplateView):
65     template_name = "dashboard/resource.html"
66
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})
74         return context
75
76
77 class LabOwnerView(TemplateView):
78     template_name = "dashboard/resource_all.html"
79
80     def get_context_data(self, **kwargs):
81         resources = Resource.objects.filter(slave__dev_pod=True)
82         pods = []
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})
89         return context
90
91
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']))
96         utilization = [
97             {
98                 'label': 'Booked',
99                 'data': utilization['booked_seconds'],
100                 'color': '#d9534f'
101             },
102             {
103                 'label': 'Available',
104                 'data': utilization['available_seconds'],
105                 'color': '#5cb85c'
106             },
107         ]
108         return JsonResponse({'data': utilization})
109
110
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))
116         utilization = [
117             {
118                 'label': 'Offline',
119                 'data': utilization['offline'],
120                 'color': '#d9534f'
121             },
122             {
123                 'label': 'Online',
124                 'data': utilization['online'],
125                 'color': '#5cb85c'
126             },
127             {
128                 'label': 'Idle',
129                 'data': utilization['idle'],
130                 'color': '#5bc0de'
131             },
132         ]
133         return JsonResponse({'data': utilization})