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