Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
lulz1 2024-03-12 16:34:58 +08:00
commit 1b028c2ac5
6 changed files with 113 additions and 17 deletions

View File

@ -0,0 +1,49 @@
package com.chint.infrastructure.config.LogConfig;
import com.chint.domain.aggregates.standards.TravelStandards;
import com.chint.infrastructure.constant.CommonMessageConstant;
import com.chint.infrastructure.util.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 日志接口
*/
@RestController()
@RequestMapping("/log")
public class LogController {
@Autowired
private LogService logService;
/**
* 清空日志
*
* @param days 清空多少天前的日志
*/
@GetMapping("/delete/days")
@ApiOperation("清空多少天前日志")
public Result<Integer> deleteLogDays(@RequestParam("days") Integer days) {
Integer count = logService.deleteLogDays(days);
return Result.Success(CommonMessageConstant.SUCCESS, count);
}
/**
* 清空日志
*
* @param rows 清空多少行日志
*/
@GetMapping("/delete/rows")
@ApiOperation("清空多少行日志")
public Result<Integer> deleteLogRows(@RequestParam("days") Integer rows) {
Integer count = logService.deleteLogRows(rows);
return Result.Success(CommonMessageConstant.SUCCESS, count);
}
}

View File

@ -98,15 +98,15 @@ public class LogService {
*/
@Transactional
@Async
@Scheduled(cron = "0 0 3 ? * 2") // 每周一凌晨3点执行一次
public void deleteLog() {
//清空日志超过一个月时间的数据
Integer count = jdbcSystemLogRepository.deleteLogsOlderThanOneMonth();
log.info("一个月以前的日志删除成功:{}条", count);
@Scheduled(cron = "0 0 4 * * 1") // 每周一凌晨4点执行一次
public void timedClearLog() {
//清空日志前一周的数据
Integer count = jdbcSystemLogRepository.deleteLogsOlderThanOneMonth(7);
log.info("日志删除成功:{}条", count);
//获取目前的日志总数量
long nums = jdbcSystemLogRepository.count();
long maxLogCount = 3000000L;
//如果删除一个月前的日志数量后表中数量还是大于300万条
long maxLogCount = 1000000L;
//如果删除一个月前的日志数量后表中数量还是大于100万条
if (nums > maxLogCount) {
// 计算需要删除的记录数
long deleteCount = nums - maxLogCount;
@ -115,4 +115,23 @@ public class LogService {
log.info("日志批量删除:{}条", batchCount);
}
}
/**
* 清空天日志
*/
@Transactional
public Integer deleteLogDays(Integer days) {
//清空日志
return jdbcSystemLogRepository.deleteLogsOlderThanOneMonth(days);
}
@Transactional
public Integer deleteLogRows(Integer rows) {
//清空日志
return jdbcSystemLogRepository.deleteBatch((long) rows);
}
}

View File

@ -84,13 +84,14 @@ public class RequestLoggingInterceptor implements HandlerInterceptor {
.employeeNo(employeeNo)
.name(name)
.accessTime(accessTime).build();
String hKey = UUID.randomUUID().toString();
//获得时间戳
DateTimeFormatter formatTimestamp = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSSSSS");
String timestamp = now.format(formatTimestamp);
String data = new Gson().toJson(systemLog);
//存入redis
redisCache.setCacheMapValue("SystemLog", hKey, data);
redisCache.setCacheMapValue("SystemLog", timestamp, data);
// 设置单独的过期时间
String hashFieldKey = "SystemLog" + ":" + hKey;
// redisCache.expire(hashFieldKey, 7, TimeUnit.DAYS);
String hashFieldKey = "SystemLog" + ":" + timestamp;
redisCache.expire(hashFieldKey, 1, TimeUnit.DAYS);
}

View File

@ -12,12 +12,19 @@ import org.springframework.stereotype.Repository;
@Repository
public interface JdbcSystemLogRepository extends CrudRepository<SystemLog, Long> {
//删除多少天前的数据
@Modifying
@Query(value = "DELETE FROM system_log sl WHERE STR_TO_DATE(sl.access_time, '%Y-%m-%d %H:%i:%s') < DATE_SUB(NOW(), INTERVAL 1 MONTH)")
Integer deleteLogsOlderThanOneMonth();
@Query(value = "DELETE FROM system_log sl WHERE STR_TO_DATE(sl.access_time, '%Y-%m-%d %H:%i:%s') < DATE_SUB(NOW(), INTERVAL :days DAY)")
Integer deleteLogsOlderThanOneMonth(@Param("days") Integer days);
//删除指定数量的日志按时间大小正序删除
@Modifying
@Query(value = "DELETE sl FROM system_log sl JOIN (SELECT id FROM system_log ORDER BY access_time LIMIT :deleteCount) AS sub ON sl.id = sub.id")
Integer deleteBatch(@Param("deleteCount") long deleteCount);
//查看日志表大小
/*@Query(value = "SELECT round(((data_length + index_length) / 1024 / 1024), 2) AS size_in_mb FROM information_schema.TABLES WHERE table_schema = 'itinerary_booking' AND table_name = 'system_log'")
String getLogTableSizeInMB();*/
}

View File

@ -4,7 +4,7 @@ import lombok.Data;
@Data
public class BPMBack {
private String tag;//BPM平台H3BPM/XNBPM/ZWBPM/CPSH3/其它
private String tag;//BPM平台H3BPM/XNBPM/ZWBPM/CPSH3/ANFSSC/其它
private String orderNo;//订单号
private String orderType;//订单类型 超标:酒店超标火车票超标机票超标 改签:机票改签机票退票火车票改签火车票退票
private String orderSource;//订单来源: 携程商旅/同程商旅

View File

@ -6,10 +6,12 @@ import com.chint.domain.aggregates.order.Leg;
import com.chint.domain.aggregates.standards.StaffRank;
import com.chint.domain.aggregates.standards.XNStaffRank;
import com.chint.domain.service.order_sync.LYOrderSyncAdapter;
import com.chint.infrastructure.config.LogConfig.LogController;
import com.chint.infrastructure.config.LogConfig.RedisCache;
import com.chint.infrastructure.config.LogConfig.SystemLog;
import com.chint.infrastructure.repository.jdbc.JdbcStaffRankRepository;
import com.chint.infrastructure.repository.jdbc.JdbcSystemLogRepository;
import com.chint.infrastructure.util.Result;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@ -159,8 +161,8 @@ public class ExcelTest {
}
// @Test
public void deleteLog() {
Integer count = jdbcSystemLogRepository.deleteLogsOlderThanOneMonth();
public void deleteLog() throws InterruptedException {
/*Integer count = jdbcSystemLogRepository.deleteLogsOlderThanOneMonth();
log.info("日志删除成功:{}条", count);
//获取目前的日志总数量
long nums = jdbcSystemLogRepository.count();
@ -172,8 +174,26 @@ public class ExcelTest {
// 批量删除数据
Integer i = jdbcSystemLogRepository.deleteBatch(deleteCount);
System.out.println("i = " + i);
}*/
for (int i = 0; i < 10; i++) {
LocalDateTime now = LocalDateTime.now();
Thread.sleep(1);
// 定义日期时间格式
// 格式化 LocalDateTime 对象
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSSSSS");
String formattedDateTime = now.format(formatter);
System.out.println("formattedDateTime = " + formattedDateTime);
}
}
@Autowired
LogController logController;
// @Test
public void logTableSizeInMB() throws InterruptedException {
Result<Integer> integerResult = logController.deleteLogDays(1);
System.out.println("logMessageDtoResult = " + integerResult);
}
}