Example #1
0
def insert_exception(d):

    exceptions = Database.Instance().exceptions()
    exception_groups = Database.Instance().exception_groups()
    applications = Database.Instance().applications()

    #Check for required keys
    required_fields = ['key', 'message', 'application', 'severity']
    if len(list(set(required_fields) & set(d))) != len(required_fields):
        raise KeyError('Some required fields are missing') 

    check_severity(d['severity'])

    #Validate the key which was passed
    if not _validate_key(d['key']):
        raise KeyError('The key you specified is invalid')

    d['application'] = ApplicationService.insert_application(d['key'], d['application'])

    if 'stacktrace' not in d:
        d['stacktrace'] = ''

    #Setup the string to be hashed
    hash_string = '%s\n%s\n%s\n%s\n%s\n%s' % (d['key'], 
                                   d['severity'],
                                   str(d['stacktrace']),
                                   d['message'],
                                   d['filename'],
                                   d['application'])
    
    #Has the string
    unique_hash = hash(hash_string)

    #Create and insert the exception record
    exception = d
    exception['insert_date'] = datetime.datetime.utcnow()
    exception['unique_hash'] = unique_hash

    exception_id = exceptions.save(exception)

    #Check if the exception group exists
    group = exception_groups.find_one({'unique_hash': unique_hash, 'status': False })

    #If Exception already exists, increment count
    if group:
        group['count'] += 1
        group['exceptions'].append(exception_id)
        group['status'] = False

    #Otherwise, create the exception group record
    else:
        group = { 'message': d['message'],
                  'severity': d['severity'],
                  'key': d['key'],
                  'application': d['application'],
                  'exceptions': [ exception_id ],
                  'unique_hash': unique_hash,
                  'status': False,
                  'count': 1,
                  'insert_date': datetime.datetime.utcnow(),
                  'filename': d['filename'],
                  'stacktrace': d['stacktrace'],
                }


        applications.update({'_id': d['application'] }, {'$inc': {'count': 1 }})
        applications.update({'_id': d['application'] }, {'$inc': {str(d['severity']): 1 }})
    
    #Set the last seen_date
    group['last_seen_on'] = datetime.datetime.utcnow()

    #Save the exception group
    exception_group_id = exception_groups.save(group)

    increment_statistics(d, exception_group_id)

    #Update the Exception with the Exception Group Id
    exception['exception_group_id'] = exception_group_id
    exceptions.save(exception)

    exception_groups.ensure_index([('severity', 1), 
                                         ('application', 1), 
                                         ('key', 1), 
                                         ('unique_hash', 1), 
                                         ('status', 1 ), 
                                         ('last_seen_on', -1)
                                        ])

    exceptions.ensure_index([('severity', 1), 
                                   ('application', 1), 
                                   ('key', 1), 
                                   ('unique_hash', 1), 
                                   ('insert_date', -1),
                                  ])

    #Index the Document
    mdb_search = mongodbsearch.mongodb_search(Database.Instance().db())
    
    text = [d['message']]
    if 'headers' in d:
        for k, v in d['headers'].iteritems():
            text.append(str(k))
            text.append(str(v))

    if 'params' in d:
        for k, v in d['params'].iteritems():
            text.append(str(k))
            text.append(str(v))
    
    for s in d['stacktrace']:
        text.extend([str(x) for x in s.values()])

    kwargs = {'key': d['key'],
              'application': str(d['application']),
              'severity': d['severity'],
              'last_seen_on': group['last_seen_on'],
              'filename': d['filename'],
              'unique_hash': unique_hash
             }

    mdb_search.index_document(str(exception_group_id), ' '.join(text), ensureindex=kwargs.keys(), **kwargs)

    return exception_group_id, exception_id
def insert_exception(d):

    exceptions = Database.Instance().exceptions()
    exception_groups = Database.Instance().exception_groups()
    applications = Database.Instance().applications()

    # Check for required keys
    required_fields = ["key", "message", "application", "severity"]
    if len(list(set(required_fields) & set(d))) != len(required_fields):
        raise KeyError("Some required fields are missing")

    check_severity(d["severity"])

    # Validate the key which was passed
    if not _validate_key(d["key"]):
        raise KeyError("The key you specified is invalid")

    d["application"] = ApplicationService.insert_application(d["key"], d["application"])

    if "stacktrace" not in d:
        d["stacktrace"] = ""

    # Setup the string to be hashed
    hash_string = "%s\n%s\n%s\n%s\n%s\n%s" % (
        d["key"],
        d["severity"],
        str(d["stacktrace"]),
        d["message"],
        d["filename"],
        d["application"],
    )

    # Has the string
    unique_hash = hash(hash_string)

    # Create and insert the exception record
    exception = d
    exception["insert_date"] = datetime.datetime.utcnow()
    exception["unique_hash"] = unique_hash

    exception_id = exceptions.save(exception)

    # Check if the exception group exists
    group = exception_groups.find_one({"unique_hash": unique_hash, "status": False})

    # If Exception already exists, increment count
    if group:
        group["count"] += 1
        group["exceptions"].append(exception_id)
        group["status"] = False

    # Otherwise, create the exception group record
    else:
        group = {
            "message": d["message"],
            "severity": d["severity"],
            "key": d["key"],
            "application": d["application"],
            "exceptions": [exception_id],
            "unique_hash": unique_hash,
            "status": False,
            "count": 1,
            "insert_date": datetime.datetime.utcnow(),
            "filename": d["filename"],
            "stacktrace": d["stacktrace"],
        }

        applications.update({"_id": d["application"]}, {"$inc": {"count": 1}})
        applications.update({"_id": d["application"]}, {"$inc": {str(d["severity"]): 1}})

    # Set the last seen_date
    group["last_seen_on"] = datetime.datetime.utcnow()

    # Save the exception group
    exception_group_id = exception_groups.save(group)

    increment_statistics(d, exception_group_id)

    # Update the Exception with the Exception Group Id
    exception["exception_group_id"] = exception_group_id
    exceptions.save(exception)

    # Index the Document
    mdb_search = mongodbsearch.mongodb_search(Database.Instance().db())

    text = [d["message"]]
    if "headers" in d:
        for k, v in d["headers"].iteritems():
            text.append(str(k))
            text.append(str(v))

    if "params" in d:
        for k, v in d["params"].iteritems():
            text.append(str(k))
            text.append(str(v))

    for s in d["stacktrace"]:
        text.extend([str(x) for x in s.values()])

    kwargs = {
        "key": d["key"],
        "application": str(d["application"]),
        "severity": d["severity"],
        "last_seen_on": group["last_seen_on"],
        "filename": d["filename"],
        "unique_hash": unique_hash,
    }

    mdb_search.index_document(str(exception_group_id), " ".join(text), ensureindex=kwargs.keys(), **kwargs)

    return exception_group_id, exception_id