<% macro generate(services, alerts) %>
[
    <% set base_tags = {
        "ctype": "prod",
        "itype": "searchproxy"
    } %>

    <% set juggler_check_template = {
        "host": "saas_disaster",
        "namespace": "search.saas",

        "aggregator": "logic_or",
        "aggregator_kwargs": {
            "unreach_service": [{
                "check": "yasm_alert:virtual-meta",
            }],
            "nodata_mode": "force_ok",
            "unreach_mode": "force_ok"
        },

        "tags": [
            "is_disaster",
            "marty",
            "sre_mute_without_2dc",
            "warden_auto_source",
            "warden_alert_create_spi",
            "warden_alert_metric_type_SLA",
            "warden_alert_metric_key_saas_availability",
            "warden_alert_account_metric",
            "warden_alert_account_background_metric"
        ],

        "flaps": {
            "critical": 600,
            "stable": 120,
            "boost": 0
        },

        "notifications": []
    } %>

    <% for alert in alerts %>
        <% set juggler_check = juggler_check_template %>
        <% if alert.flaps %>
            <% set juggler_check = juggler_check|merge_with_dict({"flaps": alert.flaps}) %>
        <% endif %>

        <% for service in services %>
            <% set alert_tags = base_tags %>
            <% if service.tags %>
                <% set alert_tags = alert_tags|merge_with_dict(service.tags) %>
            <% endif %>

            <% set juggler_tags = juggler_check.tags + ["warden_functionality_" + service.warden_functionality_slug] %>
            <% set juggler_notifications = juggler_check.notifications %>
            <% if not service.warden_test_flow %>
                <% set juggler_tags = juggler_tags + ["warden_alert_start_flow"] %>
                <% set juggler_notifications = juggler_notifications + [{
                        "template_name": "on_status_change",
                        "template_kwargs": {
                            "status": "CRIT",
                            "method": ["sms", "email", "telegram"],
                            "calendar_id": 44752
                        },
                        "description": "SAAS 5xx searchproxy alerts"
                    }] %>
            <% endif %>

            <% set warn, crit = service.alert_limits or alert.limits %>
            <% set geos = service.geos or [None] %>

            <% for geo in geos %>
                <% if geo %>
                    <% set alert_name = ["saas.disaster", service.ctype, service.name, geo, alert.name]|join(".") %>
                    <% set alert_tags = alert_tags|merge_with_dict({"geo": geo}) %>
                <% else %>
                    <% set alert_name = ["saas.disaster", service.ctype, service.name, alert.name]|join(".") %>
                <% endif %>

                <% set generated_alert = Alert(
                    name=alert_name,
                    tags=alert_tags,
                    signal=alert.signal.format(ctype=service.ctype, service=service.name),
                    warn=[warn, crit],
                    crit=[crit, None],
                    mgroups=["ASEARCH"],
                    description=alert.description,
                    juggler_check=juggler_check|merge_with_dict({
                        "service": alert_name,
                        "tags": juggler_tags,
                        "notifications": juggler_notifications,
                        "refresh_time" : 5
                    })
                ) %>

                <% set generated_alert = generated_alert|merge_with_dict({
                    "disaster": true,
                    "abc": "saas"
                }) %>

                << generated_alert >>,
            <% endfor %>
        <% endfor %>
    <% endfor %>
]
<% endmacro %>
