Esempio n. 1
0
File: log.py Progetto: 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')
Esempio n. 2
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')
Esempio n. 3
0
class TestLogsAPIModel(unittest.TestCase):
    def setUp(self):
        self.model = LogsAPIModel()

    def test_save_log(self):
        self.model.collection.remove()
        self.model.save_log({"test": "test"})
        eq_(1, self.model.collection.count())

    def test_upsert_tag(self):
        self.model.tags.remove()
        self.model.upsert_tag('test')
        eq_(1, self.model.tags.count())

    def test_duplicate_tag(self):
        self.model.tags.remove()
        self.model.upsert_tag('test')
        self.model.upsert_tag('test')
        self.model.upsert_tag('test')
        self.model.upsert_tag('test')
        eq_(1, self.model.tags.count())
Esempio n. 4
0
class TestLogsAPIModel(unittest.TestCase):
    
    def setUp(self):
        self.model = LogsAPIModel()

    def test_save_log(self):
        self.model.collection.remove()
        self.model.save_log({"test": "test"})
        eq_(1, self.model.collection.count())


    def test_upsert_tag(self):
        self.model.tags.remove()
        self.model.upsert_tag('test')
        eq_(1, self.model.tags.count())

    def test_duplicate_tag(self):
        self.model.tags.remove()
        self.model.upsert_tag('test')
        self.model.upsert_tag('test')
        self.model.upsert_tag('test')
        self.model.upsert_tag('test')
        eq_(1, self.model.tags.count())
Esempio n. 5
0
File: log.py Progetto: hartym/amon
 def __init__(self):
     self.model = LogsAPIModel()
     self.str = ""
     self.common_model = CommonAPIModel()
Esempio n. 6
0
 def __init__(self):
     self.model = LogsAPIModel()
     self.str = ""
     self.common_model = CommonAPIModel()
Esempio n. 7
0
 def setUp(self):
     self.model = LogsAPIModel()
Esempio n. 8
0
 def setUp(self):
     self.model = LogsAPIModel()