Package Products :: Package ZenModel :: Module GraphReportElement |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.GraphReportElement

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2007, Zenoss Inc.
      5  #
      6  # This program is free software; you can redistribute it and/or modify it
      7  # under the terms of the GNU General Public License version 2 as published by
      8  # the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13
     14  from Globals import InitializeClass
     15  from AccessControl import ClassSecurityInfo
     16  from ZenModelRM import ZenModelRM
     17  from Products.ZenRelations.RelSchema import *
     18  from Products.ZenUtils.ZenTales import talesCompile, getEngine
     19
     20
     21 -def manage_addGraphReportElement(context, id, REQUEST = None):
     22      """make a GraphReportElement
     23      """
     24      element = GraphReportElement(id)
     25      context._setObject(element.id, element)
     26      if REQUEST is not None:
     27          REQUEST['RESPONSE'].redirect(context.absolute_url()+'/manage_main')
     28
     29
     30 -class GraphReportElement(ZenModelRM):
     31
     32      meta_type = 'GraphReportElement'
     33
     34      deviceId = ''
     35      componentPath = ''
     36      graphId = ''
     37      sequence = 0
     38      summary = ('Device: ${dev/id}\n'
     39                  '&nbsp;&nbsp;\n'
     40                  'Component: ${comp/id}\n'
     41                  '&nbsp;&nbsp;\n'
     42                  'Graph: ${graph/id}\n')
     43      comments = ('Device: ${dev/id}<br />\n'
     44                  'Component: ${comp/id}<br />\n'
     45                  '${graph/id}')
     46
     47      _properties = ZenModelRM._properties + (
     48          {'id':'deviceId', 'type':'string', 'mode':'w'},
     49          {'id':'componentPath', 'type':'string', 'mode':'w'},
     50          {'id':'graphId', 'type':'string', 'mode':'w'},
     51          {'id':'sequence', 'type':'int', 'mode':'w'},
     52          {'id':'summary', 'type':'text', 'mode':'w'},
     53          {'id':'comments', 'type':'text', 'mode':'w'},
     54      )
     55
     56      _relations =  ZenModelRM._relations + (
     57          ("report",
     58              ToOne(ToManyCont,"Products.ZenModel.GraphReport", "elements")),
     59          )
     60
     61      factory_type_information = (
     62          {
     63              'immediate_view' : 'editGraphReportElement',
     64              'actions'        :
     65              (
     66                  {'name'          : 'Edit',
     67                  'action'        : 'editGraphReportElement',
     68                  'permissions'   : ("Manage DMD",),
     69                  },
     70              )
     71           },
     72          )
     73
     74      security = ClassSecurityInfo()
     75
     76 -    def talesEval(self, text):
     77          dev = self.getDevice()
     78          if not dev:
     79              return 'Device %s could not be found' % self.deviceId
     80          comp = self.getComponent()
     81          if not comp:
     82              return 'Component %s could not be found for %s' % (
     83                          self.componentPath, self.deviceId)
     84          graph = self.getGraphDef()
     85          if not graph:
     86              return 'Graph %s could not be found for %s' % (
     87                          self.graphId, self.deviceId)
     88          compiled = talesCompile('string:' + text)
     89          e = {'dev':dev, 'device': dev,
     90                  'comp': comp, 'component':comp,
     91                  'graph': graph}
     92          try:
     93              result = compiled(getEngine().getContext(e))
     94              if isinstance(result, Exception):
     95                  result = 'Error: %s' % str(result)
     96          except Exception, e:
     97              result = 'Error: %s' %  str(e)
     98          return result
     99
    100
    101 -    def getSummary(self):
    102          ''' Returns tales-evaluated summary
    103          '''
    104          return self.talesEval(self.summary)
    105
    106 -    def getComments(self):
    107          ''' Returns tales-evaluated comments
    108          '''
    109          return self.talesEval(self.comments)
    110
    111
    112 -    def getDevice(self):
    113          return self.dmd.Devices.findDevice(self.deviceId)
    114
    115
    116 -    def getComponent(self):
    117          component = self.getDevice()
    118          for part in self.componentPath.split('/'):
    119              if part:
    120                  component = getattr(component, part, None)
    121                  if not component:
    122                      break
    123          return component
    124
    125
    126 -    def getGraphDef(self):
    127          graphDef = self.getComponent().getGraphDef(self.graphId)
    128          return graphDef
    129
    130
    131 -    def getGraphUrl(self, drange=None):
    132          ''' Return the url for the graph
    133          '''
    134          url = ''
    135          component = self.getComponent()
    136          if component:
    137              graph = component.getGraphDef(self.graphId)
    138              if graph:
    139                  url = component.getGraphDefUrl(graph, drange, graph.rrdTemplate())
    140          return url
    141
    142
    143  InitializeClass(GraphReportElement)
    144