diff --git a/monasca_api/common/repositories/model/sub_alarm_definition.py b/monasca_api/common/repositories/model/sub_alarm_definition.py index a0d33ab28..8f736cab7 100644 --- a/monasca_api/common/repositories/model/sub_alarm_definition.py +++ b/monasca_api/common/repositories/model/sub_alarm_definition.py @@ -1,4 +1,4 @@ -# Copyright 2014 Hewlett-Packard +# (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -43,11 +43,9 @@ class SubAlarmDefinition(object): self.dimensions = self._init_dimensions(row['dimensions']) self.function = row['function'] self.operator = row['operator'] - # Make sure that the following are converted to unicode - self.period = str(row['period']).decode('utf8') - self.periods = str(row['periods']).decode('utf8') - # threshold comes from the DB as a float in 0.0 form. - self.threshold = str(row['threshold']).decode('utf8') + self.period = row['period'] + self.periods = row['periods'] + self.threshold = row['threshold'] self.deterministic = str(row['is_deterministic']) == '1' if sub_expr: @@ -91,15 +89,15 @@ class SubAlarmDefinition(object): result += ', deterministic' if self.period: - result += ", {}".format(self.period.encode('utf8')) + result += ", {}".format(str(self.period).encode('utf8')) result += ")" result += " {} {}".format(self.operator.encode('utf8'), - self.threshold.encode('utf8')) + str(self.threshold).encode('utf8')) if self.periods: - result += " times {}".format(self.periods.encode('utf8')) + result += " times {}".format(str(self.periods).encode('utf8')) return result.decode('utf8') diff --git a/monasca_api/common/repositories/sqla/alarm_definitions_repository.py b/monasca_api/common/repositories/sqla/alarm_definitions_repository.py index 9e8a9e40d..10419cc60 100644 --- a/monasca_api/common/repositories/sqla/alarm_definitions_repository.py +++ b/monasca_api/common/repositories/sqla/alarm_definitions_repository.py @@ -1,4 +1,4 @@ -# (C) Copyright 2014,2016 Hewlett Packard Enterprise Development Company LP +# (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP # Copyright 2016 FUJITSU LIMITED # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -426,9 +426,9 @@ class AlarmDefinitionsRepository(sql_repository.SQLRepository, b_function=sub_expr.normalized_func.encode('utf8'), b_metric_name=metric_name, b_operator=operator, - b_threshold=sub_expr.threshold.encode('utf8'), - b_period=sub_expr.period.encode('utf8'), - b_periods=sub_expr.periods.encode('utf8'), + b_threshold=sub_expr.threshold, + b_period=sub_expr.period, + b_periods=sub_expr.periods, b_is_deterministic=sub_expr.deterministic, b_created_at=now, b_updated_at=now) @@ -592,9 +592,9 @@ class AlarmDefinitionsRepository(sql_repository.SQLRepository, function = sub_alarm_def.function.encode('utf8') metric_name = sub_alarm_def.metric_name.encode('utf8') operator = sub_alarm_def.operator.encode('utf8') - threshold = str(sub_alarm_def.threshold).encode('utf8') - period = str(sub_alarm_def.period).encode('utf8') - periods = str(sub_alarm_def.periods).encode('utf8') + threshold = sub_alarm_def.threshold + period = sub_alarm_def.period + periods = sub_alarm_def.periods is_deterministic = sub_alarm_def.is_deterministic parms.append({'b_id': sub_alarm_def.id, 'b_alarm_definition_id': adi, diff --git a/monasca_api/expression_parser/alarm_expr_parser.py b/monasca_api/expression_parser/alarm_expr_parser.py index c88f43a52..22a2309a4 100644 --- a/monasca_api/expression_parser/alarm_expr_parser.py +++ b/monasca_api/expression_parser/alarm_expr_parser.py @@ -20,6 +20,8 @@ import pyparsing _DETERMINISTIC_ASSIGNMENT_LEN = 3 _DETERMINISTIC_ASSIGNMENT_SHORT_LEN = 1 _DETERMINISTIC_ASSIGNMENT_VALUE_INDEX = 2 +_DEFAULT_PERIOD = 60 +_DEFAULT_PERIODS = 1 class SubExpr(object): @@ -36,9 +38,15 @@ class SubExpr(object): self._metric_name = tokens.metric_name self._dimensions = tokens.dimensions_list self._operator = tokens.relational_op - self._threshold = tokens.threshold - self._period = tokens.period - self._periods = tokens.periods + self._threshold = float(tokens.threshold) + if tokens.period: + self._period = int(tokens.period) + else: + self._period = _DEFAULT_PERIOD + if tokens.periods: + self._periods = int(tokens.periods) + else: + self._periods = _DEFAULT_PERIODS self._deterministic = tokens.deterministic self._id = None @@ -51,7 +59,7 @@ class SubExpr(object): if self._dimensions is not None: result += "{" + self.dimensions_str + "}" - if self._period: + if self._period != _DEFAULT_PERIOD: result += ", {}".format(self._period) result += ")" @@ -59,7 +67,7 @@ class SubExpr(object): result += " {} {}".format(self._operator, self._threshold) - if self._periods: + if self._periods != _DEFAULT_PERIODS: result += " times {}".format(self._periods) return result diff --git a/monasca_api/tests/test_repositories.py b/monasca_api/tests/test_repositories.py index 84b5555f7..2e20b437d 100644 --- a/monasca_api/tests/test_repositories.py +++ b/monasca_api/tests/test_repositories.py @@ -1,5 +1,5 @@ # Copyright 2015 Cray Inc. All Rights Reserved. -# (C) Copyright 2016 Hewlett Packard Enterprise Development LP +# (C) Copyright 2016-2017 Hewlett Packard Enterprise Development LP # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -425,9 +425,9 @@ class TestRepoMetricsCassandra(testtools.TestCase): ], "sub_alarm_expression": { "function": "AVG", - "period": "60", - "threshold": "10.0", - "periods": "3", + "period": 60, + "threshold": 10.0, + "periods": 3, "operator": "LT", "metric_definition": { "dimensions": "{}", @@ -470,10 +470,10 @@ class TestRepoMetricsCassandra(testtools.TestCase): ], u'sub_alarm_expression': { u'dimensions': u'{}', - u'threshold': u'10.0', - u'periods': u'3', + u'threshold': 10.0, + u'periods': 3, u'operator': u'LT', - u'period': u'60', + u'period': 60, u'metric_name': u'cpu.idle_perc', u'function': u'AVG' }