def materialize_owners(devices_id): """Re-computes 'owners' for the given devices and components.""" # First let's erase all owners DeviceDomain.update_many_raw({'_id': { '$in': devices_id }}, {'$set': { 'owners': [] }}) # Then let's execute again the materialize hooks for Allocate/Deallocate query = { '$or': [{ '@type': 'devices:Allocate' }, { '@type': 'devices:Deallocate' }], 'devices': { '$in': devices_id } } order_by = {'_created': pymongo.ASCENDING} for event in DeviceEventDomain.get({ '$query': query, '$orderby': order_by }): if event['@type'] == 'devices:Allocate': materialize_actual_owners_add([event]) else: modified = materialize_actual_owners_remove([event]) if modified == 0: # This Remove does nothing and should be erased DeviceEventDomain.delete({'_id': event['_id']})
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 execute(self, database): SNAPSHOT_SOFTWARE = { 'DDI': 'Workbench', 'Scan': 'AndroidApp', 'DeviceHubClient': 'Web' } for snapshot in DeviceEventDomain.get({'@type': "devices:Snapshot"}): with suppress(KeyError): snapshot['snapshotSoftware'] = SNAPSHOT_SOFTWARE[snapshot.get( 'snapshotSoftware', 'DDI')] DeviceEventDomain.update_one_raw( snapshot['_id'], {'$set': { 'snapshotSoftware': snapshot['snapshotSoftware'] }}) for device in DeviceDomain.get({'events._id': snapshot['_id']}): materialized_snapshot = find( device['events'], lambda event: event['_id'] == snapshot['_id']) materialized_snapshot['snapshotSoftware'] = snapshot[ 'snapshotSoftware'] DeviceDomain.update_one_raw( device['_id'], {'$set': { 'events': device['events'] }})
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 execute(self, database): DeviceDomain.update_many_raw({}, {'$set': {'events': []}}) for event in DeviceEventDomain.get({ '$query': {}, '$orderby': { '_created': pymongo.ASCENDING } }): MaterializeEvents.materialize_events( Naming.resource(event['@type']), [event])
def re_materialize_events(): DeviceDomain.update_many_raw({}, {'$set': {'events': []}}) for event in DeviceEventDomain.get({ '$query': {}, '$orderby': { '_created': pymongo.ASCENDING } }): MaterializeEvents.materialize_events( Naming.resource(event['@type']), [event]) print('Events re-materialized.')
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 materialize_owners(devices_id): """Re-computes 'owners' for the given devices and components.""" # First let's erase all owners DeviceDomain.update_many_raw({'_id': {'$in': devices_id}}, {'$set': {'owners': []}}) # Then let's execute again the materialize hooks for Allocate/Deallocate query = {'$or': [{'@type': 'devices:Allocate'}, {'@type': 'devices:Deallocate'}], 'devices': {'$in': devices_id}} order_by = {'_created': pymongo.ASCENDING} for event in DeviceEventDomain.get({'$query': query, '$orderby': order_by}): if event['@type'] == 'devices:Allocate': materialize_actual_owners_add([event]) else: modified = materialize_actual_owners_remove([event]) if modified == 0: # This Remove does nothing and should be erased DeviceEventDomain.delete({'_id': event['_id']})
def re_materialize_events(): DeviceDomain.update_many_raw({}, {'$set': {'events': []}}) for event in DeviceEventDomain.get({'$query': {}, '$orderby': {'_created': pymongo.ASCENDING}}): MaterializeEvents.materialize_events(Naming.resource(event['@type']), [event]) print('Events re-materialized.')