class CheckManagerTest(TestCase): """ Base class for all check manager tests. """ def setUp(self): """ initialize a manager. """ self.manager = Linklist() self.name = 'testlinklist' self.id = str(uuid4()) self.ids = [self.id] self.document_content = { 'id': self.id, 'name': self.name, 'linklist': ['http://canopsis.org'], 'mfilter': '{"$and": [{"connector": "collectd"}]}' } def clean(self): self.manager.remove(self.ids) def get_linklist(self): return self.manager.find(ids=self.ids) def linklist_count_equals(self, count): result = list(self.get_linklist()) if DEBUG: print result result = len(result) self.assertEqual(result, count)
class engine(TaskHandler): etype = 'tasklinklist' event_projection = { 'resource': 1, 'source_type': 1, 'component': 1, 'connector_name': 1, 'connector': 1, 'event_type': 1, } def handle_task(self, job): self.link_list_manager = Linklist() self.context = Context() self.event = Event() self.entity_link_manager = Entitylink() """ This task computes all links associated to an entity. Link association are managed by entity link system. """ for entity_id in self.context.iter_ids(): self.entity_link_manager.put(entity_id, { 'computed_links': [] }) links = {} # Computes links for all context elements # may cost some memory depending on filters and context size for linklist in self.link_list_manager.find(): # condition to proceed a list link is they must be set name = linklist['name'] l_filter = linklist.get('mfilter') l_list = linklist.get('filterlink') self.logger.debug(u'proceed linklist {}'.format(name)) if not l_list or not l_filter: self.logger.info(u'Cannot proceed linklist for {}'.format(name)) else: # Find context element ids matched by filter context_ids = self.get_ids_for_filter(l_filter) # Add all linklist to matched context element for context_id in context_ids: if context_id not in links: links[context_id] = [] # Append all links/labels to the context element links[context_id] += l_list self.logger.debug(u'links') self.logger.debug(links) entities = self.context.get_entities(links.keys()) for entity in entities: self.update_context_with_links( entity, links[entity['_id']] ) return (0, 'Link list computation complete') def update_context_with_links(self, entity, links): """ Upsert computed links to the entity link storage """ self.logger.debug(u' + entity') self.logger.debug(entity) self.logger.debug(u' + links') self.logger.debug(links) context = { 'computed_links': links } _id = self.context.get_entity_id(entity) self.entity_link_manager.put(_id, context) def get_ids_for_filter(self, l_filter): """ Retrieve a list of id from event collection. Can be performance killer as matching mfilter is only available on the event collection at the moment """ context_ids = [] try: l_filter = loads(l_filter) except Exception as e: self.logger.error( 'Unable to parse mfilter, query aborted {}'.format(e) ) return context_ids events = self.event.find( query=l_filter, projection=self.event_projection ) for event in events: self.logger.debug(u'rk : {}'.format(event['_id'])) entity = self.context.get_entity(event) entity_id = self.context.get_entity_id(entity) context_ids.append(entity_id) return context_ids
class CTXLinklistRegistry(CTXPropRegistry): """In charge of ctx linklist information. """ __datatype__ = 'linklist' #: default datatype name def __init__(self, *args, **kwargs): super(CTXLinklistRegistry, self).__init__(*args, **kwargs) self.manager = Linklist() self.events = MongoStorage(table='events') self.context = Context() def _get_documents(self, ids, query): """Get documents related to input ids and query. :param list ids: entity ids. If None, get all documents. :param dict query: additional selection query. :return: list of documents. :rtype: list """ result = [] # get entity id field name ctx_id_field = self._ctx_id_field() # get a set of entity ids for execution speed reasons if ids is not None: ids = set(ids) # get documents docs = self.manager.find(_filter=query) for doc in docs: try: mfilter = loads(doc['mfilter']) except Exception: pass else: # get entities from events events = self.events.find_elements(query=mfilter) for event in events: entity = self.context.get_entity(event) entity_id = self.context.get_entity_id(entity) if ids is None or entity_id in ids: doc[ctx_id_field] = entity_id # add eid to the doc result.append(doc) return result def _get(self, ids, query, *args, **kwargs): return self._get_documents(ids=ids, query=query) def _delete(self, ids, query, *args, **kwargs): result = self._get_documents(ids=ids, query=query) ids = [doc['_id'] for doc in result] self.manager.remove(ids=ids) return result def ids(self, query=None): result = [] documents = self.manager.find(_filter=query) for document in documents: try: mfilter = loads(document['mfilter']) except Exception: pass else: # get entities from events events = self.events.find_elements(query=mfilter) for event in events: entity = self.context.get_entity(event) entity_id = self.context.get_entity_id(entity) result.append(entity_id) return result