"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-4.0.0/monasca_api/common/repositories/model/sub_alarm_definition.py" (13 May 2020, 5467 Bytes) of package /linux/misc/openstack/monasca-api-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "sub_alarm_definition.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.1.0_vs_4.0.0.

    1 # (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 # http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 from oslo_utils import encodeutils
   16 
   17 
   18 class SubAlarmDefinition(object):
   19     """Holds sub alarm definition
   20 
   21     Used for comparing sub alarm definitions for equality.
   22     """
   23 
   24     def __init__(self, row=None, sub_expr=None):
   25 
   26         """Initialize
   27 
   28         :param row: Database row
   29         :param sub_expr: Result from expression parser
   30         :return:
   31         """
   32 
   33         super(SubAlarmDefinition, self).__init__()
   34 
   35         if row and sub_expr:
   36             raise Exception('Only one of row or sub_expr can be specified, '
   37                             'not both')
   38 
   39         if row:
   40             # id is not used for compare or hash.
   41             self.id = row['id']
   42             self.alarm_definition_id = row['alarm_definition_id']
   43             self.metric_name = row['metric_name']
   44             self.dimensions_str = row['dimensions']
   45             self.dimensions = self._init_dimensions(row['dimensions'])
   46             self.function = row['function']
   47             self.operator = row['operator']
   48             self.period = row['period']
   49             self.periods = row['periods']
   50             self.threshold = row['threshold']
   51             self.deterministic = row['is_deterministic']
   52 
   53         if sub_expr:
   54             # id is not used for compare or hash.
   55             self.id = ''
   56             # Must be injected.
   57             self.alarm_definition_id = ''
   58             self.metric_name = sub_expr.metric_name
   59             self.dimensions_str = sub_expr.dimensions_str
   60             self.dimensions = self._init_dimensions(sub_expr.dimensions_str)
   61             self.function = encodeutils.safe_decode(sub_expr.normalized_func, 'utf-8')
   62             self.operator = sub_expr.normalized_operator
   63             self.period = sub_expr.period
   64             self.periods = sub_expr.periods
   65             self.threshold = sub_expr.threshold
   66             self.deterministic = sub_expr.deterministic
   67 
   68     def _init_dimensions(self, dimensions_str):
   69 
   70         dimensions = {}
   71 
   72         if dimensions_str:
   73             for dimension in dimensions_str.split(','):
   74                 name, value = dimension.split('=')
   75                 dimensions[name] = value
   76 
   77         return dimensions
   78 
   79     @property
   80     def expression(self):
   81 
   82         """Build the entire expressions as a string with spaces."""
   83 
   84         result = u"{}({}".format(self.function.lower(),
   85                                  self.metric_name)
   86 
   87         if self.dimensions_str:
   88             result += u"{{{}}}".format(self.dimensions_str)
   89 
   90         if self.deterministic:
   91             result += u", deterministic"
   92 
   93         if self.period:
   94             result += u", {}".format(str(self.period))
   95 
   96         result += u")"
   97 
   98         result += u" {} {}".format(self.operator,
   99                                    str(self.threshold))
  100 
  101         if self.periods:
  102             result += u" times {}".format(str(self.periods))
  103 
  104         return result
  105 
  106     def __hash__(self):
  107 
  108         dimensions_str = "".join(sorted([name + value for name, value in
  109                                          self.dimensions.items()]))
  110 
  111         # don't use id to hash.
  112         return (hash(self.alarm_definition_id) ^
  113                 hash(dimensions_str) ^
  114                 hash(self.function) ^
  115                 hash(self.metric_name) ^
  116                 hash(self.operator) ^
  117                 hash(self.period) ^
  118                 hash(self.periods) ^
  119                 hash(self.deterministic) ^
  120                 # Convert to float to handle cases like 0.0 == 0
  121                 hash(float(self.threshold)))
  122 
  123     def __repr__(self):
  124 
  125         result = 'id={},alarm_definition_id={},function={},metric_name={},dimensions={}' .format(
  126             self.id, self.alarm_definition_id, self.function, self.metric_name, self.dimensions)
  127         result += ',operator={},period={},periods={},determinstic={}'\
  128             .format(self.operator, self.period, self.periods, self.deterministic)
  129         return result
  130 
  131     def __eq__(self, other):
  132 
  133         if id(self) == id(other):
  134             return True
  135 
  136         if not isinstance(other, SubAlarmDefinition):
  137             return False
  138 
  139         # don't use id to compare.
  140         return (self.alarm_definition_id == other.alarm_definition_id and
  141                 self.dimensions == other.dimensions and
  142                 self.function == other.function and
  143                 self.metric_name == other.metric_name and
  144                 self.operator == other.operator and
  145                 self.period == other.period and
  146                 self.periods == other.periods and
  147                 self.deterministic == other.deterministic and
  148                 # Convert to float to handle cases like 0.0 == 0
  149                 float(self.threshold) == float(other.threshold))
  150 
  151     def same_key_fields(self, other):
  152 
  153         # The metrics matched can't change
  154         return (self.metric_name == other.metric_name and
  155                 self.dimensions == other.dimensions)