<?xml version="1.0" encoding="UTF-8"?>
<ae:configurations xmlns:view="http://icinga.org/icinga/config/global/api/views/1.0"
    xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0"
>
    <ae:configuration>
        <dql name="TARGET_HOST_STATUS_HISTORY" >
            <query>
            <![CDATA[
            SELECT DISTINCT
                h.icon_image AS HOST_ICON_IMAGE,
                i.instance_name AS INSTANCE_NAME,
                hsh.statehistory_id AS STATEHISTORY_ID,
                h.host_object_id AS HOST_OBJECT_ID,
                oh.name1 AS HOST_NAME,
                h.alias AS HOST_ALIAS,
                h.display_name AS HOST_DISPLAY_NAME,
                hsh.state AS STATEHISTORY_STATE,
                hsh.state_time AS STATEHISTORY_STATE_TIME,
                hsh.current_check_attempt AS STATEHISTORY_CURRENT_CHECK_ATTEMPT,
                hsh.max_check_attempts AS STATEHISTORY_MAX_CHECK_ATTEMPTS,
                hsh.current_check_attempt AS state_check_attempt,
                hsh.max_check_attempts AS state_max_check_attempts
            FROM IcingaStatehistory hsh
            INNER JOIN hsh.object oh WITH oh.objecttype_id = 1
            INNER JOIN oh.host h
            INNER JOIN hsh.instance i
            WHERE h.config_type = '${retained_flag}'
            ]]>
            </query>

            <!-- this merger pulls all BLOB fields after
                 selecting the data from the database
                 so we can DISTINCT them - which is required
                 due to duplicates when using credentials
                 -mfrosch -->
            <merge>
                <strategy>ForeignKey</strategy>
                <source>TARGET_HOST_STATUS_HISTORY_BLOBFIELDS</source>
                <field>STATEHISTORY_ID</field>
            </merge>

            <credential name="IcingaHost" type="MultiLike" affects="host">
                <parameter name="target">IcingaHost</parameter>
                <parameter name="column">oh.name1</parameter>
            </credential>

            <credential name="IcingaHostgroup" type="dql" affects="host">
                <leftjoin>h.hostgroups hg</leftjoin>
                <innerjoin>hg.object ohg</innerjoin>

                <orwhere>
                    <![CDATA[
                        ohg.name1 IN (${credential_value})
                    ]]>
                </orwhere>
            </credential>

            <credential name="IcingaContactgroup" type="dql" affects="host">
                <leftjoin>
                    h.contactgroups cg
                </leftjoin>
                <orwhere>
                    <![CDATA[
                    cg.contactgroup_id  IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                    ]]>
                </orwhere>
            </credential>


            <credential name="IcingaHostCustomVariablePair" type="CustomVariable" affects="host">
                <parameter name="alias">h</parameter>
                <parameter name="target">host</parameter>
            </credential>

        </dql>

        <dql name="TARGET_SERVICE_STATUS_HISTORY" >
            <query>
            <![CDATA[
            SELECT DISTINCT
                s.icon_image AS SERVICE_ICON_IMAGE,
                i.instance_name AS INSTANCE_NAME,
                ssh.statehistory_id AS STATEHISTORY_ID,
                h.host_object_id AS HOST_OBJECT_ID,
                oh.name1 AS HOST_NAME,
                h.alias AS HOST_ALIAS,
                h.display_name AS HOST_DISPLAY_NAME,
                s.service_object_id AS SERVICE_OBJECT_ID,
                os.name2 AS SERVICE_NAME,
                s.display_name AS SERVICE_DISPLAY_NAME,
                ssh.state AS STATEHISTORY_STATE,
                ssh.state_time AS STATEHISTORY_STATE_TIME,
                ssh.current_check_attempt AS STATEHISTORY_CURRENT_CHECK_ATTEMPT,
                ssh.max_check_attempts AS STATEHISTORY_MAX_CHECK_ATTEMPTS,
                ssh.current_check_attempt AS state_check_attempt,
                ssh.max_check_attempts AS state_max_check_attempts
            FROM IcingaStatehistory ssh
            INNER JOIN ssh.object os WITH os.objecttype_id = 2
            INNER JOIN os.service s
            INNER JOIN s.host h
            INNER JOIN h.object oh
            INNER JOIN ssh.instance i
            WHERE s.config_type = '${retained_flag}'
            ]]>
            </query>

            <!-- this merger pulls all BLOB fields after
                 selecting the data from the database
                 so we can DISTINCT them - which is required
                 due to duplicates when using credentials
                 -mfrosch -->
            <merge>
                <strategy>ForeignKey</strategy>
                <source>TARGET_SERVICE_STATUS_HISTORY_BLOBFIELDS</source>
                <field>STATEHISTORY_ID</field>
            </merge>

            <credential name="IcingaHost" type="MultiLike" affects="host">
                <parameter name="target">IcingaHost</parameter>
                <parameter name="column">oh.name1</parameter>
            </credential>

            <credential name="IcingaService" type="MultiLike" affects="service">
                <parameter name="target">IcingaService</parameter>
                <parameter name="column">os.name2</parameter>
            </credential>

            <credential name="IcingaServicegroup" type="dql" affects="service">
                <leftjoin>s.servicegroups sg</leftjoin>
                <innerjoin>sg.object osg</innerjoin>

                <orwhere>
                    <![CDATA[
                        osg.name1 IN (${credential_value})
                    ]]>
                </orwhere>
            </credential>

            <credential name="IcingaHostgroup" type="dql" affects="host">
                <leftjoin>h.hostgroups hg</leftjoin>
                <innerjoin>hg.object ohg</innerjoin>

                <orwhere>
                    <![CDATA[
                        ohg.name1 IN (${credential_value})
                    ]]>
                </orwhere>
            </credential>

            <credential name="IcingaContactgroup" type="dql" affects="service">
                <leftjoin>
                    s.contactgroups cg
                </leftjoin>
                <orwhere>
                    <![CDATA[
                    cg.contactgroup_id  IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                    ]]>
                </orwhere>
            </credential>


            <credential name="IcingaHostCustomVariablePair" type="CustomVariable" affects="host">
                <parameter name="alias">h</parameter>
                <parameter name="target">host</parameter>
            </credential>

            <credential name="IcingaServiceCustomVariablePair" type="CustomVariable" affects="service">
                <parameter name="alias">s</parameter>
                <parameter name="target">service</parameter>
            </credential>

        </dql>

        <dql name="TARGET_HOST_STATUS_HISTORY_BLOBFIELDS">
            <query>
            <![CDATA[
            SELECT
                hsh.statehistory_id AS STATEHISTORY_ID,
                hsh.output AS STATEHISTORY_OUTPUT,
                hsh.long_output AS STATEHISTORY_LONG_OUTPUT
            FROM IcingaStatehistory hsh
            WHERE hsh.statehistory_id IN (${TARGET_HOST_STATUS_HISTORY.STATEHISTORY_ID})
            ]]>
            </query>
        </dql>

        <dql name="TARGET_SERVICE_STATUS_HISTORY_BLOBFIELDS">
            <query>
            <![CDATA[
            SELECT
                ssh.statehistory_id AS STATEHISTORY_ID,
                ssh.output AS STATEHISTORY_OUTPUT,
                ssh.long_output AS STATEHISTORY_LONG_OUTPUT
            FROM IcingaStatehistory ssh
            WHERE ssh.statehistory_id IN (${TARGET_SERVICE_STATUS_HISTORY.STATEHISTORY_ID})
            ]]>
            </query>
        </dql>

    </ae:configuration>
</ae:configurations>
