def delete_events_in_device(resource_name: str, device: dict): """ Deletes the references of the given device in all the events, and deletes the full event if it references only to the device. """ if resource_name in Device.resource_types: _id = device["_id"] qin = {"$in": [_id]} query = {"$or": [{"device": _id}, {"devices": qin}, {"components": qin}], "@type": {"$ne": "devices:Register"}} sort = {"_created": pymongo.ASCENDING} # Order is important to find the first Snapshot (see below) first_snapshot_found = False for event in DeviceEventDomain.get({"$query": query, "$orderby": sort}): if not first_snapshot_found and event["@type"] == "devices:Snapshot": # We cannot delete the Snapshot that created the device, because there is a change to create # an infinite loop: Snapshot that created device -> Register -> DEL /device -> Snapshot that created... first_snapshot_found = True else: event_resource = Naming.resource(event["@type"]) if event.get("device", None) == _id: # Am I the 'device' of the event? execute_delete(event_resource, event["_id"]) elif [_id] == event.get("devices", []): # Is there no more 'devices' in the event, apart from me? execute_delete(event_resource, event["_id"]) # All events that do not use 'components' for materialization (aka Add/Remove) should be erased # if there are no more components elif event["@type"] in ("devices:Add", "devices:Remove") and event["components"] == [_id]: execute_delete(event_resource, event["_id"]) else: # Keep the event; just delete my reference DeviceEventDomain.update_raw(event["_id"], {"$pull": {"devices": qin, "components": qin}})
def delete_events_in_device(resource_name: str, device: dict): """ Deletes the references of the given device in all the events, and deletes the full event if it references only to the device. """ if resource_name in Device.resource_types: _id = device['_id'] qin = {'$in': [_id]} query = {'$or': [{'device': _id}, {'devices': qin}, {'components': qin}], '@type': {'$ne': 'devices:Register'}} sort = {'_created': pymongo.ASCENDING} # Order is important to find the first Snapshot (see below) first_snapshot_found = False for event in DeviceEventDomain.get({'$query': query, '$orderby': sort}): if not first_snapshot_found and event['@type'] == 'devices:Snapshot': # We cannot delete the Snapshot that created the device, because there is a change to create # an infinite loop: Snapshot that created device -> Register -> DEL /device -> Snapshot that created... first_snapshot_found = True else: event_resource = Naming.resource(event['@type']) if event.get('device', None) == _id: # Am I the 'device' of the event? execute_delete(event_resource, event['_id']) elif [_id] == event.get('devices', []): # Is there no more 'devices' in the event, apart from me? execute_delete(event_resource, event['_id']) # All events that do not use 'components' for materialization (aka Add/Remove) should be erased # if there are no more components elif event['@type'] in ('devices:Add', 'devices:Remove') and event['components'] == [_id]: execute_delete(event_resource, event['_id']) else: # Keep the event; just delete my reference DeviceEventDomain.update_raw(event['_id'], {'$pull': {'devices': qin, 'components': qin}})
def set_prefix(): for event in DeviceEventDomain.get({}): try: resource_type = DeviceEventDomain.new_type(event['@type']) except TypeError: pass else: DeviceEventDomain.update_raw(event['_id'], {'$set': {'@type': resource_type}}) print('Events prefixed.')
def set_prefix(): for event in DeviceEventDomain.get({}): try: resource_type = DeviceEventDomain.new_type(event['@type']) except TypeError: pass else: DeviceEventDomain.update_raw( event['_id'], {'$set': { '@type': resource_type }}) print('Events prefixed.')