def insert_notification_method_types(self, notification_types): try: with self._orm_engine.connect() as conn: for notification_type in notification_types: conn.execute(self.nmt.insert(), notification_type) except DatabaseError as e: log.exception("Couldn't insert notification types %s", e) raise exc.DatabaseException(e)
def insert_notification_method_types(self, notification_types): try: if self._pgsql is None: self._connect_to_pgsql() cur = self._pgsql.cursor() cur.executemany(self._insert_notification_types_sql, notification_types) except psycopg2.Error as e: log.exception("Couldn't insert notification types %s", e) raise exc.DatabaseException(e)
def fetch_notification_method_types(self): try: if self._pgsql is None: self._connect_to_pgsql() cur = self._pgsql.cursor() cur.execute(self._find_all_notification_types_sql) for row in cur: yield (row[0]) except psycopg2.Error as e: log.exception("Couldn't fetch notification types %s", e) raise exc.DatabaseException(e)
def fetch_notification_method_types(self): try: with self._orm_engine.connect() as conn: LOG.debug('Orm query {%s}', str(self._orm_nmt_query)) notification_method_types = conn.execute( self._orm_nmt_query).fetchall() return [row[0] for row in notification_method_types] except DatabaseError as e: LOG.exception("Couldn't fetch notification method types %s", e) raise exc.DatabaseException(e)
def get_alarm_current_state(self, alarm_id): try: if self._pgsql is None: self._connect_to_pgsql() cur = self._pgsql.cursor() cur.execute(self._find_alarm_state_sql, alarm_id) row = cur.fetchone() state = row[0] if row is not None else None return state except psycopg2.Error as e: log.exception("Couldn't fetch current alarm state %s", e) raise exc.DatabaseException(e)
def fetch_notifications(self, alarm): try: if self._pgsql is None: self._connect_to_pgsql() cur = self._pgsql.cursor() cur.execute(self._find_alarm_action_sql, (alarm['alarmDefinitionId'], alarm['newState'])) for row in cur: yield (row[0], row[1].lower(), row[2], row[3], row[4]) except psycopg2.Error as e: log.exception("Couldn't fetch alarms actions %s", e) raise exc.DatabaseException(e)
def insert_notification_method_types(self, notification_types): try: if self._mysql is None: self._connect_to_mysql() cur = self._mysql.cursor() cur.executemany(self._insert_notification_types_sql, notification_types) except pymysql.IntegrityError as ignoredException: # If multiple instances of the notification engine tries to write the # same content at the same time, only one of them will succeed and others will # get duplicate primary key, integrity error. We can safely ignore this error. # This may happen only during the first start when the tables are empty. code, mesg = ignoredException.args if code == pymysql.constants.ER.DUP_ENTRY: log.debug("Notification type exists in DB. Ignoring the exception {}".format(mesg)) else: raise exc.DatabaseException(ignoredException) except pymysql.Error as e: self._mysql = None log.exception("Couldn't insert notification types %s", e) raise exc.DatabaseException(e)
def get_alarm_current_state(self, alarm_id): try: with self._orm_engine.connect() as conn: LOG.debug('Orm query {%s}', str(self._orm_get_alarm_state)) result = conn.execute(self._orm_get_alarm_state, alarm_id=alarm_id) row = result.fetchone() state = row[0] if row is not None else None return state except DatabaseError as e: LOG.exception("Couldn't fetch the current alarm state %s", e) raise exc.DatabaseException(e)
def fetch_notification(self, alarm): try: with self._orm_engine.connect() as conn: log.debug('Orm query {%s}', str(self._orm_query)) notifications = conn.execute( self._orm_query, alarm_definition_id=alarm['alarmDefinitionId'], alarm_state=alarm['newState']) return [(row[1].lower(), row[0], row[2], row[3]) for row in notifications] except DatabaseError as e: log.exception("Couldn't fetch alarms actions %s", e) raise exc.DatabaseException(e)
def get_notification(self, notification_id): try: if self._pgsql is None: self._connect_to_pgsql() cur = self._pgsql.cursor() cur.execute(self._get_notification_sql, notification_id) row = cur.fetchone() if row is None: return None else: return [row[0], row[1].lower(), row[2], row[3]] except psycopg2.Error as e: log.exception("Couldn't fetch the notification method %s", e) raise exc.DatabaseException(e)
def insert_notification_method_types(self, notification_types): # This function can be called by multiple processes at same time. # In that case, only the first one will succeed and the others will fail. # We can ignore this error when the types have already been registered. try: with self._orm_engine.connect() as conn: for notification_type in notification_types: conn.execute(self._orm_add_notification_type, b_name=notification_type) except DatabaseError as e: LOG.debug("Failed to insert notification types %s", notification_types) raise exc.DatabaseException(e)
def get_notification(self, notification_id): try: with self._orm_engine.connect() as conn: LOG.debug('Orm query {%s}', str(self._orm_get_notification)) result = conn.execute(self._orm_get_notification, notification_id=notification_id) notification = result.fetchone() if notification is None: return None else: return [ notification[0], notification[1].lower(), notification[2], notification[3] ] except DatabaseError as e: LOG.exception("Couldn't fetch the notification method %s", e) raise exc.DatabaseException(e)