<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sohu.cache.dao.AppStatsDao">
    <!--
        字典sql
        set @tn='app_minute_command_statistics';
        select
            group_concat(column_name)
        from
            information_schema.columns
        where
            table_schema = 'cache-cloud' and table_name = @tn;

        select
            group_concat(case when locate('_',column_name)>0 then concat(replace(column_name , concat('_',@shortChar:=substring(column_name,locate('_',column_name)+1,1)),upper(@shortChar))) else column_name end)
        from
            information_schema.columns
        where
            table_schema = 'cache-cloud' and table_name = @tn;

        select
            group_concat(concat('#{',replace(column_name , concat('_',@shortChar:=substring(column_name,locate('_',column_name)+1,1)), upper(@shortChar) ),'}'))
        from
            information_schema.columns
        where
            table_schema = 'cache-cloud' and table_name = @tn;

    -->
    <sql id="app_minute_statistics_columns">
        app_id,collect_time,hits,misses,command_count,used_memory,
        expired_keys,evicted_keys,net_input_byte,net_output_byte,
        connected_clients,object_size,modify_time,accumulation
    </sql>

    <sql id="select_table">
        <choose>
            <when test="td.dimensionality ==0"> app_minute_command_statistics </when>
            <otherwise> app_hour_command_statistics </otherwise>
        </choose>
    </sql>
    
     <sql id="select_stat_table">
        <choose>
            <when test="td.dimensionality ==0"> app_minute_statistics </when>
            <otherwise> app_hour_statistics </otherwise>
        </choose>
    </sql>

    <sql id="app_minute_command_statistics_columns">
        app_id,collect_time,command_name,command_count,modify_time
    </sql>

    <insert id="mergeMinuteAppStats" parameterType="AppStats">
        insert into app_minute_statistics
          (<include refid="app_minute_statistics_columns"/>)
        values
          (#{appId},#{collectTime},#{hits},#{misses},${commandCount},#{usedMemory},
          #{expiredKeys},#{evictedKeys},#{netInputByte},#{netOutputByte},
          #{connectedClients},#{objectSize},#{modifyTime},1)
        on duplicate key update
          hits = hits + #{hits},
          misses = misses + #{misses},
          command_count = command_count + #{commandCount},
          used_memory = used_memory + #{usedMemory},
          expired_keys = expired_keys + #{expiredKeys},
          evicted_keys = evicted_keys + #{evictedKeys},
          net_input_byte = net_input_byte + #{netInputByte},
          net_output_byte = net_output_byte + #{netOutputByte},
          connected_clients = connected_clients + #{connectedClients},
          object_size = object_size + #{objectSize},
          modify_time = #{modifyTime},
          accumulation = accumulation + 1
    </insert>

    <insert id="mergeMinuteCommandStatus" parameterType="AppCommandStats">
        insert into app_minute_command_statistics
        (<include refid="app_minute_command_statistics_columns" />)
        values
        (#{appId},#{collectTime},#{commandName},#{commandCount},#{modifyTime})
        on duplicate key update
        command_count = command_count + #{commandCount},
        modify_time = #{modifyTime}
    </insert>

    <insert id="mergeHourAppStats" parameterType="AppStats">
        insert into app_hour_statistics
        (<include refid="app_minute_statistics_columns"/>)
        values
        (#{appId},substring(#{collectTime}, 1, 10),#{hits},#{misses},${commandCount},#{usedMemory},#{expiredKeys},
        #{evictedKeys},#{netInputByte},#{netOutputByte},#{connectedClients},#{objectSize},#{modifyTime},1)
        on duplicate key update
        hits = hits + #{hits},
        misses = misses + #{misses},
        command_count = command_count + #{commandCount},
        used_memory = used_memory + #{usedMemory},
        expired_keys = expired_keys + #{expiredKeys},
        evicted_keys = evicted_keys + #{evictedKeys},
        net_input_byte = net_input_byte + #{netInputByte},
        net_output_byte = net_output_byte + #{netOutputByte},
        connected_clients = connected_clients + #{connectedClients},
        object_size = object_size + #{objectSize},
        modify_time = #{modifyTime},
        accumulation = accumulation + 1
    </insert>

    <insert id="mergeHourCommandStatus" parameterType="AppCommandStats">
        insert into app_hour_command_statistics
        (<include refid="app_minute_command_statistics_columns" />)
        values
        (#{appId},substring(#{collectTime}, 1, 10),#{commandName},#{commandCount},#{modifyTime})
        on duplicate key update
        command_count = command_count + #{commandCount},
        modify_time = #{modifyTime}
    </insert>
    
     <select id="getAppStatsList" resultType="AppStats">
        select
          <include refid="app_minute_statistics_columns"/> , create_time
        from <include refid="select_stat_table"/>
        where
          app_id = #{appId} and collect_time between #{td.begin} and #{td.end}
    </select>

    <select id="getAppStatsByMinute" resultType="AppStats">
        select
          <include refid="app_minute_statistics_columns"/> , create_time
        from app_minute_statistics
        where
          app_id = #{appId} and collect_time between #{beginTime} and #{endTime}
    </select>
    
    
    <select id="getAppStatsByHour" resultType="AppStats">
        select
          <include refid="app_minute_statistics_columns"/> , create_time
        from app_hour_statistics
        where
          app_id = #{appId} and collect_time between #{beginTime} and #{endTime}
    </select>
    
    
    <select id="getAppCommandStatsList" resultType="AppCommandStats">
        select
          <include refid="app_minute_command_statistics_columns"/> , create_time
        from <include refid="select_table"/>
        where
          app_id = #{appId}
          and command_name = #{commandName}
          and collect_time between #{td.begin} and #{td.end}
    </select>
    
    <select id="getAppAllCommandStatsListByMinute" resultType="AppCommandStats">
        select
        app_id,
        collect_time,
        'commands' command_name,
        command_count,
        create_time,
        modify_time
        from app_minute_statistics
        where
          app_id = #{appId}
          and collect_time between #{beginTime} and #{endTime}
    </select>
    
    
    <select id="getAppAllCommandStatsListByHour" resultType="AppCommandStats">
        select
        app_id,
        collect_time,
        'commands' command_name,
        command_count,
        create_time,
        modify_time
        from app_hour_statistics
        where
          app_id = #{appId}
          and collect_time between #{beginTime} and #{endTime}
    </select>
    
    <select id="getAppCommandStatsListByMinuteWithCommand" resultType="AppCommandStats">
        select
          <include refid="app_minute_command_statistics_columns"/> , create_time
        from app_minute_command_statistics
        where
          app_id = #{appId}
          and command_name = #{commandName}
          and collect_time between #{beginTime} and #{endTime}
    </select>
    
    
    <select id="getAppCommandStatsListByHourWithCommand" resultType="AppCommandStats">
        select
          <include refid="app_minute_command_statistics_columns"/> , create_time
        from app_hour_command_statistics
        where
          app_id = #{appId}
          and command_name = #{commandName}
          and collect_time between #{beginTime} and #{endTime}
    </select>
    

    <select id="getAppAllCommandStatsList" resultType="AppCommandStats">
        select
        app_id,
        collect_time,
        'commands' command_name,
        command_count,
        create_time,
        modify_time
        from <include refid="select_stat_table"/>
        where
        app_id = #{appId}
        and collect_time between #{td.begin} and #{td.end}
    </select>

	<!-- 获取应用命令调用次数分布 -->
    <select id="getAppCommandGroup" resultType="AppCommandGroup">
        select
          command_name,sum(command_count) as count
        from <include refid="select_table"/>
        where
          app_id = #{appId}
          and collect_time between #{td.begin} and #{td.end}
          group by command_name
    </select>

    <!--查询一天中应用的命令执行次数的top-->
    <select id="getTopAppCommandStatsList" resultType="AppCommandStats">
        select
          app_id, command_name , max(collect_time) collect_time, sum(command_count) command_count ,
          max(modify_time) modify_time, max(create_time) create_time
        from <include refid="select_table"/>
        where app_id = #{appId}
        and collect_time between #{td.begin} and #{td.end}
        group by app_id,command_name
        order by command_count desc , modify_time desc
        limit #{top};
    </select>
    
    
    <select id="getTopAppCommandGroupSum" resultType="AppCommandStats">
        select
          app_id,collect_time,command_name,sum(command_count) as command_count,modify_time, create_time
        from <include refid="select_table"/>
        where app_id = #{appId}
        and collect_time between #{td.begin} and #{td.end}
        group by command_name
        order by sum(command_count) desc
        limit #{top};
    </select>

    <select id="getCommandClimax" resultType="AppCommandStats">
        select t2.*
        from (select app_id, command_name, max(command_count) command_count
            from
        <include refid="select_table"/>
            where
              app_id = #{appId}
              and command_name = #{commandName}
              and collect_time between #{td.begin} and #{td.end}
              group by app_id,command_name) t1 , <include refid="select_table"/> t2
        where t1.app_id = t2.app_id
        and t1.command_name=t2.command_name
        and t1.command_count=t2.command_count
        and t2.collect_time between #{td.begin} and #{td.end}
        limit 1;
    </select>
    
    <select id="getAppMinuteStat" resultType="hashmap">
        select 
        	avg(connected_clients) as avgClientCount,
        	max(connected_clients) as maxClientCount,
        	avg(command_count) as avgCommandCount, 
        	max(command_count) as maxCommandCount,
            sum(hits)/sum(command_count) as avgHitRatio,
	        max(hits/command_count) as maxHitRatio,
	        min(hits/command_count) as minHitRatio,
	        avg(used_memory) as avgUsedMemory,
	        max(used_memory) as maxUsedMemory,
	        sum(expired_keys) as expiredKeys,
	        sum(evicted_keys) as evictedKeys,
	        avg(net_input_byte) as avgNetInputByte,
	        max(net_input_byte) as maxNetInputByte,
	        avg(net_output_byte) as avgNetOutputByte,
	        max(net_output_byte) as maxNetOutputByte,
	        avg(object_size) as avgObjectSize,
	        max(object_size) as maxObjectSize
        from app_minute_statistics
        where app_id = #{appId} and collect_time between #{beginTime} and #{endTime}
    </select>
    

</mapper>