~launchpad-pqm/python-oops-tools/trunk

« back to all changes in this revision

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

  • Committer: Robert Collins
  • Date: 2011-10-16 22:35:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2.
  • Revision ID: robertc@robertcollins.net-20111016223501-jl5vg9mduh7n29df
Add an amqpd listener, tweak some docs to match reality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import datetime
22
22
import os.path
23
23
 
 
24
from pytz import utc
 
25
 
24
26
from django.db import models
25
27
from django.db.models.signals import pre_save
26
28
import oops_datedir_repo.serializer
278
280
    prefix = oops.get('reporter')
279
281
    if not prefix:
280
282
        # Legacy support for pre-reporter using OOPSes.
281
 
        prefix = oops_re.match(oopsid).group('oopsprefix')
 
283
        prefix_match = oops_re.match(oopsid)
 
284
        if prefix_match is not None:
 
285
            prefix = prefix_match.group('oopsprefix')
 
286
        else:
 
287
            prefix = 'UNKNOWN'
282
288
    prefix = prefix.upper()
283
289
    try:
284
290
        prefix = Prefix.objects.get(value__exact=prefix)
329
335
    if total_time < 0:
330
336
        total_time = 0
331
337
    # Get the oops infestation
332
 
    exception_type = oops.get('type')
 
338
    exception_type = oops.get('type') or ''
 
339
    exception_value = oops.get('value') or ''
333
340
    exception_value = _normalize_exception_value(
334
 
        exception_type, oops.get('value'), prefix)
 
341
        exception_type, exception_value, prefix)
335
342
    try:
336
343
        infestation = Infestation.objects.get(
337
344
            exception_type__exact=exception_type,
361
368
        most_expensive_statement = conform(most_expensive_statement, 200)
362
369
    url = conform(oops.get('url') or '', MAX_URL_LEN)
363
370
    informational = oops.get('informational', 'False').lower() == 'true'
 
371
    oops_date = oops.get('time')
 
372
    if oops_date is None:
 
373
        oops_date = datetime.datetime.now(utc)
364
374
    data.update(
365
375
        oopsid = oopsid,
366
376
        prefix = prefix,
367
 
        date = oops.get('time').replace(microsecond=0),
 
377
        date = oops_date.replace(microsecond=0),
368
378
        # Missing pageids are urls because that suits our queries.
369
379
        pageid = oops.get('topic') or url,
370
380
        url = url,
377
387
        is_bot = _robot_pat.search(data['user_agent']) is not None,
378
388
        statements_count = len(statements),
379
389
        )
380
 
    return data, req_vars, statements, oops.get('tb_text')
 
390
    return data, req_vars, statements, oops.get('tb_text') or ''
 
391
 
 
392
 
 
393
def parsed_oops_to_model_oops(parsed_oops, pathname):
 
394
    """Convert an oops report dict to an Oops object."""
 
395
    data, req_vars, statements, traceback = _get_oops_tuple(parsed_oops)
 
396
    data['pathname'] = pathname
 
397
    res = Oops(**data)
 
398
    res.appinstance = res.get_appinstance()
 
399
    res.save()
 
400
    # Get it again.  Otherwise we have discrepancies between old and
 
401
    # new oops objects: old ones have unicode attributes, and new
 
402
    # ones have string attributes, for instance.  Ideally the message
 
403
    # conversion would have converted everything to unicode, but it
 
404
    # doesn't easily.
 
405
    res = Oops.objects.get(oopsid__exact=parsed_oops['id'])
 
406
    res.parsed_oops = parsed_oops
 
407
    res.req_vars = req_vars
 
408
    res.statements = statements
 
409
    res.traceback = traceback
 
410
    res.save()
 
411
    return res
381
412
 
382
413
 
383
414
class Oops(models.Model):
432
463
        try:
433
464
            res = cls.objects.get(oopsid__exact=oopsid)
434
465
        except cls.DoesNotExist:
435
 
            data, req_vars, statements, traceback = _get_oops_tuple(parsed_oops)
436
 
            data['pathname'] = pathname
437
 
            res = cls(**data)
438
 
            res.appinstance = res.get_appinstance()
439
 
            res.save()
440
 
            # Get it again.  Otherwise we have discrepancies between old and
441
 
            # new oops objects: old ones have unicode attributes, and new
442
 
            # ones have string attributes, for instance.  Ideally the message
443
 
            # conversion would have converted everything to unicode, but it
444
 
            # doesn't easily.
445
 
            res = cls.objects.get(oopsid__exact=oopsid)
446
 
            res.parsed_oops = parsed_oops
447
 
            res.req_vars = req_vars
448
 
            res.statements = statements
449
 
            res.traceback = traceback
 
466
            res = parsed_oops_to_model_oops(parsed_oops, pathname)
450
467
        return res
451
468
 
452
469
    @readproperty