<?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="TopicMetricsDao">
	<resultMap id="TopicMetricsMap" type="com.xiaojukeji.kafka.manager.common.entity.pojo.TopicMetricsDO">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="cluster_id" 			property="clusterId" />
		<result column="topic_name" 			property="topicName" />
		<result column="metrics" 				property="metrics" />
		<result column="gmt_create"				property="gmtCreate" />
	</resultMap>

	<insert id="batchAdd" parameterType="java.util.List">
		INSERT INTO topic_metrics
			(cluster_id, topic_name, metrics, gmt_create)
		VALUES
		<foreach item="TopicMetricsDO" index="index" collection="list" separator=",">
			(#{TopicMetricsDO.clusterId}, #{TopicMetricsDO.topicName}, #{TopicMetricsDO.metrics}, now())
		</foreach>
	</insert>

	<select id="getTopicMetrics" parameterType="java.util.Map" resultMap="TopicMetricsMap">
		<![CDATA[
		SELECT * FROM topic_metrics
		WHERE cluster_id = #{clusterId}
			AND topic_name = #{topicName}
			AND gmt_create BETWEEN #{startTime} AND #{endTime}
	]]>
	</select>

	<select id="getLatestTopicMetrics" parameterType="java.util.Map" resultMap="TopicMetricsMap">
		<![CDATA[
		SELECT * FROM topic_metrics
		WHERE cluster_id = #{clusterId} AND #{afterTime} <= gmt_create
	]]>
	</select>

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