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

# Source Code for Module Products.ZenModel.RRDDataPointAlias

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2009, 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 or (at your
     8  # option) any later version as published by the Free Software Foundation.
     9  #
    10  # For complete information please visit: http://www.zenoss.com/oss/
    11  #
    12  ###########################################################################
    13
    14  __doc__="""RRDDataPointAlias
    15
    16  Create a simple level of indirection for normalization of data.  An alias is
    17  a pair of a name and an rpn formula.  The formula should convert the datapoint
    18  to the form represented by the name.
    19
    20  $Id:$"""
    21  import Globals
    22  from AccessControl import ClassSecurityInfo, Permissions
    23
    24  from Products.ZenUtils.ZenTales import talesEvalStr
    25  from Products.ZenRelations.RelSchema import ToOne, ToManyCont
    26  from Products.ZenModel.ZenModelRM import ZenModelRM
    27  from Products.ZenModel.ZenPackable import ZenPackable
    28
    29
    30  ALIAS_DELIMITER = ','
    31  EVAL_KEY = '__EVAL:'
    32
    33 -def manage_addDataPointAlias( context, id, formula=None ):
    34      """
    35      Add a datapoint alias to the datapoint given
    36      """
    37      alias = RRDDataPointAlias( id )
    38      alias.formula = formula
    39      context.aliases._setObject( id, alias )
    40      return context.aliases._getOb( id )
    41
    42 -class RRDDataPointAlias(ZenModelRM, ZenPackable):
    43
    44      meta_type = 'RRDDataPointAlias'
    45      formula = None
    46
    47      _properties = (
    48          {'id':'formula', 'type':'string', 'mode':'w'},
    49          )
    50
    51      _relations = ZenPackable._relations + (
    52          ("datapoint", ToOne(ToManyCont,"Products.ZenModel.RRDDataPoint","aliases")),
    53          )
    54
    55
    56      # Screen action bindings (and tab definitions)
    57      factory_type_information = (
    58      {
    59          'immediate_view' : 'editRRDDataPoint',
    60          'actions'        :
    61          (
    62              { 'id'            : 'edit'
    63              , 'name'          : 'Data Point'
    64              , 'action'        : 'editRRDDataPoint'
    65              , 'permissions'   : ( Permissions.view, )
    66              },
    67          )
    68      },
    69      )
    70
    71 -    def evaluate(self, context):
    72          """
    73          Evaluate the formula with the given context so that the resulting
    74          rpn can be applied to the datapoint value.  There are two possible
    75          layers of evaluation: python and then TALES evaluation.  Both use the
    76          name 'here' to name the passed context.  See testRRDDataPointAlias for
    77          examples of usage.
    78          """
    79          if self.formula:
    80              formula = self.formula
    81              if formula.startswith( EVAL_KEY ):
    82                  formula = formula[ len( EVAL_KEY ): ]
    83                  formula = str( eval( formula, { 'here' : context } ) )
    84              return talesEvalStr( formula, context )
    85          else:
    86              return None
    87