X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Fclub%2Fwpia%2Fgigi%2FdbObjects%2FDomainPingConfiguration.java;h=59db66cda5e1623f3401537b0d208ae05c175584;hp=bdaad75880a5acb1e0c9dd09428a9be7597df19e;hb=7b709637bb12efc4a593a5ca6f312ed27566dad4;hpb=056e2d36653e0dfdfa520651c1aa4e70df82ea1a diff --git a/src/club/wpia/gigi/dbObjects/DomainPingConfiguration.java b/src/club/wpia/gigi/dbObjects/DomainPingConfiguration.java index bdaad758..59db66cd 100644 --- a/src/club/wpia/gigi/dbObjects/DomainPingConfiguration.java +++ b/src/club/wpia/gigi/dbObjects/DomainPingConfiguration.java @@ -1,5 +1,6 @@ package club.wpia.gigi.dbObjects; +import java.sql.Timestamp; import java.util.Date; import club.wpia.gigi.Gigi; @@ -92,4 +93,47 @@ public class DomainPingConfiguration implements IdCachable { } 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))); } + + /** + * Return true when there was a last execution and it succeeded. + * + * @return if this ping is currently valid. + */ + public boolean isValid() { + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT state='success' AS bool from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) { + ps.setInt(1, id); + GigiResultSet rs = ps.executeQuery(); + if ( !rs.next()) { + return false; + } + return rs.getBoolean(1); + } + } + + /** + * Return true when this ping has not been successful within the last 2 + * weeks. + * + * @param time + * the point in time for which the determination is carried out. + * @return the value for this ping. + */ + public boolean isStrictlyInvalid(Date time) { + Date lastSuccess = getLastSuccess(); + if (lastSuccess.getTime() == 0) { + // never a successful ping + return true; + } + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='failed' AND `when` > ? ORDER BY `when` ASC LIMIT 1")) { + ps.setInt(1, id); + ps.setTimestamp(2, new Timestamp(lastSuccess.getTime())); + GigiResultSet rs = ps.executeQuery(); + if (rs.next()) { + Date turnedInvalid = new Date(rs.getTimestamp("stamp").getTime()); + // turned invalid older than 2 weeks ago + return turnedInvalid.getTime() < time.getTime() - 2L * 7 * 24 * 60 * 60 * 1000; + } + return false; + } + } }