Beispiel #1
0
 def get_attributes(self,device=None,attribute=None):
     """Both device and attribute are regexps"""
     getattr(self,'attributes',self.load_attributes())
     result = CaselessDict((n,f) for n,f in self.attributes.items() if 
                 (not device or fun.matchCl(device,n.rsplit('/',1)[0],terminate=True)) and
                 (not attribute or fun.matchCl(attribute,n.rsplit('/',1)[-1],terminate=True))
             )
     return result
Beispiel #2
0
 def get_attributes(self, device=None, attribute=None):
     """Both device and attribute are regexps"""
     getattr(self, 'attributes', self.load_attributes())
     result = CaselessDict(
         (n, f) for n, f in self.attributes.items()
         if (not device
             or fun.matchCl(device, n.rsplit('/', 1)[0], terminate=True))
         and (not attribute or fun.matchCl(
             attribute, n.rsplit('/', 1)[-1], terminate=True)))
     return result
Beispiel #3
0
 def set_property(self, *args):
     """ set_property(property,value) or set_property(device_regexp,property,value) """
     property, value = args[-2:]
     devs = self.get_all_devices()
     if len(args) == 3: devs = [d for d in devs if fun.matchCl(args[0], d)]
     return [(d, self.db.put_device_property(d, {property: value}))[0]
             for d in devs]
Beispiel #4
0
 def get_property(self,*args):
     """ get_property(property) or get_property(device_regexp,property) """
     property = args[-1]
     devs = self.get_all_devices()
     if len(args)==2: devs = [d for d in devs if fun.matchCl(args[0],d)]
     r = dict((d,list(fun.toList(self.db.get_device_property(d,[property])[property]))) for d in devs)
     return r # if len(r)>1 else r.values()[0]
    def read_Pressure(self, attr):
        self.debug("In " + self.get_name() + "::read_Pressure()")

        #    Add your own code here
        #attr_Pressure_read = self.ChannelValue
        #attr.set_value(attr_Pressure_read)
        state = self.dev_state()
        #av = self.ExternalAttributes[(self.GaugeController+'/'+self.Channel).lower()].read() #Reading from Tau or Cache should be the same
        av = self.Cache[self.ChannelName]
        value, date = av.value, av.time if not hasattr(
            av.time, 'totime') else av.time.totime()
        if not self.LowRange or not fun.matchCl(
                'lo.*', str(self.Cache['ChannelState'].value)):
            quality = fun.matchMap([
                ('ON|MOVING', PyTango.AttrQuality.ATTR_VALID),
                ('ALARM', PyTango.AttrQuality.ATTR_ALARM),
                ('.*', PyTango.AttrQuality.ATTR_INVALID),
            ], state)

        else:  #If Status==LOW, then the pressure value is replaced by the LowRange value (wanted like this for pressure profile visualization)
            value, quality = min([self.LowRange, (value or self.LowRange)
                                  ]), PyTango.AttrQuality.ATTR_ALARM
            date = self.Cache['ChannelState'].time if not hasattr(
                self.Cache['ChannelState'].time,
                'totime') else self.Cache['ChannelState'].time.totime()

        self.debug(
            'read_Pressure(): state is %s, value is %s, quality is %s.' %
            (state, value, quality))
        if 'set_attribute_value_date_quality' in dir(PyTango):
            PyTango.set_attribute_value_date_quality(attr, float(value), date,
                                                     quality)
        else:
            attr.set_value_date_quality(float(value), date, quality)
Beispiel #6
0
 def get_property(self, *args):
     """ get_property(property) or get_property(device_regexp,property) """
     property = args[-1]
     devs = self.get_all_devices()
     if len(args) == 2: devs = [d for d in devs if fun.matchCl(args[0], d)]
     r = dict(
         (d,
          list(
              fun.toList(
                  self.db.get_device_property(d, [property])[property])))
         for d in devs)
     return r  # if len(r)>1 else r.values()[0]
Beispiel #7
0
    def init_device(self):
        try:
            PseudoDev.init_device(self)

            self.ChannelValue = None
            self.ChannelDate = 0

            if not self.check_Properties(['IonPumpController', 'Channel']):
                self.init_error += "IonPumpController and Channel properties are mandatory, edit them and launch Init()"
                self.error(self.init_error)
                self.set_state(PyTango.DevState.UNKNOWN)
                return
            else:
                self.ChannelName = fun.first(
                    c for c in self.Channel
                    if not fun.matchCl('(*state*|*status*)', c))
                targets = ['State', self.ChannelName.split('[')[0]]
                self.debug('Creating cache values for %s:%s' %
                           (self.IonPumpController, targets))
                for attribute in targets:
                    da = PyTango.DeviceAttribute()
                    da.name, da.time, da.value = (
                        self.IonPumpController + '/' +
                        attribute), PyTango.TimeVal.fromtimestamp(0), None
                    self.Cache[attribute] = da
                    self.Errors[attribute] = 0

                self.subscribe_external_attributes(self.IonPumpController,
                                                   targets)

            self.info('Ready to accept request ...')
            self.info('-' * 80)
        except Exception, e:
            self.init_error += traceback.format_exc()
            self.error(self.init_error)
            self.set_state(PyTango.DevState.UNKNOWN)
    def load_attributes(self,
                        servfilter,
                        devfilter,
                        attrfilter,
                        warn=True,
                        exclude=('dserver', 'tango*admin', 'sys*database',
                                 'tmp', 'archiving')):
        tracer('In load_attributes(%s,%s,%s)' %
               (servfilter, devfilter, attrfilter))

        servfilter, devfilter, attrfilter = servfilter.replace(
            ' ',
            '*').strip(), devfilter.replace(' ',
                                            '*'), attrfilter.replace(' ', '*')
        attrfilter = attrfilter or 'state'
        devfilter = devfilter or attrfilter
        archive = self.archivecheck.isChecked()
        all_devs = self.all_devices if not archive else self.archdevs
        all_devs = [
            d for d in all_devs
            if not any(d.startswith(e) for e in exclude) or any(
                d.startswith(e) and fun.matchCl(e, devfilter) for e in exclude)
        ]
        if servfilter.strip('.*'):
            sdevs = map(str.lower,
                        fandango.Astor(servfilter).get_all_devices())
            all_devs = [d for d in all_devs if d in sdevs]
        #print('In load_attributes(%s,%s,%s): Searching through %d %s names'
        #%(servfilter,devfilter,attrfilter,len(all_devs),
        #'server' if servfilter else 'device'))
        if devfilter.strip().strip('.*'):
            devs = [
                d for d in all_devs
                if (fandango.searchCl(devfilter, d, extend=True))
            ]
            print('\tFound %d devs, Checking alias ...' % (len(devs)))
            alias, alias_devs = [], []
            if '&' in devfilter:
                alias = self.all_alias
            else:
                for df in devfilter.split('|'):
                    alias.extend(
                        self.tango.get_device_alias_list('*%s*' % df.strip()))
            if alias:
                print('\t%d alias found' % len(alias))
                alias_devs.extend(self.alias_devs[a] for a in alias
                                  if fun.searchCl(devfilter, a, extend=True))
                print('\t%d alias_devs found' % len(alias_devs))
                #if not self.alias_devs:
                #self.alias_devs =  dict((str(self.tango.get_device_alias(a)).lower(),a) for a in self.all_alias)
                #devs.extend(d for d,a in self.alias_devs.items() if fandango.searchCl(devfilter,a) and (not servfilter or d in all_devs))
                devs.extend(d for d in alias_devs
                            if not servfilter.strip('.*') or d in all_devs)
        else:
            devs = all_devs

        devs = sorted(set(devs))
        self.matching_devs = devs
        print('In load_attributes(%s,%s,%s): %d devices found' %
              (servfilter, devfilter, attrfilter, len(devs)))

        if False and not len(devs) and not archive:
            #Devices do not actually exist, but may exist in archiving ...
            #Option disabled, was mostly useless
            self.archivecheck.setChecked(True)
            return self.load_attributes(servfilter,
                                        devfilter,
                                        attrfilter,
                                        warn=False)

        if len(devs) > self.MAX_DEVICES and warn:
            Qt.QMessageBox.warning(
                self, "Warning",
                "Your search (%s,%s) matches too many devices!!! (%d); please refine your search\n\n%s\n..."
                % (devfilter, attrfilter, len(devs), '\n'.join(devs[:30])))
            return {}
        elif warn and len(devs) > 15:
            r = Qt.QMessageBox.warning(
                self, "Message", "Your search (%s,%s) matches %d devices." %
                (devfilter, attrfilter, len(devs)),
                Qt.QMessageBox.Ok | Qt.QMessageBox.Cancel)
            if r == Qt.QMessageBox.Cancel:
                return {}

        self.matching_attributes = {
        }  #{attribute: (device,alias,attribute,label)}
        failed_devs = []
        for d in sorted(devs):
            try:
                dp = taurus.Device(d)
                if not archive:
                    dp.ping()
                    tcs = [t for t in dp.get_attribute_list()]
                else:
                    tcs = [
                        a.split('/')[-1] for a in self.archattrs
                        if a.startswith(d + '/')
                    ]
                matches = [
                    t for t in tcs
                    if fandango.searchCl(attrfilter, t, extend=True)
                ]
                for t in sorted(tcs):
                    if not self.archivecheck.isChecked() or not matches:
                        label = dp.get_attribute_config(t).label
                    else:
                        label = t
                    if t in matches or fandango.searchCl(
                            attrfilter, label, extend=True):
                        if d in self.alias_devs: alias = self.alias_devs[d]
                        else:
                            try:
                                alias = str(self.tango.get_alias(d))
                            except:
                                alias = ''
                        self.matching_attributes['%s/%s' % (d, t)] = (d, alias,
                                                                      t, label)
                        if warn and len(self.matching_attributes
                                        ) > self.MAX_ATTRIBUTES:
                            Qt.QMessageBox.warning(
                                self, "Warning",
                                "Your search (%s,%s) matches too many attributes!!! (%d); please refine your search\n\n%s\n..."
                                % (devfilter, attrfilter,
                                   len(self.matching_attributes), '\n'.join(
                                       sorted(self.matching_attributes.keys())
                                       [:30])))
                            return {}
            except:
                print('load_attributes(%s,%s,%s => %s) failed!' %
                      (servfilter, devfilter, attrfilter, d))
                failed_devs.append(d)
                if attrfilter in ('state', '', '*', '**'):
                    self.matching_attributes[d + '/state'] = (
                        d, d, 'state', None
                    )  #A None label means device-not-readable

        if warn and len(self.matching_attributes) > 30:
            r = Qt.QMessageBox.warning(
                self, "Message", "(%s) matches %d attributes." %
                (attrfilter, len(self.matching_attributes)),
                Qt.QMessageBox.Ok | Qt.QMessageBox.Cancel)
            if r == Qt.QMessageBox.Cancel:
                return {}
        if not len(self.matching_attributes):
            Qt.QMessageBox.warning(
                self, "Warning",
                "No matching attribute has been found in %s." %
                ('Archiving DB'
                 if archive else 'Tango DB (try Archiving option)'))
        if failed_devs:
            print('\t%d failed devs!!!: %s' % (len(failed_devs), failed_devs))
            if warn:
                Qt.QMessageBox.warning(
                    self, "Warning",
                    "%d devices were not running:\n" % len(failed_devs) +
                    '\n'.join(failed_devs[:10] +
                              (['...'] if len(failed_devs) > 10 else [])))

        tracer('\t%d attributes found' % len(self.matching_attributes))
        return self.matching_attributes
class AttributesPanel(PARENT_KLASS):

    _domains = ['ALL EPS'] + ['LI', 'LT'] + [
        'LT%02d' % i for i in range(1, 3)
    ] + ['SR%02d' % i for i in range(1, 17)]
    _fes = [
        f for f in get_distinct_domains(
            fandango.get_database().get_device_exported('fe*'))
        if fun.matchCl('fe[0-9]', f)
    ]

    LABELS = 'Label/Value Device Attribute Alias Archiving Check'.split()
    SIZES = [500, 150, 90, 90, 120, 40]
    STRETCH = [8, 4, 4, 4, 2, 1]

    def __init__(self, parent=None, devices=None):
        #print '~'*80
        tracer('In AttributesPanel()')
        PARENT_KLASS.__init__(self, parent)
        self.setSizePolicy(
            Qt.QSizePolicy(Qt.QSizePolicy.Ignored, Qt.QSizePolicy.Ignored))
        self.worker = SingletonWorker(parent=self,
                                      cursor=True,
                                      sleep=50.,
                                      start=True)
        #self.worker.log.setLogLevel(self.worker.log.Debug)
        self.filters = ('', '', '')  #server/device/attribute
        self.devices = devices or []
        self.setValues(None)
        self.models = []

        self.current_item = None
        #self.connect(self, Qt.SIGNAL('customContextMenuRequested(const QPoint&)'), self.onContextMenu)
        self.popMenu = Qt.QMenu(self)

        self.actions = {
            'TestDevice':
            self.popMenu.addAction(Qt.QIcon(), "Test Device",
                                   self.onTestDevice),
            'ShowDeviceInfo':
            self.popMenu.addAction(Qt.QIcon(), "Show Device Info",
                                   self.onShowInfo),
            #'ShowDevicePanel': self.popMenu.addAction(Qt.QIcon(),"Show Info",self.onShowPanel),
            'ShowArchivingInfo':
            self.popMenu.addAction(Qt.QIcon(), "Show Archiving Info",
                                   self.onShowArchivingModes),
            'AddToTrend':
            self.popMenu.addAction(Qt.QIcon(), "Add attribute to Trend",
                                   self.addAttributeToTrend),
            'AddSelected':
            self.popMenu.addAction(Qt.QIcon(),
                                   "Add selected attributes to Trend",
                                   self.addSelectedToTrend),
            'CheckAll':
            self.popMenu.addAction(Qt.QIcon(), "Select all attributes",
                                   self.checkAll),
            'UncheckAll':
            self.popMenu.addAction(Qt.QIcon(), "Deselect all attributes",
                                   self.uncheckAll),

            #'Test Device': self.popMenu.addAction(Qt.QIcon(),"Test Device",self.onTestDevice)
        }
        #if hasattr(self,'setFrameStyle'):
        #self.setFrameStyle(self.Box)
        try:
            import PyTangoArchiving
            self.reader = PyTangoArchiving.Reader('*')
            #self.hreader = self.reader.configs['hdb']
            #self.treader = self.reader.configs.get('tdb',self.hreader)
        except:
            traceback.print_exc()

    def __del__(self):
        print 'AttributesPanel.__del__'
        QGridTable.__del__(self)

    def setItem(self, x, y, item, spanx=1, spany=1, align=None, model=None):
        align = align or Qt.Qt.AlignLeft
        try:
            if model:
                item._model = model
        except:
            pass
        self.layout().addWidget(item, x, y, spany, spanx, Qt.Qt.AlignCenter)
        if item not in self._widgets: self._widgets.append(item)

    def mousePressEvent(self, event):
        point = event.pos()
        widget = Qt.QApplication.instance().widgetAt(self.mapToGlobal(point))
        if hasattr(widget, '_model'):
            print('onMouseEvent(%s)' %
                  (getattr(widget, 'text', lambda: widget)()))
            self.current_item = widget
            if event.button() == Qt.Qt.RightButton:
                self.onContextMenu(point)
        getattr(super(type(self), self), 'mousePressEvent',
                lambda e: None)(event)

    def onContextMenu(self, point):
        print('onContextMenu()')
        try:
            self.actions['TestDevice'].setEnabled(
                '/' in self.current_item._model)
            self.actions['ShowDeviceInfo'].setEnabled(
                '/' in self.current_item._model)
            self.actions['ShowArchivingInfo'].setEnabled(
                '/' in self.current_item._model)
            self.actions['AddToTrend'].setEnabled(hasattr(self, 'trend'))
            self.actions['AddSelected'].setEnabled(hasattr(self, 'trend'))
            self.popMenu.exec_(self.mapToGlobal(point))
        except:
            traceback.print_exc()

    def getCurrentModel(self):
        return '/'.join(str(self.current_item._model).split('/')[-4:])

    def getCurrentDevice(self):
        return str(self.current_item._model.rsplit('/', 1)[0])

    def onTestDevice(self, device=None):
        from PyTangoArchiving.widget.panel import showTestDevice
        showTestDevice(device or self.getCurrentDevice())

    def onShowInfo(self, device=None):
        from PyTangoArchiving.widget.panel import showDeviceInfo
        showDeviceInfo(device=device or self.getCurrentDevice(), parent=self)

    def onShowArchivingModes(self, model=None):
        try:
            from PyTangoArchiving.widget.panel import showArchivingModes
            model = model or self.getCurrentModel()
            showArchivingModes(model, parent=self)
        except:
            Qt.QMessageBox.warning(self, "ups!", traceback.format_exc())

    def addAttributeToTrend(self, model=None):
        try:
            model = model or self.getCurrentModel()
            self.trend.addModels([model])
        except:
            Qt.QMessageBox.warning(self, "ups!", traceback.format_exc())

    def addSelectedToTrend(self):
        try:
            y = self.columnCount() - 1
            models = []
            for x in range(self.rowCount()):
                item = self.itemAt(x, y).widget()
                m = getattr(item, '_model', '')
                if m and item.isChecked():
                    models.append(m)
            if len(models) > 20:
                Qt.QMessageBox.warning(
                    self, "warning",
                    "To avoid performance issues, dynamic scale will be disabled"
                )
                self.trend.setXDynScale(False)
            self.trend.addModels(models)
        except:
            Qt.QMessageBox.warning(self, "ups!", traceback.format_exc())

    def checkAll(self):
        y = self.columnCount() - 1
        for x in range(self.rowCount()):
            self.itemAt(x, y).widget().setChecked(True)

    def uncheckAll(self):
        y = self.columnCount() - 1
        for x in range(self.rowCount()):
            self.itemAt(x, y).widget().setChecked(False)

    def setValues(self, values, filters=None):
        """ filters will be a tuple containing several regular expressions to match """
        #print('In AttributesPanel.setValues([%s])'%len(values or []))
        if values is None:
            self.generateTable([])
        elif True:  #filters is None:
            self.generateTable(values)
        #print 'In AttributesPanel.setValues(...): done'
        return

    def generateTable(self, values):

        #thermocouples = thermocouples if thermocouples is not None else self.thermocouples
        self.setRowCount(len(values))
        self.setColumnCount(5)
        #self.vheaders = []
        self.offset = 0
        self.widgetbuffer = []

        for i, tc in enumerate(sorted(values)):
            #print 'setTableRow(%s,%s)'%(i,tc)
            model, device, attribute, alias, archived, ok = tc
            model, device, attribute, alias = map(
                str.upper, (model, device, attribute, alias))

            #self.vheaders.append(model)
            def ITEM(m, model='', size=0):
                q = fandango.qt.Draggable(Qt.QLabel)(m)
                if size is not 0:
                    q.setMinimumWidth(size)  #(.7*950/5.)
                q._model = model
                q._archived = archived
                q.setDragEventCallback(lambda s=q: s._model)
                return q

            ###################################################################
            qf = Qt.QFrame()
            qf.setLayout(Qt.QGridLayout())
            qf.setMinimumWidth(self.SIZES[0])
            qf.setSizePolicy(Qt.QSizePolicy.Expanding, Qt.QSizePolicy.Fixed)
            #Order changed, it is not clear if it has to be done before or after adding TaurusValue selfect
            self.setCellWidget(i + self.offset, 0, qf)

            #print('Adding item: %s, %s, %s, %s, %s' % (model,device,attribute,alias,archived))
            if ok:
                tv = TaurusValue()  #TaurusValueLabel()
                qf.layout().addWidget(tv, 0, 0)
                tv.setParent(qf)
            else:
                self.setItem(i + self.offset, 0, ITEM(model, model))

            devlabel = ITEM(device, model, self.SIZES[1])
            self.setItem(i + self.offset, 1, devlabel)
            self.setItem(i + self.offset, 2,
                         ITEM(attribute, model, self.SIZES[2]))
            self.setItem(i + self.offset, 3, ITEM(alias, model, self.SIZES[3]))

            from PyTangoArchiving.widget.panel import showArchivingModes, show_history
            if archived:
                active = self.reader.is_attribute_archived(model, active=True)
                txt = '/'.join(a.upper() if a in active else a
                               for a in archived)
            else:
                txt = '...'
            q = Qt.QPushButton(txt)
            q.setFixedWidth(self.SIZES[-2])
            q.setToolTip("""%s<br><br><pre>
              'HDB' : Archived and updated, push to export values
              'hdb' : Archiving stopped, push to export values
              '...' : Not archived
              </pre>""" % txt)
            self.connect(q,
                         Qt.SIGNAL("pressed ()"),
                         lambda a=self.reader.get_attribute_alias(
                             model), o=q: setattr(q, 'w', show_history(a))
                         )  #showArchivingModes(a,parent=self)))
            self.setItem(i + self.offset, 4, q)

            qc = Qt.QCheckBox()
            qc.setFixedWidth(self.SIZES[-1])
            self.setItem(i + self.offset, 5, qc, 1, 1, Qt.Qt.AlignCenter,
                         model)

            if ok:
                #print('Setting Model %s'%model)
                #ADDING WIDGETS IN BACKGROUND DIDN'T WORKED, I JUST CAN SET MODELS FROM THE WORKER
                try:
                    if self.worker:
                        self.worker.put([(lambda w=tv, m=model: w.setModel(m))
                                         ])
                        #print 'worker,put,%s'%str(model)
                    else:
                        tv.setModel(model)
                except:
                    print traceback.format_exc()
                self.models.append(tv)

            #self.widgetbuffer.extend([qf,self.itemAt(i+self.offset,1),self.itemAt(i+self.offset,2),self.itemAt(i+self.offset,3),self.itemAt(i+self.offset,4)])
            fandango.threads.Event().wait(.02)

        if len(values):

            def setup(o=self):
                [o.setRowHeight(i, 20) for i in range(o.rowCount())]
                #o.setColumnWidth(0,350)
                o.update()
                o.repaint()
                #print o.rowCount()
                o.show()

            setup(self)

        if self.worker:
            print('%s.next()' % (self.worker))
            self.worker.next()

        #threading.Event().wait(10.)
        tracer('Out of generateTable()')

    def clear(self):
        try:
            #print('In AttributesPanel.clear()')
            for m in self.models:
                m.setModel(None)
            self.models = []
            self.setValues(None)

            #QGridTable.clear(self)
            def deleteItems(layout):
                if layout is not None:
                    while layout.count():
                        item = layout.takeAt(0)
                        widget = item.widget()
                        if widget is not None:
                            widget.deleteLater()
                        else:
                            deleteItems(item.layout())

            deleteItems(self.layout())
            #l = self.layout()
            #l.deleteLater()
            #self.setLayout(Qt.QGridLayout())
        except:
            traceback.print_exc()
Beispiel #10
0
def main_report():
    
    args = fandango.linos.sysargs_to_dict(['output','input','domains'])
    assert args, __doc__
    print 'archiving_report.py arguments are:\n\t%s'%args
        
    filename = args.get('output','/tmp/archiving_report.html')
    inputs = args.get('input','')
    domreg = fun.toRegexp(args.get('domains',''))
    restart = args.get('restart','')
    cycles, cycle_time = 1,6*3600
    if restart is True: restart = '.*'
    
    if 'filters' in args:
        if ':' not in args['filters']:
            argfilters = {'name':args['filters']}
        else:
            argfilters = dict((f.split(':') for f in args['filters'].split(',') if ':' in f))
    else: argfilters = {}
    
    if 'exclude' in args:
        if ':' not in args['exclude']:
            argexclude = {'name':args['exclude']}
        else:
            argexclude = dict((f.split(':') for f in args['exclude'].split(',') if ':' in f))
    else: argexclude = {}
    
    if 'type' in argexclude: 
        argexclude['type'] = '(stop|%s)'%argexclude['type']
    else: 
        argexclude['type'] = 'stop'
    
    if 'cycles' in args:
        cycles = int(args['cycles'])
        
    ######################################################################################################3
    results = {}
    summary = {'hdb':{},'tdb':{}}
    failed = {}
    polizons = {}
    
    print 'The archiving check will be performed %s.' % (cycles<0 and 'forever' or '%s times'%cycles)
    
    while cycles!=0:
        tstart = time.time()
        
        # Getting the CSV input files
        if domreg:
            configs = []
        elif inputs:
            if '/' in inputs: 
                configs = sorted(files.GetConfigFiles(*inputs.rsplit('/',1)))
            else:
                configs = sorted(files.GetConfigFiles(mask=inputs))
        else:
            configs = sorted(files.GetConfigFiles())
        
        print '#'*80
        print '%s, In archiving_report.py: reading %s, generating %s' % (time.ctime(),configs or domreg,filename)
        
        for schema in ('hdb','tdb'):
            print 'Checking %s configurations' % schema.upper()
        
            api = PyTangoArchiving.ArchivingAPI(schema,load=True)    
            api.clean_attribute_modes() #Cleanup of amt table in database
            
            active = [a for a in api if api[a].archiver]
            if not active: #No attributes being archived in the database
                continue
            dedicated = list(set([a for a in active if api[a].dedicated]))
            print 'There are %d dedicated attributes' % (len(dedicated))
            
            #Initializing variables
            results[schema] = {}
            summary[schema] = {'active':len(active),'dedicated':len(dedicated)}
            polizons[schema] = {}
            up,down,idle,archivers = [],[],[],api.check_archivers()
            for k,v in archivers.items():
                {True:up,False:down,None:idle}[v].append(k)
            summary[schema].update({'up':up,'down':down,'idle':idle,'archivers':archivers.keys()})
            
            ############################################################################
            #Checking Attribute Configuration CSV files
            ############################################################################
            if configs:
                polizons[schema] = active[:]
                for c in configs:
                    filters = dict(argfilters.items())
                    exclude = dict(argexclude.items())
                    try:
                        print '%s: Checking file %s' % (schema.upper(),c.split('/')[-1])
                        print 'filters: %s ; exclude: %s' % (filters,exclude)
                        
                        check = files.CheckArchivingConfiguration(
                            c,
                            api=api,schema=schema,
                            restart=bool(restart and fun.matchCl(restart,c)), #Restart has to be done here as it is where the modes configuration is available
                            filters=filters,exclude=exclude)
                            
                        if not any(check.values()): #No values returned?
                            continue    
                        
                        #Creating a summary of check results:
                        results[schema][c] = check
                        #check['rate'] = (float(len(check['ok']))/len(check['all'])) if (check['ok'] and check['all']) else 0.                    
                        print ('\n'.join('%s: %s'%(k.upper(),summarize(v)) for k,v in results[schema][c].items() if v))
                        [polizons[schema].remove(a) for a in check['all'] if a in polizons[schema]] #Checking how many attributes are 'alien' to specs
                        flie = open('/tmp/%s.%s.pck'%(c.split('/')[-1].rsplit('.',1)[0],schema),'w')
                        pickle.dump((c,check),flie)
                        flie.close()
                    except Exception,e:
                        if c in results[schema]: 
                            #results[schema].pop(c)
                            results[schema][c] = dict((k,0) for k in 'all  rate  ok  diff  late  hung  lost  retried  unavailable  missing  triable  dedicated  polizon'.split())
                        failed['%s:%s'%(schema,c)] = traceback.format_exc()
                        print failed['%s:%s'%(schema,c)]
                                        
            ############################################################################
            #Doing a generic check of the archiving, ignoring configurations
            ############################################################################
            elif domreg:
                domains = defaultdict(list)
                [domains[a.split('/')[0].lower()].append(a) for a in api if fun.matchCl(domreg,a.split('/')[0]) and api[a].archiver]
                
                print ('Checking %s attributes by domain(%s): %s'%(schema,domreg,[(k,len(v)) for k,v in domains.items()]))
                for d,attributes in domains.items():
                    print '%d attributes in domain %s' % (len(attributes),d)
                    ok,lost,goods,bads,retried = [],[],[],[],[]
                    try:
                        [(goods if utils.check_attribute(a,readable=True) else bads).append(a) for a in attributes]
                        if not goods:
                            continue
                        if goods: 
                            [(lost if v else ok).append(a) for k,v in api.check_attributes_errors(goods,hours=1,lazy=True).items()]
                        print '%d attributes on time'%len(ok)
                        if restart and lost:
                            for att in lost:
                                modes = api[att].modes
                                if not modes: continue
                                print 'Restarting archiving for %s' % att
                                if api.start_archiving([att],modes,load=False):
                                    retried.append(att)
                        check = {'all':attributes,'ok':ok,'lost':lost,'retried':retried,'unavailable':bads}
                        results[schema][d] = check
                        results[schema][d]['rate'] = (float(len(ok))/len(attributes)) if goods else 0
                        print ('\n'.join('%s: %s'%(k.upper(),summarize(v)) for k,v in results[schema][d].items() if v))
                    except Exception,e:
                        if d in results[schema]: results[schema].pop(d)
                        failed['%s:%s'%(schema,d)] = traceback.format_exc()
                        print failed['%s:%s'%(schema,d)]
Beispiel #11
0
 def set_property(self,*args):
     """ set_property(property,value) or set_property(device_regexp,property,value) """
     property,value = args[-2:]
     devs = self.get_all_devices()
     if len(args)==3: devs = [d for d in devs if fun.matchCl(args[0],d)]
     return [(d,self.db.put_device_property(d,{property:value}))[0] for d in devs]
    def CheckAll(self, argin=None):
        print "In ", self.get_name(), "::CheckAll()"
        #    Add your own code here
        
        confis,domreg,argfilters,argexclude = self.configs,self.domreg,self.argfilters,self.argexclude
        restart = self.Restart
        results = {}
        summary = {}
        failed = {}
        polizons = {}

        for schema in ('hdb','tdb'):
            print 'Checking %s configurations' % schema.upper()
        
            api = PyTangoArchiving.ArchivingAPI(schema,load=True)    
            active = [a for a in api if api[a].archiver]
            if not active: #No attributes being archived in the database
                continue
            dedicated = list(set([a for a in active if api[a].dedicated]))
            print 'There are %d dedicated attributes' % (len(dedicated))
            
            #Initializing variables
            results[schema] = {}
            summary[schema] = {'active':len(active),'dedicated':len(dedicated)}
            polizons[schema] = {}
            up,down,idle,archivers = [],[],[],api.check_archivers()
            for k,v in archivers.items():
                {True:up,False:down,None:idle}[v].append(k)
            summary[schema].update({'up':up,'down':down,'idle':idle,'archivers':archivers.keys()})
            
            ############################################################################
            #Checking Attribute Configuration CSV files
            ############################################################################
            if configs:
                polizons[schema] = active[:]
                for c in configs:
                    filters = dict(argfilters.items())
                    exclude = dict(argexclude.items())
                    try:
                        print '%s: Checking file %s' % (schema.upper(),c.split('/')[-1])
                        print 'filters: %s ; exclude: %s' % (filters,exclude)
                        
                        check = files.CheckArchivingConfiguration(
                            c,
                            api=api,schema=schema,
                            restart=bool(restart and fun.matchCl(restart,c)), #Restart has to be done here as it is where the modes configuration is available
                            filters=filters,exclude=exclude)
                            
                        if not any(check.values()): #No values returned?
                            continue    
                        
                        #Creating a summary of check results:
                        results[schema][c] = check
                        #check['rate'] = (float(len(check['ok']))/len(check['all'])) if (check['ok'] and check['all']) else 0.                    
                        print ('\n'.join('%s: %s'%(k.upper(),self.summarize(v)) for k,v in results[schema][c].items() if v))
                        [polizons[schema].remove(a.lower()) for a in check['all'] if a.lower() in polizons[schema]] #Checking how many attributes are 'alien' to specs
                        flie = open('/tmp/%s.%s.pck'%(c.split('/')[-1].rsplit('.',1)[0],schema),'w')
                        pickle.dump((c,check),flie)
                        flie.close()
                    except Exception,e:
                        if c in results[schema]: results[schema].pop(c)
                        failed['%s:%s'%(schema,c)] = traceback.format_exc()
                        print failed['%s:%s'%(schema,c)]
                                        
            ############################################################################
            #Doing a generic check of the archiving, ignoring configurations
            ############################################################################
            elif domreg:
                domains = defaultdict(list)
                [domains[a.split('/')[0].lower()].append(a) for a in api if fun.matchCl(domreg,a.split('/')[0]) and api[a].archiver]
                
                print ('Checking %s attributes by domain(%s): %s'%(schema,domreg,[(k,len(v)) for k,v in domains.items()]))
                for d,attributes in domains.items():
                    print '%d attributes in domain %s' % (len(attributes),d)
                    ok,lost,goods,bads,retried = [],[],[],[],[]
                    try:            
                        [(goods if utils.check_attribute(a,readable=True) else bads).append(a) for a in attributes]
                        if not goods:
                            continue
                        if goods: 
                            [(lost if v else ok).append(a) for k,v in api.check_attributes_errors(goods,hours=1,lazy=True).items()]
                        print '%d attributes on time'%len(ok)
                        if restart and lost:
                            for att in lost:
                                modes = api[att].modes
                                if not modes: continue
                                print 'Restarting archiving for %s' % att
                                if api.start_archiving([att],modes,load=False):
                                    retried.append(att)                  
                            
                        check = {'all':attributes,'ok':ok,'lost':lost,'retried':retried,'unavailable':bads} 
                        results[schema][d] = check
                        results[schema][d]['rate'] = (float(len(ok))/len(attributes)) if goods else 0
                        print ('\n'.join('%s: %s'%(k.upper(),summarize(v)) for k,v in results[schema][d].items() if v))
                    except Exception,e:
                        if d in results[schema]: results[schema].pop(d)
                        failed['%s:%s'%(schema,d)] = traceback.format_exc()
                        print failed['%s:%s'%(schema,d)]