View Javadoc
1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   package org.miloss.fgsms.statistics.jobs;
7   
8   import java.sql.Connection;
9   import java.sql.PreparedStatement;
10  import java.sql.ResultSet;
11  import java.sql.SQLException;
12  import java.util.ArrayList;
13  import java.util.Calendar;
14  import java.util.GregorianCalendar;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.UUID;
18  import org.apache.log4j.Level;
19  import org.miloss.fgsms.agentcore.MessageProcessor;
20  import org.miloss.fgsms.common.DBUtils;
21  import org.miloss.fgsms.common.Utility;
22  import org.miloss.fgsms.services.interfaces.common.PolicyType;
23  import org.miloss.fgsms.services.interfaces.dataaccessservice.OperationalRecord;
24  import static org.miloss.fgsms.statistics.FgsmsStatsv2.SERVICE_NAME;
25  import static org.miloss.fgsms.statistics.FgsmsStatsv2.allitems;
26  import static org.miloss.fgsms.statistics.FgsmsStatsv2.log;
27  import static org.miloss.fgsms.statistics.FgsmsStatsv2.myUrl;
28  
29  /**
30   *
31   * @author AO
32   */
33  public class StatusStatisticsJob extends BaseJob implements Runnable {
34  
35      List<Long> periods;
36  
37      public StatusStatisticsJob(List<Long> periods) {
38          this.periods = periods;
39      }
40  
41      @Override
42      public void run() {
43          UUID random = UUID.randomUUID();
44          MessageProcessor.getSingletonObject().processMessageInput(SERVICE_NAME, 0, myUrl, "status", "system", random.toString(), new HashMap(), "", this.getClass().getCanonicalName(), "", "");
45          Connection ConfigCon = Utility.getConfigurationDBConnection();
46          Connection PerfCon = Utility.getPerformanceDBConnection();
47          try {
48              doWorkForStatusItemsOnly(ConfigCon, PerfCon, periods);
49              MessageProcessor.getSingletonObject().processMessageOutput(random.toString(), "success", 0, false, System.currentTimeMillis(), new HashMap());
50          } catch (Exception ex) {
51              MessageProcessor.getSingletonObject().processMessageOutput(random.toString(), "error " + ex.getMessage(), 0, true, System.currentTimeMillis(), new HashMap());
52              log.log(Level.ERROR, null, ex);
53          } finally {
54              DBUtils.safeClose(PerfCon);
55              DBUtils.safeClose(ConfigCon);
56          }
57  
58      }
59  
60      private void doWorkForStatusItemsOnly(Connection ConfigCon, Connection PerfCon, List<Long> periods) throws Exception {
61          try {
62              long now = System.currentTimeMillis();
63  
64              if (ConfigCon == null || PerfCon == null) {
65                  log.log(Level.ERROR, "doWorkForStatusItemsOnly database unavailable");
66                  return;
67              }
68              PreparedStatement com = ConfigCon.prepareStatement("select uri,policytype from servicepolicies where policytype=?;");
69              com.setInt(1, PolicyType.STATUS.ordinal());
70              ResultSet rs = com.executeQuery();
71              while (rs.next()) {          //for each service
72  
73                  log.log(Level.INFO, "calculating statistics for " + rs.getString("uri"));
74                  String t = allitems;
75                  //do rollup
76                  for (int i = 0; i < periods.size(); i++) {
77                      insertRow(PerfCon, rs.getString("uri"), t, periods.get(i));
78                      PreparedStatement up = PerfCon.prepareStatement("UPDATE agg2 set "
79                              + " avail=?, "
80                              + "  timestampepoch=?,  "
81                              + "success=-1, failure=-1, avgres=-1, sla=?, mtbf=-1, maxreq=-1,  maxres=-1, maxresponsetime=-1 "
82                              + "WHERE uri=? and soapaction=? and timerange=?;");
83  
84                      double avail = getAvailability(now, periods.get(i), rs.getString("uri"), t, PerfCon, ConfigCon);
85                      long sla = getSLACount(rs.getString("uri"), periods.get(i), PerfCon);
86                      up.setDouble(1, avail);
87                      up.setLong(2, now);
88                      up.setLong(3, sla);
89                      up.setString(4, rs.getString("uri"));
90                      up.setString(5, t);
91                      up.setLong(6, periods.get(i));
92                      up.executeUpdate();
93                      log.log(Level.DEBUG, "Updated stats for service " + rs.getString("uri") + " action " + t);
94                      up.close();
95                  }
96              }
97              rs.close();
98              com.close();
99  
100             now = System.currentTimeMillis() - now;
101             log.log(Level.INFO, "Statistics calculations took " + now + "ms");
102 
103         } catch (Exception ex) {
104             //System.console().writer().println("caught error!" + ex.getLocalizedMessage());
105             log.log(Level.ERROR, null, ex);
106             throw ex;
107         }
108     }
109 
110 }