summaryrefslogtreecommitdiffstats
path: root/dashboard/src/dashboard/views.py
blob: 4bab036f72ad0f7d255508177aae6d8a0ffa8821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
##############################################################################
# Copyright (c) 2016 Max Breitenfeldt and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


from datetime import timedelta

from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.views import View
from django.views.generic import TemplateView

from booking.models import Booking
from dashboard.models import Resource
from jenkins.models import JenkinsSlave


class JenkinsSlavesView(TemplateView):
    template_name = "dashboard/jenkins_slaves.html"

    def get_context_data(self, **kwargs):
        slaves = JenkinsSlave.objects.filter(active=True)
        context = super(JenkinsSlavesView, self).get_context_data(**kwargs)
        context.update({'title': "Jenkins Slaves", 'slaves': slaves})
        return context


class CIPodsView(TemplateView):
    template_name = "dashboard/ci_pods.html"

    def get_context_data(self, **kwargs):
        ci_pods = Resource.objects.filter(slave__ci_slave=True, slave__active=True)
        context = super(CIPodsView, self).get_context_data(**kwargs)
        context.update({'title': "CI Pods", 'ci_pods': ci_pods})
        return context


class DevelopmentPodsView(TemplateView):
    template_name = "dashboard/dev_pods.html"

    def get_context_data(self, **kwargs):
        resources = Resource.objects.filter(dev_pod=True)

        bookings = Booking.objects.filter(start__lte=timezone.now())
        bookings = bookings.filter(end__gt=timezone.now())

        dev_pods = []
        for resource in resources:
            booking_utilization = resource.get_booking_utilization(weeks=4)
            total = booking_utilization['booked_seconds'] + booking_utilization['available_seconds']
            try:
                utilization_percentage = "%d%%" % (float(booking_utilization['booked_seconds']) /
                                                   total * 100)
            except (ValueError, ZeroDivisionError):
                return ""

            dev_pod = (resource, None, utilization_percentage)
            for booking in bookings:
                if booking.resource == resource:
                    dev_pod = (resource, booking, utilization_percentage)
            dev_pods.append(dev_pod)

        context = super(DevelopmentPodsView, self).get_context_data(**kwargs)
        context.update({'title': "Development Pods", 'dev_pods': dev_pods})
        return context


class ResourceView(TemplateView):
    template_name = "dashboard/resource.html"

    def get_context_data(self, **kwargs):
        resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
        bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
        context = super(ResourceView, self).get_context_data(**kwargs)
        context.update({'title': str(resource), 'resource': resource, 'bookings': bookings})
        return context


class LabOwnerView(TemplateView):
    template_name = "dashboard/resource_all.html"

    def get_context_data(self, **kwargs):
        resources = Resource.objects.filter(slave__dev_pod=True, slave__active=True)
        pods = []
        for resource in resources:
            utilization = resource.slave.get_utilization(timedelta(days=7))
            bookings = Booking.objects.filter(resource=resource, end__gt=timezone.now())
            pods.append((resource, utilization, bookings))
        context = super(LabOwnerView, self).get_context_data(**kwargs)
        context.update({'title': "Overview", 'pods': pods})
        return context


class BookingUtilizationJSON(View):
    def get(self, request, *args, **kwargs):
        resource = get_object_or_404(Resource, id=kwargs['resource_id'])
        utilization = resource.get_booking_utilization(int(kwargs['weeks']))
        utilization = [
            {
                'label': 'Booked',
                'data': utilization['booked_seconds'],
                'color': '#d9534f'
            },
            {
                'label': 'Available',
                'data': utilization['available_seconds'],
                'color': '#5cb85c'
            },
        ]
        return JsonResponse({'data': utilization})


class JenkinsUtilizationJSON(View):
    def get(self, request, *args, **kwargs):
        resource = get_object_or_404(Resource, id=kwargs['resource_id'])
        weeks = int(kwargs['weeks'])
        try:
            utilization = resource.slave.get_utilization(timedelta(weeks=weeks))
            utilization = [
                {
                    'label': 'Offline',
                    'data': utilization['offline'],
                    'color': '#d9534f'
                },
                {
                    'label': 'Online',
                    'data': utilization['online'],
                    'color': '#5cb85c'
                },
                {
                    'label': 'Idle',
                    'data': utilization['idle'],
                    'color': '#5bc0de'
                },
            ]
            jutilization = JsonResponse({'data': utilization})
        except AttributeError:
            return JsonResponse({'data': ''})
        if jutilization:
            return jutilization