View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
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  }