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

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