diff --git a/src/main/java/app/controllers/user/PublicController.java b/src/main/java/app/controllers/user/PublicController.java index 14dc6b2..c6ed14a 100644 --- a/src/main/java/app/controllers/user/PublicController.java +++ b/src/main/java/app/controllers/user/PublicController.java @@ -2,6 +2,7 @@ package app.controllers.user; import app.annotations.enums.AuthMethod; import app.annotations.enums.FirstTouch; +import app.annotations.interfaces.CheckPermitionFlag; import app.annotations.interfaces.CheckWebAccess; import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.WaitAfterNext; @@ -145,4 +146,21 @@ public class PublicController { searchFilter ), HttpStatus.OK); } + + @PostMapping("/doxing/graph") + @WaitAfterNext(order = "doxing") + @CheckWebAccess + @CheckPermitionFlag(flag = "d") + public ResponseEntity> getDoxing(@RequestBody(required = false) SearchFilter searchFilter) { + if (searchFilter == null) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + 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.getDoxingOnPeriod( + searchFilter + ), HttpStatus.OK); + } } diff --git a/src/main/java/app/services/db/GraphService.java b/src/main/java/app/services/db/GraphService.java index ff066ec..c81171b 100644 --- a/src/main/java/app/services/db/GraphService.java +++ b/src/main/java/app/services/db/GraphService.java @@ -3,6 +3,7 @@ package app.services.db; import app.entities.SearchFilter; import app.entities.db.period.PerPeriodStatistic; import app.services.ProfileService; +import app.services.io.readers.GeoIP; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import org.slf4j.Logger; @@ -17,6 +18,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.util.List; +import java.util.stream.Collectors; @Service public class GraphService { @@ -26,6 +28,9 @@ public class GraphService { @Autowired private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + @Autowired + private GeoIP geoIP; + private final static Logger logger = LoggerFactory.getLogger(GraphService.class); @Scheduled(cron = "@hourly") @@ -95,6 +100,33 @@ public class GraphService { } } + @Data + public static class CountryCityPerPeriodStatistic extends PerPeriodStatistic { + private String connect_ip; + private String countryAndCity; + + @JsonIgnore + @Override + public String getSrv_id() { + return super.getSrv_id(); + } + + @JsonIgnore + @Override + public Long getValue() { + return super.getValue(); + } + + @JsonIgnore + public String getConnect_ip() { + return connect_ip; + } + + public void fillCountryAndCity(GeoIP geoIP) { + this.countryAndCity = geoIP.GetCountry(connect_ip) + "/" + geoIP.GetCity(countryAndCity); + } + } + @Cacheable(value = "graphs.vip", key = "#searchFilter.cacheKey") public List getVipOnPeriod(SearchFilter searchFilter) { final String sql = "select date_trunc(:delta, timestamp) AS ts, :delta as period, count(*) as value, givemethod, amount from gived_vip " + @@ -111,4 +143,27 @@ public class GraphService { return namedParameterJdbcTemplate.query(sql, map, new BeanPropertyRowMapper(VipPerPeriodStatistic.class)); } + + public List getDoxingOnPeriod(SearchFilter searchFilter) { + final String sql = "\n" + + "select date_trunc(:delta, timestamp) AS ts, :delta as period, connect_ip from user_connections " + + "where timestamp between :begindate and :enddate " + + //" and migration_id is null " + + " and connect_duration = 0 " + + " and account_id in (:account_ids) " + + "group by ts, connect_ip"; + + MapSqlParameterSource map = new MapSqlParameterSource(); + map.addValue("delta", searchFilter.getMaybePeriod()); + String profiles = searchFilter.getAccountsSteam2(profileService); + //map.addValue("account_ids_empty", profiles.isEmpty()); + map.addValue("account_ids", profiles.isEmpty() ? "0" : profiles); + map.addValue("begindate", searchFilter.getBeginTime()); + map.addValue("enddate", searchFilter.getEndTime()); + + return namedParameterJdbcTemplate.query(sql, map, new BeanPropertyRowMapper<>(CountryCityPerPeriodStatistic.class)) + .stream() + .peek(d -> d.fillCountryAndCity(geoIP)) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/app/services/io/readers/GeoIP.java b/src/main/java/app/services/io/readers/GeoIP.java index 59c7fe3..7b92ca9 100644 --- a/src/main/java/app/services/io/readers/GeoIP.java +++ b/src/main/java/app/services/io/readers/GeoIP.java @@ -34,6 +34,14 @@ public class GeoIP extends BaseReader { } } + public String GetCity(String ip) { + try { + return databaseReader.city(InetAddress.getByName(ip)).getCountry().getName(); + } catch (GeoIp2Exception | IOException e) { + return "Unknown"; + } + } + public DatabaseReader loadConfiguration() { return (DatabaseReader) super.loadConfiguration( "GEOIP_FILE",