<?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.InstanceStatsDao">

    <sql id="instance_statistics_columns">
        inst_id,app_id,host_id,ip,port,role,max_memory,used_memory,curr_items,curr_connections,misses,hits,modify_time,mem_fragmentation_ratio,aof_delayed_fsync
    </sql>

    <insert id="updateInstanceStats" parameterType="InstanceStats">
        INSERT INTO instance_statistics
          (<include refid="instance_statistics_columns"/>)
        VALUES (#{instId},#{appId},#{hostId},#{ip},#{port},#{role},#{maxMemory},#{usedMemory},#{currItems},#{currConnections},#{misses},#{hits},
                  #{modifyTime},#{memFragmentationRatio},#{aofDelayedFsync})
        ON DUPLICATE KEY UPDATE
          inst_id = #{instId},
          host_id = #{hostId},
          role = #{role},
          max_memory = #{maxMemory},
          used_memory = #{usedMemory},
          curr_items = #{currItems},
          curr_connections = #{currConnections},
          misses = #{misses},
          hits = #{hits},
          modify_time = #{modifyTime},
          mem_fragmentation_ratio = #{memFragmentationRatio},
          aof_delayed_fsync = #{aofDelayedFsync}
    </insert>

    <select id="getInstanceStatsByHost" resultType="InstanceStats">
        select id,<include refid="instance_statistics_columns"/>
        from instance_statistics
        where ip = #{ip} and port = #{port}
    </select>

    <select id="getInstanceStatsByIp" resultType="InstanceStats">
        select id,<include refid="instance_statistics_columns"/>
        from instance_statistics
        where ip = #{ip}
    </select>

    <select id="getInstanceStatsByInsId" resultType="InstanceStats">
        select id,<include refid="instance_statistics_columns"/>
        from instance_statistics
        where inst_id = #{id}
    </select>

    <select id="getInstanceStatsByAppId" resultType="InstanceStats">
        select id,<include refid="instance_statistics_columns"/>
        from instance_statistics
        where app_id = #{appId}
    </select>

    <select id="getInstanceStats" resultType="InstanceStats">
        select id,<include refid="instance_statistics_columns"/>
        from instance_statistics
    </select>

    <insert id="mergeStandardStats" parameterType="StandardStats">
        replace into standard_statistics(collect_time,ip,port,db_type,info_json,diff_json)
        values (#{collectTime},#{ip},#{port},#{dbType},#{infoJson},#{diffJson})
    </insert>

    <select id="getStandardStats" resultType="StandardStats">
        select id,collect_time,ip,port,db_type,info_json,diff_json,created_time
        from standard_statistics
        where  ip=#{ip} and port=#{port} and db_type=#{dbType}
          and collect_time = #{collectTime}
    </select>

    <select id="getDiffJsonList" resultType="StandardStats">
        select collect_time,diff_json
        from standard_statistics
        where  ip=#{ip} and port=#{port} and db_type=#{dbType}
        and collect_time between #{beginTime} and #{endTime}
    </select>

    <delete id="deleteStandardStatsByCreatedTime">
        delete
        from standard_statistics
        where <![CDATA[ created_time <=  #{createdTime}]]>
    </delete>

</mapper>