Treat period, periods and threshold as numbers

These are really numbers, not strings so always use them as their
real type

Change-Id: I0de4875300f77ea4219576195af967f944cf8005
This commit is contained in:
Craig Bryant
2017-01-17 11:59:29 -07:00
parent c1ab9792f6
commit 380dedfe11
4 changed files with 34 additions and 28 deletions

View File

@@ -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 # 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 # 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.dimensions = self._init_dimensions(row['dimensions'])
self.function = row['function'] self.function = row['function']
self.operator = row['operator'] self.operator = row['operator']
# Make sure that the following are converted to unicode self.period = row['period']
self.period = str(row['period']).decode('utf8') self.periods = row['periods']
self.periods = str(row['periods']).decode('utf8') self.threshold = row['threshold']
# threshold comes from the DB as a float in 0.0 form.
self.threshold = str(row['threshold']).decode('utf8')
self.deterministic = str(row['is_deterministic']) == '1' self.deterministic = str(row['is_deterministic']) == '1'
if sub_expr: if sub_expr:
@@ -91,15 +89,15 @@ class SubAlarmDefinition(object):
result += ', deterministic' result += ', deterministic'
if self.period: if self.period:
result += ", {}".format(self.period.encode('utf8')) result += ", {}".format(str(self.period).encode('utf8'))
result += ")" result += ")"
result += " {} {}".format(self.operator.encode('utf8'), result += " {} {}".format(self.operator.encode('utf8'),
self.threshold.encode('utf8')) str(self.threshold).encode('utf8'))
if self.periods: if self.periods:
result += " times {}".format(self.periods.encode('utf8')) result += " times {}".format(str(self.periods).encode('utf8'))
return result.decode('utf8') return result.decode('utf8')

View File

@@ -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 # Copyright 2016 FUJITSU LIMITED
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # 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_function=sub_expr.normalized_func.encode('utf8'),
b_metric_name=metric_name, b_metric_name=metric_name,
b_operator=operator, b_operator=operator,
b_threshold=sub_expr.threshold.encode('utf8'), b_threshold=sub_expr.threshold,
b_period=sub_expr.period.encode('utf8'), b_period=sub_expr.period,
b_periods=sub_expr.periods.encode('utf8'), b_periods=sub_expr.periods,
b_is_deterministic=sub_expr.deterministic, b_is_deterministic=sub_expr.deterministic,
b_created_at=now, b_created_at=now,
b_updated_at=now) b_updated_at=now)
@@ -592,9 +592,9 @@ class AlarmDefinitionsRepository(sql_repository.SQLRepository,
function = sub_alarm_def.function.encode('utf8') function = sub_alarm_def.function.encode('utf8')
metric_name = sub_alarm_def.metric_name.encode('utf8') metric_name = sub_alarm_def.metric_name.encode('utf8')
operator = sub_alarm_def.operator.encode('utf8') operator = sub_alarm_def.operator.encode('utf8')
threshold = str(sub_alarm_def.threshold).encode('utf8') threshold = sub_alarm_def.threshold
period = str(sub_alarm_def.period).encode('utf8') period = sub_alarm_def.period
periods = str(sub_alarm_def.periods).encode('utf8') periods = sub_alarm_def.periods
is_deterministic = sub_alarm_def.is_deterministic is_deterministic = sub_alarm_def.is_deterministic
parms.append({'b_id': sub_alarm_def.id, parms.append({'b_id': sub_alarm_def.id,
'b_alarm_definition_id': adi, 'b_alarm_definition_id': adi,

View File

@@ -20,6 +20,8 @@ import pyparsing
_DETERMINISTIC_ASSIGNMENT_LEN = 3 _DETERMINISTIC_ASSIGNMENT_LEN = 3
_DETERMINISTIC_ASSIGNMENT_SHORT_LEN = 1 _DETERMINISTIC_ASSIGNMENT_SHORT_LEN = 1
_DETERMINISTIC_ASSIGNMENT_VALUE_INDEX = 2 _DETERMINISTIC_ASSIGNMENT_VALUE_INDEX = 2
_DEFAULT_PERIOD = 60
_DEFAULT_PERIODS = 1
class SubExpr(object): class SubExpr(object):
@@ -36,9 +38,15 @@ class SubExpr(object):
self._metric_name = tokens.metric_name self._metric_name = tokens.metric_name
self._dimensions = tokens.dimensions_list self._dimensions = tokens.dimensions_list
self._operator = tokens.relational_op self._operator = tokens.relational_op
self._threshold = tokens.threshold self._threshold = float(tokens.threshold)
self._period = tokens.period if tokens.period:
self._periods = tokens.periods 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._deterministic = tokens.deterministic
self._id = None self._id = None
@@ -51,7 +59,7 @@ class SubExpr(object):
if self._dimensions is not None: if self._dimensions is not None:
result += "{" + self.dimensions_str + "}" result += "{" + self.dimensions_str + "}"
if self._period: if self._period != _DEFAULT_PERIOD:
result += ", {}".format(self._period) result += ", {}".format(self._period)
result += ")" result += ")"
@@ -59,7 +67,7 @@ class SubExpr(object):
result += " {} {}".format(self._operator, result += " {} {}".format(self._operator,
self._threshold) self._threshold)
if self._periods: if self._periods != _DEFAULT_PERIODS:
result += " times {}".format(self._periods) result += " times {}".format(self._periods)
return result return result

View File

@@ -1,5 +1,5 @@
# Copyright 2015 Cray Inc. All Rights Reserved. # 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 # 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 # not use this file except in compliance with the License. You may obtain
@@ -425,9 +425,9 @@ class TestRepoMetricsCassandra(testtools.TestCase):
], ],
"sub_alarm_expression": { "sub_alarm_expression": {
"function": "AVG", "function": "AVG",
"period": "60", "period": 60,
"threshold": "10.0", "threshold": 10.0,
"periods": "3", "periods": 3,
"operator": "LT", "operator": "LT",
"metric_definition": { "metric_definition": {
"dimensions": "{}", "dimensions": "{}",
@@ -470,10 +470,10 @@ class TestRepoMetricsCassandra(testtools.TestCase):
], ],
u'sub_alarm_expression': { u'sub_alarm_expression': {
u'dimensions': u'{}', u'dimensions': u'{}',
u'threshold': u'10.0', u'threshold': 10.0,
u'periods': u'3', u'periods': 3,
u'operator': u'LT', u'operator': u'LT',
u'period': u'60', u'period': 60,
u'metric_name': u'cpu.idle_perc', u'metric_name': u'cpu.idle_perc',
u'function': u'AVG' u'function': u'AVG'
} }