Example #1
0
 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']
                 }})
Example #2
0
 def execute(self, database):
     for device in DeviceDomain.get({'hid': 'dummy'}):
         try:
             hid = DeviceDomain.hid(device['manufacturer'], device['serialNumber'], device['model'])
             DeviceDomain.update_one_raw(device['_id'], {'$set': {'hid': hid}})
         except KeyError:
             DeviceDomain.update_one_raw(device['_id'], {'$unset': {'hid': ''}, '$set': {'isUidSecured': False}})
Example #3
0
def _execute_register(device: dict, created: str, log: list):
    """
    Tries to POST the device and updates the `device` dict with the resource from the database; if the device could
    not be uploaded the `device` param will contain the database version of the device, not the inputting one. This is
    because the majority of the information of a device is immutable (in concrete the fields used to compute
    the ETAG).

    :param device: Inputting device. It is replaced (keeping the reference) with the db version.
    :param created: Set the _created value to be the same for the device as for the register
    :param log: A log where to append the resulting device if execute_register has been successful
    :raise InnerRequestError: any internal error in the POST that is not about the device already existing.
    """
    new = True
    try:
        if created:
            device['created'] = created
        db_device = execute_post_internal(Naming.resource(device['@type']),
                                          device)
    except InnerRequestError as e:
        new = False
        try:
            db_device = _get_existing_device(e)
            # We add a benchmark todo move to another place?
            device['_id'] = db_device['_id']
            ComponentDomain.benchmark(device)
            external_synthetic_id_fields = pick(
                device, *DeviceDomain.external_synthetic_ids)
            # If the db_device was a placeholder
            # We want to override it with the new device
            if db_device.get('placeholder', False):
                # Eve do not generate defaults from sub-resources
                # And we really need the placeholder default set, specially when
                # discovering a device
                device['placeholder'] = False
                # We create hid when we validate (wrong thing) so we need to manually set it here as we won't
                # validate in this db operation
                device['hid'] = DeviceDomain.hid(device['manufacturer'],
                                                 device['serialNumber'],
                                                 device['model'])
                DeviceDomain.update_one_raw(db_device['_id'], {'$set': device})
            elif not is_empty(external_synthetic_id_fields):
                # External Synthetic identifiers are not intrinsically inherent
                # of devices, and thus can be added later in other Snapshots
                # Note that the device / post and _get_existing_device() have already validated those ids
                DeviceDomain.update_one_raw(
                    db_device['_id'], {'$set': external_synthetic_id_fields})
        except DeviceNotFound:
            raise e
    else:
        log.append(db_device)
    device.clear()
    device.update(db_device)
    device['new'] = new  # Note that the device is 'cleared' before
    return db_device
Example #4
0
 def materialize_component_info():
     devices = DeviceDomain.get({'@type': Computer.type_name})
     for device in devices:
         if 'components' in device:
             # Let's reset the ram and hard-drive counter
             for _type in ('totalRamSize', 'totalHardDriveSize'):
                 DeviceDomain.update_one_raw(device['_id'],
                                             {'$set': {
                                                 _type: 0
                                             }})
             update_materialized_computer(device['_id'],
                                          device['components'],
                                          add=True)
Example #5
0
def _update_same_as(returned_same_as: dict):
    for url, same_as in list(returned_same_as.items()):
        _id = url.split('/')[-1]
        device = DeviceDomain.get_one({'_id': _id})
        same_as = set(device.get('sameAs', [])) | set(same_as)
        DeviceDomain.update_one_raw(_id, {'$set': {'sameAs': list(same_as)}})
Example #6
0
 def _update_same_as(device: dict, same_as: list):
     DeviceDomain.update_one_raw(device['_id'],
                                 {'$set': {
                                     'sameAs': same_as
                                 }})
Example #7
0
def _update_same_as(returned_same_as: dict):
    for url, same_as in returned_same_as.items():
        _id = url.split('/')[-1]
        device = DeviceDomain.get_one({'_id': _id})
        same_as = set(device.get('sameAs', [])) | set(same_as)
        DeviceDomain.update_one_raw(_id, {'$set': {'sameAs': list(same_as)}})