Browse Source

schedule config / true stats

master
gsd 1 week ago
parent
commit
02c0551b91
  1. 2
      src/main/java/app/MainApi.java
  2. 45
      src/main/java/app/configurations/AppCacheConfig.java
  3. 22
      src/main/java/app/configurations/CacheConfig.java
  4. 20
      src/main/java/app/configurations/ScheduledConfig.java
  5. 17
      src/main/java/app/services/db/GraphService.java
  6. 18
      src/main/java/app/updates/OnlineUpdater.java

2
src/main/java/app/MainApi.java

@ -3,11 +3,13 @@ package app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.time.Instant;
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class MainApi {
public static void main(String[] args) {
try {

45
src/main/java/app/configurations/AppCacheConfig.java

@ -0,0 +1,45 @@
package app.configurations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching
public class AppCacheConfig {
private static final Logger logger = LoggerFactory.getLogger(AppCacheConfig.class);
public static final String g_usertime = "graphs.usertime";
public static final String g_vip = "graphs.vip";
public static final String g_online = "graphs.online";
public static final String g_usertime_connections = "graphs.usertime.connections";
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager(
"steam_data",
"steam_search_data",
g_usertime,
g_vip,
g_online,
g_usertime_connections);
}
@Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS)
public void purgeCacheSched() {
purgeGraphCache();
}
@CacheEvict(value = {g_usertime, g_vip, g_online, g_usertime_connections}, allEntries = true)
public void purgeGraphCache() {
logger.info("Cache clean of graphs");
}
}

22
src/main/java/app/configurations/CacheConfig.java

@ -1,22 +0,0 @@
package app.configurations;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager(
"steam_data",
"steam_search_data",
"graphs.usertime",
"graphs.vip",
"graphs.online",
"graphs.usertime.connections");
}
}

20
src/main/java/app/configurations/ScheduledConfig.java

@ -0,0 +1,20 @@
package app.configurations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableScheduling
public class ScheduledConfig {
private static final TimeUnit defaultUnit = TimeUnit.HOURS;
private static final int defaultDuration = 1;
private static final Logger logger = LoggerFactory.getLogger(ScheduledConfig.class);
public ScheduledConfig() {
logger.info("Enabled scheduling");
}
}

17
src/main/java/app/services/db/GraphService.java

@ -1,5 +1,6 @@
package app.services.db;
import app.configurations.AppCacheConfig;
import app.entities.SearchFilter;
import app.entities.db.period.PerPeriodStatistic;
import app.services.ProfileService;
@ -38,16 +39,6 @@ public class GraphService {
private final static Logger logger = LoggerFactory.getLogger(GraphService.class);
@Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS)
public void purgeCacheSched() {
purgeCache();
}
@CacheEvict(value = {"graphs.usertime", "graphs.vip", "graphs.online", "graphs.usertime.connections"}, allEntries = true)
public void purgeCache() {
logger.info("Cache clean");
}
/**
* 2021-03-08 00:00:00.000000 +00:00,3853,srv1
* 2021-03-12 00:00:00.000000 +00:00,930,srv1
@ -57,7 +48,7 @@ public class GraphService {
* @param searchFilter
* @return
*/
@Cacheable(value = "graphs.usertime", key = "#searchFilter.cacheKey") //"graphs.usertime", "graphs.vip"
@Cacheable(value = AppCacheConfig.g_usertime, key = "#searchFilter.cacheKey")
public List<PerPeriodStatistic> getUsertimeOnPeriod(SearchFilter searchFilter) {
final String sql = "select date_trunc(:delta, timestamp) AS ts, :delta as period, sum(connect_duration) as value, srv_id from user_connections " +
"where (:account_ids_empty = true or account_id in (:account_ids)) and connect_duration != 0 and timestamp between :begindate and :enddate " +
@ -76,7 +67,7 @@ public class GraphService {
return namedParameterJdbcTemplate.query(sql, map, new BeanPropertyRowMapper(PerPeriodStatistic.class));
}
@Cacheable(value = "graphs.usertime.connections", key = "#searchFilter.cacheKey")
@Cacheable(value = AppCacheConfig.g_usertime_connections, key = "#searchFilter.cacheKey")
public List<PerPeriodStatistic> getConnectionsOnPeriod(SearchFilter searchFilter) {
final String sql = "select acc.ts, acc.period, count(acc.value) as value, acc.srv_id from (select date_trunc(:delta, timestamp) AS ts, :delta as period, count(account_id) as value, srv_id, account_id from user_connections " +
" where timestamp between :begindate and :enddate " +
@ -136,7 +127,7 @@ public class GraphService {
}
}
@Cacheable(value = "graphs.vip", key = "#searchFilter.cacheKey")
@Cacheable(value = AppCacheConfig.g_vip, key = "#searchFilter.cacheKey")
public List<VipPerPeriodStatistic> getVipOnPeriod(SearchFilter searchFilter) {
final String sql = "select date_trunc(:delta, timestamp) AS ts, :delta as period, count(*) as value, givemethod, amount from gived_vip " +
"where (:account_ids_empty = true or steam2 in (:account_ids)) and givemethod != 4 and timestamp between :begindate and :enddate " +

18
src/main/java/app/updates/OnlineUpdater.java

@ -1,5 +1,6 @@
package app.updates;
import app.configurations.AppCacheConfig;
import app.entities.Stats;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
@ -92,7 +93,7 @@ public class OnlineUpdater extends BaseUpdater {
}
private boolean updateCurrentDayPeak() {
this.current_day_max_player_count = getMaxPeakOfDays(null,1).get(0).getPlayer_count();
this.current_day_max_player_count = getAvgPeakOfDays(null,1).get(0).getPlayer_count();
return true;
}
@ -132,7 +133,7 @@ GROUP BY ts, server_id) as s group by s.ts order by s.ts desc limit ?
(rs, rowNum) -> new AvgPeakOfDay(rs));
}
@Cacheable(value = "graphs.online", key = "#cacheKey")
@Cacheable(value = AppCacheConfig.g_online, key = "#cacheKey")
public List<StatsOfPeakOfPerFiveMinutes> getMaxPeakOfFiveMinutes(String server_id, int limit, Integer minutes, String cacheKey) {
long utime = LocalDate.now().minusDays(limit -1).toEpochSecond(LocalTime.MIN, ZoneOffset.ofHours(3));
return jdbcTemplate_ro.query("select round(sum(sub.player_count))::int as player_count, sub.ts as fulldate from ("+
@ -147,13 +148,13 @@ GROUP BY ts, server_id) as s group by s.ts order by s.ts desc limit ?
(rs, rowNum) -> new StatsOfPeakOfPerFiveMinutes(rs));
}
@Cacheable(value = "graphs.online", key = "#cacheKey")
@Cacheable(value = AppCacheConfig.g_online, key = "#cacheKey")
public List<StatsOfPeakOfDay> getPeakOfDays(String server_id, int limit, String cacheKey) {
List<MaxPeakOfDay> maxPeakOfDays = getMaxPeakOfDays(server_id, limit);
//List<MaxPeakOfDay> maxPeakOfDays = getMaxPeakOfDays(server_id, limit);
List<AvgPeakOfDay> avgPeakOfDays = getAvgPeakOfDays(server_id, limit);
List<StatsOfPeakOfDay> statsOfPeakOfDays = new ArrayList<>();
for (int i = 0; i < maxPeakOfDays.size(); i++) {
statsOfPeakOfDays.add(new StatsOfPeakOfDay(avgPeakOfDays.get(i), maxPeakOfDays.get(i)));
for (int i = 0; i < avgPeakOfDays.size(); i++) {
statsOfPeakOfDays.add(new StatsOfPeakOfDay(avgPeakOfDays.get(i), null));
}
return statsOfPeakOfDays;
}
@ -184,7 +185,10 @@ GROUP BY ts, server_id) as s group by s.ts order by s.ts desc limit ?
StatsOfPeakOfDay(AvgPeakOfDay avgPeakOfDay, MaxPeakOfDay maxPeakOfDay) {
date = avgPeakOfDay.getDate();
avg = avgPeakOfDay.getPlayer_count();
max = maxPeakOfDay.getPlayer_count();
if (maxPeakOfDay != null)
max = maxPeakOfDay.getPlayer_count();
else
max = 0;
}
public Date getDate() {

Loading…
Cancel
Save