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 or (at your
      8  # option) any later version as published by 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.Utils import getObjByPath
     19  from Products.ZenUtils.ZenTales import talesCompile, getEngine
     20
     21
     22 -def manage_addGraphReportElement(context, id, REQUEST = None):
     23      """make a GraphReportElement
     24      """
     25      element = GraphReportElement(id)
     26      context._setObject(element.id, element)
     27      if REQUEST is not None:
     28          REQUEST['RESPONSE'].redirect(context.absolute_url()+'/manage_main')
     29
     30
     31 -class GraphReportElement(ZenModelRM):
     32
     33      meta_type = 'GraphReportElement'
     34
     35      deviceId = ''
     36      componentPath = ''
     37      graphId = ''
     38      sequence = 0
     39      summary = ('Device: ${dev/titleOrId}\n'
     40                  '&nbsp;&nbsp;\n'
     41                  'Component: ${comp/name}\n'
     42                  '&nbsp;&nbsp;\n'
     43                  'Graph: ${graph/id}\n')
     44      comments = ('Device: ${dev/titleOrId}<br />\n'
     45                  'Component: ${comp/name}<br />\n'
     46                  '${graph/id}')
     47
     48      _properties = ZenModelRM._properties + (
     49          {'id':'deviceId', 'type':'string', 'mode':'w'},
     50          {'id':'componentPath', 'type':'string', 'mode':'w'},
     51          {'id':'graphId', 'type':'string', 'mode':'w'},
     52          {'id':'sequence', 'type':'int', 'mode':'w'},
     53          {'id':'summary', 'type':'text', 'mode':'w'},
     54          {'id':'comments', 'type':'text', 'mode':'w'},
     55      )
     56
     57      _relations =  ZenModelRM._relations + (
     58          ("report",
     59              ToOne(ToManyCont,"Products.ZenModel.GraphReport", "elements")),
     60          )
     61
     62      factory_type_information = (
     63          {
     64              'immediate_view' : 'editGraphReportElement',
     65              'actions'        :
     66              (
     67                  {'name'          : 'Edit',
     68                  'action'        : 'editGraphReportElement',
     69                  'permissions'   : ("Manage DMD",),
     70                  },
     71              )
     72           },
     73          )
     74
     75      security = ClassSecurityInfo()
     76
     77 -    def talesEval(self, text):
     78          dev = self.getDevice()
     79          if not dev:
     80              return 'Device %s could not be found' % self.deviceId
     81          comp = self.getComponent()
     82          if not comp:
     83              return 'Component %s could not be found for %s' % (
     84                          self.componentPath, self.deviceId)
     85          graph = self.getGraphDef()
     86          if not graph:
     87              return 'Graph %s could not be found for %s' % (
     88                          self.graphId, self.deviceId)
     89          compiled = talesCompile('string:' + text)
     90          e = {'dev':dev, 'device': dev,
     91                  'comp': comp, 'component':comp,
     92                  'graph': graph}
     93          try:
     94              result = compiled(getEngine().getContext(e))
     95              if isinstance(result, Exception):
     96                  result = 'Error: %s' % str(result)
     97          except Exception, e:
     98              result = 'Error: %s' %  str(e)
     99          return result
    100
    101
    102 -    def getSummary(self):
    103          ''' Returns tales-evaluated summary
    104          '''
    105          return self.talesEval(self.summary)
    106
    107 -    def getComments(self):
    108          ''' Returns tales-evaluated comments
    109          '''
    110          return self.talesEval(self.comments)
    111
    112
    113 -    def getDevice(self):
    114          return self.dmd.Devices.findDevice(self.deviceId)
    115
    116
    117 -    def getComponent(self):
    118          component = self.getDevice()
    119          for part in self.componentPath.split('/'):
    120              if part:
    121                  component = getattr(component, part, None)
    122                  if not component:
    123                      break
    124          return component
    125
    126
    127 -    def getComponentName(self):
    128          if self.componentPath:
    129              try:
    130                  name = getObjByPath(self.getDevice(), self.componentPath).name
    131                  return callable(name) and name() or name
    132              except KeyError:
    133                  return 'Not Found'
    134          else:
    135              return ''
    136
    137
    138 -    def getGraphDef(self):
    139          graphDef = self.getComponent().getGraphDef(self.graphId)
    140          return graphDef
    141
    142
    143 -    def getGraphUrl(self, drange=None):
    144          ''' Return the url for the graph
    145          '''
    146          url = ''
    147          component = self.getComponent()
    148          if component:
    149              graph = component.getGraphDef(self.graphId)
    150              if graph:
    151                  url = component.getGraphDefUrl(graph, drange, graph.rrdTemplate())
    152          return url
    153
    154
    155  InitializeClass(GraphReportElement)
    156