Source code for otestpoint.labtools.variable
#
# Copyright (c) 2016-2017 - Adjacent Link LLC, Bridgewater, New Jersey
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Adjacent Link LLC nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
from subject import Subject
import traceback
import numpy as np
[docs]class Variable(Subject):
def __init__(self,stream,measurement,attribute,**kwargs):
"""Creates a Variable instance that will receive attribute measurement
updates.
You must be sure to instantiate the Stream with a probe that
provides the specified measurement in order to receive
updates.
Args:
measurement (str): Name of the measurement to associate with
the variable.
attribute (str): Name of the attribute contained in the
measurment to associate with the variable.
Keyword Args:
apply (callable): Callable applied to a measurment prior to
storing. Useful to reduce storage to just the relevant
measurement information of interest. Default: lambda x: x.
cache (int): Number of measurments to cache per reporting
tag. Default: Value set for stream.
tags ([str]): Names of tags to include measurments from. If
empty all tags are included. Default: [].
Raises:
KeyError: If an invalid keyword is found.
"""
Subject.__init__(self,stream)
self._measurement = measurement
self._attribute = attribute
self._cache = kwargs.pop('cache',5)
self._tags = set(kwargs.pop('tags',[]))
self._apply = kwargs.pop('apply',lambda x: x)
self._store = {}
self._updates = set()
if kwargs:
raise KeyError("Unknown key(s): %s" % ", ".join(kwargs.keys()))
[docs] def name(self):
"""Gets the variable name
Returns:
str: Variable name.
"""
if self._tags:
tags = '::[%s]' % ','.join(self._tags)
else:
tags = ''
return '%s::%s%s' % (self._measurement,self._attribute,tags)
def update(self,timestamp,tag,msg):
if not self._tags or tag in self._tags:
if timestamp not in self._store:
if len(self._store) == self._cache:
self._store.pop(sorted(self._store.keys())[0])
self._store[timestamp] = {tag:np.nan for tag in self._tags}
try:
self._store[timestamp][tag] = self._apply(getattr(msg,self._attribute))
self._updates.add(timestamp)
return True
except Exception as exp:
traceback.print_exc()
return False
def notify(self):
self.notify_observers()
self._updates = set()
def state(self):
return self._store,self._updates
def reset(self):
self._store.clear()
self._updates.clear()
self.notify_observers()