125 lines
4.6 KiB
Java
125 lines
4.6 KiB
Java
package ru.soune.nocopy.service.dockview;
|
|||
|
|
|
||
|
|
import lombok.RequiredArgsConstructor;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.springframework.data.domain.Page;
|
||
|
|
import org.springframework.data.domain.Pageable;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
import org.springframework.transaction.annotation.Transactional;
|
||
|
|
import ru.soune.nocopy.dto.docviewer.DocViewerRequest;
|
||
|
|
import ru.soune.nocopy.dto.docviewer.DockViewStatsResponse;
|
||
|
|
import ru.soune.nocopy.entity.docviewer.DockView;
|
||
|
|
import ru.soune.nocopy.repository.DockViewRepository;
|
||
|
|
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.util.*;
|
||
|
|
import java.util.stream.Collectors;
|
||
|
|
|
||
|
|
@Service
|
||
|
|
@Slf4j
|
||
|
|
@RequiredArgsConstructor
|
||
|
|
public class DockViewService {
|
||
|
|
|
||
|
|
private final DockViewRepository dockViewRepository;
|
||
|
|
|
||
|
|
@Transactional
|
||
|
|
public DockView createDockView(DocViewerRequest request, Long userId) {
|
||
|
|
DockView dockView = new DockView();
|
||
|
|
dockView.setViewerId(userId);
|
||
|
|
dockView.setViewerIp(request.getIp() != null ? request.getIp() : "-");
|
||
|
|
dockView.setUserAgent(request.getUserAgent() != null ? request.getUserAgent() : "-");
|
||
|
|
dockView.setFileId(request.getFileId());
|
||
|
|
dockView.setCreatedAt(LocalDateTime.now());
|
||
|
|
|
||
|
|
DockView saved = dockViewRepository.save(dockView);
|
||
|
|
|
||
|
|
log.info("Created dock view record: fileId={}, viewerId={}, ip={}",
|
||
|
|
saved.getFileId(), saved.getViewerId(), saved.getViewerIp());
|
||
|
|
|
||
|
|
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 getBasicStatsByFileId(String fileId) {
|
||
|
|
long totalViews = dockViewRepository.countByFileId(fileId);
|
||
|
|
long uniqueIps = dockViewRepository.countUniqueIpsByFileId(fileId);
|
||
|
|
|
||
|
|
return DockViewStatsResponse.builder()
|
||
|
|
.fileId(fileId)
|
||
|
|
.totalViews(totalViews)
|
||
|
|
.uniqueIps(uniqueIps)
|
||
|
|
.build();
|
||
|
|
}
|
||
|
|
|
||
|
|
public DockViewStatsResponse getAllFilesStatsPaginated(Pageable pageable) {
|
||
|
|
Page<Object[]> statsPage = dockViewRepository.findViewsStatsPaginated(pageable);
|
||
|
|
|
||
|
|
List<DockViewStatsResponse> statsList = statsPage.getContent().stream()
|
||
|
|
.map(row -> DockViewStatsResponse.builder()
|
||
|
|
.fileId((String) row[0])
|
||
|
|
.totalViews((Long) row[1])
|
||
|
|
.uniqueIps((Long) row[2])
|
||
|
|
.lastViewDate((LocalDateTime) row[3])
|
||
|
|
.build())
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
|
||
|
|
return DockViewStatsResponse.builder()
|
||
|
|
.content(statsList)
|
||
|
|
.pageNumber(statsPage.getNumber())
|
||
|
|
.pageSize(statsPage.getSize())
|
||
|
|
.totalElements(statsPage.getTotalElements())
|
||
|
|
.totalPages(statsPage.getTotalPages())
|
||
|
|
.isLast(statsPage.isLast())
|
||
|
|
.isFirst(statsPage.isFirst())
|
||
|
|
.hasNext(statsPage.hasNext())
|
||
|
|
.hasPrevious(statsPage.hasPrevious())
|
||
|
|
.build();
|
||
|
|
}
|
||
|
|
|
||
|
|
public boolean hasViewedFromIp(String fileId, String ip) {
|
||
|
|
return dockViewRepository.existsByFileIdAndViewerIp(fileId, ip);
|
||
|
|
}
|
||
|
|
|
||
|
|
public Optional<LocalDateTime> getLastViewDate(String fileId) {
|
||
|
|
return dockViewRepository.findLastViewDateByFileId(fileId);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Transactional
|
||
|
|
public void deleteAllViewsByFileId(String fileId) {
|
||
|
|
dockViewRepository.deleteAllByFileId(fileId);
|
||
|
|
log.info("Deleted all views for fileId: {}", fileId);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Transactional
|
||
|
|
public void cleanOldViews() {
|
||
|
|
LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(30);
|
||
|
|
dockViewRepository.deleteAllByCreatedAtBefore(thirtyDaysAgo);
|
||
|
|
log.info("Cleaned views older than: {}", thirtyDaysAgo);
|
||
|
|
}
|
||
|
|
}
|