Browse Source

graph cache 3

master
gsd 4 weeks ago
parent
commit
eb9f9e4bb1
  1. 8
      src/main/java/app/configurations/CacheConfig.java
  2. 17
      src/main/java/app/controllers/user/UsertimeController.java
  3. 5
      src/main/java/app/entities/db/period/PerPeriodStatistic.java
  4. 26
      src/main/java/app/services/db/GraphService.java

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

@ -11,6 +11,12 @@ import org.springframework.context.annotation.Configuration;
public class CacheConfig { public class CacheConfig {
@Bean @Bean
public CacheManager cacheManager() { 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");
} }
} }

17
src/main/java/app/controllers/user/UsertimeController.java

@ -48,4 +48,21 @@ public class UsertimeController {
searchFilter searchFilter
), HttpStatus.OK); ), HttpStatus.OK);
} }
@PostMapping("/connections/graph")
@CheckWebAccess(auth_method = AuthMethod.STEAM64)
@WaitAfterNext(order = "usertime")
@CollectStatistic
public ResponseEntity<List<PerPeriodStatistic>> 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);
}
} }

5
src/main/java/app/entities/db/period/PerPeriodStatistic.java

@ -30,4 +30,9 @@ public class PerPeriodStatistic {
default -> ""; default -> "";
}; };
} }
@JsonIgnore
public String getPeriod() {
return period;
}
} }

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

@ -29,7 +29,7 @@ public class GraphService {
private final static Logger logger = LoggerFactory.getLogger(GraphService.class); private final static Logger logger = LoggerFactory.getLogger(GraphService.class);
@Scheduled(cron = "@hourly") @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() { public void purgeCache() {
logger.info("Cache clean"); logger.info("Cache clean");
} }
@ -46,7 +46,7 @@ public class GraphService {
@Cacheable(value = "graphs.usertime", key = "#searchFilter.cacheKey") //"graphs.usertime", "graphs.vip" @Cacheable(value = "graphs.usertime", key = "#searchFilter.cacheKey") //"graphs.usertime", "graphs.vip"
public List<PerPeriodStatistic> getUsertimeOnPeriod(SearchFilter searchFilter) { 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 " + 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) " + "and (:srv_id like '' or srv_id like :srv_id) " +
"group by ts, srv_id " + "group by ts, srv_id " +
"order by ts asc"; "order by ts asc";
@ -62,6 +62,28 @@ public class GraphService {
return namedParameterJdbcTemplate.query(sql, map, new BeanPropertyRowMapper(PerPeriodStatistic.class)); return namedParameterJdbcTemplate.query(sql, map, new BeanPropertyRowMapper(PerPeriodStatistic.class));
} }
@Cacheable(value = "graphs.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 " +
" 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<Long> 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 @Data
public static class VipPerPeriodStatistic extends PerPeriodStatistic { public static class VipPerPeriodStatistic extends PerPeriodStatistic {
private Integer givemethod; private Integer givemethod;

Loading…
Cancel
Save