<?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_SERVICE" >
            <query>
            <![CDATA[
            SELECT DISTINCT
                s.icon_image AS SERVICE_ICON_IMAGE,
                i.instance_name AS INSTANCE_NAME,
                h.host_object_id AS HOST_OBJECT_ID,
                s.service_object_id AS SERVICE_OBJECT_ID,
                oh.name1 AS HOST_NAME,
                h.alias AS HOST_ALIAS,
                h.display_name AS HOST_DISPLAY_NAME,
                os.name2 AS SERVICE_NAME,
                s.display_name AS SERVICE_DISPLAY_NAME,
                ss.process_performance_data AS SERVICE_PROCESS_PERFORMANCE_DATA,
                COALESCE(ss.current_state,0) AS SERVICE_CURRENT_STATE,
                COALESCE(ss.current_state,0) AS SERVICE_STATUS,
                ss.last_hard_state AS SERVICE_HARD_STATE,
                hs.last_hard_state AS HOST_HARD_STATE,
                COALESCE(hs.current_state,0) AS HOST_CURRENT_STATE,
                (CASE WHEN ss.last_state_change<='1970-01-01 00:00:00' THEN ps.program_start_time ELSE ss.last_state_change END) as DURATION_START,
                ps.program_start_time AS SERVICE_PROGRAM_START_TIME,
                ss.last_check AS SERVICE_LAST_CHECK,
                ss.next_check AS SERVICE_NEXT_CHECK,
                ss.current_check_attempt AS SERVICE_CURRENT_CHECK_ATTEMPT,
                ss.max_check_attempts AS service_max_attempt,
                i.instance_id AS SERVICE_INSTANCE_ID,
                s.service_id AS SERVICE_ID,
                ss.notifications_enabled as notifications_enabled,
                ss.problem_has_been_acknowledged as problem_acknowledged,
                ss.scheduled_downtime_depth as scheduled_downtime_depth,
                s.action_url AS SERVICE_ACTION_URL,
                h.notes as HOST_NOTES,
                h.notes_url AS HOST_NOTES_URL,
                s.notes as SERVICE_NOTES,
                s.notes_url AS SERVICE_NOTES_URL,
                COALESCE(ss.has_been_checked,0) as service_has_been_checked,
                ss.state_type AS SERVICE_STATE_TYPE
            FROM IcingaServices s
            INNER JOIN s.instance i
            LEFT JOIN s.status ss
            INNER JOIN s.host h
            LEFT JOIN h.status hs
            INNER JOIN h.object oh
            INNER JOIN s.object os
            INNER JOIN i.programstatus as ps

            WHERE
                os.is_active = '${active_flag}' and
                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_BLOBFIELDS</source>
                <field>SERVICE_OBJECT_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>

            <!--
                Additional fields that are used in cronk filtering
            -->
            <filter name="customvariable_name" type="dql">
                <join>s.customvariables cvs</join>
                <resolve>cvs.varname</resolve>
            </filter>

            <filter name="customvariable_value" type="dql">
                <join>s.customvariables cvs</join>
                <resolve>cvs.varvalue</resolve>
            </filter>
            <filter name="host_customvariable_name" type="dql">
                <leftjoin>h.customvariables cvh</leftjoin>
                <resolve>cvh.varname</resolve>
            </filter>

            <filter name="host_customvariable_value" type="dql">
                <leftjoin>h.customvariables cvh</leftjoin>
                <resolve>cvh.varvalue</resolve>
            </filter>

            <filter name="hostgroup_name" type="dql">
                <leftjoin>h.hostgroups hg</leftjoin>
                <innerjoin>hg.object ohg</innerjoin>
                <select>ohg.name1 AS hostgroup_name</select>
            </filter>

            <filter name="hostgroup_object_id" type="dql">
                <leftjoin>h.hostgroups hg</leftjoin>
                <innerjoin>hg.object ohg</innerjoin>
                <select>ohg.object_id AS hostgroup_object_id</select>
            </filter>

            <filter name="servicegroup_name" type="dql">
                <innerjoin>s.servicegroups sg</innerjoin>
                <innerjoin>sg.object osg</innerjoin>
                <select>osg.name1 AS servicegroup_name</select>
            </filter>

            <filter name="servicegroup_object_id" type="dql">
                <innerjoin>s.servicegroups sg</innerjoin>
                <innerjoin>sg.object osg</innerjoin>
                <select>osg.object_id AS servicegroup_object_id</select>
            </filter>

            <!--
                the following filters has been changed/introduced to
                allow missing initial states to show up
                as pending (#3844 #3838)
            -->
            <filter name="service_is_pending" type="dql">
                <!--
                <resolve>((ss.has_been_checked-1)*-1)</resolve>
                <select>(ss.has_been_checked-1)*-1 AS service_is_pending</select>
                -->
                <resolve>(CASE WHEN ss.has_been_checked IS NULL THEN 1 ELSE (ss.has_been_checked-1)*-1 END)</resolve>
                <select>(CASE WHEN ss.has_been_checked IS NULL THEN 1 ELSE (ss.has_been_checked-1)*-1 END) AS service_is_pending</select>
            </filter>

            <filter name="service_has_been_checked" type="dql">
                <resolve>COALESCE(ss.has_been_checked,0)</resolve>
            </filter>

            <filter name="service_status" type="dql">
                <resolve>COALESCE(ss.current_state,0)</resolve>
            </filter>

            <filter name="service_current_state" type="dql">
                <resolve>COALESCE(ss.current_state,0)</resolve>
            </filter>

            <filter name="host_current_state" type="dql">
                <resolve>COALESCE(hs.current_state,0)</resolve>
            </filter>

        </dql>

        <dql name="TARGET_SERVICE_OPEN_PROBLEMS" base="TARGET_SERVICE" >
            <query>
            <![CDATA[
            SELECT DISTINCT
                s.icon_image AS SERVICE_ICON_IMAGE,
                i.instance_name AS INSTANCE_NAME,
                h.host_object_id AS HOST_OBJECT_ID,
                s.service_object_id AS SERVICE_OBJECT_ID,
                oh.name1 AS HOST_NAME,
                h.alias AS HOST_ALIAS,
                h.display_name AS HOST_DISPLAY_NAME,
                os.name2 AS SERVICE_NAME,
                s.display_name AS SERVICE_DISPLAY_NAME,
                ss.current_state AS SERVICE_CURRENT_STATE,
                ss.last_hard_state AS SERVICE_HARD_STATE,
                (CASE WHEN ss.last_state_change<='1970-01-01 00:00:00' THEN ps.program_start_time ELSE ss.last_state_change END) as DURATION_START,
                ss.process_performance_data AS SERVICE_PROCESS_PERFORMANCE_DATA,
                ss.last_check AS SERVICE_LAST_CHECK,
                ss.next_check AS SERVICE_NEXT_CHECK,
                ss.current_check_attempt AS SERVICE_CURRENT_CHECK_ATTEMPT,
                ss.max_check_attempts AS SERVICE_MAX_CHECK_ATTEMPTS,
                ss.max_check_attempts AS service_max_attempt,
                i.instance_id AS SERVICE_INSTANCE_ID,
                s.service_id AS SERVICE_ID,
                ss.notifications_enabled as notifications_enabled,
                ss.problem_has_been_acknowledged as problem_acknowledged,
                ss.scheduled_downtime_depth as scheduled_downtime_depth,
                ss.has_been_checked as service_has_been_checked,
                s.action_url AS SERVICE_ACTION_URL,
                s.notes_url AS SERVICE_NOTES_URL,
                ss.state_type AS SERVICE_STATE_TYPE
            FROM IcingaServices s
            INNER JOIN s.instance i
            LEFT JOIN s.status ss
            INNER JOIN s.host h
            INNER JOIN h.object oh
            INNER JOIN s.object os
            INNER JOIN i.programstatus as ps
            WHERE
                os.is_active = '${active_flag}' AND
                s.config_type = '${retained_flag}' AND
                ss.scheduled_downtime_depth = 0 AND
                ss.current_state != 0 AND
                ss.problem_has_been_acknowledged != 1
            ]]>
            </query>

        </dql>

        <dql name="TARGET_SERVICE_PROBLEMS" base="TARGET_SERVICE" >
            <query>
            <![CDATA[
            SELECT DISTINCT
                s.icon_image AS SERVICE_ICON_IMAGE,
                i.instance_name AS INSTANCE_NAME,
                h.host_object_id AS HOST_OBJECT_ID,
                s.service_object_id AS SERVICE_OBJECT_ID,
                oh.name1 AS HOST_NAME,
                h.alias AS HOST_ALIAS,
                h.display_name AS HOST_DISPLAY_NAME,
                os.name2 AS SERVICE_NAME,
                s.display_name AS SERVICE_DISPLAY_NAME,
                ss.current_state AS SERVICE_CURRENT_STATE,
                ss.last_hard_state AS SERVICE_HARD_STATE,
                (CASE WHEN ss.last_state_change<='1970-01-01 00:00:00' THEN ps.program_start_time ELSE ss.last_state_change END) as DURATION_START,
                ss.process_performance_data AS SERVICE_PROCESS_PERFORMANCE_DATA,
                ss.last_check AS SERVICE_LAST_CHECK,
                ss.next_check AS SERVICE_NEXT_CHECK,
                ss.current_check_attempt AS SERVICE_CURRENT_CHECK_ATTEMPT,
                ss.max_check_attempts AS SERVICE_MAX_CHECK_ATTEMPTS,
                ss.max_check_attempts AS service_max_attempt,
                i.instance_id AS SERVICE_INSTANCE_ID,
                s.service_id AS SERVICE_ID,
                ss.notifications_enabled as notifications_enabled,
                ss.problem_has_been_acknowledged as problem_acknowledged,
                ss.scheduled_downtime_depth as scheduled_downtime_depth,
                ss.has_been_checked as service_has_been_checked,
                s.action_url AS SERVICE_ACTION_URL,
                s.notes_url AS SERVICE_NOTES_URL,
                ss.state_type AS SERVICE_STATE_TYPE
            FROM IcingaServices s
            INNER JOIN s.instance i
            LEFT JOIN s.status ss
            INNER JOIN s.host h
            INNER JOIN h.object oh
            INNER JOIN s.object os
            INNER JOIN i.programstatus as ps
            WHERE
                os.is_active = '${active_flag}' AND
                s.config_type = '${retained_flag}' AND
                ss.current_state != 0
            ]]>
            </query>

        </dql>

        <dql name="TARGET_SERVICE_BLOBFIELDS">
            <query>
            <![CDATA[
            SELECT
                ss.service_object_id AS SERVICE_OBJECT_ID,
                ss.output AS SERVICE_OUTPUT,
                ss.long_output AS SERVICE_LONG_OUTPUT,
                ss.perfdata AS SERVICE_PERFORMANCE_DATA
            FROM IcingaServicestatus ss
            WHERE ss.service_object_id IN (${TARGET_SERVICE.SERVICE_OBJECT_ID})
            ]]>
            </query>
        </dql>

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