Пример #1
0
def check_device_last_contact(context):
    key = DEVICE_LAST_CONTACT_KEY.format(device_id=context.device_id)
    value = context.cache.get(key).decode()
    assert_that(value, not_none())

    last_contact_ts = datetime.strptime(value, '%Y-%m-%d %H:%M:%S.%f')
    assert_that(last_contact_ts.date(), equal_to(datetime.utcnow().date()))
Пример #2
0
    def _get_device_last_contact(self, device):
        """Get the last time the device contacted the backend.

        The timestamp returned by this method will be used to determine if a
        device is active or not.

        The device table has a last contacted column but it is only updated
        daily via batch script.  The real-time values are kept in Redis.
        If the Redis query returns nothing, the device hasn't contacted the
        backend yet.  This could be because it was just activated. Give the
        device a couple of minutes to make that first call to the backend.
        """
        last_contact_ts = self.cache.get(
            DEVICE_LAST_CONTACT_KEY.format(device_id=device.id)
        )
        if last_contact_ts is None:
            if device.last_contact_ts is None:
                last_contact_age = datetime.utcnow() - device.add_ts
            else:
                last_contact_age = datetime.utcnow() - device.last_contact_ts
        else:
            last_contact_ts = last_contact_ts.decode()
            last_contact_ts = datetime.strptime(
                last_contact_ts,
                '%Y-%m-%d %H:%M:%S.%f'
            )
            last_contact_age = datetime.utcnow() - last_contact_ts

        return last_contact_age
Пример #3
0
def update_device_last_contact():
    """Update the timestamp on the device table indicating last contact.

    This should only be done on public API calls because we are tracking
    device activity only.
    """
    if 'public' in current_app.name and 'device_id' in global_context:
        key = DEVICE_LAST_CONTACT_KEY.format(
            device_id=global_context.device_id)
        global_context.cache.set(key, str(datetime.utcnow()))
Пример #4
0
    def _get_ts_from_cache(self, device_id):
        last_contact_ts = None
        cache_key = DEVICE_LAST_CONTACT_KEY.format(device_id=device_id)
        value = self.cache.get(cache_key)
        if value is not None:
            last_contact_ts = datetime.strptime(value.decode(),
                                                '%Y-%m-%d %H:%M:%S.%f')
            self.cache.delete(cache_key)

        return last_contact_ts