Пример #1
0
Файл: log.py Проект: hartym/amon
class Log(object):

    def __init__(self):
        self.model = LogsAPIModel()
        self.str = ""
        self.common_model = CommonAPIModel()

    def flatten_dict(self, d, parent_key=''):
        items = []
        for k, v in d.items():
            if isinstance(v, dict):
                self.str+=":%s" % k
                items.extend(self.flatten_dict(v, k).items())
            else:
                self.str+=":%s" % k
                items.append((k, v))
        
        return dict(items)  
    
    # Checks the tags in the database and adds them if they are new entries
    def check_tags(self, tags):

        if isinstance(tags, list):
            for el in tags:
                self.model.upsert_tag(el)
                        
        elif isinstance(tags, str) or isinstance(tags, unicode):
            self.model.upsert_tag(tags)

    def __call__(self, *args, **kwargs):

        log_dict = args[0]

        try:
            tags = log_dict.get('tags')
        except: 
            tags = None
        
        message = log_dict.get('message', '')

        now = int(time())

        self.check_tags(tags)

        entry = {'time': now, 'message': message, 'tags': tags}
        
        # Add the data to a separate field, for easy searching 
        if isinstance(message, dict):
             self.str = ""
             self.flatten_dict(message)
             _searchable = self.str
        elif isinstance(message, list):
            _searchable = ":".join(["%s" % el for el in message])
        else:
            _searchable = message
        
        entry['_searchable'] = _searchable

        self.model.save_log(entry)
        self.common_model.upsert_unread('logs')
Пример #2
0
class TestCommonAPIModel(unittest.TestCase):


    def setUp(self):
        self.model = CommonAPIModel()

    def test_upsert_unread_logs(self):
        self.model.unread.remove()

        self.model.upsert_unread('logs')
        eq_(1, self.model.unread.count())

        entry = self.model.unread.find_one()
        eq_(1,entry['logs'])
        eq_(0,entry['exceptions'])


    def test_upsert_unread_exceptions(self):
        self.model.unread.remove()

        self.model.upsert_unread('exceptions')
        eq_(1, self.model.unread.count())

        entry = self.model.unread.find_one()
        eq_(1,entry['exceptions'])
        eq_(0,entry['logs'])
Пример #3
0
class Log(object):
    def __init__(self):
        self.model = LogsAPIModel()
        self.str = ""
        self.common_model = CommonAPIModel()

    def flatten_dict(self, d, parent_key=''):
        items = []
        for k, v in d.items():
            if isinstance(v, dict):
                self.str += ":%s" % k
                items.extend(self.flatten_dict(v, k).items())
            else:
                self.str += ":%s" % k
                items.append((k, v))

        return dict(items)

    # Checks the tags in the database and adds them if they are new entries
    def check_tags(self, tags):

        if isinstance(tags, list):
            for el in tags:
                self.model.upsert_tag(el)

        elif isinstance(tags, str) or isinstance(tags, unicode):
            self.model.upsert_tag(tags)

    def __call__(self, *args, **kwargs):

        log_dict = args[0]

        try:
            tags = log_dict.get('tags')
        except:
            tags = None

        message = log_dict.get('message', '')

        now = int(time())

        self.check_tags(tags)

        entry = {'time': now, 'message': message, 'tags': tags}

        # Add the data to a separate field, for easy searching
        if isinstance(message, dict):
            self.str = ""
            self.flatten_dict(message)
            _searchable = self.str
        elif isinstance(message, list):
            _searchable = ":".join(["%s" % el for el in message])
        else:
            _searchable = message

        entry['_searchable'] = _searchable

        self.model.save_log(entry)
        self.common_model.upsert_unread('logs')
Пример #4
0
class Exception(object):
    def __init__(self):
        self.exception = {}
        self.model = ExceptionAPIModel()
        self.common_model = CommonAPIModel()

    def __call__(self, *args, **kwargs):

        now = int(time())

        exception_dict = args[0]
        exception_class = exception_dict.get('exception_class', '')
        url = exception_dict.get('url', '')
        backtrace = exception_dict.get('backtrace', '')

        message = exception_dict.get('message', '')
        enviroment = exception_dict.get('enviroment', '')
        data = exception_dict.get('data', '')

        exception_string = "{0}{1}{2}".format(exception_class, url, backtrace)
        exception_id = md5(exception_string).hexdigest()

        additional_data = {'occurrence': now}

        if message: additional_data['message'] = message
        if enviroment: additional_data['enviroment'] = enviroment
        if data: additional_data['data'] = data

        exception_in_db = self.model.collection.find_one(
            {"exception_id": exception_id})

        if exception_in_db is not None:
            exception_in_db['last_occurrence'] = now
            exception_in_db['additional_data'].insert(0, additional_data)
            exception_in_db[
                'total_occurrences'] = exception_in_db['total_occurrences'] + 1

            self.model.collection.update({'_id': exception_in_db['_id']},
                                         exception_in_db)
        else:
            entry = {
                'last_occurrence': now,
                'exception_id': exception_id,
                'exception_class': exception_class,
                'url': url,
                'backtrace': backtrace,
            }

            entry['additional_data'] = [additional_data]
            entry['total_occurrences'] = 1

            self.model.save_exception(entry)

        self.common_model.upsert_unread('exceptions')
Пример #5
0
class Exception(object):

    def __init__(self):
        self.exception = {}
        self.model = ExceptionAPIModel()
        self.common_model = CommonAPIModel()

    def __call__(self, *args, **kwargs):
        
        now = int(time())

        exception_dict = args[0]
        exception_class = exception_dict.get('exception_class', '')
        url = exception_dict.get('url', '')
        backtrace = exception_dict.get('backtrace', '')
        
        message= exception_dict.get('message', '')
        enviroment = exception_dict.get('enviroment', '')
        data = exception_dict.get('data', '')
        
        
        exception_string = "{0}{1}{2}".format(exception_class, url, backtrace)
        exception_id = md5(exception_string).hexdigest()
        
        additional_data = {'occurrence': now}

        if message: additional_data['message'] = message
        if enviroment: additional_data['enviroment'] = enviroment
        if data: additional_data['data'] = data

        exception_in_db = self.model.collection.find_one({"exception_id" : exception_id})

        if exception_in_db is not None:
            exception_in_db['last_occurrence'] = now
            exception_in_db['additional_data'].insert(0, additional_data)
            exception_in_db['total_occurrences']  = exception_in_db['total_occurrences']+1

            self.model.collection.update({'_id' : exception_in_db['_id']}, exception_in_db)
        else:
            entry = {'last_occurrence': now,
                     'exception_id': exception_id,
                     'exception_class': exception_class,
                     'url': url,
                     'backtrace' : backtrace,
                     }

            entry['additional_data'] = [additional_data]
            entry['total_occurrences'] = 1
            
            self.model.save_exception(entry)
            
        self.common_model.upsert_unread('exceptions')
Пример #6
0
class TestCommonAPIModel(unittest.TestCase):
    def setUp(self):
        self.model = CommonAPIModel()

    def test_upsert_unread_logs(self):
        self.model.unread.remove()

        self.model.upsert_unread('logs')
        eq_(1, self.model.unread.count())

        entry = self.model.unread.find_one()
        eq_(1, entry['logs'])
        eq_(0, entry['exceptions'])

    def test_upsert_unread_exceptions(self):
        self.model.unread.remove()

        self.model.upsert_unread('exceptions')
        eq_(1, self.model.unread.count())

        entry = self.model.unread.find_one()
        eq_(1, entry['exceptions'])
        eq_(0, entry['logs'])