022a4af065defe7873e472b09cb872af74be789f
[pharos.git] / tools / pharos-dashboard / 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.all()
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)
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(slave__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         utilization = resource.slave.get_utilization(timedelta(days=7))
80         bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
81         context = super(ResourceView, self).get_context_data(**kwargs)
82         context.update({'title': str(resource), 'resource': resource, 'utilization': utilization,
83                         'bookings': bookings})
84         return context
85
86
87 class LabOwnerView(TemplateView):
88     template_name = "dashboard/resource_all.html"
89
90     def get_context_data(self, **kwargs):
91         resources = Resource.objects.filter(slave__dev_pod=True)
92         pods = []
93         for resource in resources:
94             utilization = resource.slave.get_utilization(timedelta(days=7))
95             bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
96             pods.append((resource, utilization, bookings))
97         context = super(LabOwnerView, self).get_context_data(**kwargs)
98         context.update({'title': "Overview", 'pods': pods})
99         return context
100
101
102 class BookingUtilizationJSON(View):
103     def get(self, request, *args, **kwargs):
104         resource = get_object_or_404(Resource, id=kwargs['resource_id'])
105         utilization = resource.get_booking_utilization(int(kwargs['weeks']))
106         utilization = [
107             {
108                 'label': 'Booked',
109                 'data': utilization['booked_seconds'],
110                 'color': '#d9534f'
111             },
112             {
113                 'label': 'Available',
114                 'data': utilization['available_seconds'],
115                 'color': '#5cb85c'
116             },
117         ]
118         return JsonResponse({'data': utilization})
119
120
121 class JenkinsUtilizationJSON(View):
122     def get(self, request, *args, **kwargs):
123         resource = get_object_or_404(Resource, id=kwargs['resource_id'])
124         weeks = int(kwargs['weeks'])
125         utilization = resource.slave.get_utilization(timedelta(weeks=weeks))
126         utilization = [
127             {
128                 'label': 'Offline',
129                 'data': utilization['offline'],
130                 'color': '#d9534f'
131             },
132             {
133                 'label': 'Online',
134                 'data': utilization['online'],
135                 'color': '#5cb85c'
136             },
137             {
138                 'label': 'Idle',
139                 'data': utilization['idle'],
140                 'color': '#5bc0de'
141             },
142         ]
143         return JsonResponse({'data': utilization})