~lifeless/python-oops-tools/prune

« back to all changes in this revision

Viewing changes to src/oopstools/oops/views.py

  • Committer: Robert Collins
  • Date: 2011-10-13 20:18:51 UTC
  • Revision ID: robertc@robertcollins.net-20111013201851-ym8jmdhoeol3p83s
Export of cruft-deleted tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2005-2011 Canonical Ltd.  All rights reserved.
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Affero General Public License as published by
 
5
# the Free Software Foundation, either version 3 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Affero General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Affero General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
import os.path
 
17
 
 
18
from datetime import datetime, timedelta
 
19
 
 
20
from django.conf import settings
 
21
from django.http import Http404, HttpResponseRedirect
 
22
from django.shortcuts import render_to_response
 
23
 
 
24
from oopstools.oops import dbsummaries
 
25
from oopstools.oops.helpers import parsedate
 
26
from oopstools.oops.models import Oops, Report
 
27
from oopstools.oops.prefixloader import PrefixLoader
 
28
from oopstools.oops.oopsstore import OopsStore, OopsNotFound
 
29
 
 
30
 
 
31
def index(request):
 
32
    # Check both the given ID and a fixed-up version of it.
 
33
    oopsids = [request.GET.get('oopsid', '')]
 
34
    oopsid_fixed = oopsids[0].upper()
 
35
    if not oopsid_fixed.startswith('OOPS-'):
 
36
        oopsid_fixed = 'OOPS-' + oopsid_fixed
 
37
    if oopsid_fixed not in oopsids:
 
38
        oopsids.append(oopsid_fixed)
 
39
 
 
40
    # Check each ID in both the database and filesystem
 
41
    # (should maybe have an API option for this, instead of doing it manually?)
 
42
    oops = None
 
43
    for oopsid in oopsids:
 
44
        try:
 
45
            oops = Oops.objects.get(oopsid__exact=oopsid)
 
46
            break
 
47
        except Oops.DoesNotExist:
 
48
            # Try to find the OOPS report in the filesystem.
 
49
            store = OopsStore(settings.OOPSDIR)
 
50
            try:
 
51
                oops = store.find_oops(oopsid)
 
52
                break
 
53
            except OopsNotFound:
 
54
                pass
 
55
 
 
56
    if oops and oops.exists():
 
57
        tmpl = "oops.html"
 
58
        data = {
 
59
            'oops': oops
 
60
        }
 
61
    else:
 
62
        tmpl = settings.INDEX_TEMPLATE
 
63
        reports = Report.objects.filter(active=True)
 
64
        data = {
 
65
            'userdata': request.GET,
 
66
            'reports': reports
 
67
        }
 
68
    return render_to_response(tmpl, dictionary=data)
 
69
 
 
70
 
 
71
def meta(request):
 
72
    bug_number = request.POST.get('bug_number', '')
 
73
    oopsid = request.POST.get('oopsid')
 
74
    oops = Oops.objects.get(oopsid__exact=oopsid)
 
75
    oops.oopsinfestation.bug = bug_number
 
76
    oops.oopsinfestation.save()
 
77
    return HttpResponseRedirect('/oops.py/?oopsid=%s' % oopsid)
 
78
 
 
79
 
 
80
def prefixloader(request):
 
81
    prefixes = None
 
82
    if request.method == "POST":
 
83
        prefixloader = PrefixLoader(settings.LAZR_CONFIG)
 
84
        prefixes = prefixloader.load_prefixes_into_database()
 
85
 
 
86
    return render_to_response("prefixloader.html", dictionary={
 
87
        "LAZR_CONFIG": settings.LAZR_CONFIG,
 
88
        "prefixes": prefixes})
 
89
 
 
90
 
 
91
def report(request, report_name):
 
92
    try:
 
93
        r = Report.objects.get(name=report_name, active=True)
 
94
    except Report.DoesNotExist:
 
95
        raise Http404
 
96
    else:
 
97
        # Since reports are created by an external script, let's build URLs
 
98
        # for the past ten days.
 
99
        now = datetime.utcnow()
 
100
        dates = []
 
101
        for day in range(1, 11):
 
102
            dates.append(now - timedelta(day))
 
103
        data = {
 
104
            'report': r,
 
105
            'dates': dates,
 
106
            'SUMMARY_URI': settings.SUMMARY_URI,
 
107
            }
 
108
        return render_to_response("report.html", dictionary=data)
 
109
 
 
110
 
 
111
def report_day_view(request, report_name, year, month, day):
 
112
    filename = '-'.join([report_name, year, month, day]) + '.html'
 
113
    # Try to find out if the report exists already.
 
114
    if os.path.exists(os.path.join(settings.SUMMARY_DIR, filename)):
 
115
        url = settings.SUMMARY_URI + '/' + filename
 
116
        return HttpResponseRedirect(url)
 
117
    else:
 
118
        # Build it dynamically. This might take some time...
 
119
        try:
 
120
            report = Report.objects.get(name=report_name, active=True)
 
121
        except Report.DoesNotExist:
 
122
            raise Http404
 
123
        else:
 
124
            summary_class = getattr(dbsummaries, report.get_summary_display())
 
125
            prefixes = [prefix.value for prefix in report.prefixes.all()]
 
126
            start = end = parsedate("-".join([year, month, day]))
 
127
            # XXX Unpack the data needed from the summary object to avoid
 
128
            # doing processing in the template.
 
129
            # There's probably a better way of doing this.
 
130
            summary = summary_class(start, end, prefixes)
 
131
            durations_section = summary.get_section_by_id('durations')
 
132
            stmt_counts_section = summary.get_section_by_id(
 
133
                'statement-counts')
 
134
            timeout_counts = summary.get_section_by_id(
 
135
                'time-out-counts-by-page-id')
 
136
            data = {
 
137
                'ROOT_URL': settings.ROOT_URL,
 
138
                'summary': summary,
 
139
                'durations': durations_section,
 
140
                'stmt_counts': stmt_counts_section,
 
141
                'timeout_counts': timeout_counts
 
142
                }
 
143
            return render_to_response("summary.html", dictionary=data)
 
144
 
 
145
 
 
146