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.jsonutils import json
     18  from Products.ZenUtils.Utils import formreq, getObjByPath
     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='', dataRoot='devices'):
     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          if dataRoot != 'devices':
     38              import exceptions
     39              raise exceptions.ValueError("dataRoot should only be 'devices'")
     40          catalog = getToolByName(self.context.dmd.Devices,
     41              self.context.dmd.Devices.default_catalog)
     42          query = MatchGlob('titleOrId', query.rstrip('*') + '*')
     43          if isinstance(self.context, DeviceOrganizer):
     44              query = query & Eq('path', "/".join(self.context.getPhysicalPath()))
     45          brains = catalog.evalAdvancedQuery(query)
     46
     47          # TODO: Add titleOrId to the catalog's metadata.
     48          deviceIds = [b.getObject().titleOrId() for b in brains]
     49          deviceIds.sort(lambda x, y: cmp(x.lower(), y.lower()))
     50          return deviceIds
     51
     52
     53 -class ComponentPaths(BrowserView):
     54      """
     55      Get component paths and names associated with a given device or group of
     56      devices.
     57
     58      Adapts DeviceClasses.
     59      """
     60      @json
     61      @formreq
     62 -    def __call__(self, deviceIds=()):
     63          """
     64          @param deviceIds: One ore more device ids under which components should be
     65          sought
     66          @type deviceIds: str
     67          @return: A JSON representation of a list of tuples describing components
     68          under devices specified
     69          @rtype: "[('/path/to/comp1', 'comp1'), ...]"
     70          """
     71          paths = set()
     72          if isinstance(deviceIds, basestring):
     73              deviceIds = [deviceIds]
     74          for devId in deviceIds:
     75              d = self.context.findDevice(devId)
     76              if d:
     77                  for comp in d.getReportableComponents():
     78                      name = comp.name
     79                      if callable(name):
     80                          name = name()
     81                      paths.add((comp.getPrimaryId(), name))
     82          paths = list(paths)
     83          paths.sort(lambda x,y: cmp(x[1], y[1]))
     84          return paths
     85
     86
     87 -class GraphIds(BrowserView):
     88      """
     89      Get a list of the graph defs available for the given device
     90      and component.
     91
     92      Adapts DeviceClasses.
     93      """
     94      @json
     95      @formreq
     96 -    def __call__(self, deviceIds=(), componentPaths=()):
     97          """
     98          @param deviceIds: One ore more device ids under which graphs should be
     99          sought
    100          @type deviceIds: str, list
    101          @param componentPaths: Path(s) to components under which graphs should
    102          be sought
    103          @type componentPaths: str, list
    104          @return: A JSON representation of a list of ids
    105          @rtype: "['id1', 'id2', 'id3']"
    106          """
    107          graphIds = set()
    108          if isinstance(deviceIds, basestring):
    109              deviceIds = [deviceIds]
    110          if isinstance(componentPaths, basestring):
    111              componentPaths = [componentPaths]
    112          if not componentPaths:
    113              componentPaths = ('',)
    114          for devId in deviceIds:
    115              thing = self.context.findDevice(devId)
    116              if thing:
    117                  for compPath in componentPaths:
    118                      if compPath:
    119                          thing = getObjByPath(thing, compPath)
    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