@@ -23,6 +23,12 @@ public class DockViewStatsResponse {
|
|||||||
|
|
||||||
private Long uniqueUsers;
|
private Long uniqueUsers;
|
||||||
|
|
||||||
|
private int documentsCount;
|
||||||
|
|
||||||
|
private int uniqueCountryCount;
|
||||||
|
|
||||||
|
private int uniqueCityCount;
|
||||||
|
|
||||||
private LocalDateTime firstViewDate;
|
private LocalDateTime firstViewDate;
|
||||||
|
|
||||||
private LocalDateTime lastViewDate;
|
private LocalDateTime lastViewDate;
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ public interface DockViewRepository extends JpaRepository<DockView, Long> {
|
|||||||
|
|
||||||
List<DockView> findAllByFileId(String fileId);
|
List<DockView> findAllByFileId(String fileId);
|
||||||
|
|
||||||
|
@Query("SELECT d.viewerIp FROM DockView d WHERE d.fileId IN :fileIds")
|
||||||
|
List<String> findAllViewerIps(@Param("fileIds") List<String> fileIds);
|
||||||
|
|
||||||
List<DockView> findAllByViewerId(Long viewerId);
|
List<DockView> findAllByViewerId(Long viewerId);
|
||||||
|
|
||||||
Optional<DockView> findByFileIdAndViewerId(String fileId, Long viewerId);
|
Optional<DockView> findByFileIdAndViewerId(String fileId, Long viewerId);
|
||||||
|
|||||||
@@ -53,34 +53,10 @@ public class DockViewService {
|
|||||||
return saved;
|
return saved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public DockViewStatsResponse getStatsByFileId(String fileId) {
|
|
||||||
Optional<Object[]> statsOpt = dockViewRepository.findViewsStatsByFileId(fileId);
|
|
||||||
|
|
||||||
if (statsOpt.isEmpty()) {
|
|
||||||
return DockViewStatsResponse.builder()
|
|
||||||
.fileId(fileId)
|
|
||||||
.totalViews(0L)
|
|
||||||
.uniqueIps(0L)
|
|
||||||
.uniqueUsers(0L)
|
|
||||||
.firstViewDate(null)
|
|
||||||
.lastViewDate(null)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
Object[] stats = statsOpt.get();
|
|
||||||
return DockViewStatsResponse.builder()
|
|
||||||
.fileId((String) stats[0])
|
|
||||||
.totalViews((Long) stats[1])
|
|
||||||
.uniqueIps((Long) stats[2])
|
|
||||||
.uniqueUsers((Long) stats[3])
|
|
||||||
.lastViewDate((LocalDateTime) stats[4])
|
|
||||||
.firstViewDate((LocalDateTime) stats[5])
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public DockViewStatsResponse getTotalStatsByFileIds(List<String> fileIds) {
|
public DockViewStatsResponse getTotalStatsByFileIds(List<String> fileIds) {
|
||||||
List<Object[]> statsList = dockViewRepository.findTotalViewsStatsByFileIds(fileIds);
|
List<Object[]> statsList = dockViewRepository.findTotalViewsStatsByFileIds(fileIds);
|
||||||
|
List<String> allViewerIps = dockViewRepository.findAllViewerIps(fileIds);
|
||||||
|
Map<String, Integer> uniqueGeoCount = geoCountryService.getUniqueGeoCount(allViewerIps);
|
||||||
|
|
||||||
if (statsList.isEmpty()) {
|
if (statsList.isEmpty()) {
|
||||||
return DockViewStatsResponse.builder()
|
return DockViewStatsResponse.builder()
|
||||||
@@ -98,6 +74,9 @@ public class DockViewService {
|
|||||||
.totalViews((Long) stats[0])
|
.totalViews((Long) stats[0])
|
||||||
.uniqueIps((Long) stats[1])
|
.uniqueIps((Long) stats[1])
|
||||||
.uniqueUsers((Long) stats[2])
|
.uniqueUsers((Long) stats[2])
|
||||||
|
.documentsCount(fileIds.size())
|
||||||
|
.uniqueCityCount(uniqueGeoCount.get("uniqueCities"))
|
||||||
|
.uniqueCountryCount(uniqueGeoCount.get("uniqueCountries"))
|
||||||
.lastViewDate((LocalDateTime) stats[3])
|
.lastViewDate((LocalDateTime) stats[3])
|
||||||
.firstViewDate((LocalDateTime) stats[4])
|
.firstViewDate((LocalDateTime) stats[4])
|
||||||
.build();
|
.build();
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -40,6 +44,41 @@ public class GeoCountryService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<String, Integer> getUniqueGeoCount(List<String> ipList) {
|
||||||
|
Set<String> uniqueCountries = new HashSet<>();
|
||||||
|
Set<String> uniqueCountryCodes = new HashSet<>();
|
||||||
|
Set<String> uniqueCities = new HashSet<>();
|
||||||
|
|
||||||
|
if (ipList != null) {
|
||||||
|
for (String ip : ipList) {
|
||||||
|
try {
|
||||||
|
String country = getCountryByIp(ip);
|
||||||
|
if (!"UNKNOWN".equals(country)) {
|
||||||
|
uniqueCountries.add(country);
|
||||||
|
}
|
||||||
|
|
||||||
|
String countryCode = getCountryCodeByIp(ip);
|
||||||
|
if (!"UNKNOWN".equals(countryCode)) {
|
||||||
|
uniqueCountryCodes.add(countryCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
String city = getCityByIp(ip);
|
||||||
|
if (!"UNKNOWN".equals(city)) {
|
||||||
|
uniqueCities.add(city);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error processing IP: {}", ip, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Map.of(
|
||||||
|
"uniqueCountries", uniqueCountries.size(),
|
||||||
|
"uniqueCountryCodes", uniqueCountryCodes.size(),
|
||||||
|
"uniqueCities", uniqueCities.size()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public String getCountryCode(String input) {
|
public String getCountryCode(String input) {
|
||||||
try {
|
try {
|
||||||
CountryResponse response = extractCountry(input);
|
CountryResponse response = extractCountry(input);
|
||||||
|
|||||||
Reference in New Issue
Block a user