Search in sources :

Example 1 with TaskRunningStatistics

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

the class StatisticRdbRepositoryTest method assertFindLatestTaskRunningStatistics.

@Test
public void assertFindLatestTaskRunningStatistics() {
    repository.add(new TaskRunningStatistics(100, new Date()));
    repository.add(new TaskRunningStatistics(200, new Date()));
    Optional<TaskRunningStatistics> po = repository.findLatestTaskRunningStatistics();
    assertThat(po.get().getRunningCount(), is(200));
}
Also used : TaskRunningStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics) Date(java.util.Date) Test(org.junit.Test)

Example 2 with TaskRunningStatistics

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

the class StatisticRdbRepositoryTest method assertFindTaskRunningStatisticsWithDifferentFromDate.

@Test
public void assertFindTaskRunningStatisticsWithDifferentFromDate() {
    Date now = new Date();
    Date yesterday = getYesterday();
    assertTrue(repository.add(new TaskRunningStatistics(100, yesterday)));
    assertTrue(repository.add(new TaskRunningStatistics(100, now)));
    assertThat(repository.findTaskRunningStatistics(yesterday).size(), is(2));
    assertThat(repository.findTaskRunningStatistics(now).size(), is(1));
}
Also used : Date(java.util.Date) TaskRunningStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics) Test(org.junit.Test)

Example 3 with TaskRunningStatistics

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

the class JobRunningStatisticJob method statisticTask.

private void statisticTask(final int runningCount) {
    Optional<TaskRunningStatistics> latestOne = repository.findLatestTaskRunningStatistics();
    if (latestOne.isPresent()) {
        fillBlankIfNeeded(latestOne.get());
    }
    TaskRunningStatistics taskRunningStatistics = new TaskRunningStatistics(runningCount, StatisticTimeUtils.getCurrentStatisticTime(execInterval));
    log.debug("Add taskRunningStatistics, runningCount is:{}", runningCount);
    repository.add(taskRunningStatistics);
}
Also used : TaskRunningStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics)

Example 4 with TaskRunningStatistics

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

the class StatisticManagerTest method assertFindTaskRunningStatisticsWhenRdbIsConfigured.

@Test
public void assertFindTaskRunningStatisticsWhenRdbIsConfigured() throws NoSuchFieldException {
    ReflectionUtils.setFieldValue(statisticManager, "rdbRepository", rdbRepository);
    when(rdbRepository.findTaskRunningStatistics(any(Date.class))).thenReturn(Lists.newArrayList(new TaskRunningStatistics(10, new Date())));
    assertThat(statisticManager.findTaskRunningStatisticsWeekly().size(), is(1));
    verify(rdbRepository).findTaskRunningStatistics(any(Date.class));
}
Also used : Date(java.util.Date) TaskRunningStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics) Test(org.junit.Test)

Example 5 with TaskRunningStatistics

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

the class StatisticRdbRepository method findLatestTaskRunningStatistics.

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

Aggregations

TaskRunningStatistics (com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics)7 Date (java.util.Date)5 Test (org.junit.Test)4 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 TaskContext (com.dangdang.ddframe.job.context.TaskContext)1 JobRunningStatistics (com.dangdang.ddframe.job.statistics.type.job.JobRunningStatistics)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1