~sylvain-pineau/launchpad-results/trend-reports-jqplot

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
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Test stats (benchmarks trend reports by default) related view."""

__metaclass__ = type

__all__ = [
    "teststats",
    ]

import re

from urlparse import urlparse

from django.http import HttpResponseNotFound
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.datastructures import SortedDict

from zope.component import getUtility

from lpresults.tracker.interfaces.testpillar import ITestPillarSet
from lpresults.registry.interfaces.launchpad import ILaunchpad
from lpresults.tracker.interfaces.testresult import (
    ITestResultSet,
    TestResultSearchParams,
    )


def teststats(request, pillar_name, series_name):
    dictionary = {}
    bench_pattern = re.compile(
        r'^(?P<res>\d+(\.\d+)?)\s?(\((?P<scale>.*)\))?$')
    series = None
    stats_report = dict()
    search_pattern = u'benchmark'

    if 'search_text' in request.GET:
        s = request.GET['search_text']
        if not s:
            search_pattern = u'benchmark'
        else:
            search_pattern = s

    pillar = getUtility(ITestPillarSet).getByName(pillar_name)

    if pillar is None:
        return HttpResponseNotFound(
            "Invalid pillar name: %s" % pillar_name)

    dictionary["pillar"] = pillar

    if series_name:
        series = pillar.getSeries(series_name)
        if series is None:
            return HttpResponseNotFound(
                "Invalid series name: %s" % series_name)

        dictionary["series"] = series

    launchpad = getUtility(ILaunchpad)
    launchpad_pillar = launchpad.getPillar(pillar_name)
    resource_type_link = launchpad_pillar["resource_type_link"]
    resource_type = urlparse(resource_type_link)[5]

    search_params = TestResultSearchParams(search_text=search_pattern)

    if resource_type == 'project':
        if not series:
            series = pillar.development_focus
        search_params.distro_series = None
        search_params.project_series = series
    else:
        if not series:
            series = pillar
        search_params.distro_series = series
        search_params.project_series = None

    results = getUtility(ITestResultSet).search(search_params).result_set

    for values in results:
        match = bench_pattern.search(values[0].output)
        if match:
            name = re.sub(r'.*/(.*/.*)$', r'\1', values[1].name)
            name = re.sub(r'\(|\)', r'', name)
            name = re.sub(r'^\d+-', r'', name)
            date = values[0].date_created.strftime('%Y-%m-%d %H:%M:%S')
            stats_report.setdefault(name, [])
            stats_report[name].append(
                {'ts': date,
                 'data': match.group('res'),
                 'scale': match.group('scale')})

    dictionary["stats_report"] = SortedDict(
        [(k, stats_report[k]) for k in sorted(stats_report.iterkeys())])

    return render_to_response(
        "teststats.html",
        dictionary,
        RequestContext(request))