This commit is contained in:
@@ -46,7 +46,8 @@ public class HandlerConfig {
|
|||||||
StatisticViolationHandler statisticViolationHandler,
|
StatisticViolationHandler statisticViolationHandler,
|
||||||
StatisticSubscriberHandler statisticSubscriberHandler,
|
StatisticSubscriberHandler statisticSubscriberHandler,
|
||||||
StatisticTokenHandler statisticTokenHandler,
|
StatisticTokenHandler statisticTokenHandler,
|
||||||
StatisticIncomeHandler statisticIncomeHandler
|
StatisticIncomeHandler statisticIncomeHandler,
|
||||||
|
MonitoringStatisticHandler monitoringStatisticHandler
|
||||||
|
|
||||||
) {
|
) {
|
||||||
Map<Integer, RequestHandler> map = new HashMap<>();
|
Map<Integer, RequestHandler> map = new HashMap<>();
|
||||||
@@ -85,6 +86,7 @@ public class HandlerConfig {
|
|||||||
map.put(30024, statisticSubscriberHandler);
|
map.put(30024, statisticSubscriberHandler);
|
||||||
map.put(30025, statisticTokenHandler);
|
map.put(30025, statisticTokenHandler);
|
||||||
map.put(30026, statisticIncomeHandler);
|
map.put(30026, statisticIncomeHandler);
|
||||||
|
map.put(30027, monitoringStatisticHandler);
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package ru.soune.nocopy.dto.statistic;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class MonitoringStatisticRequest {
|
||||||
|
private String authToken;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package ru.soune.nocopy.dto.statistic;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class MonitoringStatisticResponse {
|
||||||
|
|
||||||
|
@JsonProperty("day")
|
||||||
|
private Long everyDay;
|
||||||
|
|
||||||
|
@JsonProperty("month")
|
||||||
|
private Long everyMonth;
|
||||||
|
|
||||||
|
@JsonProperty("weekly")
|
||||||
|
private Long everyWeekly;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package ru.soune.nocopy.handler;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import ru.soune.nocopy.dto.BaseRequest;
|
||||||
|
import ru.soune.nocopy.dto.BaseResponse;
|
||||||
|
import ru.soune.nocopy.dto.MessageCode;
|
||||||
|
import ru.soune.nocopy.dto.statistic.MonitoringStatisticRequest;
|
||||||
|
import ru.soune.nocopy.dto.statistic.MonitoringStatisticResponse;
|
||||||
|
import ru.soune.nocopy.entity.company.Company;
|
||||||
|
import ru.soune.nocopy.entity.monitoring.MonitoringType;
|
||||||
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
import ru.soune.nocopy.repository.FileMonitoringRepository;
|
||||||
|
import ru.soune.nocopy.repository.UserRepository;
|
||||||
|
import ru.soune.nocopy.service.register.AuthService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class MonitoringStatisticHandler implements RequestHandler {
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
private final AuthService authService;
|
||||||
|
|
||||||
|
private final FileMonitoringRepository fileMonitoringRepository;
|
||||||
|
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||||
|
MonitoringStatisticRequest monitoringStatisticRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||||
|
MonitoringStatisticRequest.class);
|
||||||
|
try {
|
||||||
|
String authToken = monitoringStatisticRequest.getAuthToken();
|
||||||
|
Long userId = authService.useUserAuthToken(authToken);
|
||||||
|
|
||||||
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
|
Company company = user.getCompany();
|
||||||
|
List<Long> userIds = company == null ? List.of(userId) :
|
||||||
|
user.getCompany()
|
||||||
|
.getUsers()
|
||||||
|
.stream()
|
||||||
|
.map(User::getId).toList();
|
||||||
|
|
||||||
|
MonitoringStatisticResponse monitoringStatisticResponse = MonitoringStatisticResponse.builder()
|
||||||
|
.everyDay(fileMonitoringRepository.countByUserIdsAndMonitoringType(userIds,
|
||||||
|
MonitoringType.MONITORING_DAILY))
|
||||||
|
.everyMonth(fileMonitoringRepository.countByUserIdsAndMonitoringType(userIds,
|
||||||
|
MonitoringType.MONITORING_MONTHLY))
|
||||||
|
.everyWeekly(fileMonitoringRepository.countByUserIdsAndMonitoringType(userIds,
|
||||||
|
MonitoringType.MONITORING_WEEKLY))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return new BaseResponse(
|
||||||
|
request.getMsgId(),
|
||||||
|
MessageCode.SUCCESS.getCode(),
|
||||||
|
MessageCode.SUCCESS.getDescription(),
|
||||||
|
monitoringStatisticResponse);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new BaseResponse(
|
||||||
|
request.getMsgId(),
|
||||||
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
||||||
|
MessageCode.INVALID_JSON_BODY.getDescription(),
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,4 +22,11 @@ public interface FileMonitoringRepository extends JpaRepository<FileMonitoringEn
|
|||||||
List<FileMonitoringEntity> findReadyForRun(@Param("now") LocalDateTime now);
|
List<FileMonitoringEntity> findReadyForRun(@Param("now") LocalDateTime now);
|
||||||
|
|
||||||
List<FileMonitoringEntity> findByMonitoringTypeAndIsActiveTrue(MonitoringType type);
|
List<FileMonitoringEntity> findByMonitoringTypeAndIsActiveTrue(MonitoringType type);
|
||||||
|
|
||||||
|
@Query("SELECT COUNT(m) FROM FileMonitoringEntity m " +
|
||||||
|
"WHERE m.userId IN :userIds " +
|
||||||
|
"AND m.monitoringType = :monitoringType " +
|
||||||
|
"AND m.isActive = true")
|
||||||
|
long countByUserIdsAndMonitoringType(@Param("userIds") List<Long> userIds,
|
||||||
|
@Param("monitoringType") MonitoringType monitoringType);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user