#include "DatabaseThreadShared.h" AlertId getLastAlertId(DatabaseWithRetry &database, LastIdCacheInfo &cache, bool live) { TimeVal currentTime(true); if ((cache.lastAlertIdValidCount > 0) || (cache.nextAlertIdCheck < currentTime)) { static const std::string sql[] = { "SELECT MAX(id) FROM alerts", /* "SELECT id FROM alerts" " WHERE timestamp < NOW() - INTERVAL 6 HOUR" " ORDER BY timestamp DESC LIMIT 1", */ "SELECT id FROM alerts" " WHERE timestamp < NOW() - INTERVAL 20 MINUTE" " ORDER BY timestamp DESC LIMIT 1" }; DatabaseWithRetry::ResultList result = database.tryAllUntilSuccess(sql, sql+2); cache.lastAlertId = result[0]->getIntegerField(0, -1); cache.delayedAlertId = result[1]->getIntegerField(0, -1); if ((cache.lastAlertId == -1) || (cache.delayedAlertId == -1)) { cache.nextAlertIdCheck.clear(); cache.lastAlertIdValidCount = 0; cache.lastAlertId = -1; cache.delayedAlertId = -1; } else { cache.nextAlertIdCheck = currentTime; cache.nextAlertIdCheck.addSeconds(1); cache.lastAlertIdValidCount = 15; } } else { cache.lastAlertIdValidCount--; } return live?cache.lastAlertId:cache.delayedAlertId; }