Browse Source

jdbc template migrate 3

master
gsd 1 year ago
parent
commit
a22804b135
  1. 12
      src/main/java/app/services/ShardingService.java
  2. 2
      src/main/java/app/services/db/KillfeedService.java
  3. 26
      src/main/java/app/updates/BanCountUpdater.java
  4. 33
      src/main/java/app/updates/CountriesUpdater.java
  5. 1
      src/main/java/app/updates/PreviewUpdater.java
  6. 2
      src/main/java/app/updates/SocialUpdater.java
  7. 33
      src/main/java/app/updates/UniqueUpdater.java
  8. 37
      src/main/java/app/updates/VipCountUpdater.java

12
src/main/java/app/services/ShardingService.java

@ -1,25 +1,23 @@
package app.services;
import app.entities.other.SteamID;
import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional("RoTransactionManager")
public class ShardingService {
@Autowired
@Qualifier(value = "RwEntityManager")
EntityManager entityManager_rw;
@Qualifier(value = "jt_rw")
private JdbcTemplate jdbcTemplate_rw;
@Autowired
@Qualifier(value = "RoEntityManager")
EntityManager entityManager_ro;
@Qualifier(value = "jt_ro")
private JdbcTemplate jdbcTemplate_ro;
public List<SteamID> getAccountsPerSteamID(SteamID steamID) {
//async here

2
src/main/java/app/services/db/KillfeedService.java

@ -105,7 +105,7 @@ public class KillfeedService {
}
});
/*Long count = (Long) entityManager.createNativeQuery("SELECT COUNT(`weapon_index`) FROM `user_killfeed` WHERE `attacker_id` = ?1 AND server_id like ?2 GROUP BY `weapon_index`")
/*Long count = (Long) createNativeQuery("SELECT COUNT(`weapon_index`) FROM `user_killfeed` WHERE `attacker_id` = ?1 AND server_id like ?2 GROUP BY `weapon_index`")
.setParameter(1, steamID.account_id)
.setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id)
.getSingleResult();*/

26
src/main/java/app/updates/BanCountUpdater.java

@ -2,32 +2,32 @@ package app.updates;
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.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
@Component
@Transactional("RoTransactionManager")
public class BanCountUpdater extends BaseUpdater{
@Autowired
@Qualifier("jt_ro")
private JdbcTemplate jdbcTemplate;
private Stats stats;
@Value("${backend.updates.ban_count}")
boolean update = false;
EntityManager entityManager;
private final Logger logger = LoggerFactory.getLogger(BanCountUpdater.class);
@Autowired
public BanCountUpdater(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) {
this.entityManager = entityManager;
public BanCountUpdater(Stats stats) {
this.stats = stats;
}
@ -41,7 +41,13 @@ public class BanCountUpdater extends BaseUpdater{
public boolean UpdateBanCount(){
logger.info("Update current ban count");
stats.setBan_count((Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `light_bans` WHERE active = 1").getSingleResult());
long ban_count = jdbcTemplate.query("SELECT COUNT(*) as count FROM `light_bans` WHERE active = 1", new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int numRow) throws SQLException {
return 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;
}

33
src/main/java/app/updates/CountriesUpdater.java

@ -5,26 +5,30 @@ import app.services.io.GeoIP;
import app.entities.Stats;
import com.maxmind.geoip2.exception.GeoIp2Exception;
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.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@Component
@Transactional("RoTransactionManager")
public class CountriesUpdater extends BaseUpdater{
Stats stats;
GeoIP geoIP;
EntityManager entityManager;
@Autowired
@Qualifier("jt_ro")
private JdbcTemplate jdbcTemplate;
private Stats stats;
private GeoIP geoIP;
private final Logger logger = LoggerFactory.getLogger(CountriesUpdater.class);
@ -35,9 +39,7 @@ public class CountriesUpdater extends BaseUpdater{
@Autowired
public CountriesUpdater(Stats stats,
GeoIP geoIP,
@Qualifier(value = "RoEntityManager") EntityManager entityManager) {
this.entityManager = entityManager;
GeoIP geoIP) {
this.stats = stats;
this.geoIP = geoIP;
}
@ -59,14 +61,19 @@ public class CountriesUpdater extends BaseUpdater{
query += countries_in_current_year.formatted(stringServerEntry.getValue().getDb());
}
query = query.substring(0, query.length()-7) + ";";
for(Object ip:entityManager.createNativeQuery(query).getResultList()) {
jdbcTemplate.query(query, new RowMapper<String>() {
@Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString("connect_ip");
}
}).forEach(ip -> {
try {
stats.getCountries().merge(geoIP.GetCountry(String.valueOf(ip)), 1, (x, y) -> x+y);
stats.getCountries().merge(geoIP.GetCountry(ip), 1, (x, y) -> x+y);
} catch (UnknownHostException e) {
} catch (IOException e) {
} catch (GeoIp2Exception e) {
}
}
});
return true;
}
}

1
src/main/java/app/updates/PreviewUpdater.java

@ -6,7 +6,6 @@ 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.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

2
src/main/java/app/updates/SocialUpdater.java

@ -1,7 +1,6 @@
package app.updates;
import app.entities.Stats;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
@ -12,7 +11,6 @@ import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Instant;

33
src/main/java/app/updates/UniqueUpdater.java

@ -3,24 +3,26 @@ 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.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import static java.time.Instant.now;
import java.sql.ResultSet;
import java.sql.SQLException;
@Component
@Transactional("RoTransactionManager")
public class UniqueUpdater extends BaseUpdater{
Stats stats;
private Stats stats;
EntityManager entityManager;
@Autowired
@Qualifier("jt_ro")
private JdbcTemplate jdbcTemplate;
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";
@ -35,8 +37,7 @@ public class UniqueUpdater extends BaseUpdater{
boolean server_update = false;
@Autowired
public UniqueUpdater(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) {
this.entityManager = entityManager;
public UniqueUpdater(Stats stats) {
this.stats = stats;
}
@ -62,7 +63,12 @@ public class UniqueUpdater extends BaseUpdater{
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();
return jdbcTemplate.query(query, new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int numRow) throws SQLException {
return rs.getLong("count");
}
}).stream().findFirst().orElse(0L);
}
public Long getServerUniqueFromQuery(String query) {
@ -71,7 +77,12 @@ public class UniqueUpdater extends BaseUpdater{
final_query += query.formatted(server.getDb());
}
final_query = final_query.substring(0, final_query.length()-7) + ") x;";
return (Long) entityManager.createNativeQuery(final_query).getSingleResult();
return jdbcTemplate.query(final_query, new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int numRow) throws SQLException {
return rs.getLong("count");
}
}).stream().findFirst().orElse(0L);
}
///////////////////////////////////////////////////////////////////////////////////////////

37
src/main/java/app/updates/VipCountUpdater.java

@ -4,23 +4,26 @@ import app.entities.Stats;
import app.services.ServerService;
import app.services.db.VIPService;
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.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;
import java.util.concurrent.CompletableFuture;
@Component
public class VipCountUpdater extends BaseUpdater{
private Stats stats;
EntityManager entityManager;
@Autowired
@Qualifier("jt_ro")
private JdbcTemplate jdbcTemplate;
private VIPService vipService;
private ServerService serverService;
@ -34,12 +37,10 @@ public class VipCountUpdater extends BaseUpdater{
@Autowired
public VipCountUpdater(Stats stats,
VIPService vipService,
ServerService serverService,
@Qualifier(value = "RoEntityManager") EntityManager entityManager) {
ServerService serverService) {
this.stats = stats;
this.vipService = vipService;
this.serverService = serverService;
this.entityManager = entityManager;
}
@PostConstruct
@ -54,18 +55,28 @@ public class VipCountUpdater extends BaseUpdater{
public boolean UpdateVIPCount() {
logger.info("Update current active VIP profiles");
stats.setVip_players(
(Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND (`comment` LIKE 'Donate.User' OR `comment` LIKE 'f13bot.User')")
.getSingleResult());
Long count = jdbcTemplate.query("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND (`comment` LIKE 'Donate.User' OR `comment` LIKE 'f13bot.User')",
new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int numRow) throws SQLException {
return rs.getLong("count");
}
}).stream().findFirst().orElse(0L);
stats.setVip_players(count);
stats.getUpdates().merge("vip_count", Instant.now().getEpochSecond(), (x, y) -> y);
return true;
}
public boolean UpdateFreeVIPCount() {
logger.info("Update current active FreeVIP profiles");
stats.setFreevip_players(
(Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND `comment` LIKE 'f13bot.FreeVIP'")
.getSingleResult());
Long count = jdbcTemplate.query("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND `comment` LIKE 'f13bot.FreeVIP'",
new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int numRow) throws SQLException {
return rs.getLong("count");
}
}).stream().findFirst().orElse(0L);
stats.setFreevip_players(count);
stats.getUpdates().merge("freevip_count", Instant.now().getEpochSecond(), (x, y) -> y);
return true;
}

Loading…
Cancel
Save