class ArchivingDevice(TaurusDevice): """Archiving device object. """ _scheme = 'archiving' _description = "A Archiving Device" def __init__(self, name, **kwargs): TaurusDevice.__init__(self, name, **kwargs) self._validator = ArchivingDeviceNameValidator() g = self._validator.getUriGroups(self.getFullName()) db = g.get('devname') tango_host = '%s:%s' % (g.get('host'), g.get('port')) self._reader = Reader(db, tango_host=tango_host, logger=self) def getReader(self): """Get the PyTangoArchiving Reader :return PyTangoArchiving.Reader """ return self._reader def getAttribute(self, attrname): """Returns the attribute object given its name""" attrname = "%s/%s" % (self.getFullName(), attrname) return self.factory().getAttribute(attrname) def add_attribute(self, attribute, device, period): pass def getArchivedAttributes(self, active=False): """getArchivedAttributes show the names of the archived attributes :return: A list with the names of the current archived attributes. """ return self._reader.get_attributes(active)
def tdb_to_hdb(attribute,start=0,stop=fun.END_OF_TIME,modes={},delete=False): """ This method allows to copy an attribute from TDB to HDB, inserting the contents of the current TDB buffer into the HDB tables. @param start/stop allow to limit the dates for insertion @param delete will remove all existing values in HDB for the given interval """ from PyTangoArchiving import Reader hdb,tdb = Reader('hdb'),Reader('tdb') assert attribute in tdb.get_attributes() values = tdb.get_attribute_values(attribute,start or time.time()-tdb.RetentionPeriod,stop) if attribute not in hdb.get_attributes(): from PyTangoArchiving import ArchivingAPI api = ArchivingAPI('hdb') api.start_archiving(*((attribute,modes) if modes else (attribute,))) api.load_attribute_descriptions() db = hdb.get_database() table = db.get_table_name(db.get_attribute_ID(attribute)) import_into_db(db,table,values,delete)
def show_history(self, attribute): TABS = [] print 'getting archiving readers ...' from PyTangoArchiving import Reader hdb = Reader(db='hdb', schema='hdb') tdb = Reader(db='tdb', schema='tdb') tformat = '%Y-%m-%d %H:%M:%S' str2epoch = lambda s: time.mktime(time.strptime(s, tformat)) epoch2str = lambda f: time.strftime(tformat, time.localtime(f)) attribute = attribute.lower() if attribute in hdb.get_attributes( ) or attribute in tdb.get_attributes(): print '%s is being archived' % attribute di = Qt.QDialog() wi = di #QtGui.QWidget(di) wi.setLayout(Qt.QGridLayout()) begin = Qt.QLineEdit() begin.setText(epoch2str(time.time() - 3600)) end = Qt.QLineEdit() end.setText(epoch2str(time.time())) wi.setWindowTitle('Show Archiving') wi.layout().addWidget( Qt.QLabel('Enter Begin and End dates in %s format' % tformat), 0, 0, 1, 2) wi.layout().addWidget(Qt.QLabel('Begin:'), 1, 0, 1, 1) wi.layout().addWidget(Qt.QLabel('End:'), 2, 0, 1, 1) wi.layout().addWidget(begin, 1, 1, 1, 1) wi.layout().addWidget(end, 2, 1, 1, 1) buttons = Qt.QDialogButtonBox(Qt.QDialogButtonBox.Ok | Qt.QDialogButtonBox.Cancel) wi.connect(buttons, Qt.SIGNAL('accepted()'), wi.accept) wi.connect(buttons, Qt.SIGNAL('rejected()'), wi.reject) wi.layout().addWidget(buttons, 3, 0, 1, 2) def check_values(): di.exec_() if di.result(): print 'checking result ...' start, stop = str(begin.text()), str(end.text()) if not all( re.match( '[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+', str(s).strip()) for s in (start, stop)): print 'dates are wrong ...' Qt.QMessageBox.warning( None, 'Show archiving', 'Dates seem not in %s format' % (tformat), Qt.QMessageBox.Ok) return check_values() else: print 'getting values ...' reader = tdb if str2epoch(start) > ( time.time() - 5 * 24 * 3600. ) and attribute in tdb.get_attributes() else hdb print 'using %s reader' % reader.schema values = reader.get_attribute_values( attribute, str2epoch(start), str2epoch(stop)) if not len( values ) and reader is tdb and attribute in hdb.get_attributes( ): print 'tdb failed, retrying with hdb' values = hdb.get_attribute_values( attribute, str2epoch(start), str2epoch(stop)) print 'drawing table from %d values' % len(values) tab = Qt.QTableWidget() tab.setWindowTitle('%s: %s to %s' % (attribute, start, stop)) tab.setRowCount(len(values)) tab.setColumnCount(2) tab.setHorizontalHeaderLabels(['TIME', 'VALUE']) for i, tup in enumerate(values): date, value = tup tab.setItem(i, 0, Qt.QTableWidgetItem(epoch2str(date))) tab.setItem(i, 1, Qt.QTableWidgetItem(str(value))) tab.show() tab.resizeColumnsToContents() tab.horizontalHeader().setStretchLastSection(True) TABS.append(tab) tab.connect(tab, Qt.SIGNAL('close()'), lambda o=tab: TABS.remove(o)) print 'show_history done ...' return tab else: print 'dialog closed' return None print 'asking for dates ...' return check_values() else: Qt.QMessageBox.warning( None, 'Show archiving', 'Attribute %s is not being archived' % attribute, Qt.QMessageBox.Ok)
def show_history(self, attribute): TABS=[] print 'getting archiving readers ...' from PyTangoArchiving import Reader hdb = Reader(db='hdb',schema='hdb') tdb = Reader(db='tdb',schema='tdb') tformat = '%Y-%m-%d %H:%M:%S' str2epoch = lambda s: time.mktime(time.strptime(s,tformat)) epoch2str = lambda f: time.strftime(tformat,time.localtime(f)) attribute = attribute.lower() if attribute in hdb.get_attributes() or attribute in tdb.get_attributes(): print '%s is being archived' % attribute di = Qt.QDialog() wi = di #QtGui.QWidget(di) wi.setLayout(Qt.QGridLayout()) begin = Qt.QLineEdit() begin.setText(epoch2str(time.time()-3600)) end = Qt.QLineEdit() end.setText(epoch2str(time.time())) wi.setWindowTitle('Show Archiving') wi.layout().addWidget(Qt.QLabel('Enter Begin and End dates in %s format'%tformat),0,0,1,2) wi.layout().addWidget(Qt.QLabel('Begin:'),1,0,1,1) wi.layout().addWidget(Qt.QLabel('End:'),2,0,1,1) wi.layout().addWidget(begin,1,1,1,1) wi.layout().addWidget(end,2,1,1,1) buttons = Qt.QDialogButtonBox(Qt.QDialogButtonBox.Ok|Qt.QDialogButtonBox.Cancel) wi.connect(buttons,Qt.SIGNAL('accepted()'),wi.accept) wi.connect(buttons,Qt.SIGNAL('rejected()'),wi.reject) wi.layout().addWidget(buttons,3,0,1,2) def check_values(): di.exec_() if di.result(): print 'checking result ...' start,stop = str(begin.text()),str(end.text()) if not all(re.match('[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+',str(s).strip()) for s in (start,stop)): print 'dates are wrong ...' Qt.QMessageBox.warning(None,'Show archiving', 'Dates seem not in %s format'%(tformat), Qt.QMessageBox.Ok) return check_values() else: print 'getting values ...' reader = tdb if str2epoch(start)>(time.time()-5*24*3600.) and attribute in tdb.get_attributes() else hdb print 'using %s reader' % reader.schema values = reader.get_attribute_values(attribute,str2epoch(start),str2epoch(stop)) if not len(values) and reader is tdb and attribute in hdb.get_attributes(): print 'tdb failed, retrying with hdb' values = hdb.get_attribute_values(attribute,str2epoch(start),str2epoch(stop)) print 'drawing table from %d values' % len(values) tab = Qt.QTableWidget() tab.setWindowTitle('%s: %s to %s' % (attribute,start,stop)) tab.setRowCount(len(values)) tab.setColumnCount(2) tab.setHorizontalHeaderLabels(['TIME','VALUE']) for i,tup in enumerate(values): date,value = tup tab.setItem(i,0,Qt.QTableWidgetItem(epoch2str(date))) tab.setItem(i,1,Qt.QTableWidgetItem(str(value))) tab.show() tab.resizeColumnsToContents() tab.horizontalHeader().setStretchLastSection(True) TABS.append(tab) tab.connect(tab,Qt.SIGNAL('close()'),lambda o=tab: TABS.remove(o)) print 'show_history done ...' return tab else: print 'dialog closed' return None print 'asking for dates ...' return check_values() else: Qt.QMessageBox.warning(None,'Show archiving', 'Attribute %s is not being archived'%attribute, Qt.QMessageBox.Ok)