Search in sources :

Example 1 with JobRunningStatistics

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

the class StatisticRdbRepository method findLatestJobRunningStatistics.

/**
     * 获取最近一条运行中的任务统计数据.
     * 
     * @return 运行中的任务统计数据对象
     */
public Optional<JobRunningStatistics> findLatestJobRunningStatistics() {
    JobRunningStatistics result = null;
    String sql = String.format("SELECT id, running_count, statistics_time, creation_time FROM %s order by id DESC LIMIT 1", TABLE_JOB_RUNNING_STATISTICS);
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery()) {
        while (resultSet.next()) {
            result = new JobRunningStatistics(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 jobRunningStatistics from DB error:", ex);
    }
    return Optional.fromNullable(result);
}
Also used : SQLException(java.sql.SQLException) JobRunningStatistics(com.dangdang.ddframe.job.statistics.type.job.JobRunningStatistics) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date)

Example 2 with JobRunningStatistics

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

the class StatisticRdbRepository method findJobRunningStatistics.

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

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

the class StatisticRdbRepositoryTest method assertFindLatestJobRunningStatistics.

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

Example 4 with JobRunningStatistics

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

the class JobRunningStatisticJob method statisticJob.

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

Example 5 with JobRunningStatistics

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

the class StatisticManagerTest method assertFindJobRunningStatisticsWhenRdbIsConfigured.

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

Aggregations

JobRunningStatistics (com.dangdang.ddframe.job.statistics.type.job.JobRunningStatistics)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 TaskRunningStatistics (com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics)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