add missing timestamp to alarm_transitioned_event

Affects Patch Alarm and Update Alarm endpoints in Python API.
Fixes smoketest failure "Wrong number of history entries"
Python API passes smoketest with this correction.

Change-Id: Id94d345efc66b7754545e19ab67a8fa85996f3b6
This commit is contained in:
Michael Bielinski
2015-10-28 13:27:28 -05:00
parent 9095853bb8
commit 49f488a1fa
3 changed files with 26 additions and 13 deletions

View File

@@ -12,7 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
from datetime import datetime
from oslo_log import log
from time import time
from monasca_api.common.repositories import alarms_repository
from monasca_api.common.repositories import exceptions
@@ -113,6 +115,8 @@ class AlarmsRepository(mysql_repository.MySQLRepository,
def update_alarm(self, tenant_id, id, state, lifecycle_state, link):
cnxn, cursor = self._get_cnxn_cursor_tuple()
time_ms = int(round(time() * 1000.0))
now = datetime.utcfromtimestamp(time_ms / 1000.0)
with cnxn:
@@ -130,12 +134,13 @@ class AlarmsRepository(mysql_repository.MySQLRepository,
prev_alarm = cursor.fetchone()
parms = [lifecycle_state, link]
set_str = "lifecycle_state = %s, link = %s, updated_at = NOW()"
parms = [lifecycle_state, link, now]
set_str = "lifecycle_state = %s, link = %s, updated_at = %s"
if state != prev_alarm['state']:
parms.append(state)
set_str += ",state = %s, state_updated_at = NOW()"
parms.append(now)
set_str += ",state = %s, state_updated_at = %s"
parms.extend([tenant_id, id])
@@ -155,7 +160,7 @@ class AlarmsRepository(mysql_repository.MySQLRepository,
cursor.execute(update_query, parms)
return prev_alarm['state']
return prev_alarm['state'], time_ms
@mysql_repository.mysql_try_catch_block
def delete_alarm(self, tenant_id, id):

View File

@@ -44,8 +44,10 @@ class Alarming(object):
def _send_alarm_transitioned_event(self, tenant_id, alarm_id,
alarm_definition_row,
alarm_metric_rows,
old_state, new_state):
old_state, new_state,
time_ms):
sub_alarms = []
metrics = []
alarm_transitioned_event_msg = {u'alarm-transitioned': {
u'tenantId': tenant_id,
@@ -58,9 +60,13 @@ class Alarming(object):
u'severity': alarm_definition_row['severity'],
u'oldState': old_state,
u'newState': new_state,
u'timestamp': time_ms,
u'subAlarms': sub_alarms,
u'metrics': metrics}
}
# TODO(msbielinski): need to populate subalarms
for alarm_metric_row in alarm_metric_rows:
metric = self._build_metric(alarm_metric_row)
metrics.append(metric)

View File

@@ -142,9 +142,9 @@ class Alarms(alarms_api_v2.AlarmsV2API,
alarm_metric_rows = self._alarms_repo.get_alarm_metrics(alarm_id)
sub_alarm_rows = self._alarms_repo.get_sub_alarms(tenant_id, alarm_id)
old_state = self._alarms_repo.update_alarm(tenant_id, alarm_id,
new_state,
lifecycle_state, link)
old_state, time_ms = self._alarms_repo.update_alarm(tenant_id, alarm_id,
new_state,
lifecycle_state, link)
# alarm_definition_id is the same for all rows.
alarm_definition_id = sub_alarm_rows[0]['alarm_definition_id']
@@ -168,7 +168,8 @@ class Alarms(alarms_api_v2.AlarmsV2API,
self._send_alarm_transitioned_event(tenant_id, alarm_id,
alarm_definition_row,
alarm_metric_rows,
old_state, new_state)
old_state, new_state,
time_ms)
@resource.resource_try_catch_block
def _alarm_patch(self, tenant_id, alarm_id, new_state, lifecycle_state,
@@ -177,9 +178,9 @@ class Alarms(alarms_api_v2.AlarmsV2API,
alarm_metric_rows = self._alarms_repo.get_alarm_metrics(alarm_id)
sub_alarm_rows = self._alarms_repo.get_sub_alarms(tenant_id, alarm_id)
old_state = self._alarms_repo.update_alarm(tenant_id, alarm_id,
new_state,
lifecycle_state, link)
old_state, time_ms = self._alarms_repo.update_alarm(tenant_id, alarm_id,
new_state,
lifecycle_state, link)
# alarm_definition_id is the same for all rows.
alarm_definition_id = sub_alarm_rows[0]['alarm_definition_id']
@@ -203,7 +204,8 @@ class Alarms(alarms_api_v2.AlarmsV2API,
self._send_alarm_transitioned_event(tenant_id, alarm_id,
alarm_definition_row,
alarm_metric_rows,
old_state, new_state)
old_state, new_state,
time_ms)
@resource.resource_try_catch_block
def _alarm_delete(self, tenant_id, id):