📄 presenter.py
字号:
def timeLast(self,timeframe='day', value=1): """Sets the timeframe of the presentation Currently valid timeframes: year,month,week,hour,day""" self.toTime = 'now' if timeframe == 'year': self.fromTime = 'now-%sY' % value self._timeFrame = 'year' elif timeframe == 'month': self.fromTime = 'now-%sm' % value self._timeFrame = 'month' elif timeframe == 'week': self.fromTime = 'now-%sw' % value self._timeFrame = 'week' elif timeframe == 'day': self.fromTime = 'now-%sd' % value self._timeFrame = 'day' elif timeframe == 'hour': self.fromTime = 'now-%sh' % value self._timeFrame = 'hour' else: self.fromTime = 'now-%sd' % value self._timeFrame = 'day' def removeAllDs(self): """Removes all datasources from the presentation object""" self.datasources = [] def removeDs(self,ds_id): """Removes the datasource specified by rrd_datasourceid""" ds = datasource(ds_id) self.datasources.remove(ds) def setYAxis(self, y): self.yaxis = y def graphUrl(self): """Generates an url to a image representing the current presentation""" url = 'graph.py' index = 0 params = ['-w' + str(self.graphWidth), '-h' + str(self.graphHeight), '-s' + self.fromTime, '-e' + self.toTime, '--no-minor', ] try: params.append('-t %s' % self.title) except NameError: pass if self.yaxis: params.append('--rigid') # Rigid boundry mode params.append('--upper-limit') params.append(str(self.yaxis)) # allows 'zooming' units = [] for ds in self.datasources: color_max = {0:'#6b69e1', 1:'#007F00', 2:'#7F0000', 3:'#007F7F', 4:'#7F7F00' ,5:'#7F007F' ,6:'#000022' ,7:'#002200' ,8:'#220000'} color ={0:"#00cc00", 1:"#0000ff", 2:"#ff0000", 3:"#00ffff", 4:"#ff00ff", 5:"#ffff00", 6:"#cc0000", 7:"#0000cc", 8:"#0080C0", 9:"#8080C0", 10:"#FF0080", 11:"#800080", 12:"#0000A0", 13:"#408080", 14:"#808000", 15:"#000000", 16:"#00FF00", 17:"#0080FF", 18:"#FF8000", 19:"#800000", 20:"#FB31FB"} #color = {0:'#0F0CFF', # 1:'#00FF00', # 2:'#FF0000', # 3:'#00FFFF', # 4:'#FFFF00', # 5:'#FF00FF', # 6:'#000044', # 7:'#004400', # 8:'#440000'} rrd_variable = 'avg'+str(index) rrd_max_variable = 'max'+str(index) rrd_filename = ds.fullPath() rrd_datasourcename = ds.name linetype = ds.linetype linetype_max = 'LINE1' legend = ds.legend if ds.units and ds.units.count("%"): # limit to [0,100] params += ['--upper-limit', '100', '--lower-limit', '0'] params += ['DEF:'+rrd_variable+'='+rrd_filename+':'+rrd_datasourcename+':AVERAGE'] # Define virtuals to possibly do some percentage magical # flipping virtual = 'CDEF:v_'+rrd_variable+'=' if ds.units and ds.units.startswith('-'): # availability is flipped up-side down, revert # and show as percentage virtual += '1,%s,-' % rrd_variable units.append(ds.units[1:]) else: if ds.units: units.append(ds.units) virtual += rrd_variable if ds.units and ds.units.endswith("%"): # percent, check if we have to do some scaling... scalingfactor = ds.units[:-1] # strip % if scalingfactor.startswith('-'): scalingfactor = scalingfactor[1:] try: int(scalingfactor) virtual += ',100,*' except ValueError: pass params += [virtual] params += [linetype+':v_'+rrd_variable+color[index % len(color)]+':'+''+legend+''] a = rrdtool.info(rrd_filename) # HVA I HELVETE SKJER HER!?!?!??!?! if self.showmax and 'MAX' in [a.get('rra')[i].get('cf') for i in range(len(a.get('rra')))] : legend += ' - MAX' params += ['DEF:'+rrd_max_variable+'='+rrd_filename+':'+rrd_datasourcename+':MAX'] virtual = 'CDEF:v_'+rrd_max_variable+'=' if ds.units and ds.units.startswith('-'): # begins with - # availability is flipped up-side down, revert # and show as percentage virtual += '1,%s,-' % rrd_max_variable else: virtual += rrd_max_variable if ds.units and ds.units.endswith("%"): # percent, check if we have to do some scaling... scalingfactor = ds.units[:-1] # strip % if scalingfactor.startswith('-'): scalingfactor = scalingfactor[1:] try: int(scalingfactor) virtual += ',100,*' except ValueError: pass params += [virtual] params += [linetype_max+':v_'+rrd_max_variable+color_max[index]+':'+''+legend+''] index += 1 if index == 0: params += ["COMMENT:''"] if units: params.insert(0,'-v') # Ok, join together with / if there is several # different units def uniq(list): a = {} return [x for x in list if not a.has_key(x) and a.setdefault(x,True)] units = uniq(units) unitStrings = [] for unit in units: unitStrings.append(unitmap.get(unit, unit)) params.insert(1, '/'.join(unitStrings)) id = self.genImage(*params) #raise str(params) #return '/browse/rrd/graph?id=%s' % id return urlbuilder.createUrl(subsystem='rrd', division='graph', id=id) def genImage (self,*rrd_params): conf = nav.config.readConfig(configfile) id = str(random.randint(1,10**9)) imagefilename = conf['fileprefix'] + id + conf['filesuffix'] rrd_params = (imagefilename,) + rrd_params try: size = rrdtool.graph(*rrd_params) except rrdtool.error, err: pass deadline = 60*10 for i in glob.glob('/tmp/rrd*'): if os.path.getmtime(i) < (time.time() - deadline): try: os.unlink(i) except: pass return idclass page: def __init__(self, repr=None): """ repr must be a dict as created by serialize() """ self.presentations = [] self.timeframe = "day" self.name = '' self.timeframeIndex = 1 if repr: self.deSerialize(repr) def deSerialize(self, repr): if type(repr) != dict: return presentations = repr['presentations'] self.timeframe = repr['timeframe'] for pres in presentations: newPres = presentation(tf=self.timeframe) for ds in pres['datasources']: newPres.addDs(ds) self.presentations.append(newPres) def serialize(self): repr = {} repr['presentations'] = [] for i in self.presentations: repr['presentations'].append(i.serialize()) repr['timeframe'] = self.timeframe repr['name'] = self.name return repr def __repr__(self): return self.name def __str__(self): return self.name def graph(req,id): conf = nav.config.readConfig(configfile) filename = conf['fileprefix'] + id + conf['filesuffix'] req.content_type = 'image/gif' req.send_http_header() f = open(filename) req.write(f.read()) f.close()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -