-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
332 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
saturn-console-api/src/main/java/com/vip/saturn/job/console/mybatis/entity/DomainCount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.vip.saturn.job.console.mybatis.entity; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* @author Ray Leung | ||
*/ | ||
public class DomainCount { | ||
|
||
private int id; | ||
private String zkCluster; | ||
private int successCount; | ||
private int failCount; | ||
private Date recordDate; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public int getSuccessCount() { | ||
return successCount; | ||
} | ||
|
||
public void setSuccessCount(int successCount) { | ||
this.successCount = successCount; | ||
} | ||
|
||
public int getFailCount() { | ||
return failCount; | ||
} | ||
|
||
public void setFailCount(int failCount) { | ||
this.failCount = failCount; | ||
} | ||
|
||
public String getZkCluster() { | ||
return zkCluster; | ||
} | ||
|
||
public void setZkCluster(String zkCluster) { | ||
this.zkCluster = zkCluster; | ||
} | ||
|
||
public Date getRecordDate() { | ||
return recordDate; | ||
} | ||
|
||
public void setRecordDate(Date recordDate) { | ||
this.recordDate = recordDate; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...pi/src/main/java/com/vip/saturn/job/console/mybatis/repository/DomainCountRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.vip.saturn.job.console.mybatis.repository; | ||
|
||
import com.vip.saturn.job.console.mybatis.entity.DomainCount; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Ray Leung | ||
*/ | ||
@Repository | ||
public interface DomainCountRepository { | ||
|
||
/** | ||
* 创建或更新全域统计信息 | ||
* @param zkCluster | ||
* @param recordDate | ||
* @param successCount | ||
* @param failCount | ||
* @return | ||
*/ | ||
int createOrUpdateDomainCount(@Param("zkCluster") String zkCluster, @Param("recordDate") Date recordDate, | ||
@Param("successCount") Integer successCount, @Param("failCount") Integer failCount); | ||
|
||
/** | ||
* 通过zkCluster和recordDate查询全域信息 | ||
* @param zkCluster | ||
* @param recordDate | ||
* @return | ||
*/ | ||
DomainCount selectByZkClusterAndRecordDate(@Param("zkCluster") String zkCluster, | ||
@Param("recordDate") Date recordDate); | ||
|
||
/** | ||
* 按时间范围返回全域信息 | ||
* @param zkCluster | ||
* @param startDate | ||
* @param endDate | ||
* @return | ||
*/ | ||
List<DomainCount> selectByZkClusterAndFromStartDateToEndDate(@Param("zkCluster") String zkCluster, | ||
@Param("startDate") Date startDate, @Param("endDate") Date endDate); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
saturn-console-api/src/main/resources/mapper/DomainCountMapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?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.vip.saturn.job.console.mybatis.repository.DomainCountRepository"> | ||
<resultMap id="BaseResultMap" type="com.vip.saturn.job.console.mybatis.entity.DomainCount"> | ||
<id column="id" property="id" jdbcType="BIGINT"/> | ||
<result column="zk_cluster" property="zkCluster" jdbcType="VARCHAR"/> | ||
<result column="success_count" property="successCount" jdbcType="INTEGER"/> | ||
<result column="fail_count" property="failCount" jdbcType="INTEGER"/> | ||
<result column="record_date" property="recordDate" jdbcType="VARCHAR"/> | ||
</resultMap> | ||
|
||
<sql id="Base_Column_List"> | ||
id, zk_cluster, success_count, fail_count, record_date | ||
</sql> | ||
|
||
<select id="selectByZkClusterAndRecordDate" resultMap="BaseResultMap"> | ||
select | ||
<include refid="Base_Column_List"/> | ||
from saturn_domain_process_history | ||
where | ||
zk_cluster = #{zkCluster} and DATE_FORMAT(record_date, '%Y-%m-%d') = DATE_FORMAT(#{recordDate}, '%Y-%m-%d') | ||
</select> | ||
|
||
<select id="selectByZkClusterAndFromStartDateToEndDate" resultMap="BaseResultMap"> | ||
select | ||
<include refid="Base_Column_List"/> | ||
from saturn_domain_process_history | ||
where | ||
zk_cluster = #{zkCluster} and | ||
DATE_FORMAT(#{startDate}, '%Y-%m-%d') <![CDATA[<=]]> DATE_FORMAT(record_date, '%Y-%m-%d') and | ||
DATE_FORMAT(record_date, '%Y-%m-%d') <![CDATA[>=]]> DATE_FORMAT(#{endDate}, '%Y-%m-%d') | ||
</select> | ||
|
||
<insert id="createOrUpdateDomainCount" parameterType="map"> | ||
insert into saturn_domain_process_history( | ||
zk_cluster, success_count, fail_count, record_date | ||
) values ( | ||
#{zkCluster}, #{successCount}, #{failCount}, #{recordDate} | ||
) on duplicate key update | ||
success_count = #{successCount}, | ||
fail_count = #{failCount} | ||
</insert> | ||
|
||
</mapper> |
84 changes: 84 additions & 0 deletions
84
.../job/console/com/vip/saturn/job/console/mybatis/repository/DomainCountRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.vip.saturn.job.console.com.vip.saturn.job.console.mybatis.repository; | ||
|
||
import com.vip.saturn.job.console.mybatis.entity.DomainCount; | ||
import com.vip.saturn.job.console.mybatis.repository.DomainCountRepository; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Ray Leung | ||
*/ | ||
@Ignore | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(locations = "classpath:applicationContext.xml") | ||
public class DomainCountRepositoryTest { | ||
|
||
@Autowired | ||
private DomainCountRepository domainCountRepository; | ||
|
||
private static EmbeddedDatabase embeddedDatabase; | ||
|
||
|
||
@BeforeClass | ||
public static void before() { | ||
EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder(); | ||
embeddedDatabaseBuilder.setType(EmbeddedDatabaseType.H2).addScript("classpath:global.sql") | ||
.addScript("classpath:schema.sql"); | ||
embeddedDatabase = embeddedDatabaseBuilder.build(); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void testCreateDomainCount() { | ||
domainCountRepository.createOrUpdateDomainCount("testCluster", new Date(), 0, 0); | ||
DomainCount domainCount = domainCountRepository.selectByZkClusterAndRecordDate("testCluster", new Date()); | ||
Assert.assertEquals(0, domainCount.getSuccessCount()); | ||
Assert.assertEquals(0, domainCount.getFailCount()); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void testUpdateDomainCount() { | ||
Date dateNow = new Date(); | ||
domainCountRepository.createOrUpdateDomainCount("testCluster", dateNow, 0, 0); | ||
DomainCount domainCount = domainCountRepository.selectByZkClusterAndRecordDate("testCluster", new Date()); | ||
Assert.assertEquals(0, domainCount.getSuccessCount()); | ||
Assert.assertEquals(0, domainCount.getFailCount()); | ||
domainCountRepository.createOrUpdateDomainCount("testCluster", dateNow, 10, 10); | ||
domainCount = domainCountRepository.selectByZkClusterAndRecordDate("testCluster", new Date()); | ||
Assert.assertEquals(10, domainCount.getSuccessCount()); | ||
Assert.assertEquals(10, domainCount.getFailCount()); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void testSelectByZkClusterAndFromStartDateToEndDate() { | ||
|
||
domainCountRepository.createOrUpdateDomainCount("testCluster", new Date(), 0, 0); | ||
Calendar d1 = Calendar.getInstance(); | ||
d1.add(Calendar.DATE, -1); | ||
Calendar d2 = Calendar.getInstance(); | ||
d2.add(Calendar.DATE, -2); | ||
domainCountRepository.createOrUpdateDomainCount("testCluster", d1.getTime(), 0, 0); | ||
domainCountRepository.createOrUpdateDomainCount("testCluster", d2.getTime(), 0, 0); | ||
|
||
List<DomainCount> domainCounts = domainCountRepository | ||
.selectByZkClusterAndFromStartDateToEndDate("testCluster", d2.getTime(), d1.getTime()); | ||
Assert.assertEquals(2, domainCounts.size()); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
saturn-console-api/src/test/resources/applicationContext.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> | ||
|
||
<!--<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">--> | ||
<!--<property name="driverClassName" value="org.h2.Driver"/>--> | ||
<!--<property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false"/>--> | ||
<!--<property name="username" value="sa"/>--> | ||
<!--<property name="password" value=""/>--> | ||
<!--</bean>--> | ||
|
||
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close" | ||
lazy-init="true"> | ||
<property name="driverClassName" value="com.mysql.jdbc.Driver"/> | ||
<property name="url" value="jdbc:mysql://localhost:3306/saturn"/> | ||
<property name="username" value="root"/> | ||
<property name="password" value="890707"/> | ||
<property name="initialSize" value="1"/> | ||
<property name="minIdle" value="1"/> | ||
<property name="maxActive" value="20"/> | ||
<property name="maxWait" value="60000"/> | ||
<property name="timeBetweenEvictionRunsMillis" value="60000"/> | ||
<property name="minEvictableIdleTimeMillis" value="300000"/> | ||
<property name="validationQuery" value="SELECT 'x'"/> | ||
<property name="testOnBorrow" value="false"/> | ||
<property name="testOnReturn" value="false"/> | ||
<property name="testWhileIdle" value="true"/> | ||
</bean> | ||
|
||
|
||
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> | ||
<property name="basePackage" value="com.vip.saturn.job.console.mybatis.repository"/> | ||
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> | ||
<property name="annotationClass" value="org.springframework.stereotype.Repository"/> | ||
</bean> | ||
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> | ||
<property name="dataSource" ref="dataSource"/> | ||
<property name="typeAliasesPackage" value="com.vip.saturn.job.console.mybatis.entity"/> | ||
<property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"/> | ||
</bean> | ||
|
||
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> | ||
<property name="dataSource" ref="dataSource"/> | ||
</bean> | ||
|
||
<tx:annotation-driven transaction-manager="txManager"/> | ||
|
||
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SET MODE MySQL; |
Oops, something went wrong.