|
|
|
|
@@ -4,9 +4,12 @@ package com.sdm.data.job;
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
import com.sdm.common.common.SdmResponse;
|
|
|
|
|
import com.sdm.common.common.ThreadLocalContext;
|
|
|
|
|
import com.sdm.common.entity.constants.CommonConstants;
|
|
|
|
|
import com.sdm.common.entity.enums.MessageTemplateEnum;
|
|
|
|
|
import com.sdm.common.entity.req.system.SendMsgReq;
|
|
|
|
|
import com.sdm.common.feign.impl.system.MessageFeignClientImpl;
|
|
|
|
|
import com.sdm.common.feign.impl.system.SysUserFeignClientImpl;
|
|
|
|
|
import com.sdm.common.service.UserNameCacheService;
|
|
|
|
|
import com.sdm.data.model.entity.FileStorageQuota;
|
|
|
|
|
import com.sdm.data.service.DataStorageAnalysis;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
@@ -15,7 +18,10 @@ import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
@Slf4j
|
|
|
|
|
@@ -24,25 +30,47 @@ public class DataStorageMonitorJob {
|
|
|
|
|
|
|
|
|
|
private final DataStorageAnalysis dataStorageAnalysis;
|
|
|
|
|
private final MessageFeignClientImpl messageFeignClient;
|
|
|
|
|
private final SysUserFeignClientImpl sysUserFeignClient;
|
|
|
|
|
private final UserNameCacheService userNameCacheService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 定时筛选存储空间达到阈值的用户 发送告警消息
|
|
|
|
|
*/
|
|
|
|
|
@Scheduled(cron = "${data.storage-monitor.cron:0 */1 * * * ?}")
|
|
|
|
|
@Scheduled(cron = "${data.storage-monitor.cron:0 */10 * * * ?}")
|
|
|
|
|
public void checkDataStorageSpaceAndSendAlert() {
|
|
|
|
|
log.info("开始执行定时筛选存储空间达到阈值的用户任务");
|
|
|
|
|
try {
|
|
|
|
|
SdmResponse<List<FileStorageQuota>> sdmResponse = dataStorageAnalysis.listAllUserQuotaForJob();
|
|
|
|
|
if (CollectionUtils.isNotEmpty(sdmResponse.getData())) {
|
|
|
|
|
List<FileStorageQuota> fileStorageQuotaList = sdmResponse.getData();
|
|
|
|
|
log.info("[DataStorageMonitorJob] checkDataStorageSpaceAndSendAlert fileStorageQuotaList:{}", JSON.toJSONString(fileStorageQuotaList));
|
|
|
|
|
ThreadLocalContext.setTenantId(fileStorageQuotaList.get(0).getTenantId());
|
|
|
|
|
ThreadLocalContext.setUserId(fileStorageQuotaList.get(0).getUserId());
|
|
|
|
|
Set<Long> userIdsSet = new HashSet<>(fileStorageQuotaList.stream().map(FileStorageQuota::getUserId).toList());
|
|
|
|
|
Map<Long, String> longStringMap = userNameCacheService.batchGetUserNames(userIdsSet);
|
|
|
|
|
|
|
|
|
|
fileStorageQuotaList.forEach(quota -> {
|
|
|
|
|
SendMsgReq req = new SendMsgReq();
|
|
|
|
|
req.setTitle(MessageTemplateEnum.DATA_ALERT.getTitle());
|
|
|
|
|
req.setContent(MessageTemplateEnum.DATA_ALERT.getContent());
|
|
|
|
|
req.setTenantId(String.valueOf(quota.getTenantId()));
|
|
|
|
|
req.setUserId(String.valueOf(quota.getUserId()));
|
|
|
|
|
log.info("[DataStorageMonitorJob] checkDataStorageSpaceAndSendAlert param:{}", JSON.toJSONString(req));
|
|
|
|
|
log.info("[DataStorageMonitorJob] checkDataStorageSpaceAndSendAlert user param:{}", JSON.toJSONString(req));
|
|
|
|
|
messageFeignClient.sendMessage(req);
|
|
|
|
|
// 系统管理员
|
|
|
|
|
SdmResponse<List<Long>> response = sysUserFeignClient.getUserByRoleCode(CommonConstants.ROLE_CODE_ADMIN, quota.getTenantId());
|
|
|
|
|
if (response.getData() != null) {
|
|
|
|
|
List<Long> managerUserIds = response.getData();
|
|
|
|
|
managerUserIds.removeIf(managerUserId -> managerUserId.equals(quota.getUserId()));
|
|
|
|
|
if (CollectionUtils.isNotEmpty(managerUserIds)) {
|
|
|
|
|
for (Long managerUserId : managerUserIds) {
|
|
|
|
|
req.setContent(MessageTemplateEnum.DATA_ALERT_MANAGER.getContent(longStringMap.get(quota.getUserId())));
|
|
|
|
|
req.setUserId(String.valueOf(managerUserId));
|
|
|
|
|
log.info("[DataStorageMonitorJob] checkDataStorageSpaceAndSendAlert manager param:{}", JSON.toJSONString(req));
|
|
|
|
|
messageFeignClient.sendMessage(req);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
log.info("定时筛选存储空间达到阈值的用户任务执行完成");
|
|
|
|
|
|