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);
}
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;
}
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));
}
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);
}
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));
}
Aggregations