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

# Source Code for Module Products.ZenModel.GraphReport

      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 GraphReportElement import GraphReportElement
     19  from Products.ZenUtils.Utils import getObjByPath
     20  from Products.ZenUtils.ZenTales import talesCompile, getEngine
     21  from Products.ZenWidgets import messaging
     22  from DateTime import DateTime
     23
     24 -def manage_addGraphReport(context, id, REQUEST = None):
     25      ''' Create a new GraphReport
     26      '''
     27      gr = GraphReport(id)
     28      context._setObject(gr.id, gr)
     29      if REQUEST is not None:
     30          REQUEST['RESPONSE'].redirect(context.absolute_url()+'/manage_main')
     31
     32
     33 -class GraphReport(ZenModelRM):
     34
     35      meta_type = "GraphReport"
     36
     37      numColumns = 1
     38      numColumnsOptions = (1, 2, 3)
     39      comments = (
     40          '<div style="float: right;"><img src="img/onwhitelogo.png"></div>\n'
     41          '<div style="font-size: 16pt;">${report/id}</div>\n'
     42          '<div style="font-size:12pt;">${now/aDay} ${now/aMonth} ${now/day},'
     43          ' ${now/year}<br />\n'
     44          '${now/AMPMMinutes}\n'
     45          '</div>\n'
     46          '<div style="clear: both" />')
     47
     48      _properties = ZenModelRM._properties + (
     49          {'id':'comments', 'type':'text', 'mode':'w'},
     50          {'id':'numColumns', 'type':'int',
     51              'select_variable' : 'numColumnOptions', 'mode':'w'},
     52      )
     53
     54      _relations =  (
     55          ("elements",
     56              ToManyCont(ToOne,"Products.ZenModel.GraphReportElement", "report")),
     57          )
     58
     59      factory_type_information = (
     60          {
     61              'immediate_view' : '',
     62              'actions'        :
     63              (
     64                  {'name'          : 'View Report',
     65                  'action'        : '',
     66                  'permissions'   : ("View",),
     67                  },
     68                  {'name'          : 'Edit Report',
     69                  'action'        : 'editGraphReport',
     70                  'permissions'   : ("Manage DMD",),
     71                  },
     72              )
     73           },
     74          )
     75
     76      security = ClassSecurityInfo()
     77
     78
     79 -    def getBreadCrumbUrlPath(self):
     80          '''
     81          Return the url to be used in breadcrumbs for this object.
     82          '''
     83          return self.getPrimaryUrlPath() + '/editGraphReport'
     84
     85 -    def getThing(self, deviceId, componentPath):
     86          ''' Return either a device or a component, or None if not found
     87          '''
     88          thing = self.dmd.Devices.findDevice(deviceId)
     89          if thing and componentPath:
     90              try:
     91                  return getObjByPath(thing, componentPath)
     92              except KeyError:
     93                  return None
     94          return thing
     95
     96
     97      security.declareProtected('Manage DMD', 'manage_addGraphElement')
     98 -    def manage_addGraphElement(self, deviceIds='', componentPaths='',
     99                              graphIds=(), REQUEST=None):
    100          ''' Add a new graph report element
    101          '''
    102          def GetId(deviceId, componentPath, graphId):
    103              component = componentPath.split('/')[-1]
    104              parts = [p for p in (deviceId, component, graphId) if p]
    105              root = ' '.join(parts)
    106              candidate = self.prepId(root)
    107              i = 2
    108              while candidate in self.elements.objectIds():
    109                  candidate = self.prepId('%s-%s' % (root, i))
    110                  i += 1
    111              return candidate
    112
    113          if isinstance(deviceIds, basestring):
    114              deviceIds = [deviceIds]
    115          if isinstance(componentPaths, basestring):
    116              componentPaths = [componentPaths]
    117          componentPaths = componentPaths or ('')
    118          for devId in deviceIds:
    119              dev = self.dmd.Devices.findDevice(devId)
    120              for cPath in componentPaths:
    121                  try:
    122                      thing = getObjByPath(dev, cPath)
    123                  except KeyError:
    124                      continue
    125                  else:
    126                      for graphId in graphIds:
    127                          graph = thing.getGraphDef(graphId)
    128                          if graph:
    129                              newId = thing.name
    130                              if callable(newId):
    131                                  newId = newId()
    132
    133                              newId = GetId(devId, cPath, graphId)
    134                              ge = GraphReportElement(newId)
    135                              ge.deviceId = dev.titleOrId()
    136                              ge.componentPath = cPath
    137                              ge.graphId = graphId
    138                              ge.sequence = len(self.elements())
    139                              self.elements._setObject(ge.id, ge)
    140
    141          if REQUEST:
    142              return self.callZenScreen(REQUEST)
    143
    144
    145      security.declareProtected('Manage DMD', 'manage_deleteGraphReportElements')
    146 -    def manage_deleteGraphReportElements(self, ids=(), REQUEST=None):
    147          ''' Delete elements from this report
    148          '''
    149          for id in ids:
    150              self.elements._delObject(id)
    151          self.manage_resequenceGraphReportElements()
    152          if REQUEST:
    153              messaging.IMessageSender(self).sendToBrowser(
    154                  'Graphs Deleted',
    155                  '%s graph%s were deleted.' % (len(ids),
    156                                                len(ids)>1 and 's' or '')
    157              )
    158              return self.callZenScreen(REQUEST)
    159
    160
    161      security.declareProtected('Manage DMD',
    162                                      'manage_resequenceGraphReportElements')
    163 -    def manage_resequenceGraphReportElements(self, seqmap=(), origseq=(),
    164                                      REQUEST=None):
    165          """Reorder the sequecne of the graphs.
    166          """
    167          from Products.ZenUtils.Utils import resequence
    168          return resequence(self, self.elements(), seqmap, origseq, REQUEST)
    169
    170
    171      security.declareProtected('View', 'getComments')
    172 -    def getComments(self):
    173          ''' Returns tales-evaluated comments
    174          '''
    175          compiled = talesCompile('string:' + self.comments)
    176          e = {'rpt': self, 'report': self, 'now':DateTime()}
    177          result = compiled(getEngine().getContext(e))
    178          if isinstance(result, Exception):
    179              result = 'Error: %s' % str(result)
    180          return result
    181
    182
    183      # def getGraphs(self, drange=None):
    184      #     """get the default graph list for this object"""
    185      #     def cmpGraphs(a, b):
    186      #         return cmp(a['sequence'], b['sequence'])
    187      #     graphs = []
    188      #     for element in self.elements():
    189      #         graphs.append({
    190      #             'title': element.getDesc(),
    191      #             'url': element.getGraphUrl(),
    192      #             'sequence': element.sequence,
    193      #             })
    194      #     graphs.sort(cmpGraphs)
    195      #     return graphs
    196
    197
    198 -    def getElements(self):
    199          """get the ordered elements
    200          """
    201          def cmpElements(a, b):
    202              return cmp(a.sequence, b.sequence)
    203          elements = [e for e in self.elements()]
    204          elements.sort(cmpElements)
    205          return elements
    206
    207
    208  InitializeClass(GraphReport)
    209