Search in sources :

Example 1 with TaskResultStatistics

use of com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics in project elastic-job by dangdangdotcom.

the class TaskResultStatisticJobTest method assertExecuteWhenRepositoryIsEmpty.

@Test
public void assertExecuteWhenRepositoryIsEmpty() throws SchedulerException {
    Optional<TaskResultStatistics> latestOne = Optional.absent();
    for (StatisticInterval each : StatisticInterval.values()) {
        taskResultStatisticJob.setStatisticInterval(each);
        when(repository.findLatestTaskResultStatistics(each)).thenReturn(latestOne);
        when(repository.add(any(TaskResultStatistics.class))).thenReturn(true);
        taskResultStatisticJob.execute(null);
        verify(repository).findLatestTaskResultStatistics(each);
    }
    verify(repository, times(3)).add(any(TaskResultStatistics.class));
}
Also used : TaskResultStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics) StatisticInterval(com.dangdang.ddframe.job.statistics.StatisticInterval) Test(org.junit.Test)

Example 2 with TaskResultStatistics

use of com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics in project elastic-job by dangdangdotcom.

the class StatisticRdbRepository method findLatestTaskResultStatistics.

/**
     * 获取最近一条任务运行结果统计数据.
     * 
     * @param statisticInterval 统计时间间隔
     * @return 任务运行结果统计数据对象
     */
public Optional<TaskResultStatistics> findLatestTaskResultStatistics(final StatisticInterval statisticInterval) {
    TaskResultStatistics result = null;
    String sql = String.format("SELECT id, success_count, failed_count, statistics_time, creation_time FROM %s order by id DESC LIMIT 1", TABLE_TASK_RESULT_STATISTICS + "_" + statisticInterval);
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery()) {
        while (resultSet.next()) {
            result = new TaskResultStatistics(resultSet.getLong(1), resultSet.getInt(2), resultSet.getInt(3), statisticInterval, new Date(resultSet.getTimestamp(4).getTime()), new Date(resultSet.getTimestamp(5).getTime()));
        }
    } catch (final SQLException ex) {
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error("Fetch latest taskResultStatistics from DB error:", ex);
    }
    return Optional.fromNullable(result);
}
Also used : TaskResultStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date)

Example 3 with TaskResultStatistics

use of com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics in project elastic-job by dangdangdotcom.

the class StatisticRdbRepository method getSummedTaskResultStatistics.

/**
     * 获取合计后的任务运行结果统计数据.
     * 
     * @param from 统计开始时间
     * @param statisticInterval 统计时间间隔
     * @return 合计后的任务运行结果统计数据对象
     */
public TaskResultStatistics getSummedTaskResultStatistics(final Date from, final StatisticInterval statisticInterval) {
    TaskResultStatistics result = new TaskResultStatistics(0, 0, statisticInterval, new Date());
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String sql = String.format("SELECT sum(success_count), sum(failed_count) FROM %s WHERE statistics_time >= '%s'", TABLE_TASK_RESULT_STATISTICS + "_" + statisticInterval, formatter.format(from));
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery()) {
        while (resultSet.next()) {
            result = new TaskResultStatistics(resultSet.getInt(1), resultSet.getInt(2), statisticInterval, new Date());
        }
    } catch (final SQLException ex) {
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error("Fetch summed taskResultStatistics from DB error:", ex);
    }
    return result;
}
Also used : TaskResultStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with TaskResultStatistics

use of com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics in project elastic-job by dangdangdotcom.

the class StatisticRdbRepository method findTaskResultStatistics.

/**
     * 获取任务运行结果统计数据集合.
     * 
     * @param from 统计开始时间
     * @param statisticInterval 统计时间间隔
     * @return 任务运行结果统计数据集合
     */
public List<TaskResultStatistics> findTaskResultStatistics(final Date from, final StatisticInterval statisticInterval) {
    List<TaskResultStatistics> result = new LinkedList<>();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String sql = String.format("SELECT id, success_count, failed_count, statistics_time, creation_time FROM %s WHERE statistics_time >= '%s' order by id ASC", TABLE_TASK_RESULT_STATISTICS + "_" + statisticInterval, formatter.format(from));
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery()) {
        while (resultSet.next()) {
            TaskResultStatistics taskResultStatistics = new TaskResultStatistics(resultSet.getLong(1), resultSet.getInt(2), resultSet.getInt(3), statisticInterval, new Date(resultSet.getTimestamp(4).getTime()), new Date(resultSet.getTimestamp(5).getTime()));
            result.add(taskResultStatistics);
        }
    } catch (final SQLException ex) {
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error("Fetch taskResultStatistics from DB error:", ex);
    }
    return result;
}
Also used : TaskResultStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SimpleDateFormat(java.text.SimpleDateFormat) LinkedList(java.util.LinkedList) Date(java.util.Date)

Example 5 with TaskResultStatistics

use of com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics in project elastic-job by dangdangdotcom.

the class StatisticRdbRepositoryTest method assertFindTaskResultStatisticsWithDifferentFromDate.

@Test
public void assertFindTaskResultStatisticsWithDifferentFromDate() {
    Date now = new Date();
    Date yesterday = getYesterday();
    for (StatisticInterval each : StatisticInterval.values()) {
        assertTrue(repository.add(new TaskResultStatistics(100, 0, each, yesterday)));
        assertTrue(repository.add(new TaskResultStatistics(100, 0, each, now)));
        assertThat(repository.findTaskResultStatistics(yesterday, each).size(), is(2));
        assertThat(repository.findTaskResultStatistics(now, each).size(), is(1));
    }
}
Also used : TaskResultStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics) Date(java.util.Date) StatisticInterval(com.dangdang.ddframe.job.statistics.StatisticInterval) Test(org.junit.Test)

Aggregations

TaskResultStatistics (com.dangdang.ddframe.job.statistics.type.task.TaskResultStatistics)16 Test (org.junit.Test)12 StatisticInterval (com.dangdang.ddframe.job.statistics.StatisticInterval)10 Date (java.util.Date)10 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 SimpleDateFormat (java.text.SimpleDateFormat)2 LinkedList (java.util.LinkedList)1