~qajenkinsbot/qa-dashboard/production

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
147
148
149
150
151
152
153
154
155
# QA Dashboard
# Copyright 2013 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.views.decorators.http import require_GET
from django.template import RequestContext

from django_tables2 import RequestConfig

from django.shortcuts import (
    render_to_response,
    get_object_or_404,
    redirect,
)

from common.bread_crumbs import (
    BreadCrumb,
    BreadCrumbTrail,
)

from common.views import index
from power.models import (
    PowerMachine,
    PowerMetric,
)

from power.tables import MetricTable


def _get_power_arches():
    arches = []

    power_arches = PowerMetric.objects.values('image__arch')
    power_arches = power_arches.distinct().exclude(publish=False)
    power_arches = power_arches.order_by('-image__arch')

    for arch in power_arches:
        arches.append(arch['image__arch'])

    return arches


def _get_metrics(arch=None):
    pmetrics = PowerMetric.objects.all()

    if arch is not None:
        pmetrics = pmetrics.filter(image__arch=arch)

    metrics = []
    for metric in pmetrics.values('name').distinct().exclude(publish=False):
        metrics.append(metric['name'])

    return metrics


@BreadCrumb("Power testing", parent=index)
@require_GET
def overview(request):
    data = {
        'bread_crumb_trail': BreadCrumbTrail.leading_to(overview),
    }

    return _arch_overview(request, data=data, add_url='hardware/')


@BreadCrumb("Hardware measurement", parent=overview)
@require_GET
def arch_overview(request, arch=None):
    data = {
        'bread_crumb_trail': BreadCrumbTrail.leading_to(arch_overview),
    }
    return _arch_overview(request, arch, data)


@require_GET
def _arch_overview(request, arch=None, data={}, add_url=''):
    arches = _get_power_arches()

    chart_size = request.GET.get('size', '')

    if arch is None:
        return redirect(
            '{}arch/{}/'.format(
                add_url, arches[0] if len(arches) > 0 else "amd64"
            )
        )

    result = PowerMetric.objects.filter(image__arch=arch)
    result = result.distinct('machine__name')
    arches = _get_power_arches()
    metrics = _get_metrics(arch=arch)
    data.update({
        'result': result,
        'arch': arch,
        'arches': arches,
        'active': arch,
        'metrics': metrics,
        'chart_size': chart_size,
    })

    return render_to_response('power/arch_overview.html', data,
                              RequestContext(request))


@BreadCrumb("Power usage", parent=arch_overview, needs=['machine_id', 'arch'])
@require_GET
def machine_detail(request, machine_id, arch):
    machine = get_object_or_404(PowerMachine, id=machine_id)
    qs = PowerMetric.objects.filter(machine__id=machine_id, image__arch=arch)

    data = []
    for q in qs:
        data.append(
            dict(
                machine=q.machine,
                image=q.image,
                image__build_number=q.image.build_number,
                image__arch=q.image.arch,
                name=q.name,
                kernel=q.kernel,
                maximum=q.maximum,
                minimum=q.minimum,
                average=q.average,
                stddev=q.stddev,
                files=q.log_links,
                jenkins_url=q.jenkins_url,
                duration=q.duration,
            )
        )

    table = MetricTable(data)
    RequestConfig(request, paginate={"per_page": 100}).configure(table)
    data = {
        'machine': machine,
        'table': table,
        'bread_crumb_trail': BreadCrumbTrail.leading_to(
            machine_detail,
            machine_id=machine_id,
            arch=arch,
        ),
    }

    return render_to_response('power/machine_detail.html', data,
                              RequestContext(request))