def get_latest(instrument_id, key_id):
    """
        Returns the latest entry for a given key on a given instrument
        @param instrument_id: Instrument object
        @param key_id: Parameter object
    """
    # First get it from the cache
    try:
        last_value = StatusCache.objects.filter(instrument_id=instrument_id,
                                                key_id=key_id).latest('timestamp')
    except:
        # If that didn't work, get it from the table of values
        values = StatusVariable.objects.filter(instrument_id=instrument_id,
                                                   key_id=key_id)
        # If we don't have any entry yet, just return Non
        if len(values)==0:
            return None
        last_value = values.latest('timestamp')
        
        # Put the latest value in the cache so we don't have to go through this again
        cached = StatusCache(instrument_id=last_value.instrument_id,
                             key_id=last_value.key_id,
                             value=last_value.value,
                             timestamp=last_value.timestamp)
        cached.save()
        
    return last_value
def store_and_cache(instrument_id, key, value):
    """
        Store and cache a DASMON parameter
        @param instrument_id: Instrument object
        @param key: key string
        @param value: value for the given key
    """
    try:
        key_id = Parameter.objects.get(name=key)
    except:
        key_id = Parameter(name=key)
        key_id.save()
    status_entry = StatusVariable(instrument_id=instrument_id,
                                  key_id=key_id,
                                  value=value)
    status_entry.save()
    
    # Update the latest value
    try:
        last_value = StatusCache.objects.filter(instrument_id=instrument_id,
                                             key_id=key_id).latest('timestamp')
        last_value.value = value = status_entry.value
        last_value.timestamp = status_entry.timestamp
        last_value.save()
    except:
        last_value = StatusCache(instrument_id=instrument_id,
                                 key_id=key_id,
                                 value=status_entry.value,
                                 timestamp=status_entry.timestamp)
        last_value.save()
Beispiel #3
0
def store_and_cache_(instrument_id, key_id, value, timestamp=None, cache_only=False):
    """
        Store and cache a DASMON parameter
        @param instrument_id: Instrument object
        @param key_id: key Parameter object
        @param value: value for the given key
        @param cache_only: only update cache
    """
    # Do bother with parameter that are not monitored
    if key_id.monitored is False:
        return

    # The longest allowable string is 128 characters
    value_string = str(value)
    if len(value_string) > 128:
        value_string = value_string[:128]

    datetime_timestamp = datetime.datetime.fromtimestamp(time.time()).replace(tzinfo=timezone.get_current_timezone())
    if cache_only is False:
        status_entry = StatusVariable(instrument_id=instrument_id,
                                      key_id=key_id,
                                      value=value_string)
        # Force the timestamp value as needed
        if timestamp is not None:
            try:
                datetime_timestamp = datetime.datetime.fromtimestamp(timestamp).replace(tzinfo=timezone.get_current_timezone())
                status_entry.timestamp = datetime_timestamp
            except:
                logging.error("Could not process timestamp [%s]: %s", timestamp, sys.exc_value)
        status_entry.save()
        datetime_timestamp = status_entry.timestamp

    # Update the latest value
    try:
        last_value = StatusCache.objects.filter(instrument_id=instrument_id,
                                                key_id=key_id).latest('timestamp')
        last_value.value = value_string
        last_value.timestamp = datetime_timestamp
        last_value.save()
    except:
        last_value = StatusCache(instrument_id=instrument_id,
                                 key_id=key_id,
                                 value=value_string,
                                 timestamp=datetime_timestamp)
        last_value.save()