Browse Source

graph cache

master
gsd 1 month ago
parent
commit
6fd039a6bc
  1. 2
      src/main/java/app/configurations/CacheConfig.java
  2. 1
      src/main/java/app/controllers/user/PublicController.java
  3. 1
      src/main/java/app/controllers/user/UsertimeController.java
  4. 33
      src/main/java/app/entities/SearchFilter.java
  5. 15
      src/main/java/app/services/db/GraphService.java

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

@ -11,6 +11,6 @@ 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"); return new ConcurrentMapCacheManager("steam_data", "steam_search_data", "graphs");
} }
} }

1
src/main/java/app/controllers/user/PublicController.java

@ -139,6 +139,7 @@ public class PublicController {
searchFilter.setEnd(Timestamp.from(Instant.now()).toLocalDateTime()); searchFilter.setEnd(Timestamp.from(Instant.now()).toLocalDateTime());
if (searchFilter.getBeginTime() == null) if (searchFilter.getBeginTime() == null)
searchFilter.setBegin(Timestamp.from(Instant.now().minusSeconds(30 * 3600* 24)).toLocalDateTime()); searchFilter.setBegin(Timestamp.from(Instant.now().minusSeconds(30 * 3600* 24)).toLocalDateTime());
searchFilter.setEndOrBeginTimeOfDay();
return new ResponseEntity<>(graphService.getVipOnPeriod( return new ResponseEntity<>(graphService.getVipOnPeriod(
searchFilter searchFilter

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

@ -42,6 +42,7 @@ public class UsertimeController {
searchFilter.setEnd(Timestamp.from(Instant.now()).toLocalDateTime()); searchFilter.setEnd(Timestamp.from(Instant.now()).toLocalDateTime());
if (searchFilter.getBeginTime() == null) if (searchFilter.getBeginTime() == null)
searchFilter.setBegin(Timestamp.from(Instant.now().minusSeconds(30 * 3600* 24)).toLocalDateTime()); searchFilter.setBegin(Timestamp.from(Instant.now().minusSeconds(30 * 3600* 24)).toLocalDateTime());
searchFilter.setEndOrBeginTimeOfDay();
return new ResponseEntity<>(graphService.getUsertimeOnPeriod( return new ResponseEntity<>(graphService.getUsertimeOnPeriod(
searchFilter searchFilter

33
src/main/java/app/entities/SearchFilter.java

@ -7,6 +7,7 @@ import org.springframework.format.annotation.DateTimeFormat;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -22,12 +23,30 @@ public class SearchFilter {
private String serverId = null; private String serverId = null;
private Long utc = 0L; private Long utc = 0L;
public boolean isEmpty() { public void setEndOrBeginTimeOfDay() {
return accounts == null || accounts.isEmpty() || begin == null || end == null; if (begin != null) { //todo utc support
begin = begin.toLocalDate().atStartOfDay();
}
if (end != null) {
end = end.toLocalDate().atStartOfDay().minusSeconds(1).plusDays(1);
}
}
public String getCacheKey() {
StringBuilder stringBuilder = new StringBuilder();
if (accounts != null)
stringBuilder.append(String.join(",", accounts)).append(";");
if (begin != null)
stringBuilder.append(begin).append(";");
if (end != null)
stringBuilder.append(end).append(";");
if (serverId != null)
stringBuilder.append(serverId).append(";");
return stringBuilder.toString();
} }
/** /**
* Выдает список из ид 3 версии [U:1:127332962] * Выдает список из ид 3 версии [U:1:127332962], а точнее 127332962
* @param profileService * @param profileService
* @return * @return
*/ */
@ -105,12 +124,20 @@ public class SearchFilter {
return serverId == null || serverId.isEmpty() ? null : serverId; return serverId == null || serverId.isEmpty() ? null : serverId;
} }
/**
* разница в днях между заданными данными, иначе 0
* @return
*/
public Long getDayDelta() { public Long getDayDelta() {
if (begin != null && end != null) if (begin != null && end != null)
return ChronoUnit.DAYS.between(begin, end); return ChronoUnit.DAYS.between(begin, end);
else return 0L; else return 0L;
} }
/**
* высираем период исходя из заданного участка времени
* @return
*/
public String getMaybePeriod() { public String getMaybePeriod() {
long delta = getDayDelta(); long delta = getDayDelta();
if (delta <= 30) return "day"; if (delta <= 30) return "day";

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

@ -5,10 +5,15 @@ import app.entities.db.period.PerPeriodStatistic;
import app.services.ProfileService; import app.services.ProfileService;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -21,6 +26,14 @@ public class GraphService {
@Autowired @Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate; private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private final static Logger logger = LoggerFactory.getLogger(GraphService.class);
@Scheduled(cron = "@hourly")
@CacheEvict(value = "graphs", allEntries = true)
public void purgeCache() {
logger.info("Cache clean");
}
/** /**
* 2021-03-08 00:00:00.000000 +00:00,3853,srv1 * 2021-03-08 00:00:00.000000 +00:00,3853,srv1
* 2021-03-12 00:00:00.000000 +00:00,930,srv1 * 2021-03-12 00:00:00.000000 +00:00,930,srv1
@ -30,6 +43,7 @@ public class GraphService {
* @param searchFilter * @param searchFilter
* @return * @return
*/ */
@Cacheable(value = "graphs", key = "#searchFilter.cacheKey")
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 connect_duration != 0 and timestamp between :begindate and :enddate " +
@ -60,6 +74,7 @@ public class GraphService {
} }
} }
@Cacheable(value = "graphs", key = "#searchFilter.cacheKey")
public List<VipPerPeriodStatistic> getVipOnPeriod(SearchFilter searchFilter) { 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 " + 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 " + "where (:account_ids_empty = true or steam2 in (:account_ids)) and givemethod != 4 and timestamp between :begindate and :enddate " +

Loading…
Cancel
Save