From dfb94300b4fed8d126d222d2f88b05d7b4cf41b6 Mon Sep 17 00:00:00 2001 From: zhangqiyang <1066386929@qq.com> Date: Thu, 18 Apr 2024 09:05:19 +0800 Subject: [PATCH] Configure Continuous Integration .... --- .../src/main/resources/views/actionGroup.xml | 12 + .../test/src/main/resources/views/menu.xml | 4 +- .../system/log/controller/LogController.java | 39 +- .../com/system/log/service/LogService.java | 354 +++++++++++++++++- src/main/resources/application.properties | 23 +- 5 files changed, 408 insertions(+), 24 deletions(-) diff --git a/modules/test/src/main/resources/views/actionGroup.xml b/modules/test/src/main/resources/views/actionGroup.xml index dd4d727..9c94b7a 100644 --- a/modules/test/src/main/resources/views/actionGroup.xml +++ b/modules/test/src/main/resources/views/actionGroup.xml @@ -45,4 +45,16 @@ + + + + + + + + + + + + diff --git a/modules/test/src/main/resources/views/menu.xml b/modules/test/src/main/resources/views/menu.xml index f765997..7ea9f9b 100644 --- a/modules/test/src/main/resources/views/menu.xml +++ b/modules/test/src/main/resources/views/menu.xml @@ -5,6 +5,6 @@ - - + + diff --git a/src/main/java/com/system/log/controller/LogController.java b/src/main/java/com/system/log/controller/LogController.java index 6de3d5d..d876481 100644 --- a/src/main/java/com/system/log/controller/LogController.java +++ b/src/main/java/com/system/log/controller/LogController.java @@ -60,14 +60,41 @@ public class LogController extends JpaSupport { FileUtils.downloadFile(response, file); } - // @GET - // @Path("/getFile") - // public JSONObject getFileChange() { - // return logService.getFileChange(); - // } - + // 导出操作日志 public void exportLog(ActionRequest request, ActionResponse response) throws IOException { File file = logService.exportLog(request, response); FileUtils.downloadFile(response, file); } + + // 日志配置接口 + /* + * threshold 在线日志存储阈值 + * backupCycle 备份周期 + * backupStartTime 备份开始日期 + */ + @POST + @Path("/setLogConfig") + public JSONObject setLogConfig(JSONObject jsonObject) { + return logService.setLogConfig( + jsonObject.getInteger("threshold"), + jsonObject.getInteger("backupCycle"), + jsonObject.getString("backupStartTime")); + } + + // 日志配置查询接口 + @GET + @Path("/selectLogConfig") + public JSONObject selectLogConfig() { + return logService.selectLogConfig(); + } + + // 删除系统管理员操作日志备份 + public void deleteSystemManageUserBackup(ActionRequest request, ActionResponse response) { + logService.deleteSystemManageUserBackup(request, response); + } + + // 重新备份系统管理员操作日志 + public void secondBackup(ActionRequest request, ActionResponse response) { + logService.secondBackup(request, response); + } } diff --git a/src/main/java/com/system/log/service/LogService.java b/src/main/java/com/system/log/service/LogService.java index 99b8b57..2fa7901 100644 --- a/src/main/java/com/system/log/service/LogService.java +++ b/src/main/java/com/system/log/service/LogService.java @@ -1,20 +1,28 @@ package com.system.log.service; +import Log.LogBackup; +import Log.LogConfig; import Log.LogEntity; +import Log.repo.LogBackupRepository; +import Log.repo.LogConfigRepository; import Log.repo.LogEntityRepository; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.inject.Inject; +import com.google.inject.persist.Transactional; +import com.hypaas.db.JPA; import com.hypaas.rpc.ActionRequest; import com.hypaas.rpc.ActionResponse; import com.system.log.LogTypeEnum; import com.system.log.vo.LogRecordVO; import java.io.*; +import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import module.LogStatus; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; @@ -29,6 +37,10 @@ public class LogService { @Inject LogEntityRepository logEntityRepository; + @Inject LogConfigRepository logConfigRepository; + + @Inject LogBackupRepository logBackupRepository; + public JSONObject getDirContent(String logType, String startTime, String endTime, String keyWord) throws IOException { // 定义集合,存放文件名称 @@ -138,7 +150,7 @@ public class LogService { } // 输入开始时间与结束时间获得对应的标准毫秒数,方便筛选每个日志文件中的数据,必须以"yyyy-MM-dd HH:mm:ss"的形式输入 - private long getLongTime(String time) { + public long getLongTime(String time) { Date date; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { @@ -391,7 +403,7 @@ public class LogService { return exportLogExcelZip(selectResult, password); } - private long getSqlLongTime(Object time) { + public long getSqlLongTime(Object time) { // logEntity.getTime()形式 "2024-02-04T14:03:45.114" // startTime()形式 "2024-02-04 14:03" Date date; @@ -529,4 +541,342 @@ public class LogService { throw new RuntimeException(e); } } + + public JSONObject setLogConfig(Integer threshold, Integer backupCycle, String backupStartTime) { + List logBackupList = logBackupRepository.all().fetch(); + if (logBackupList.size() == 0) { + LogBackup logBackup = new LogBackup(); + logBackup.setBackupCycle(backupCycle); + logBackup.setThreshold(threshold); + logBackup.setBackupStartTime(backupStartTime); + saveBackup(logBackup); + } else { + LogBackup logBackupSelectResult = logBackupRepository.find(logBackupList.get(0).getId()); + logBackupSelectResult.setBackupCycle(backupCycle); + logBackupSelectResult.setThreshold(threshold); + logBackupSelectResult.setBackupStartTime(backupStartTime); + saveBackup(logBackupSelectResult); + } + + LogConfig logConfig = new LogConfig(); + logConfig.setThreshold(threshold); + logConfig.setBackupCycle(backupCycle); + logConfig.setStatus(LogStatus.waitBackup); + + // 起始备份时间为手动输入的备份开始时间,结束时间需要通过备份周期计算 + logConfig.setLogStartTime(backupStartTime); + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Calendar calendar = Calendar.getInstance(); + Date date = null; + String logEndTime = null; + try { + date = simpleDateFormat.parse(backupStartTime); + calendar.setTime(date); + calendar.add(Calendar.DAY_OF_YEAR, backupCycle); + Long longTime = calendar.getTime().getTime(); + // 将日志结束时间转化为"yyyy-MM-dd HH:mm:ss"格式 + logEndTime = simpleDateFormat.format(longTime); + } catch (ParseException e) { + throw new RuntimeException(e); + } + logConfig.setLogEndTime(logEndTime); + List list = logConfigRepository.all().fetch(); + if (list.size() != 0) { + // 说明已经有记录了,要先将原来的主配置找到改为历史配置 + for (LogConfig config : list) { + if ("Current".equals(config.getRecordType())) { + LogConfig logConfig1 = logConfigRepository.find(config.getId()); + logConfig1.setRecordType("History"); + saveConfig(logConfig1); + break; + } + } + } + logConfig.setRecordType("Current"); + saveConfig(logConfig); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("code", 200); + jsonObject.put("msg", "保存成功"); + return jsonObject; + } + + @Transactional + public LogConfig saveConfig(LogConfig entity) { + return JPA.save(entity); + } + + @Transactional + public LogBackup saveBackup(LogBackup entity) { + return JPA.save(entity); + } + + public void deleteSystemManageUserBackup(ActionRequest request, ActionResponse response) { + Long id = (Long) request.getContext().get("id"); + LogConfig logConfig = logConfigRepository.find(id); + if (null == logConfig) { + response.setError("未查询到此条记录"); + return; + } + String filePath = logConfig.getFilePath(); + File file = new File(filePath + "systemManageUser.zip"); + if (file.delete()) { + logConfig.setStatus(LogStatus.alreadyDelete); + response.setNotify("日志备份文件删除成功!"); + saveConfig(logConfig); + response.setReload(true); + } else { + response.setError("日志备份文件删除失败!"); + response.setReload(true); + } + } + + public void secondBackup(ActionRequest request, ActionResponse response) { + Long id = (Long) request.getContext().get("id"); + LogConfig logConfig = logConfigRepository.find(id); + if (null == logConfig) { + response.setError("未查询到此条记录"); + response.setReload(true); + return; + } + if (!LogStatus.backupFail.getValue().equals(logConfig.getStatus().getValue())) { + response.setNotify("该条记录状态非备份失败"); + response.setReload(true); + return; + } + String backupTime = null; + String filePath = null; + try { + filePath = backupLogExcelZip(selectRecord(logConfig)); + } catch (IOException e) { + response.setError("重新备份失败!"); + response.setReload(true); + throw new RuntimeException(e); + } + // 需要将状态改为已备份,并添加备份时间,计算备份大小,将当前配置改为历史配置 + logConfig.setStatus(LogStatus.alreadyBackup); + backupTime = getStringTime(System.currentTimeMillis()); + logConfig.setBackupStartTime(backupTime); + logConfig.setBackupTime(backupTime); + // 计算文件大小,单位以M表示,精确小数点后两位 + Map fileSizeMap = getSize(filePath); + logConfig.setThresholdSystemManageUserSize(fileSizeMap.get("16")); + logConfig.setThresholdSecurityUserSize("1"); + logConfig.setThresholdSecurityAuditUserSize("2"); + logConfig.setThresholdServiceUserSize("3"); + logConfig.setFilePath(filePath); + if (logConfig.getRecordType().equals("Current")) { + logConfig.setRecordType("History"); + // 如果是当前配置,要添加一条新的记录,将记录改为待配置,因为当前是主配置,历史配置的话不需要,备份一次即可 + LogConfig newRecord = new LogConfig(); + newRecord.setRecordType("Current"); + // 日志开始时间就是上次备份时间,根据备份周期计算日志结束时间 + newRecord.setLogStartTime(backupTime); + newRecord.setLogEndTime(getEndTime(backupTime, logConfig.getBackupCycle())); + newRecord.setStatus(LogStatus.waitBackup); + logConfigRepository.save(newRecord); + } + saveConfig(logConfig); + response.setReload(true); + } + + public Map> selectRecord(LogConfig logConfig) { + Map> map = new HashMap<>(); + List serviceUserSelectResult = new ArrayList<>(); + List securityUserSelectResult = new ArrayList<>(); + List securityAuditUserSelectResult = new ArrayList<>(); + List systemManageUserResult = new ArrayList<>(); + + List logEntityList = logEntityRepository.all().fetch(); + for (LogEntity logEntity : logEntityList) { + if (getSqlLongTime(logEntity.getTime()) > getLongTime(logConfig.getLogStartTime()) + && getSqlLongTime(logEntity.getTime()) < getLongTime(logConfig.getLogEndTime())) { + // 如果当条记录时间大于开始时间小于结束时间,则判断角色 + if (logEntity.getUserAuth().getValue().equals("16")) { + // 系统管理员 + systemManageUserResult.add(logEntity); + } + if (logEntity.getUserAuth().getValue().equals("1")) { + // 安全保密员 + securityUserSelectResult.add(logEntity); + } + if (logEntity.getUserAuth().getValue().equals("2")) { + // 安全审计员 + securityAuditUserSelectResult.add(logEntity); + } + if (logEntity.getUserAuth().getValue().equals("3")) { + // 业务用户 + serviceUserSelectResult.add(logEntity); + } + map.put("systemManageUser", systemManageUserResult); + map.put("securityUser", securityUserSelectResult); + map.put("securityAuditUser", securityAuditUserSelectResult); + map.put("serviceUser", serviceUserSelectResult); + } + } + return map; + } + + // 备份日志,导出为Excel类型 + public String backupLogExcelZip(Map> selectResult) throws IOException { + String tempFilePath = null; + Set>> entries = selectResult.entrySet(); + // 根据时间设定输出文件名 + SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS"); + String date = simpleDateFormat1.format(new Date(System.currentTimeMillis())); + for (Map.Entry> entry : entries) { + // 创建一个工作簿,也就是Excel文件 + HSSFWorkbook wb = new HSSFWorkbook(); + // 创建一个工作表 + HSSFSheet sheet = wb.createSheet(); + // 第一行,标题 + HSSFRow row0 = sheet.createRow(0); + HSSFCell cell0 = row0.createCell(0); + cell0.setCellValue("操作日志表"); + // 第二行,表头 + HSSFRow row1 = sheet.createRow(1); + row1.createCell(0).setCellValue("id"); + row1.createCell(1).setCellValue("address"); + row1.createCell(2).setCellValue("content"); + row1.createCell(3).setCellValue("hashValue"); + row1.createCell(4).setCellValue("levelDesc"); + row1.createCell(5).setCellValue("levelNum"); + row1.createCell(6).setCellValue("module"); + row1.createCell(7).setCellValue("objectName"); + row1.createCell(8).setCellValue("opType"); + row1.createCell(9).setCellValue("result"); + row1.createCell(10).setCellValue("resultDesc"); + row1.createCell(11).setCellValue("status"); + row1.createCell(12).setCellValue("time"); + row1.createCell(13).setCellValue("userAuth"); + row1.createCell(14).setCellValue("userAuthDesc"); + row1.createCell(15).setCellValue("userId"); + + // 向表中写入数据 + int rowInt = 2; + for (LogEntity logEntity : entry.getValue()) { + HSSFRow row = sheet.createRow(rowInt++); + int index = 0; + row.createCell(index++).setCellValue(logEntity.getId()); + row.createCell(index++).setCellValue(logEntity.getAddress()); + row.createCell(index++).setCellValue(logEntity.getContent()); + row.createCell(index++).setCellValue(logEntity.getHashValue()); + row.createCell(index++).setCellValue(logEntity.getLevelDesc()); + row.createCell(index++).setCellValue(logEntity.getLevelNum().getValue()); + row.createCell(index++).setCellValue(logEntity.getModule()); + row.createCell(index++).setCellValue(logEntity.getObjectName()); + row.createCell(index++).setCellValue(logEntity.getOpType().getValue()); + row.createCell(index++).setCellValue(logEntity.getResult()); + row.createCell(index++).setCellValue(logEntity.getResultDesc()); + row.createCell(index++).setCellValue(logEntity.getStatus()); + row.createCell(index++).setCellValue(logEntity.getTime().toString()); + row.createCell(index++).setCellValue(logEntity.getUserAuth().getValue()); + row.createCell(index++).setCellValue(logEntity.getUserAuthDesc()); + row.createCell(index++).setCellValue(logEntity.getUserId()); + } + FileOutputStream fos = null; + try { + + // 输出文件,创建字节输出流 + File tempFile = new File(File.createTempFile("log", ".xls").getPath()); + fos = new FileOutputStream(tempFile); + wb.write(fos); + fos.flush(); + + // 获取临时文件目录 + String tempDir = System.getProperty("java.io.tmpdir"); + tempFilePath = tempDir + "\\" + date; + String realFilePath = tempDir + "\\" + date + entry.getKey() + ".zip"; + // 在临时文件目录中创建一个随机名称的zip文件 + File file = new File(realFilePath); + ZipFile zipFile = new ZipFile(file); + ZipParameters parameters = new ZipParameters(); // 设置zip包的一些参数集合 + // parameters.setEncryptFiles(true); // 是否设置密码(此处设置为:是) + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式(默认值) + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 普通级别(参数很多) + // parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密级别 + // 向zip文件中根据设置的参数写入数据 + zipFile.createZipFile(tempFile, parameters); + + } catch (ZipException e) { + throw new RuntimeException(e); + } finally { + fos.close(); + } + } + return tempFilePath; + } + + public String getStringTime(long time) { + SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return simpleDateFormat1.format(time); + } + + public String getEndTime(String time, Integer backupCycle) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Calendar calendar = Calendar.getInstance(); + Date date = null; + String logEndTime = null; + try { + date = simpleDateFormat.parse(time); + calendar.setTime(date); + calendar.add(Calendar.DAY_OF_YEAR, backupCycle); + Long longTime = calendar.getTime().getTime(); + // 将日志结束时间转化为"yyyy-MM-dd HH:mm:ss"格式 + logEndTime = simpleDateFormat.format(longTime); + return logEndTime; + } catch (ParseException e) { + throw new RuntimeException(e); + } + } + + public Map getSize(String filePath) { + Map map = new HashMap<>(); + map.put("16", getFileSize(filePath + "systemManageUser.zip")); + map.put("1", getFileSize(filePath + "securityUser.zip")); + map.put("2", getFileSize(filePath + "securityAuditUser.zip")); + map.put("3", getFileSize(filePath + "serviceUser.zip")); + return map; + } + + // 计算文件大小,单位为M,保留两位小数 + public String getFileSize(String filePath) { + File file = new File(filePath); + long fileSize = file.length(); + DecimalFormat df = new DecimalFormat("#.00"); + // 将字节转换为兆字节(MB)并格式化输出 + double fileSizeInKB = (double) fileSize / 1024; + if (fileSizeInKB > 1024) { + double fileSizeInMB = (double) fileSize / (1024 * 1024); + if (fileSizeInMB > 1024) { + double fileSizeInGB = (double) fileSize / (1024 * 1024 * 1024); + String formattedNumber = df.format(fileSizeInGB); + double result = Double.parseDouble(formattedNumber); + return result + "GB"; + } + String formattedNumber = df.format(fileSizeInMB); + double result = Double.parseDouble(formattedNumber); + return result + "MB"; + } + String formattedNumber = df.format(fileSizeInKB); + double result = Double.parseDouble(formattedNumber); + return result + "KB"; + } + + public JSONObject selectLogConfig() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("code", 200); + jsonObject.put("msg", "操作成功"); + List logBackupList = logBackupRepository.all().fetch(); + if (logBackupList.size() == 0) { + jsonObject.put("data", null); + return jsonObject; + } else { + JSONObject selectResult = new JSONObject(); + selectResult.put("threshold", logBackupList.get(0).getThreshold()); + selectResult.put("backupCycle", logBackupList.get(0).getBackupCycle()); + selectResult.put("backupStartTime", logBackupList.get(0).getBackupStartTime()); + jsonObject.put("data", selectResult); + return jsonObject; + } + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5b76e1e..d092df5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -62,14 +62,14 @@ auth.logout.local=true #db.default.password = hypaas # MySQL -db.default.driver= +db.default.driver=com.mysql.cj.jdbc.Driver db.default.ddl=update -db.default.url=jdbc:://?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true -db.default.user= -db.default.password= +db.default.url=jdbc:mysql://139.9.112.197:3308/portal-gateway?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true +db.default.user=portal +db.default.password=Sun@qc2023 -system.operate.log.switch=true +system.operate.log.switch=false event.center.url=http://123.57.213.228:1117/prod-api/event/send_event_info #db.company1.name=Company 1 @@ -239,7 +239,7 @@ ldap.user.filter=(uid={0}) # ~~~~~ # quartz job scheduler # Specify whether to enable quartz scheduler -quartz.enable=false +quartz.enable=true # total number of threads in quartz thread pool # the number of jobs that can run simultaneously quartz.threadCount=3 @@ -321,14 +321,9 @@ logging.level.com.hypaas=INFO # Log connection pooling #logging.level.com.zaxxer.hikari = INFO -auth.jwt.basic.auth.enabled=true -application.microcontext=/lowcode-portal-logmanage/ -application.gatewaycontext=/lowcode-portal-logmanage/ - - - - - +#auth.jwt.basic.auth.enabled=true +#application.microcontext=/lowcode-portal-logmanage/ +#application.gatewaycontext=/lowcode-portal-logmanage/