monasca-api  4.0.0
About: OpenStack Monasca API is a RESTful API server that is designed with a layered architecture and supports Monitoring as a Service (MONaaS).
The "Ussuri" series (maintained release).
  Fossies Dox: monasca-api-4.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

sub_alarm_definition.py
Go to the documentation of this file.
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)
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.same_key_fields
def same_key_fields(self, other)
Definition: sub_alarm_definition.py:151
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.metric_name
metric_name
Definition: sub_alarm_definition.py:43
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.periods
periods
Definition: sub_alarm_definition.py:49
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.period
period
Definition: sub_alarm_definition.py:48
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.__init__
def __init__(self, row=None, sub_expr=None)
Definition: sub_alarm_definition.py:24
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.function
function
Definition: sub_alarm_definition.py:46
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.dimensions
dimensions
Definition: sub_alarm_definition.py:45
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.threshold
threshold
Definition: sub_alarm_definition.py:50
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition._init_dimensions
def _init_dimensions(self, dimensions_str)
Definition: sub_alarm_definition.py:68
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.__repr__
def __repr__(self)
Definition: sub_alarm_definition.py:123
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition
Definition: sub_alarm_definition.py:18
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.operator
operator
Definition: sub_alarm_definition.py:47
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.dimensions_str
dimensions_str
Definition: sub_alarm_definition.py:44
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.deterministic
deterministic
Definition: sub_alarm_definition.py:51
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.expression
def expression(self)
Definition: sub_alarm_definition.py:80
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.alarm_definition_id
alarm_definition_id
Definition: sub_alarm_definition.py:42
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.__eq__
def __eq__(self, other)
Definition: sub_alarm_definition.py:131
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.__hash__
def __hash__(self)
Definition: sub_alarm_definition.py:106
monasca_api.common.repositories.model.sub_alarm_definition.SubAlarmDefinition.id
id
Definition: sub_alarm_definition.py:41