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

Source Code for Module Products.ZenModel.browser.Autocompleters

  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 Products.CMFCore.utils import getToolByName 
 15  from Products.Five.browser import BrowserView 
 16  from Products.ZenModel.DeviceOrganizer import DeviceOrganizer 
 17  from Products.ZenUtils.json import json 
 18  from Products.ZenUtils.Utils import formreq 
 19  from Products.AdvancedQuery import Eq, MatchGlob 
 20   
 21   
22 -class DeviceNames(BrowserView):
23 """ 24 Provides device names for autocompleter population. 25 26 Adapts DeviceClasses. 27 """ 28 @json 29 @formreq
30 - def __call__(self, query=''):
31 """ 32 @param query: A glob by which to filter device names 33 @type query: str 34 @return: A JSON representation of a list of ids 35 @rtype: "['id1', 'id2', 'id3']" 36 """ 37 catalog = getToolByName(self.context.dmd.Devices, 38 self.context.dmd.Devices.default_catalog) 39 query = MatchGlob('id', query.rstrip('*') + '*') 40 if isinstance(self.context, DeviceOrganizer): 41 query = query & Eq('path', "/".join(self.context.getPhysicalPath())) 42 brains = catalog.evalAdvancedQuery(query) 43 deviceIds = [b.id for b in brains] 44 deviceIds.sort(lambda x, y: cmp(x.lower(), y.lower())) 45 return deviceIds
46 47
48 -class ComponentPaths(BrowserView):
49 """ 50 Get component paths and names associated with a given device or group of 51 devices. 52 53 Adapts DeviceClasses. 54 """ 55 @json 56 @formreq
57 - def __call__(self, deviceIds=()):
58 """ 59 @param deviceIds: One ore more device ids under which components should be 60 sought 61 @type deviceIds: str 62 @return: A JSON representation of a list of tuples describing components 63 under devices specified 64 @rtype: "[('/path/to/comp1', 'comp1'), ...]" 65 """ 66 paths = set() 67 if isinstance(deviceIds, basestring): 68 deviceIds = [deviceIds] 69 for devId in deviceIds: 70 d = self.context.findDevice(devId) 71 if d: 72 dPathLen = len(d.getPrimaryId()) + 1 73 for comp in d.getMonitoredComponents(): 74 paths.add((comp.getPrimaryId()[dPathLen:], comp.name())) 75 paths = list(paths) 76 paths.sort(lambda x,y: cmp(x[0], y[0])) 77 return paths
78 79
80 -class GraphIds(BrowserView):
81 """ 82 Get a list of the graph defs available for the given device 83 and component. 84 85 Adapts DeviceClasses. 86 """ 87 @json 88 @formreq
89 - def __call__(self, deviceIds=(), componentPaths=()):
90 """ 91 @param deviceIds: One ore more device ids under which graphs should be 92 sought 93 @type deviceIds: str, list 94 @param componentPaths: Path(s) to components under which graphs should 95 be sought 96 @type componentPaths: str, list 97 @return: A JSON representation of a list of ids 98 @rtype: "['id1', 'id2', 'id3']" 99 """ 100 graphIds = set() 101 if isinstance(deviceIds, basestring): 102 deviceIds = [deviceIds] 103 if isinstance(componentPaths, basestring): 104 componentPaths = [componentPaths] 105 if not componentPaths: 106 componentPaths = ('',) 107 for devId in deviceIds: 108 thing = self.context.findDevice(devId) 109 if thing: 110 for compPath in componentPaths: 111 compPath = compPath or '' 112 parts = compPath.split('/') 113 for part in parts: 114 if part: 115 if hasattr(thing, part): 116 thing = getattr(thing, part) 117 else: 118 break 119 else: 120 for t in thing.getRRDTemplates(): 121 for g in t.getGraphDefs(): 122 graphIds.add(g.id) 123 graphIds = list(graphIds) 124 graphIds.sort() 125 return graphIds
126 127
128 -class ServiceList(BrowserView):
129 """ 130 Get a list of id and descriptions for a live search 131 132 """ 133 @json 134 @formreq
135 - def __call__(self, dataRoot='serviceclasses'):
136 """ 137 @param dataRoot: The name of the relation under which services should 138 be sought 139 @type dataRoot: str 140 @return: A JSON representation of a list of service ids 141 @rtype: "['id1', 'id2', ...]" 142 """ 143 liveSearchList = [] 144 for srv in self.context.getSubInstancesGen(rel='serviceclasses'): 145 if getattr(srv, 'description', None): 146 liveSearchList.append('%s [%s]' % (srv.id, srv.description)) 147 else: 148 liveSearchList.append(srv.id) 149 return liveSearchList
150 151
152 -class EventClassNames(BrowserView):
153 """ 154 Get a list of all event classes that match the filter. 155 """ 156 @json 157 @formreq
158 - def __call__(self):
159 """ 160 @return: A JSON representation of a list of paths 161 @rtype: "['/path/1', '/path/2', ...]" 162 """ 163 orgs = self.context.dmd.Events.getSubOrganizers() 164 paths = ['/'.join(x.getPrimaryPath()) for x in orgs] 165 paths = [p.replace('/zport/dmd','') for p in paths] 166 return paths
167 168
169 -class OrganizerNames(BrowserView):
170 """ 171 Return the organizer names to which this user has access 172 """ 173 @json 174 @formreq
175 - def __call__(self, dataRoot="Devices"):
176 """ 177 @return: A JSON representation of a list of organizers 178 @rtype: "['/Systems/Sys1', '/Groups/Group1', ...]" 179 """ 180 root = self.context.dmd.getDmdRoot(dataRoot) 181 return root.getOrganizerNames()
182