1
2
3
4
5
6
7 package org.miloss.fgsms.statistics.jobs;
8
9 import java.sql.Connection;
10 import java.sql.PreparedStatement;
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13 import java.util.ArrayList;
14 import java.util.Calendar;
15 import java.util.GregorianCalendar;
16 import java.util.List;
17 import org.apache.log4j.Level;
18 import org.miloss.fgsms.common.DBUtils;
19 import org.miloss.fgsms.services.interfaces.dataaccessservice.OperationalRecord;
20 import static org.miloss.fgsms.statistics.FgsmsStatsv2.log;
21
22
23
24
25
26 public abstract class BaseJob implements Runnable{
27
28 protected long getSLACount(final String url, final Long ts, Connection con) {
29 long r = 0;
30 PreparedStatement cmd = null;
31 ResultSet rs = null;
32 try {
33 cmd = con.prepareStatement("select count(*) from slaviolations where uri=? and utcdatetime > ?");
34 cmd.setString(1, url);
35 cmd.setLong(2, System.currentTimeMillis() - ts);
36 rs = cmd.executeQuery();
37 if (rs.next()) {
38 r = rs.getLong(1);
39 }
40 } catch (Exception ex) {
41 log.log(Level.ERROR, null, ex);
42 } finally {
43 DBUtils.safeClose(rs);
44 DBUtils.safeClose(cmd);
45 }
46 return r;
47 }
48 protected void insertRow(Connection perf, final String url, final String action, final long period) {
49 PreparedStatement prepareStatement = null;
50 try {
51 prepareStatement = perf.prepareStatement("BEGIN WORK;"
52 + ";LOCK TABLE agg2 IN ACCESS EXCLUSIVE MODE;"
53 + "INSERT INTO agg2(uri, soapaction, timestampepoch, timerange) SELECT ?, ?, ?, ? WHERE NOT EXISTS"
54 + "(Select uri, soapaction, timerange from agg2 where uri=? and soapaction=? and timerange=?);"
55 + "COMMIT WORK;");
56
57 prepareStatement.setString(1, url);
58 prepareStatement.setString(2, action);
59 prepareStatement.setLong(3, System.currentTimeMillis());
60 prepareStatement.setLong(4, period);
61 prepareStatement.setString(5, url);
62 prepareStatement.setString(6, action);
63 prepareStatement.setLong(7, period);
64 prepareStatement.executeUpdate();
65 prepareStatement.close();
66 } catch (SQLException ex) {
67 log.log(Level.FATAL, "unable to insert row, attempting rollback", ex);
68 PreparedStatement rollback = null;
69 try {
70 rollback = perf.prepareStatement("ROLLBACK WORK;");
71 rollback.execute();
72 } catch (Throwable x) {
73 log.log(Level.WARN, "unable to roll back transaction", ex);
74 } finally {
75 DBUtils.safeClose(rollback);
76 }
77 } finally {
78 DBUtils.safeClose(prepareStatement);
79 }
80 }
81
82 protected double getAvailability(final long now, final long limit, final String uri, final String action, Connection PerfCon, Connection config) {
83 try {
84 List<OperationalRecord> res = new ArrayList<OperationalRecord>();
85
86 PreparedStatement com = PerfCon.prepareStatement("SELECT uri, utcdatetime, status, id, message "
87 + "FROM availability where utcdatetime <= ? and uri=? order by utcdatetime desc limit 1");
88 com.setString(2, uri);
89 com.setLong(1, now - limit);
90 ResultSet rs = com.executeQuery();
91 while (rs.next()) {
92 OperationalRecord cur = new OperationalRecord();
93 cur.setID(rs.getString("id"));
94 cur.setOperational(rs.getBoolean("status"));
95 cur.setMessage(rs.getString("message"));
96 GregorianCalendar cal = new GregorianCalendar();
97 cal.setTimeInMillis(now - limit);
98 cur.setTimestamp((cal));
99 res.add(cur);
100 }
101 rs.close();
102 com.close();
103
104
105 com = PerfCon.prepareStatement("SELECT uri, utcdatetime, status, id, message "
106 + "FROM availability where utcdatetime > ? and uri=? order by utcdatetime asc");
107 com.setString(2, uri);
108 com.setLong(1, now - limit);
109 rs = com.executeQuery();
110 while (rs.next()) {
111 OperationalRecord cur = new OperationalRecord();
112 cur.setID(rs.getString("id"));
113 cur.setOperational(rs.getBoolean("status"));
114 cur.setMessage(rs.getString("message"));
115 GregorianCalendar cal = new GregorianCalendar();
116 cal.setTimeInMillis(rs.getLong("utcdatetime"));
117 cur.setTimestamp((cal));
118 res.add(cur);
119 }
120 rs.close();
121 com.close();
122
123 if (res.isEmpty()) {
124
125 log.log(Level.DEBUG, "special case for " + uri + " no status data is available, return 0");
126 return 0;
127 }
128
129 GregorianCalendar cal = new GregorianCalendar();
130 cal.setTimeInMillis(System.currentTimeMillis());
131 OperationalRecord cur = new OperationalRecord();
132 cur.setOperational(res.get(res.size() - 1).isOperational());
133 cur.setTimestamp((cal));
134 res.add(cur);
135
136 long totaluptime = 0;
137 long totaldowntime = 0;
138
139
140 for (int i = 1; i < res.size(); i++) {
141 Calendar tempcal = res.get(i).getTimestamp();
142
143 long current = (tempcal.getTimeInMillis());
144 long previous = res.get(i - 1).getTimestamp().getTimeInMillis();
145
146 if (res.get(i - 1).isOperational()) {
147 totaluptime += current - previous;
148 } else {
149 totaldowntime += current - previous;
150 }
151
152 }
153
154
155 if (res.get(res.size() - 1).isOperational()) {
156 totaluptime += System.currentTimeMillis() - (res.get(res.size() - 1).getTimestamp().getTimeInMillis());
157 } else {
158
159 totaldowntime += System.currentTimeMillis() - (res.get(res.size() - 1).getTimestamp().getTimeInMillis());
160 }
161
162 double d = (double) ((double) totaluptime / (double) (totaldowntime + totaluptime)) * 100.0;
163
164
165
166
167
168
169
170
171
172 return d;
173
174 } catch (Exception ex) {
175 log.log(Level.WARN, "error calculating availability", ex);
176 }
177 return 0;
178 }
179
180 }