1 package club.wpia.gigi.dbObjects;
3 import java.sql.Timestamp;
6 import club.wpia.gigi.Gigi;
7 import club.wpia.gigi.GigiApiException;
8 import club.wpia.gigi.database.GigiPreparedStatement;
9 import club.wpia.gigi.database.GigiResultSet;
10 import club.wpia.gigi.output.template.SprintfCommand;
12 public class DomainPingConfiguration implements IdCachable {
14 private static final int REPING_MINIMUM_DELAY = 5 * 60 * 1000;
18 private Domain target;
20 private DomainPingType type;
24 private DomainPingConfiguration(int id) {
25 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?")) {
28 GigiResultSet rs = ps.executeQuery();
30 throw new IllegalArgumentException("Invalid pingconfig id " + id);
32 this.id = rs.getInt("id");
33 target = Domain.getById(rs.getInt("domainid"));
34 type = DomainPingType.valueOf(rs.getString("type").toUpperCase());
35 info = rs.getString("info");
44 public Domain getTarget() {
48 public DomainPingType getType() {
52 public String getInfo() {
56 private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
58 public static synchronized DomainPingConfiguration getById(int id) {
59 DomainPingConfiguration res = cache.get(id);
61 cache.put(res = new DomainPingConfiguration(id));
66 public Date getLastExecution() {
67 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) {
69 GigiResultSet rs = ps.executeQuery();
71 return new Date(rs.getTimestamp("stamp").getTime());
77 public Date getLastSuccess() {
78 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1")) {
80 GigiResultSet rs = ps.executeQuery();
82 return new Date(rs.getTimestamp("stamp").getTime());
88 public synchronized void requestReping() throws GigiApiException {
89 Date lastExecution = getLastExecution();
90 if (lastExecution.getTime() + REPING_MINIMUM_DELAY < System.currentTimeMillis()) {
91 Gigi.notifyPinger(this);
94 throw new GigiApiException(SprintfCommand.createSimple("Reping is only allowed after {0} minutes, yours end at {1}.", REPING_MINIMUM_DELAY / 60 / 1000, new Date(lastExecution.getTime() + REPING_MINIMUM_DELAY)));
98 * Return true when there was a last execution and it succeeded.
100 * @return if this ping is currently valid.
102 public boolean isValid() {
103 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT state='success' AS bool from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) {
105 GigiResultSet rs = ps.executeQuery();
109 return rs.getBoolean(1);
114 * Return true when this ping has not been successful within the last 2
118 * the point in time for which the determination is carried out.
119 * @return the value for this ping.
121 public boolean isStrictlyInvalid(Date time) {
122 Date lastSuccess = getLastSuccess();
123 if (lastSuccess.getTime() == 0) {
124 // never a successful ping
127 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='failed' AND `when` > ? ORDER BY `when` ASC LIMIT 1")) {
129 ps.setTimestamp(2, new Timestamp(lastSuccess.getTime()));
130 GigiResultSet rs = ps.executeQuery();
132 Date turnedInvalid = new Date(rs.getTimestamp("stamp").getTime());
133 // turned invalid older than 2 weeks ago
134 return turnedInvalid.getTime() < time.getTime() - 2L * 7 * 24 * 60 * 60 * 1000;