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

# Source Code for Module Products.ZenModel.browser.EventConsole

      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  import urllib
     15  from Products.Five.browser import BrowserView
     16  #from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
     17  from Products.ZenUtils.json import json
     18  from Products.ZenUtils.Utils import unused
     19
     20
     21 -class EventConsole(BrowserView):
     22      """
     23      A view for Devices, CustomEventViews and EventViews that provides JSON data
     24      to populate the event console widget.
     25      """
     26 -    def __call__(self, **kwargs):
     27          return self._getEventsData(**kwargs)
     28
     29      @json
     30 -    def _getEventsData(self, offset=0, count=50, getTotalCount=True,
     31                         startdate=None, enddate=None, filter='', severity=2,
     32                         state=1, orderby='', **kwargs):
     33          """
     34          Data that populates the event console.
     35          """
     36          unused(kwargs)
     37
     38          context = self.context
     39          zem = self._getEventManager()
     40
     41          if hasattr(context, 'getResultFields'):
     42              fields = context.getResultFields()
     43          else:
     44              # Use default result fields
     45              if hasattr(context, 'event_key'):
     46                  base = context
     47              else:
     48                  base = zem.dmd.Events
     49              fields = zem.lookupManagedEntityResultFields(base.event_key)
     50
     51          data, totalCount = zem.getEventListME(context,
     52              offset=offset, rows=count, resultFields=fields,
     53              getTotalCount=getTotalCount, filter=filter, severity=severity,
     54              state=state, orderby=orderby, startdate=startdate, enddate=enddate)
     55
     56          results = [self._extract_data_from_zevent(ev, fields) for ev in data]
     57          return (results, totalCount)
     58
     59 -    def _getEventManager(self):
     60          return self.context.dmd.ZenEventManager
     61
     62 -    def _extract_data_from_zevent(self, zevent, fields):
     63          """return a list of data elements that map to the fields parameter.
     64          """
     65          def _sanitize(val):
     66              return val.replace('<', '&lt;').replace('>','&gt;')
     67
     68          data = []
     69          for field in fields:
     70              value = getattr(zevent, field)
     71              _shortvalue = str(value) or ''
     72              if field == "device":
     73                  value = urllib.quote('<a class="%s"' % ('') +
     74                              ' href="/zport/dmd/deviceSearchResults'
     75                              '?query=%s">%s</a>' % (value, _shortvalue))
     76              elif field == 'eventClass':
     77                  _shortvalue = _shortvalue.replace('/','/&shy;')
     78                  if not zevent.eventPermission:
     79                      value = _shortvalue
     80                  else:
     81                      value = urllib.quote('<a class="%s" ' % ('') +
     82                        'href="/zport/dmd/Events%s">%s</a>' % (value,_shortvalue))
     83              elif field == 'component' and getattr(zevent, 'device', None):
     84                  value = urllib.quote('<a class="%s"' % ('') +
     85                              ' href="/zport/dmd/searchComponents'
     86                              '?device=%s&component=%s">%s</a>' % (
     87                                  getattr(zevent, 'device'), value, _shortvalue))
     88              elif field == 'summary':
     89                  value = urllib.quote(
     90                      value.replace('<','&lt;').replace('>','&gt;'))
     91              else:
     92                  value = _shortvalue
     93              data.append(value)
     94          data.append('evid')
     95          data.append(zevent.getCssClass())
     96          return data
     97
     98
     99 -class EventConsoleFields(BrowserView):
    100      """
    101      Get the fields for the event console. This is a separate call so that the
    102      header DOM elements can be created first.
    103
    104      FIXME: Make the event console a single call.
    105      """
    106 -    def __call__(self):
    107          return self._getFields()
    108
    109      @json
    110 -    def _getFields(self):
    111          context = self.context
    112          zem = self._getEventManager()
    113          if hasattr(context, 'getResultFields'):
    114              fields = context.getResultFields()
    115          else:
    116              if hasattr(context, 'event_key'): base = context
    117              else: base = zem.dmd.Events
    118              fields = zem.lookupManagedEntityResultFields(base.event_key)
    119          lens = map(zem.getAvgFieldLength, fields)
    120          total = sum(lens)
    121          lens = map(lambda x:x/total*100, lens)
    122          zipped = zip(fields, lens)
    123          return zipped
    124
    125 -    def _getEventManager(self):
    126          return self.context.dmd.ZenEventManager
    127
    128
    129 -class HistoryConsole(EventConsole):
    130      """
    131      Same as the event console, only it accesses the history table.
    132      """
    133 -    def _getEventManager(self):
    134          return self.context.dmd.ZenEventHistory
    135
    136
    137 -class HistoryConsoleFields(EventConsoleFields):
    138      """
    139      Same as the event console fields, only for history.
    140
    141      FIXME: Is this used anywhere?
    142      """
    143 -    def _getEventManager(self):
    144          return self.context.dmd.ZenEventHistory
    145