Merge "Adding Labels"
[pharos.git] / tools / pharos-dashboard / src / dashboard / views.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
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 ##############################################################################
9
10
11 from datetime import timedelta
12
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
18
19 from booking.models import Booking
20 from dashboard.models import Resource
21 from jenkins.models import JenkinsSlave
22
23
24 class JenkinsSlavesView(TemplateView):
25     template_name = "dashboard/jenkins_slaves.html"
26
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})
31         return context
32
33
34 class CIPodsView(TemplateView):
35     template_name = "dashboard/ci_pods.html"
36
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})
41         return context
42
43
44 class DevelopmentPodsView(TemplateView):
45     template_name = "dashboard/dev_pods.html"
46
47     def get_context_data(self, **kwargs):
48         resources = Resource.objects.filter(dev_pod=True)
49
50         bookings = Booking.objects.filter(start__lte=timezone.now())
51         bookings = bookings.filter(end__gt=timezone.now())
52
53         dev_pods = []
54         for resource in resources:
55             booking_utilization = resource.get_booking_utilization(weeks=4)
56             total = booking_utilization['booked_seconds'] + booking_utilization['available_seconds']
57             try:
58                 utilization_percentage = "%d%%" % (float(booking_utilization['booked_seconds']) /
59                                                    total * 100)
60             except (ValueError, ZeroDivisionError):
61                 return ""
62
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)
68
69         context = super(DevelopmentPodsView, self).get_context_data(**kwargs)
70         context.update({'title': "Development Pods", 'dev_pods': dev_pods})
71         return context
72
73
74 class ResourceView(TemplateView):
75     template_name = "dashboard/resource.html"
76
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})
82         return context
83
84
85 class LabOwnerView(TemplateView):
86     template_name = "dashboard/resource_all.html"
87
88     def get_context_data(self, **kwargs):
89         resources = Resource.objects.filter(slave__dev_pod=True, slave__active=True)
90         pods = []
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})
97         return context
98
99
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']))
104         utilization = [
105             {
106                 'label': 'Booked',
107                 'data': utilization['booked_seconds'],
108                 'color': '#d9534f'
109             },
110             {
111                 'label': 'Available',
112                 'data': utilization['available_seconds'],
113                 'color': '#5cb85c'
114             },
115         ]
116         return JsonResponse({'data': utilization})
117
118
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'])
123         utilization = resource.slave.get_utilization(timedelta(weeks=weeks))
124         utilization = [
125             {
126                 'label': 'Offline',
127                 'data': utilization['offline'],
128                 'color': '#d9534f'
129             },
130             {
131                 'label': 'Online',
132                 'data': utilization['online'],
133                 'color': '#5cb85c'
134             },
135             {
136                 'label': 'Idle',
137                 'data': utilization['idle'],
138                 'color': '#5bc0de'
139             },
140         ]
141         return JsonResponse({'data': utilization})