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.
 
 
 
 

109 lines
5.2 KiB

package app.updates;
import app.entities.server.Server;
import app.entities.Stats;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
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.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import static java.time.Instant.now;
@Component
@Transactional("RoTransactionManager")
public class UniqueUpdater extends BaseUpdater{
Stats stats;
EntityManager entityManager;
private final String query_total = "SELECT DISTINCT `account_id` FROM `%s`.`user_connections` WHERE `connection_type` LIKE \"disconnect\" AND `connect_duration` > 300 UNION\n";
private final String query_day = "SELECT DISTINCT `account_id` FROM `%s`.`user_connections` WHERE `connection_type` LIKE \"disconnect\" AND `connect_duration` > 300 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,'%%Y-%%m-%%d') as DATE) UNION\n";
private final String query_month = "SELECT DISTINCT `account_id` FROM `%s`.`user_connections` WHERE `connection_type` LIKE \"disconnect\" AND `connect_duration` > 300 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,'%%Y-%%m-01') as DATE) UNION\n";
private final String query_year = "SELECT DISTINCT `account_id` FROM `%s`.`user_connections` WHERE `connection_type` LIKE \"disconnect\" AND `connect_duration` > 300 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,'%%Y-01-01') as DATE) UNION\n";
private final Logger logger = LoggerFactory.getLogger(UniqueUpdater.class);
@Value("${backend.updates.unique_global}")
boolean global_update = false;
@Value("${backend.updates.unique_server}")
boolean server_update = false;
@Autowired
public UniqueUpdater(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) {
this.entityManager = entityManager;
this.stats = stats;
}
@PostConstruct
public void updateValues() throws InterruptedException {
if(global_update) {
logger.warn("Global updater enabled");
Thread.sleep(2000);
CreateTaskUpdater(this::UpdateServerUniqueTotal, 24 * 60 * 60 * 1000);
CreateTaskUpdater(this::UpdateServerUniqueYear, 24 * 60 * 60 * 1000);
CreateTaskUpdater(this::UpdateServerUniqueMonth, 24 * 60 * 60 * 1000);
CreateTaskUpdater(this::UpdateServerUniqueDay, 60 * 60 * 1000);
}
///////////////////////////////////////////////////////////////////////////////////////
if(server_update) {
logger.warn("Per server updater enabled");
stats.getServers().forEach((server_name, server) -> {
CreateTaskUpdater(() -> getServerUnique(server_name, server.getDb()), 5 * 60 * 1000);
});
}
}
///////////////////////////////////////////////////////////////////////////////////////////
public Long getServerUniqueFromQuery(String query, String db) {
query = String.format(query, db);
query = "SELECT COUNT(*) as count FROM (" + query.substring(0, query.length()-7) + ") x;";
return (Long) entityManager.createNativeQuery(query).getSingleResult();
}
public Long getServerUniqueFromQuery(String query) {
String final_query = "SELECT COUNT(*) as count FROM (";
for(Server server: stats.getServers().values()){
final_query += query.formatted(server.getDb());
}
final_query = final_query.substring(0, final_query.length()-7) + ") x;";
return (Long) entityManager.createNativeQuery(final_query).getSingleResult();
}
///////////////////////////////////////////////////////////////////////////////////////////
public boolean getServerUnique(String server_name, String db) {
stats.getServers().get(server_name).UpdateUniq("total", getServerUniqueFromQuery(query_total, db));
stats.getServers().get(server_name).UpdateUniq("day", getServerUniqueFromQuery(query_day, db));
stats.getServers().get(server_name).UpdateUniq("month", getServerUniqueFromQuery(query_month, db));
stats.getServers().get(server_name).UpdateUniq("year", getServerUniqueFromQuery(query_year, db));
return true;
}
public boolean UpdateServerUniqueTotal() {
logger.info("Update >total< servers player unique count");
stats.UpdateUniq("total", getServerUniqueFromQuery(query_total));
return true;
}
public boolean UpdateServerUniqueYear() {
logger.info("Update >current year< servers player unique count");
stats.UpdateUniq("year", getServerUniqueFromQuery(query_year));
return true;
}
public boolean UpdateServerUniqueMonth() {
logger.info("Update >current month< servers player unique count");
stats.UpdateUniq("month", getServerUniqueFromQuery(query_month));
return true;
}
public boolean UpdateServerUniqueDay() {
logger.info("Update >current day< servers player unique count");
stats.UpdateUniq("day", getServerUniqueFromQuery(query_day));
return true;
}
}