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

« back to all changes in this revision

Viewing changes to lib/lpresults/tracker/views/teststats.py

  • Committer: Sylvain Pineau
  • Date: 2011-11-16 11:34:28 UTC
  • Revision ID: sylvain.pineau@canonical.com-20111116113428-6odycuhwfuflq350
Add a new view to display trend reports based on system benchmarks.
JS rendering powered by jqPlot (http://www.jqplot.com)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Test stats (benchmarks trend reports by default) related view."""
 
5
 
 
6
__metaclass__ = type
 
7
 
 
8
__all__ = [
 
9
    "teststats",
 
10
    ]
 
11
 
 
12
import re
 
13
 
 
14
from urlparse import urlparse
 
15
 
 
16
from django.http import HttpResponseNotFound
 
17
from django.shortcuts import render_to_response
 
18
from django.template import RequestContext
 
19
from django.utils.datastructures import SortedDict
 
20
 
 
21
from zope.component import getUtility
 
22
 
 
23
from lpresults.tracker.interfaces.testpillar import ITestPillarSet
 
24
from lpresults.registry.interfaces.launchpad import ILaunchpad
 
25
from lpresults.tracker.interfaces.testresult import (
 
26
    ITestResultSet,
 
27
    TestResultSearchParams,
 
28
    )
 
29
 
 
30
 
 
31
def teststats(request, pillar_name, series_name):
 
32
    dictionary = {}
 
33
    bench_pattern = re.compile(
 
34
        r'^(?P<res>\d+(\.\d+)?)\s?(\((?P<scale>.*)\))?$')
 
35
    series = None
 
36
    stats_report = dict()
 
37
    search_pattern = u'benchmark'
 
38
 
 
39
    if 'search_text' in request.GET:
 
40
        s = request.GET['search_text']
 
41
        if not s:
 
42
            search_pattern = u'benchmark'
 
43
        else:
 
44
            search_pattern = s
 
45
 
 
46
    pillar = getUtility(ITestPillarSet).getByName(pillar_name)
 
47
 
 
48
    if pillar is None:
 
49
        return HttpResponseNotFound(
 
50
            "Invalid pillar name: %s" % pillar_name)
 
51
 
 
52
    dictionary["pillar"] = pillar
 
53
 
 
54
    if series_name:
 
55
        series = pillar.getSeries(series_name)
 
56
        if series is None:
 
57
            return HttpResponseNotFound(
 
58
                "Invalid series name: %s" % series_name)
 
59
 
 
60
        dictionary["series"] = series
 
61
 
 
62
    launchpad = getUtility(ILaunchpad)
 
63
    launchpad_pillar = launchpad.getPillar(pillar_name)
 
64
    resource_type_link = launchpad_pillar["resource_type_link"]
 
65
    resource_type = urlparse(resource_type_link)[5]
 
66
 
 
67
    search_params = TestResultSearchParams(search_text=search_pattern)
 
68
 
 
69
    if resource_type == 'project':
 
70
        if not series:
 
71
            series = pillar.development_focus
 
72
        search_params.distro_series = None
 
73
        search_params.project_series = series
 
74
    else:
 
75
        if not series:
 
76
            series = pillar
 
77
        search_params.distro_series = series
 
78
        search_params.project_series = None
 
79
 
 
80
    results = getUtility(ITestResultSet).search(search_params).result_set
 
81
 
 
82
    for values in results:
 
83
        match = bench_pattern.search(values[0].output)
 
84
        if match:
 
85
            name = re.sub(r'.*/(.*/.*)$', r'\1', values[1].name)
 
86
            name = re.sub(r'\(|\)', r'', name)
 
87
            name = re.sub(r'^\d+-', r'', name)
 
88
            date = values[0].date_created.strftime('%Y-%m-%d %H:%M:%S')
 
89
            stats_report.setdefault(name, [])
 
90
            stats_report[name].append(
 
91
                {'ts': date,
 
92
                 'data': match.group('res'),
 
93
                 'scale': match.group('scale')})
 
94
 
 
95
    dictionary["stats_report"] = SortedDict(
 
96
        [(k, stats_report[k]) for k in sorted(stats_report.iterkeys())])
 
97
 
 
98
    return render_to_response(
 
99
        "teststats.html",
 
100
        dictionary,
 
101
        RequestContext(request))