def test_01_mapped_state(self): statemap = Statemap(statemap=[0, 0, 1, 1, 1, 2, 2, 2]) self.assertEqual(0, statemap.get_mapped_state(0)) self.assertEqual(0, statemap.get_mapped_state(1)) self.assertEqual(1, statemap.get_mapped_state(2)) self.assertEqual(1, statemap.get_mapped_state(3)) self.assertEqual(1, statemap.get_mapped_state(4)) self.assertEqual(2, statemap.get_mapped_state(5)) self.assertEqual(2, statemap.get_mapped_state(6)) self.assertEqual(2, statemap.get_mapped_state(7))
def actions(self, event, derogation): name = derogation.get('crecord_name', None) description = derogation.get('description', None) actions = derogation.get('actions', None) _id = derogation.get('_id', None) if not _id or not name or not description or not actions: self.logger.error("Malformed derogation: %s" % derogation) return event # If _id is ObjectId(), transform it to str() if not isinstance(_id, basestring): _id = str(_id) if not isinstance(actions, list): self.logger.error( "Invalid actions field in '%s': %s" % (_id, actions)) return event derogated = False for action in actions: atype = action.get('type', None) if atype == "override": afield = action.get('field', None) avalue = action.get('value', None) self.logger.debug( " + %s: Override: '%s' -> '%s'" % ( event['rk'], afield, avalue ) ) if afield and avalue: event[afield] = avalue derogated = True else: self.logger.error( "Action malformed (needs 'field' and 'value'): %s" % action ) elif atype == "requalificate": statemap_id = action.get('statemap', None) self.logger.debug( "+ %s: Requalificate using statemap '%s'" % ( event['rk'], statemap_id ) ) if statemap_id: record = self.storage.find_one( mfilter={ 'crecord_type': 'statemap', '_id': statemap_id } ) if not record: self.logger.error( "Statemap '%s' not found" % statemap_id ) statemap = Statemap(record=record) event['real_state'] = event['state'] event['state'] = statemap.get_mapped_state( event['real_state'] ) derogated = True else: self.logger.error( "Action malformed (needs 'statemap'): %s" % action ) else: self.logger.warning("Unknown action '%s'" % atype) # If the event was derogated, fill some informations if derogated: event["derogation_id"] = _id event["derogation_description"] = description event["derogation_name"] = name event["tags"].append(name) event["tags"].append("derogated") return event