Skip to content

Commit

Permalink
#527 初次提交
Browse files Browse the repository at this point in the history
  • Loading branch information
lzw2006 committed Oct 23, 2018
1 parent ed7d02d commit e7f500d
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 10 deletions.
20 changes: 20 additions & 0 deletions saturn-console-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,30 @@
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>test</scope>
</dependency>
<!-- DB end -->

<!-- utils begin -->
Expand Down
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;
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public void persist(StatisticsModel statisticsModel, ZkCluster zkCluster) {
// 全域当天处理总数,失败总数
saveOrUpdateDomainProcessCount(
new ZkStatistics(statisticsModel.getZkClusterDailyCountAnalyzer().getTotalCount(),
statisticsModel.getZkClusterDailyCountAnalyzer().getErrorCount()),
zkCluster.getZkAddr());
statisticsModel.getZkClusterDailyCountAnalyzer().getErrorCount()), zkCluster.getZkAddr());

saveOrUpdateDomainProcessCountHistory(
new ZkStatistics(statisticsModel.getZkClusterDailyCountAnalyzer().getTotalCount(),
statisticsModel.getZkClusterDailyCountAnalyzer().getErrorCount()), zkCluster.getZkAddr());


// 失败率Top10的域列表
saveOrUpdateTop10FailDomain(statisticsModel.getDomainStatisticsAnalyzer().getDomainList(),
Expand Down Expand Up @@ -93,6 +97,15 @@ public void persist(StatisticsModel statisticsModel, ZkCluster zkCluster) {
saveOrUpdateJobCount(jobList.size(), zkCluster.getZkAddr());
}

private void saveOrUpdateDomainProcessCountHistory(ZkStatistics zkStatistics, String zkAddr) {

int successCount = zkStatistics.getCount();
int failCount = zkStatistics.getError();

// saturnStatisticsService.

}

private void saveOrUpdateJobCount(int jobCount, String zkAddr) {
try {
String jobCountString = JSON.toJSONString(jobCount);
Expand Down Expand Up @@ -174,8 +187,8 @@ private void saveOrUpdateJobRankDistribution(List<JobStatistics> jobList, String
private void saveOrUpdateTop10FailExecutor(List<ExecutorStatistics> executorList, String zkAddr) {
try {
executorList = DashboardServiceHelper.sortExecutorByFailureRate(executorList);
List<ExecutorStatistics> top10FailExecutor = executorList.subList(0,
executorList.size() > 9 ? 10 : executorList.size());
List<ExecutorStatistics> top10FailExecutor = executorList
.subList(0, executorList.size() > 9 ? 10 : executorList.size());
String top10FailExecutorJsonString = JSON.toJSONString(top10FailExecutor);
SaturnStatistics top10FailExecutorFromDB = saturnStatisticsService
.findStatisticsByNameAndZkList(StatisticsTableKeyConstant.TOP_10_FAIL_EXECUTOR, zkAddr);
Expand All @@ -196,8 +209,8 @@ private void saveOrUpdateTop10FailExecutor(List<ExecutorStatistics> executorList
private void saveOrUpdateTop10FailDomain(List<DomainStatistics> domainList, String zkAddr) {
try {
domainList = DashboardServiceHelper.sortDomainByAllTimeFailureRate(domainList);
List<DomainStatistics> top10FailDomainList = domainList.subList(0,
domainList.size() > 9 ? 10 : domainList.size());
List<DomainStatistics> top10FailDomainList = domainList
.subList(0, domainList.size() > 9 ? 10 : domainList.size());
String top10FailDomainJsonString = JSON.toJSONString(top10FailDomainList);
SaturnStatistics top10FailDomainFromDB = saturnStatisticsService
.findStatisticsByNameAndZkList(StatisticsTableKeyConstant.TOP_10_FAIL_DOMAIN, zkAddr);
Expand All @@ -217,8 +230,8 @@ private void saveOrUpdateTop10FailDomain(List<DomainStatistics> domainList, Stri
private void saveOrUpdateTop10UnstableDomain(List<DomainStatistics> domainList, String zkAddr) {
try {
domainList = DashboardServiceHelper.sortDomainByShardingCount(domainList);
List<DomainStatistics> top10UnstableDomain = domainList.subList(0,
domainList.size() > 9 ? 10 : domainList.size());
List<DomainStatistics> top10UnstableDomain = domainList
.subList(0, domainList.size() > 9 ? 10 : domainList.size());
String top10UnstableDomainJsonString = JSON.toJSONString(top10UnstableDomain);
SaturnStatistics top10UnstableDomainFromDB = saturnStatisticsService
.findStatisticsByNameAndZkList(StatisticsTableKeyConstant.TOP_10_UNSTABLE_DOMAIN, zkAddr);
Expand Down Expand Up @@ -298,8 +311,8 @@ private void saveOrUpdateTop10LoadJob(List<JobStatistics> jobList, String zkAddr
private void saveOrUpdateTop10LoadExecutor(List<ExecutorStatistics> executorList, String zkAddr) {
try {
executorList = DashboardServiceHelper.sortExecutorByLoadLevel(executorList);
List<ExecutorStatistics> top10LoadExecutor = executorList.subList(0,
executorList.size() > 9 ? 10 : executorList.size());
List<ExecutorStatistics> top10LoadExecutor = executorList
.subList(0, executorList.size() > 9 ? 10 : executorList.size());
String top10LoadExecutorJsonString = JSON.toJSONString(top10LoadExecutor);
SaturnStatistics top10LoadExecutorFromDB = saturnStatisticsService
.findStatisticsByNameAndZkList(StatisticsTableKeyConstant.TOP_10_LOAD_EXECUTOR, zkAddr);
Expand Down
44 changes: 44 additions & 0 deletions saturn-console-api/src/main/resources/mapper/DomainCountMapper.xml
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>
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 saturn-console-api/src/test/resources/applicationContext.xml
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>
1 change: 1 addition & 0 deletions saturn-console-api/src/test/resources/global.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SET MODE MySQL;
Loading

0 comments on commit e7f500d

Please sign in to comment.