~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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# 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.conf.urls import include, patterns, url
from django.core.management import call_command
from django.core.urlresolvers import reverse_lazy
from django.db import models
from django.template.loader import render_to_string
from django.utils.http import urlquote
from common.plugin_helper import (
    Extension,
    KPI,
)

from bootspeed.models import ImageMachineAgg


DIGITS_BUILD_NUMBER = 8  # YYYYMMDD


class BootspeedSimpleKPI(KPI):

    @classmethod
    def name(self):
        return "Bootspeed"

    def importance(self):
        return self.IMPORTANCE_MEDIUM

    def _get_latest_build_number(self):
        latest_build_number = ImageMachineAgg.objects.filter(
            publish=True,
            image__arch='amd64',
            image__publish=True,
        ).aggregate(
            models.Max('image__build_number'),
        )['image__build_number__max']

        return str(latest_build_number)[:DIGITS_BUILD_NUMBER]

    def _get_results(self, metrics):
        results = {}
        for metric in metrics:
            machine = metric['machine__name']
            arch = metric['image__arch']
            build_number = metric['image__build_number']
            average = metric['boot_avg']

            results.setdefault(
                machine,
                {},
            ).setdefault(
                arch,
                {},
            )[build_number] = average

        return results

    def _get_entries(self, metrics, build_number):

        results = self._get_results(metrics)

        entries = {}
        for machine, arches in results.items():
            for arch, data in arches.items():
                latest_builds = sorted(data.keys(), reverse=True)[:2]

                latest_bn = latest_builds[0][:DIGITS_BUILD_NUMBER]

                if build_number != latest_bn:
                    continue

                latest_build = data[latest_builds[0]]
                next_latest_build = data[latest_builds[1]]

                entry_machine = entries.setdefault(
                    machine,
                    {},
                )

                entry_machine.setdefault(
                    'builds',
                    [],
                ).append(latest_build)

                entry_machine.setdefault(
                    'old_builds',
                    [],
                ).append(next_latest_build)

                entry_machine.setdefault(
                    'arch',
                    [],
                ).append(urlquote(arch))

        return entries

    def _aggregate_entries(self, entries):
        for entry, items in entries.items():
            builds = items['builds']

            average = 0

            if len(builds) != 0:
                average = sum(builds) / len(builds)

            entries[entry]['average'] = average

            old_average = 0

            if 'old_builds' in items:
                old_builds = items['old_builds']

                if len(old_builds) != 0:
                    old_average = sum(old_builds) / len(old_builds)

            entries[entry]['old_average'] = old_average

            entries[entry]['change'] = 0
            if old_average != 0:
                entries[entry]['change'] = (
                    average - old_average
                ) / old_average * 100

        return entries

    def render(self):

        build_number = self._get_latest_build_number()

        metrics = ImageMachineAgg.objects.filter(
            publish=True,
            image__arch='amd64',
            image__publish=True,
        ).distinct(
            'machine__name',
            'image__arch',
            'image__build_number',
        ).values(
            'machine__name',
            'image__arch',
            'image__build_number',
            'boot_avg',
        )

        entries = self._get_entries(metrics, build_number)

        entries = self._aggregate_entries(entries)

        retval = render_to_string(
            'bootspeed/kpi_simple_average.html',
            dict(
                entries=entries,
                build_number=build_number,
                help_info="""The average time in which the image boots on the
                    given machine.""",
            ),
        )

        return retval


class BootspeedExtension(Extension):

    URL_PATTERNS = patterns(
        '',
        url(r'^bootspeed/', include('bootspeed.urls')),
        url(r'^api/bootspeed/', include('bootspeed.urls_api')),
    )

    LINKS = [
        {
            'name': 'bootspeed',
            'url': reverse_lazy('bootspeed_arch_overview'),
            'label': 'Bootspeed',
            'index': 300,
        },
    ]

    def contribute_to_kpis(self, kpi_mgr):
        return [
            BootspeedSimpleKPI,
        ]

    def pull_data(self):
        call_command('jenkins_pull_bootspeed')

    @classmethod
    def name(cls):
        return cls.__name__

extension = BootspeedExtension()