1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.jumpmind.symmetric.service.impl;
21
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.List;
27
28 import org.jumpmind.symmetric.model.StatisticAlertThresholds;
29 import org.jumpmind.symmetric.service.IStatisticService;
30 import org.jumpmind.symmetric.statistic.Statistic;
31 import org.jumpmind.symmetric.util.AppUtils;
32 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
33 import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
34
35 public class StatisticService extends AbstractService implements IStatisticService {
36
37 public void save(Collection<Statistic> stats, Date captureEndTime) {
38 if (stats != null) {
39 for (Statistic statistic : stats) {
40 jdbcTemplate.update(getSql("insertStatisticSql"), new Object[] { statistic.getNodeId(),
41 AppUtils.getServerId(), statistic.getName().name(), statistic.getCaptureStartTimeMs(),
42 captureEndTime, statistic.getTotal(), statistic.getCount() });
43 }
44 }
45 }
46
47 public List<StatisticAlertThresholds> getAlertThresholds() {
48 return getSimpleTemplate().query(getSql("getAlertThresholdsSql"),
49 new ParameterizedRowMapper<StatisticAlertThresholds>() {
50 public StatisticAlertThresholds mapRow(ResultSet rs, int rowNum) throws SQLException {
51 return new StatisticAlertThresholds(rs.getString("statistic_name"), rs
52 .getBigDecimal("threshold_total_max"), rs.getLong("threshold_count_max"), rs
53 .getBigDecimal("threshold_total_min"), rs.getLong("threshold_count_min"), rs
54 .getBigDecimal("threshold_avg_max"), rs.getBigDecimal("threshold_avg_min"));
55 }
56 });
57 }
58
59 public void saveStatisticAlertThresholds(StatisticAlertThresholds threshold) {
60 SimpleJdbcTemplate template = getSimpleTemplate();
61 int updated = template.update(getSql("updateAlertThresholdsSql"), threshold.getThresholdTotalMax(), threshold
62 .getThresholdCountMax(), threshold.getThresholdAvgMax(), threshold.getThresholdTotalMin(), threshold
63 .getThresholdCountMin(), threshold.getThresholdAvgMin(), threshold.getStatisticName());
64 if (updated == 0) {
65 template.update(getSql("insertAlertThresholdsSql"), threshold.getStatisticName(), threshold
66 .getThresholdTotalMax(), threshold.getThresholdCountMax(), threshold.getThresholdAvgMax(),
67 threshold.getThresholdTotalMin(), threshold.getThresholdCountMin(), threshold
68 .getThresholdAvgMin());
69 }
70 }
71
72 public boolean removeStatisticAlertThresholds(String statisticName) {
73 return 1 == getSimpleTemplate().update(getSql("deleteAlertThresholdsSql"), statisticName);
74 }
75 }