def check_archived_attribute():

    # Get current value/timestamp
    vv, t = getattr(v, 'value', v), getattr(v, 'time', 0)
    t = t and fn.ctime2time(t)

    if isinstance(vv, (type(None), Exception)):
        # attribute is not readable
        r.nok.append(a)
    elif r.vals[a] and 0 < t <= r.vals[a][0]:
        # attribute timestamp doesnt change
        r.stall.append(a)
    elif r.vals[a] and fbool(vv == r.vals[a][1]):
        # attribute value doesnt change
        r.stall.append(a)
    else:
        r.evs[a] = fn.tango.check_attribute_events(a)
        if not r.evs[a]:
            # attribute doesnt send events
            r.noev.append(a)
        else:
            # archiving failure (events or polling)
            r.lost.append(a)

    return state
Exemple #2
0
def check_archived_attribute(attribute, value = False, state = CheckState.OK, 
        default = CheckState.LOST, cache = None, tref = None, 
        check_events = True):
    """
    generic method to check the state of an attribute (readability/events)
    
    value = AttrValue object returned by check_attribute
    cache = result from check_db_schema containing archived values
    
    this method will not query the database; database values should be 
    given using the chache dictionary argument
    """
    # readable and/or no reason known for archiving failure
    state = default # do not remove this line
    
    # Get current value/timestamp
    if cache:
        stored = cache.values[attribute]
        #evs = cache.evs[attribute]
        if stored is None or (fn.isSequence(stored) and not len(stored)):
            return CheckState.UNK
        else:
            t,v = stored[0],stored[1]
            if t>=tref and not isinstance(v,(type(None),Exception)):
                print('%s should not be in check list! (%s,%s)' % (attribute,t,v))
                return CheckState.OK
        
    if value is False:
        value = fn.check_attribute(attribute, brief=False)
        
    vv,t = getattr(value,'value',value),getattr(value,'time',0)
    t = t and fn.ctime2time(t)
    
    if isinstance(vv,(type(None),Exception)):
        # attribute is not readable
        state = CheckState.NO_READ
    elif cache and stored and 0 < t <= stored[0]:
        # attribute timestamp doesnt change
        state = CheckState.STALL
    elif cache and stored and fbool(vv == stored[1]):
        # attribute value doesnt change
        state = CheckState.STALL
    elif check_events:
        # READABLE NOT STORED WILL ARRIVE HERE
        evs = fn.tango.check_attribute_events(attribute)
        if cache:
            cache.evs[attribute] = evs
        if not evs:
            # attribute doesnt send events
            state = CheckState.NO_EVENTS

    return state
 def check_attribute_ok(self,a,v,t=0):
     """
     arguments are attribute name and last value from db, plus ref. time
     """
     r = check_attribute_value(a)
     rv = getattr(r,'value',None)
     if isinstance(rv,(type(None),Exception)):
         # Attribute not readable
         self.attr_nok.append(a)
     elif self.is_hpp and not check_attribute_events(a):
         self.attr_nevs.append(a)
     else:
         if v is None or fn.isSequence(v) and not len(v):
             # Attribute has no values in DB
             self.attr_lost.append(a)
         else:
             # Time is compared against last update, current or read time
             t = min((t or fn.now(),fn.ctime2time(r.time)))
             v = self.get_last_value(a,v)
             try: 
                 diff = v[1]!=rv
             except: 
                 diff = 1
             if v[0] < t-3600:
                 if any(diff) if fn.isSequence(diff) else bool(diff):
                     # Last value much older than current data
                     self.attr_lost.append(a)
                 else:
                     self.attr_stall.append(a)
                     self.attr_ok.append(a)
             elif v[1] is None:
                 # Value is readable but not from DB
                 self.attr_err.append(a)
             else:
                 self.attr_ok.append(a)
     return
Exemple #4
0
 def check_attribute_ok(self, a, v, t=0):
     """
     arguments are attribute name and last value from db, plus ref. time
     """
     r = check_attribute_value(a)
     rv = getattr(r, 'value', None)
     if isinstance(rv, (type(None), Exception)):
         # Attribute not readable
         self.attr_nok.append(a)
     elif self.is_hpp and not check_attribute_events(a):
         self.attr_nevs.append(a)
     else:
         if v is None or fn.isSequence(v) and not len(v):
             # Attribute has no values in DB
             self.attr_lost.append(a)
         else:
             # Time is compared against last update, current or read time
             t = min((t or fn.now(), fn.ctime2time(r.time)))
             v = self.get_last_value(a, v)
             try:
                 diff = v[1] != rv
             except:
                 diff = 1
             if v[0] < t - 3600:
                 if any(diff) if fn.isSequence(diff) else bool(diff):
                     # Last value much older than current data
                     self.attr_lost.append(a)
                 else:
                     self.attr_stall.append(a)
                     self.attr_ok.append(a)
             elif v[1] is None:
                 # Value is readable but not from DB
                 self.attr_err.append(a)
             else:
                 self.attr_ok.append(a)
     return
def check_db_schema(schema, tref=None):

    r = fn.Struct()
    r.api = api = pta.api(schema)
    r.tref = fn.notNone(tref, fn.now() - 3600)

    r.attrs = api.keys()
    r.on = api.get_archived_attributes()
    r.off = [a for a in r.attrs if a not in r.on]
    if schema in ('tdb', 'hdb'):
        ups = api.db.get_table_updates()
        r.vals = dict((k, (ups[api[k].table], None)) for k in r.on)
    else:
        r.vals = dict(fn.kmap(api.load_last_values, r.on))
        r.vals = dict((k, v and v.values()[0]) for k, v in r.vals.items())

    dups = fn.defaultdict(list)
    if getattr(api, 'dedicated', None):
        [
            dups[a].append(k) for a in r.on for k, v in api.dedicated.items()
            if a in v
        ]
        nups = [a for a, v in dups.items() if len(v) <= 1]
        [dups.pop(a) for a in nups]
    r.dups = dict(dups)

    # Get all updated attributes
    r.ok = [a for a, v in r.vals.items() if v and v[0] > r.tref]
    # Try to read not-updated attributes
    r.check = dict((a, fn.check_attribute(a)) for a in r.on if a not in r.ok)
    r.nok, r.stall, r.noev, r.lost, r.evs = [], [], [], [], {}
    # Method to compare numpy values
    fbool = lambda x: all(x) if fn.isSequence(x) else bool(x)

    for a, v in r.check.items():
        # Get current value/timestamp
        vv, t = getattr(v, 'value', v), getattr(v, 'time', 0)
        t = t and fn.ctime2time(t)

        if isinstance(vv, (type(None), Exception)):
            # attribute is not readable
            r.nok.append(a)
        elif r.vals[a] and 0 < t <= r.vals[a][0]:
            # attribute timestamp doesnt change
            r.stall.append(a)
        elif r.vals[a] and fbool(vv == r.vals[a][1]):
            # attribute value doesnt change
            r.stall.append(a)
        else:
            r.evs[a] = fn.tango.check_attribute_events(a)
            if not r.evs[a]:
                # attribute doesnt send events
                r.noev.append(a)
            else:
                # archiving failure (events or polling)
                r.lost.append(a)

    # SUMMARY
    print(schema)
    for k in 'attrs on off dups ok nok noev stall lost'.split():
        print('\t%s:\t:%d' % (k, len(r.get(k))))

    return r