From eb9f9e4bb1ec61613a3c234f5c63b06d4eaf7f6d Mon Sep 17 00:00:00 2001 From: gsd Date: Tue, 12 May 2026 20:30:48 +0300 Subject: [PATCH] graph cache 3 --- .../java/app/configurations/CacheConfig.java | 8 +++++- .../controllers/user/UsertimeController.java | 17 ++++++++++++ .../db/period/PerPeriodStatistic.java | 5 ++++ .../java/app/services/db/GraphService.java | 26 +++++++++++++++++-- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/main/java/app/configurations/CacheConfig.java b/src/main/java/app/configurations/CacheConfig.java index c194eb7..5437c8b 100644 --- a/src/main/java/app/configurations/CacheConfig.java +++ b/src/main/java/app/configurations/CacheConfig.java @@ -11,6 +11,12 @@ import org.springframework.context.annotation.Configuration; public class CacheConfig { @Bean public CacheManager cacheManager() { - return new ConcurrentMapCacheManager("steam_data", "steam_search_data", "graphs.usertime", "graphs.vip", "graphs.online"); + return new ConcurrentMapCacheManager( + "steam_data", + "steam_search_data", + "graphs.usertime", + "graphs.vip", + "graphs.online", + "graphs.usertime.connections"); } } diff --git a/src/main/java/app/controllers/user/UsertimeController.java b/src/main/java/app/controllers/user/UsertimeController.java index e01c311..3ffe0de 100644 --- a/src/main/java/app/controllers/user/UsertimeController.java +++ b/src/main/java/app/controllers/user/UsertimeController.java @@ -48,4 +48,21 @@ public class UsertimeController { searchFilter ), HttpStatus.OK); } + + @PostMapping("/connections/graph") + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "usertime") + @CollectStatistic + public ResponseEntity> getConnectionsOnPeriod(@RequestBody(required = false) SearchFilter searchFilter) { + if (searchFilter == null) searchFilter = new SearchFilter(); + if (searchFilter.getEndTime() == null) + searchFilter.setEnd(Timestamp.from(Instant.now()).toLocalDateTime()); + if (searchFilter.getBeginTime() == null) + searchFilter.setBegin(Timestamp.from(Instant.now().minusSeconds(30 * 3600* 24)).toLocalDateTime()); + searchFilter.setEndOrBeginTimeOfDay(); + + return new ResponseEntity<>(graphService.getConnectionsOnPeriod( + searchFilter + ), HttpStatus.OK); + } } diff --git a/src/main/java/app/entities/db/period/PerPeriodStatistic.java b/src/main/java/app/entities/db/period/PerPeriodStatistic.java index 2629564..2b8841a 100644 --- a/src/main/java/app/entities/db/period/PerPeriodStatistic.java +++ b/src/main/java/app/entities/db/period/PerPeriodStatistic.java @@ -30,4 +30,9 @@ public class PerPeriodStatistic { default -> ""; }; } + + @JsonIgnore + public String getPeriod() { + return period; + } } diff --git a/src/main/java/app/services/db/GraphService.java b/src/main/java/app/services/db/GraphService.java index 5792581..eeeb53a 100644 --- a/src/main/java/app/services/db/GraphService.java +++ b/src/main/java/app/services/db/GraphService.java @@ -29,7 +29,7 @@ public class GraphService { private final static Logger logger = LoggerFactory.getLogger(GraphService.class); @Scheduled(cron = "@hourly") - @CacheEvict(value = {"graphs.usertime", "graphs.vip", "graphs.online"}, allEntries = true) + @CacheEvict(value = {"graphs.usertime", "graphs.vip", "graphs.online", "graphs.usertime.connections"}, allEntries = true) public void purgeCache() { logger.info("Cache clean"); } @@ -46,7 +46,7 @@ public class GraphService { @Cacheable(value = "graphs.usertime", key = "#searchFilter.cacheKey") //"graphs.usertime", "graphs.vip" public List 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 " + + "where (:account_ids_empty = true or account_id in (:account_ids)) and migration_id is null and connect_duration != 0 and timestamp between :begindate and :enddate " + "and (:srv_id like '' or srv_id like :srv_id) " + "group by ts, srv_id " + "order by ts asc"; @@ -62,6 +62,28 @@ public class GraphService { return namedParameterJdbcTemplate.query(sql, map, new BeanPropertyRowMapper(PerPeriodStatistic.class)); } + @Cacheable(value = "graphs.usertime.connections", key = "#searchFilter.cacheKey") + public List 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 " + + " and (:account_ids_empty = true or account_id in (:account_ids))" + + " and migration_id is null " + + " and connect_duration > 0 " + + " and (:srv_id like '' or srv_id like :srv_id) " + + " group by ts, srv_id, account_id) as acc " + + "group by acc.ts, acc.period, acc.srv_id " + + "order by acc.ts asc"; + MapSqlParameterSource map = new MapSqlParameterSource(); + map.addValue("delta", searchFilter.getMaybePeriod()); + List profiles = searchFilter.getAccounts(profileService); + map.addValue("account_ids_empty", profiles.isEmpty()); + map.addValue("account_ids", profiles.isEmpty() ? List.of(1) : profiles); + map.addValue("begindate", searchFilter.getBeginTime()); + map.addValue("enddate", searchFilter.getEndTime()); + map.addValue("srv_id", searchFilter.getServerId() == null ? "" : searchFilter.getServerId()); + return namedParameterJdbcTemplate.query(sql, map, new BeanPropertyRowMapper(PerPeriodStatistic.class)); + } + @Data public static class VipPerPeriodStatistic extends PerPeriodStatistic { private Integer givemethod;