You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.7 KiB
51 lines
1.7 KiB
package app.updates;
|
|
|
|
import app.entities.Stats;
|
|
import jakarta.annotation.PostConstruct;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
|
import org.springframework.context.event.EventListener;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.jdbc.core.RowMapper;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.time.Instant;
|
|
|
|
@Component
|
|
public class BanCountUpdater extends BaseUpdater{
|
|
|
|
@Autowired
|
|
@Qualifier("jt_ro")
|
|
private JdbcTemplate jdbcTemplate;
|
|
private Stats stats;
|
|
@Value("${backend.updates.ban_count}")
|
|
boolean update = false;
|
|
private final Logger logger = LoggerFactory.getLogger(BanCountUpdater.class);
|
|
|
|
@Autowired
|
|
public BanCountUpdater(Stats stats) {
|
|
this.stats = stats;
|
|
}
|
|
|
|
@EventListener(ApplicationReadyEvent.class)
|
|
public void SetUpdater(){
|
|
if(update) {
|
|
logger.warn("Updater enabled");
|
|
CreateTaskUpdater(this::UpdateBanCount, 5 * 60 * 1000, getClass().getName());
|
|
}
|
|
}
|
|
|
|
public boolean UpdateBanCount(){
|
|
logger.info("Update current ban count");
|
|
long ban_count = jdbcTemplate.query("SELECT COUNT(*) as count FROM light_bans WHERE active = true", (rs, n) -> rs.getLong("count")).stream().findFirst().orElse(0L);
|
|
stats.setBan_count(ban_count);
|
|
stats.getUpdates().merge("ban_count", Instant.now().getEpochSecond(), (x, y) -> y);
|
|
return true;
|
|
}
|
|
}
|
|
|