Пример #1
0
    def setUpClass(cls):
        from sqlalchemy import engine_from_config

        engine = engine_from_config({'url': 'sqlite://'}, prefix='')

        qry = open('monasca_api/tests/sqlite_alarm.sql', 'r').read()
        sconn = engine.raw_connection()
        c = sconn.cursor()
        c.executescript(qry)
        sconn.commit()
        c.close()
        cls.engine = engine

        def _fake_engine_from_config(*args, **kw):
            return cls.engine
        cls.fixture = fixtures.MonkeyPatch(
            'sqlalchemy.create_engine', _fake_engine_from_config)
        cls.fixture.setUp()

        metadata = MetaData()
        cls.nm = models.create_nm_model(metadata)
        cls._delete_nm_query = delete(cls.nm)
        cls._insert_nm_query = (insert(cls.nm)
                                .values(
                                    id=bindparam('id'),
                                    tenant_id=bindparam('tenant_id'),
                                    name=bindparam('name'),
                                    type=bindparam('type'),
                                    address=bindparam('address'),
                                    created_at=bindparam('created_at'),
                                    updated_at=bindparam('updated_at')))
Пример #2
0
    def setUpClass(cls):
        from sqlalchemy import engine_from_config

        engine = engine_from_config({'url': 'sqlite://'}, prefix='')

        qry = open('monasca_api/tests/sqlite_alarm.sql', 'r').read()
        sconn = engine.raw_connection()
        c = sconn.cursor()
        c.executescript(qry)
        sconn.commit()
        c.close()
        cls.engine = engine

        def _fake_engine_from_config(*args, **kw):
            return cls.engine

        cls.fixture = fixtures.MonkeyPatch('sqlalchemy.create_engine',
                                           _fake_engine_from_config)
        cls.fixture.setUp()

        metadata = MetaData()
        cls.nm = models.create_nm_model(metadata)
        cls._delete_nm_query = delete(cls.nm)
        cls._insert_nm_query = (insert(cls.nm).values(
            id=bindparam('id'),
            tenant_id=bindparam('tenant_id'),
            name=bindparam('name'),
            type=bindparam('type'),
            address=bindparam('address'),
            period=bindparam('period'),
            created_at=bindparam('created_at'),
            updated_at=bindparam('updated_at')))
    def __init__(self):

        super(NotificationsRepository, self).__init__()

        metadata = MetaData()
        self.nm = models.create_nm_model(metadata)

        nm = self.nm

        self._select_nm_count_name_query = (select([func.count()])
                                            .select_from(nm)
                                            .where(
                                                and_(nm.c.tenant_id == bindparam('b_tenant_id'),
                                                     nm.c.name == bindparam('b_name'))))

        self._select_nm_count_id_query = (select([func.count()])
                                          .select_from(nm)
                                          .where(
                                              and_(nm.c.tenant_id == bindparam('b_tenant_id'),
                                                   nm.c.id == bindparam('b_id'))))

        self._insert_nm_query = (insert(nm)
                                 .values(
                                     id=bindparam('b_id'),
                                     tenant_id=bindparam('b_tenant_id'),
                                     name=bindparam('b_name'),
                                     type=bindparam('b_type'),
                                     address=bindparam('b_address'),
                                     period=bindparam('b_period'),
                                     created_at=bindparam('b_created_at'),
                                     updated_at=bindparam('b_updated_at')))

        self._delete_nm_query = (delete(nm)
                                 .where(nm.c.tenant_id == bindparam('b_tenant_id'))
                                 .where(nm.c.id == bindparam('b_id')))

        self._update_nm_query = (update(nm)
                                 .where(nm.c.tenant_id == bindparam('b_tenant_id'))
                                 .where(nm.c.id == bindparam('b_id'))
                                 .values(
                                     name=bindparam('b_name'),
                                     type=bindparam('b_type'),
                                     address=bindparam('b_address'),
                                     period=bindparam('b_period'),
                                     updated_at=bindparam('b_updated_at')))

        self._select_nm_id_query = (select([nm])
                                    .where(
                                        and_(nm.c.tenant_id == bindparam('b_tenant_id'),
                                             nm.c.id == bindparam('b_id'))))

        self._select_nm_name_query = (select([nm])
                                      .where(
                                          and_(nm.c.tenant_id == bindparam('b_tenant_id'),
                                               nm.c.name == bindparam('b_name'))))
Пример #4
0
    def __init__(self):

        super(NotificationsRepository, self).__init__()

        metadata = MetaData()
        self.nm = models.create_nm_model(metadata)

        nm = self.nm

        self._select_nm_count_name_query = (select([func.count()])
                                            .select_from(nm)
                                            .where(
                                                and_(nm.c.tenant_id == bindparam('b_tenant_id'),
                                                     nm.c.name == bindparam('b_name'))))

        self._select_nm_count_id_query = (select([func.count()])
                                          .select_from(nm)
                                          .where(
                                              and_(nm.c.tenant_id == bindparam('b_tenant_id'),
                                                   nm.c.id == bindparam('b_id'))))

        self._insert_nm_query = (insert(nm)
                                 .values(
                                     id=bindparam('b_id'),
                                     tenant_id=bindparam('b_tenant_id'),
                                     name=bindparam('b_name'),
                                     type=bindparam('b_type'),
                                     address=bindparam('b_address'),
                                     created_at=bindparam('b_created_at'),
                                     updated_at=bindparam('b_updated_at')))

        self._delete_nm_query = (delete(nm)
                                 .where(nm.c.tenant_id == bindparam('b_tenant_id'))
                                 .where(nm.c.id == bindparam('b_id')))

        self._update_nm_query = (update(nm)
                                 .where(nm.c.tenant_id == bindparam('b_tenant_id'))
                                 .where(nm.c.id == bindparam('b_id'))
                                 .values(
                                     name=bindparam('b_name'),
                                     type=bindparam('b_type'),
                                     address=bindparam('b_address'),
                                     updated_at=bindparam('b_updated_at')))

        self._select_nm_id_query = (select([nm])
                                    .where(
                                        and_(nm.c.tenant_id == bindparam('b_tenant_id'),
                                             nm.c.id == bindparam('b_id'))))

        self._select_nm_name_query = (select([nm])
                                      .where(
                                          and_(nm.c.tenant_id == bindparam('b_tenant_id'),
                                               nm.c.name == bindparam('b_name'))))
    def setUpClass(cls):
        engine = create_engine('sqlite://')
        qry = open('monasca_api/tests/sqlite_alarm.sql', 'r').read()
        sconn = engine.raw_connection()
        c = sconn.cursor()
        c.executescript(qry)
        sconn.commit()
        c.close()
        cls.engine = engine

        def _fake_engine_from_config(*args, **kw):
            return cls.engine

        cls.fixture = fixtures.MonkeyPatch('sqlalchemy.create_engine',
                                           _fake_engine_from_config)
        cls.fixture.setUp()
        metadata = MetaData()

        cls.aa = models.create_aa_model(metadata)
        cls._delete_aa_query = delete(cls.aa)
        cls._insert_aa_query = (insert(cls.aa).values(
            alarm_definition_id=bindparam('alarm_definition_id'),
            alarm_state=bindparam('alarm_state'),
            action_id=bindparam('action_id')))

        cls.ad = models.create_ad_model(metadata)
        cls._delete_ad_query = delete(cls.ad)
        cls._insert_ad_query = (insert(cls.ad).values(
            id=bindparam('id'),
            tenant_id=bindparam('tenant_id'),
            name=bindparam('name'),
            severity=bindparam('severity'),
            expression=bindparam('expression'),
            match_by=bindparam('match_by'),
            actions_enabled=bindparam('actions_enabled'),
            created_at=bindparam('created_at'),
            updated_at=bindparam('updated_at'),
            deleted_at=bindparam('deleted_at')))

        cls.sad = models.create_sad_model(metadata)
        cls._delete_sad_query = delete(cls.sad)
        cls._insert_sad_query = (insert(cls.sad).values(
            id=bindparam('id'),
            alarm_definition_id=bindparam('alarm_definition_id'),
            function=bindparam('function'),
            metric_name=bindparam('metric_name'),
            operator=bindparam('operator'),
            threshold=bindparam('threshold'),
            period=bindparam('period'),
            periods=bindparam('periods'),
            is_deterministic=bindparam('is_deterministic'),
            created_at=bindparam('created_at'),
            updated_at=bindparam('updated_at')))

        cls.sadd = models.create_sadd_model(metadata)
        cls._delete_sadd_query = delete(cls.sadd)
        cls._insert_sadd_query = (insert(cls.sadd).values(
            sub_alarm_definition_id=bindparam('sub_alarm_definition_id'),
            dimension_name=bindparam('dimension_name'),
            value=bindparam('value')))

        cls.nm = models.create_nm_model(metadata)
        cls._delete_nm_query = delete(cls.nm)
        cls._insert_nm_query = (insert(cls.nm).values(
            id=bindparam('id'),
            tenant_id=bindparam('tenant_id'),
            name=bindparam('name'),
            type=bindparam('type'),
            address=bindparam('address'),
            created_at=bindparam('created_at'),
            updated_at=bindparam('updated_at')))
    def __init__(self):
        super(AlarmDefinitionsRepository, self).__init__()

        metadata = MetaData()
        self.a = models.create_a_model(metadata)
        self.aa = models.create_aa_model(metadata)
        self.ad = models.create_ad_model(metadata)
        self.am = models.create_am_model(metadata)
        self.nm = models.create_nm_model(metadata)
        self.md = models.create_md_model(metadata)
        self.mde = models.create_mde_model(metadata)
        self.mdd = models.create_mdd_model(metadata)
        self.sa = models.create_sa_model(metadata)
        self.sad = models.create_sad_model(metadata)
        self.sadd = models.create_sadd_model(metadata)
        a = self.a
        aa = self.aa
        ad = self.ad
        am = self.am
        nm = self.nm
        md = self.md
        sa = self.sa
        mdd = self.mdd
        mde = self.mde
        sad = self.sad
        sadd = self.sadd

        a_s = a.alias('a')
        ad_s = ad.alias('ad')
        self.ad_s = ad_s
        am_s = am.alias('am')
        nm_s = nm.alias('nm')
        md_s = md.alias('md')
        sa_s = sa.alias('sa')
        mdd_s = mdd.alias('mdd')
        mde_s = mde.alias('mde')
        sad_s = sad.alias('sad')
        sadd_s = sadd.alias('sadd')

        aaa_aa = aa.alias('aaa_aa')
        aaa = (select([
            aaa_aa.c.alarm_definition_id,
            models.group_concat([aaa_aa.c.action_id]).label('alarm_actions')
        ]).select_from(aaa_aa).where(
            aaa_aa.c.alarm_state == text("'ALARM'")).group_by(
                aaa_aa.c.alarm_definition_id).alias('aaa'))

        aao_aa = aa.alias('aao_aa')
        aao = (select([
            aao_aa.c.alarm_definition_id,
            models.group_concat([aao_aa.c.action_id]).label('ok_actions')
        ]).select_from(aao_aa).where(
            aao_aa.c.alarm_state == text("'OK'")).group_by(
                aao_aa.c.alarm_definition_id).alias('aao'))

        aau_aa = aa.alias('aau_aa')
        aau = (select([
            aau_aa.c.alarm_definition_id,
            models.group_concat([aau_aa.c.action_id
                                 ]).label('undetermined_actions')
        ]).select_from(aau_aa).where(
            aau_aa.c.alarm_state == text("'UNDETERMINED'")).group_by(
                aau_aa.c.alarm_definition_id).alias('aau'))

        self.base_query_from = (ad_s.outerjoin(
            aaa, aaa.c.alarm_definition_id == ad_s.c.id).outerjoin(
                aao, aao.c.alarm_definition_id == ad_s.c.id).outerjoin(
                    aau, aau.c.alarm_definition_id == ad_s.c.id))

        self.base_query = (select([
            ad_s.c.id, ad_s.c.name, ad_s.c.description, ad_s.c.expression,
            ad_s.c.match_by, ad_s.c.severity, ad_s.c.actions_enabled,
            aaa.c.alarm_actions, aao.c.ok_actions, aau.c.undetermined_actions
        ]))

        self.get_sub_alarms_query = (select([
            sa_s.c.id.label('sub_alarm_id'), sa_s.c.alarm_id, sa_s.c.expression
        ]).select_from(
            sa_s.join(a_s, a_s.c.id == sa_s.c.alarm_id).join(
                ad_s, ad_s.c.id == a_s.c.alarm_definition_id)).where(
                    ad_s.c.tenant_id == bindparam('b_tenant_id')).where(
                        ad_s.c.id == bindparam('b_id')).distinct())

        mdg = (select([
            md_s.c.dimension_set_id,
            models.group_concat([md_s.c.name + text("'='") + md_s.c.value
                                 ]).label('dimensions')
        ]).select_from(md_s).group_by(md_s.c.dimension_set_id).alias('mdg'))

        self.get_alarm_metrics_query = (select([
            a_s.c.id.label('alarm_id'), mde_s.c.name, mdg.c.dimensions
        ]).select_from(
            a_s.join(ad_s, ad_s.c.id == a_s.c.alarm_definition_id).join(
                am_s, am_s.c.alarm_id == a_s.c.id).join(
                    mdd_s,
                    mdd_s.c.id == am_s.c.metric_definition_dimensions_id).join(
                        mde_s,
                        mde_s.c.id == mdd_s.c.metric_definition_id).outerjoin(
                            mdg, mdg.c.dimension_set_id ==
                            mdd_s.c.metric_dimension_set_id)
        ).where(ad_s.c.tenant_id == bindparam('b_tenant_id')).where(
            ad_s.c.id == bindparam('b_id')).order_by(a_s.c.id).distinct())

        self.soft_delete_ad_query = (update(ad).where(
            ad.c.tenant_id == bindparam('b_tenant_id')).where(
                ad.c.id == bindparam('b_id')).where(
                    ad.c.deleted_at == null()).values(
                        deleted_at=datetime.datetime.utcnow()))

        self.delete_a_query = (delete(a).where(
            a.c.alarm_definition_id == bindparam('b_id')))

        columns_gc = [sadd_s.c.dimension_name + text("'='") + sadd_s.c.value]
        saddg = (select([
            sadd_s.c.sub_alarm_definition_id,
            models.group_concat(columns_gc).label('dimensions')
        ]).select_from(sadd_s).group_by(
            sadd_s.c.sub_alarm_definition_id).alias('saddg'))

        self.get_sub_alarm_definitions_query = (select([
            sad_s, saddg.c.dimensions
        ]).select_from(
            sad_s.outerjoin(
                saddg, saddg.c.sub_alarm_definition_id == sad_s.c.id)).where(
                    sad_s.c.alarm_definition_id == bindparam(
                        'b_alarm_definition_id')))

        self.create_alarm_definition_insert_ad_query = (insert(ad).values(
            id=bindparam('b_id'),
            tenant_id=bindparam('b_tenant_id'),
            name=bindparam('b_name'),
            description=bindparam('b_description'),
            expression=bindparam('b_expression'),
            severity=bindparam('b_severity'),
            match_by=bindparam('b_match_by'),
            actions_enabled=bindparam('b_actions_enabled'),
            created_at=bindparam('b_created_at'),
            updated_at=bindparam('b_updated_at')))

        self.create_alarm_definition_insert_sad_query = (insert(sad).values(
            id=bindparam('b_id'),
            alarm_definition_id=bindparam('b_alarm_definition_id'),
            function=bindparam('b_function'),
            metric_name=bindparam('b_metric_name'),
            operator=bindparam('b_operator'),
            threshold=bindparam('b_threshold'),
            period=bindparam('b_period'),
            periods=bindparam('b_periods'),
            is_deterministic=bindparam('b_is_deterministic'),
            created_at=bindparam('b_created_at'),
            updated_at=bindparam('b_updated_at')))

        b_sad_id = bindparam('b_sub_alarm_definition_id')
        self.create_alarm_definition_insert_sadd_query = (insert(sadd).values(
            sub_alarm_definition_id=b_sad_id,
            dimension_name=bindparam('b_dimension_name'),
            value=bindparam('b_value')))

        self.update_or_patch_alarm_definition_update_ad_query = (
            update(ad).where(ad.c.tenant_id == bindparam('b_tenant_id')).where(
                ad.c.id == bindparam('b_id')))

        self.update_or_patch_alarm_definition_delete_sad_query = (
            delete(sad).where(sad.c.id == bindparam('b_id')))

        self.update_or_patch_alarm_definition_update_sad_query = (
            update(sad).where(sad.c.id == bindparam('b_id')).values(
                operator=bindparam('b_operator'),
                threshold=bindparam('b_threshold'),
                is_deterministic=bindparam('b_is_deterministic'),
                updated_at=bindparam('b_updated_at')))

        b_ad_id = bindparam('b_alarm_definition_id'),
        self.update_or_patch_alarm_definition_insert_sad_query = (
            insert(sad).values(
                id=bindparam('b_id'),
                alarm_definition_id=b_ad_id,
                function=bindparam('b_function'),
                metric_name=bindparam('b_metric_name'),
                operator=bindparam('b_operator'),
                threshold=bindparam('b_threshold'),
                period=bindparam('b_period'),
                periods=bindparam('b_periods'),
                is_deterministic=bindparam('b_is_deterministic'),
                created_at=bindparam('b_created_at'),
                updated_at=bindparam('b_updated_at')))

        self.update_or_patch_alarm_definition_insert_sadd_query = (
            insert(sadd).values(sub_alarm_definition_id=b_sad_id,
                                dimension_name=bindparam('b_dimension_name'),
                                value=bindparam('b_value')))

        self.delete_aa_query = (delete(aa).where(
            aa.c.alarm_definition_id == bindparam('b_alarm_definition_id')))

        self.delete_aa_state_query = (delete(aa).where(
            aa.c.alarm_definition_id == bindparam('b_alarm_definition_id')
        ).where(aa.c.alarm_state == bindparam('b_alarm_state')))

        self.select_nm_query = (select([
            nm_s.c.id
        ]).select_from(nm_s).where(nm_s.c.id == bindparam('b_id')))

        self.insert_aa_query = (insert(aa).values(
            alarm_definition_id=bindparam('b_alarm_definition_id'),
            alarm_state=bindparam('b_alarm_state'),
            action_id=bindparam('b_action_id')))
Пример #7
0
    def setUpClass(cls):
        from sqlalchemy import engine_from_config

        engine = engine_from_config({'url': 'sqlite://'}, prefix='')

        qry = open('monasca_api/tests/sqlite_alarm.sql', 'r').read()
        sconn = engine.raw_connection()
        c = sconn.cursor()
        c.executescript(qry)
        sconn.commit()
        c.close()
        cls.engine = engine

        def _fake_engine_from_config(*args, **kw):
            return cls.engine
        cls.fixture = fixtures.MonkeyPatch(
            'sqlalchemy.create_engine', _fake_engine_from_config)
        cls.fixture.setUp()

        metadata = MetaData()

        cls.aa = models.create_aa_model(metadata)
        cls._delete_aa_query = delete(cls.aa)
        cls._insert_aa_query = (insert(cls.aa)
                                .values(
                                    alarm_definition_id=bindparam('alarm_definition_id'),
                                    alarm_state=bindparam('alarm_state'),
                                    action_id=bindparam('action_id')))

        cls.ad = models.create_ad_model(metadata)
        cls._delete_ad_query = delete(cls.ad)
        cls._insert_ad_query = (insert(cls.ad)
                                .values(
                                    id=bindparam('id'),
                                    tenant_id=bindparam('tenant_id'),
                                    name=bindparam('name'),
                                    severity=bindparam('severity'),
                                    expression=bindparam('expression'),
                                    match_by=bindparam('match_by'),
                                    actions_enabled=bindparam('actions_enabled'),
                                    created_at=bindparam('created_at'),
                                    updated_at=bindparam('updated_at'),
                                    deleted_at=bindparam('deleted_at')))
        cls.sad = models.create_sad_model(metadata)
        cls._delete_sad_query = delete(cls.sad)
        cls._insert_sad_query = (insert(cls.sad)
                                 .values(
                                     id=bindparam('id'),
                                     alarm_definition_id=bindparam('alarm_definition_id'),
                                     function=bindparam('function'),
                                     metric_name=bindparam('metric_name'),
                                     operator=bindparam('operator'),
                                     threshold=bindparam('threshold'),
                                     period=bindparam('period'),
                                     periods=bindparam('periods'),
                                     is_deterministic=bindparam('is_deterministic'),
                                     created_at=bindparam('created_at'),
                                     updated_at=bindparam('updated_at')))

        cls.sadd = models.create_sadd_model(metadata)
        cls._delete_sadd_query = delete(cls.sadd)
        cls._insert_sadd_query = (insert(cls.sadd)
                                  .values(
                                      sub_alarm_definition_id=bindparam('sub_alarm_definition_id'),
                                      dimension_name=bindparam('dimension_name'),
                                      value=bindparam('value')))

        cls.nm = models.create_nm_model(metadata)
        cls._delete_nm_query = delete(cls.nm)
        cls._insert_nm_query = (insert(cls.nm)
                                .values(
                                    id=bindparam('id'),
                                    tenant_id=bindparam('tenant_id'),
                                    name=bindparam('name'),
                                    type=bindparam('type'),
                                    address=bindparam('address'),
                                    created_at=bindparam('created_at'),
                                    updated_at=bindparam('updated_at')))
    def __init__(self):
        super(AlarmDefinitionsRepository, self).__init__()

        metadata = MetaData()
        self.a = models.create_a_model(metadata)
        self.aa = models.create_aa_model(metadata)
        self.ad = models.create_ad_model(metadata)
        self.am = models.create_am_model(metadata)
        self.nm = models.create_nm_model(metadata)
        self.md = models.create_md_model(metadata)
        self.mde = models.create_mde_model(metadata)
        self.mdd = models.create_mdd_model(metadata)
        self.sa = models.create_sa_model(metadata)
        self.sad = models.create_sad_model(metadata)
        self.sadd = models.create_sadd_model(metadata)
        a = self.a
        aa = self.aa
        ad = self.ad
        am = self.am
        nm = self.nm
        md = self.md
        sa = self.sa
        mdd = self.mdd
        mde = self.mde
        sad = self.sad
        sadd = self.sadd

        a_s = a.alias('a')
        ad_s = ad.alias('ad')
        self.ad_s = ad_s
        am_s = am.alias('am')
        nm_s = nm.alias('nm')
        md_s = md.alias('md')
        sa_s = sa.alias('sa')
        mdd_s = mdd.alias('mdd')
        mde_s = mde.alias('mde')
        sad_s = sad.alias('sad')
        sadd_s = sadd.alias('sadd')

        aaa_aa = aa.alias('aaa_aa')
        aaa = (select([aaa_aa.c.alarm_definition_id,
                       models.group_concat([aaa_aa.c.action_id]).label('alarm_actions')])
               .select_from(aaa_aa)
               .where(aaa_aa.c.alarm_state == text("'ALARM'"))
               .group_by(aaa_aa.c.alarm_definition_id)
               .alias('aaa'))

        aao_aa = aa.alias('aao_aa')
        aao = (select([aao_aa.c.alarm_definition_id,
                       models.group_concat([aao_aa.c.action_id]).label('ok_actions')])
               .select_from(aao_aa)
               .where(aao_aa.c.alarm_state == text("'OK'"))
               .group_by(aao_aa.c.alarm_definition_id)
               .alias('aao'))

        aau_aa = aa.alias('aau_aa')
        aau = (select([aau_aa.c.alarm_definition_id,
                       models.group_concat([aau_aa.c.action_id]).label('undetermined_actions')])
               .select_from(aau_aa)
               .where(aau_aa.c.alarm_state == text("'UNDETERMINED'"))
               .group_by(aau_aa.c.alarm_definition_id)
               .alias('aau'))

        self.base_query_from = (ad_s.outerjoin(aaa, aaa.c.alarm_definition_id == ad_s.c.id)
                                .outerjoin(aao, aao.c.alarm_definition_id == ad_s.c.id)
                                .outerjoin(aau, aau.c.alarm_definition_id == ad_s.c.id))

        self.base_query = (select([ad_s.c.id,
                                   ad_s.c.name,
                                   ad_s.c.description,
                                   ad_s.c.expression,
                                   ad_s.c.match_by,
                                   ad_s.c.severity,
                                   ad_s.c.actions_enabled,
                                   aaa.c.alarm_actions,
                                   aao.c.ok_actions,
                                   aau.c.undetermined_actions]))

        self.get_sub_alarms_query = (
            select(
                [
                    sa_s.c.id.label('sub_alarm_id'),
                    sa_s.c.alarm_id,
                    sa_s.c.expression]) .select_from(
                sa_s.join(
                    a_s,
                    a_s.c.id == sa_s.c.alarm_id) .join(
                        ad_s,
                        ad_s.c.id == a_s.c.alarm_definition_id)) .where(
                            ad_s.c.tenant_id == bindparam('b_tenant_id')) .where(
                                ad_s.c.id == bindparam('b_id')) .distinct())

        mdg = (select([md_s.c.dimension_set_id,
                       models.group_concat(
                           [md_s.c.name + text("'='") + md_s.c.value]).label('dimensions')])
               .select_from(md_s)
               .group_by(md_s.c.dimension_set_id)
               .alias('mdg'))

        self.get_alarm_metrics_query = (
            select(
                [a_s.c.id.label('alarm_id'),
                 mde_s.c.name,
                 mdg.c.dimensions]) .select_from(
                a_s.join(
                    ad_s,
                    ad_s.c.id == a_s.c.alarm_definition_id) .join(
                    am_s,
                    am_s.c.alarm_id == a_s.c.id) .join(
                    mdd_s,
                    mdd_s.c.id == am_s.c.metric_definition_dimensions_id) .join(
                    mde_s,
                    mde_s.c.id == mdd_s.c.metric_definition_id) .outerjoin(
                    mdg,
                    mdg.c.dimension_set_id == mdd_s.c.metric_dimension_set_id)) .where(
                ad_s.c.tenant_id == bindparam('b_tenant_id')) .where(
                ad_s.c.id == bindparam('b_id')) .order_by(
                a_s.c.id) .distinct())

        self.soft_delete_ad_query = (update(ad)
                                     .where(ad.c.tenant_id == bindparam('b_tenant_id'))
                                     .where(ad.c.id == bindparam('b_id'))
                                     .where(ad.c.deleted_at == null())
                                     .values(deleted_at=datetime.datetime.utcnow()))

        self.delete_a_query = (delete(a)
                               .where(a.c.alarm_definition_id == bindparam('b_id')))

        columns_gc = [sadd_s.c.dimension_name + text("'='") + sadd_s.c.value]
        saddg = (select([sadd_s.c.sub_alarm_definition_id,
                         models.group_concat(columns_gc).label('dimensions')])
                 .select_from(sadd_s)
                 .group_by(sadd_s.c.sub_alarm_definition_id)
                 .alias('saddg'))

        self.get_sub_alarm_definitions_query = (
            select(
                [
                    sad_s,
                    saddg.c.dimensions]) .select_from(
                sad_s.outerjoin(
                    saddg,
                    saddg.c.sub_alarm_definition_id == sad_s.c.id)) .where(
                        sad_s.c.alarm_definition_id == bindparam('b_alarm_definition_id')))

        self.create_alarm_definition_insert_ad_query = (
            insert(ad) .values(
                id=bindparam('b_id'),
                tenant_id=bindparam('b_tenant_id'),
                name=bindparam('b_name'),
                description=bindparam('b_description'),
                expression=bindparam('b_expression'),
                severity=bindparam('b_severity'),
                match_by=bindparam('b_match_by'),
                actions_enabled=bindparam('b_actions_enabled'),
                created_at=bindparam('b_created_at'),
                updated_at=bindparam('b_updated_at')))

        self.create_alarm_definition_insert_sad_query = (
            insert(sad) .values(
                id=bindparam('b_id'),
                alarm_definition_id=bindparam('b_alarm_definition_id'),
                function=bindparam('b_function'),
                metric_name=bindparam('b_metric_name'),
                operator=bindparam('b_operator'),
                threshold=bindparam('b_threshold'),
                period=bindparam('b_period'),
                periods=bindparam('b_periods'),
                is_deterministic=bindparam('b_is_deterministic'),
                created_at=bindparam('b_created_at'),
                updated_at=bindparam('b_updated_at')))

        b_sad_id = bindparam('b_sub_alarm_definition_id')
        self.create_alarm_definition_insert_sadd_query = (
            insert(sadd) .values(
                sub_alarm_definition_id=b_sad_id,
                dimension_name=bindparam('b_dimension_name'),
                value=bindparam('b_value')))

        self.update_or_patch_alarm_definition_update_ad_query = (
            update(ad) .where(
                ad.c.tenant_id == bindparam('b_tenant_id')) .where(
                ad.c.id == bindparam('b_id')))

        self.update_or_patch_alarm_definition_delete_sad_query = (
            delete(sad) .where(sad.c.id == bindparam('b_id')))

        self.update_or_patch_alarm_definition_update_sad_query = (
            update(sad) .where(
                sad.c.id == bindparam('b_id')) .values(
                operator=bindparam('b_operator'),
                threshold=bindparam('b_threshold'),
                is_deterministic=bindparam('b_is_deterministic'),
                updated_at=bindparam('b_updated_at')))

        b_ad_id = bindparam('b_alarm_definition_id'),
        self.update_or_patch_alarm_definition_insert_sad_query = (
            insert(sad) .values(
                id=bindparam('b_id'),
                alarm_definition_id=b_ad_id,
                function=bindparam('b_function'),
                metric_name=bindparam('b_metric_name'),
                operator=bindparam('b_operator'),
                threshold=bindparam('b_threshold'),
                period=bindparam('b_period'),
                periods=bindparam('b_periods'),
                is_deterministic=bindparam('b_is_deterministic'),
                created_at=bindparam('b_created_at'),
                updated_at=bindparam('b_updated_at')))

        self.update_or_patch_alarm_definition_insert_sadd_query = (
            insert(sadd) .values(
                sub_alarm_definition_id=b_sad_id,
                dimension_name=bindparam('b_dimension_name'),
                value=bindparam('b_value')))

        self.delete_aa_query = (delete(aa)
                                .where(aa.c.alarm_definition_id
                                       == bindparam('b_alarm_definition_id')))

        self.delete_aa_state_query = (
            delete(aa) .where(
                aa.c.alarm_definition_id == bindparam('b_alarm_definition_id')) .where(
                aa.c.alarm_state == bindparam('b_alarm_state')))

        self.select_nm_query = (select([nm_s.c.id])
                                .select_from(nm_s)
                                .where(nm_s.c.id == bindparam('b_id')))

        self.insert_aa_query = (insert(aa)
                                .values(
                                    alarm_definition_id=bindparam('b_alarm_definition_id'),
                                    alarm_state=bindparam('b_alarm_state'),
                                    action_id=bindparam('b_action_id')))