<?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="TopicThrottledMetricsDao">
    <resultMap id="TopicThrottledMetricsDOMap" type="com.xiaojukeji.kafka.manager.common.entity.pojo.TopicThrottledMetricsDO">
        <id column="id"                         property="id"/>
        <result column="cluster_id"             property="clusterId"/>
        <result column="topic_name"             property="topicName"/>
        <result column="app_id"                 property="appId"/>
        <result column="produce_throttled"      property="produceThrottled"/>
        <result column="fetch_throttled"        property="fetchThrottled"/>
        <result column="gmt_create"             property="gmtCreate"/>
    </resultMap>

    <select id="getTopicThrottle" parameterType="java.util.Map" resultMap="TopicThrottledMetricsDOMap">
        SELECT *
        FROM topic_throttled_metrics
        WHERE cluster_id = #{clusterId}
            AND topic_name = #{topicName}
            AND app_id = #{appId}
            AND gmt_create BETWEEN #{startTime} AND #{endTime}
    </select>

    <select id="getAppIdThrottle" parameterType="java.util.Map" resultMap="TopicThrottledMetricsDOMap">
        SELECT *
        FROM topic_throttled_metrics
        WHERE cluster_id = #{clusterId}
            AND app_id = #{appId}
            AND gmt_create BETWEEN #{startTime} AND #{endTime}
    </select>

    <insert id="insertBatch" parameterType="java.util.List">
      INSERT INTO topic_throttled_metrics(
        cluster_id,
        topic_name,
        app_id,
        produce_throttled,
        fetch_throttled
      )
      VALUES
        <foreach collection="list" item="topicThrottledMetricsDO" index="index" separator=",">(
            #{topicThrottledMetricsDO.clusterId},
            #{topicThrottledMetricsDO.topicName},
            #{topicThrottledMetricsDO.appId},
            #{topicThrottledMetricsDO.produceThrottled},
            #{topicThrottledMetricsDO.fetchThrottled}
          )
        </foreach>
    </insert>

    <select id="getLatestTopicThrottledMetrics" parameterType="java.util.Map" resultMap="TopicThrottledMetricsDOMap">
        SELECT * FROM topic_throttled_metrics
        WHERE cluster_id = #{clusterId}
        AND gmt_create > #{afterTime}
    </select>

    <delete id="deleteBeforeTime" parameterType="java.util.Date">
        <![CDATA[
		DELETE FROM topic_throttled_metrics WHERE gmt_create < #{endTime} LIMIT 3000
	]]>
    </delete>
</mapper>