refactor(系统设置): 优化任务中心日志记录

This commit is contained in:
wxg0103 2024-05-07 17:49:02 +08:00 committed by Craftsman
parent bb6556ae4d
commit c00bf37336
6 changed files with 38 additions and 39 deletions

View File

@ -15,6 +15,10 @@ ALTER TABLE test_plan_config ADD COLUMN test_planning BIT NOT NULL DEFAULT 0 CO
ALTER TABLE api_definition_mock ADD COLUMN update_user VARCHAR(50) COMMENT '更新人';
ALTER TABLE operation_history MODIFY COLUMN module VARCHAR(100);
ALTER TABLE operation_log MODIFY COLUMN module VARCHAR(100);
-- set innodb lock wait timeout to default
SET SESSION innodb_lock_wait_timeout = DEFAULT;

View File

@ -281,11 +281,11 @@ public class ApiTaskCenterService {
extApiReportMapper.updateReportStatus(subList, System.currentTimeMillis(), userId);
extApiReportMapper.updateApiCaseStatus(subList);
//记录日志
saveLog(subList, userId, path, method, module, TaskCenterResourceType.API_CASE.toString());
saveLog(subList, userId, path, method, StringUtils.join(module, "_REAL_TIME_API_CASE"), TaskCenterResourceType.API_CASE.toString());
} else if (request.getModuleType().equals(TaskCenterResourceType.API_SCENARIO.toString())) {
extApiScenarioReportMapper.updateReportStatus(subList, System.currentTimeMillis(), userId);
extApiScenarioReportMapper.updateApiScenario(subList);
saveLog(subList, userId, path, method, module, TaskCenterResourceType.API_SCENARIO.toString());
saveLog(subList, userId, path, method, StringUtils.join(module,"_REAL_TIME_API_SCENARIO"), TaskCenterResourceType.API_SCENARIO.toString());
}
}
});

View File

@ -66,7 +66,7 @@ public class TaskCenterController {
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void delete(@PathVariable String moduleType, @PathVariable String id) {
hasPermission(SYSTEM, moduleType);
taskCenterService.delete(id, SessionUtils.getUserId(), "/task/center/system/schedule/delete/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER);
taskCenterService.delete(id, moduleType, SessionUtils.getUserId(), "/task/center/system/schedule/delete/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER);
}
@GetMapping("/org/schedule/delete/{moduleType}/{id}")
@ -74,7 +74,7 @@ public class TaskCenterController {
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void deleteOrg(@PathVariable String moduleType, @PathVariable String id) {
hasPermission(ORG, moduleType);
taskCenterService.delete(id, SessionUtils.getUserId(), "/task/center/org/schedule/delete/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER);
taskCenterService.delete(id, moduleType,SessionUtils.getUserId(), "/task/center/org/schedule/delete/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER);
}
@GetMapping("/project/schedule/delete/{moduleType}/{id}")
@ -82,7 +82,7 @@ public class TaskCenterController {
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void deleteProject(@PathVariable String moduleType, @PathVariable String id) {
hasPermission(PROJECT, moduleType);
taskCenterService.delete(id, SessionUtils.getUserId(), "/task/center/project/schedule/delete/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER);
taskCenterService.delete(id, moduleType,SessionUtils.getUserId(), "/task/center/project/schedule/delete/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER);
}
@GetMapping("/system/schedule/switch/{moduleType}/{id}")
@ -90,7 +90,7 @@ public class TaskCenterController {
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void enable(@PathVariable String moduleType, @PathVariable String id) {
hasPermission(SYSTEM, moduleType);
taskCenterService.enable(id, SessionUtils.getUserId(), "/task/center/system/schedule/switch/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER);
taskCenterService.enable(id, moduleType,SessionUtils.getUserId(), "/task/center/system/schedule/switch/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER);
}
@ -99,7 +99,7 @@ public class TaskCenterController {
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void enableOrg(@PathVariable String moduleType, @PathVariable String id) {
hasPermission(ORG, moduleType);
taskCenterService.enable(id, SessionUtils.getUserId(), "/task/center/org/schedule/switch/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER);
taskCenterService.enable(id, moduleType,SessionUtils.getUserId(), "/task/center/org/schedule/switch/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER);
}
@GetMapping("/project/schedule/switch/{moduleType}/{id}")
@ -107,7 +107,7 @@ public class TaskCenterController {
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void enableProject(@PathVariable String moduleType, @PathVariable String id) {
hasPermission(PROJECT, moduleType);
taskCenterService.enable(id, SessionUtils.getUserId(), "/task/center/project/schedule/switch/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER);
taskCenterService.enable(id, moduleType,SessionUtils.getUserId(), "/task/center/project/schedule/switch/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER);
}
@PostMapping("/system/schedule/update/{moduleType}/{id}")
@ -115,7 +115,7 @@ public class TaskCenterController {
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void update(@PathVariable String moduleType, @PathVariable String id, @RequestBody Object cron) {
hasPermission(SYSTEM, moduleType);
taskCenterService.update(id, cron.toString(), SessionUtils.getUserId(), "/task/center/system/schedule/update/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER);
taskCenterService.update(id, moduleType,cron.toString(), SessionUtils.getUserId(), "/task/center/system/schedule/update/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER);
}
@PostMapping("/org/schedule/update/{moduleType}/{id}")
@ -123,7 +123,7 @@ public class TaskCenterController {
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void updateOrg(@PathVariable String moduleType, @PathVariable String id, @RequestBody Object cron) {
hasPermission(ORG, moduleType);
taskCenterService.update(id, cron.toString(), SessionUtils.getUserId(), "/task/center/org/schedule/update/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER);
taskCenterService.update(id, moduleType,cron.toString(), SessionUtils.getUserId(), "/task/center/org/schedule/update/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER);
}
@PostMapping("/project/schedule/update/{moduleType}/{id}")
@ -131,7 +131,7 @@ public class TaskCenterController {
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void updateProject(@PathVariable String moduleType, @PathVariable String id, @RequestBody Object cron) {
hasPermission(PROJECT, moduleType);
taskCenterService.update(id, cron.toString(), SessionUtils.getUserId(), "/task/center/project/schedule/update/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER);
taskCenterService.update(id, moduleType,cron.toString(), SessionUtils.getUserId(), "/task/center/project/schedule/update/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER);
}
@PostMapping("/system/schedule/batch-enable")

View File

@ -1,27 +1,14 @@
package io.metersphere.system.dto.taskcenter.enums;
import java.util.List;
/**
* @author: LAN
* @date: 2024/1/22 16:41
* @version: 1.0
*/
public enum ScheduleTagType {
API_IMPORT("API_IMPORT"),
API_SCENARIO("API_SCENARIO"),
UI_SCENARIO("UI_TEST"),
LOAD_TEST("LOAD_TEST"),
TEST_PLAN("TEST_PLAN");
API_IMPORT,
API_SCENARIO,
TEST_PLAN;
private List<String> names;
ScheduleTagType(String... names) {
this.names = List.of(names);
}
public List<String> getNames() {
return names;
}
}

View File

@ -189,13 +189,22 @@ public class TaskCenterService {
return organization;
}
public void delete(String id, String userId, String path, String module) {
public void delete(String id,String moduleType, String userId, String path, String module) {
Schedule schedule = checkScheduleExit(id);
if (ScheduleTagType.API_IMPORT.getNames().contains(schedule.getResourceType())) {
String logModule = getLogModule(moduleType, module);
if (StringUtils.equals(schedule.getResourceType(), ScheduleTagType.API_IMPORT.name())) {
extSwaggerMapper.deleteByPrimaryKey(schedule.getResourceId());
}
scheduleService.deleteByResourceId(schedule.getResourceId(), schedule.getJob());
saveLog(List.of(schedule), userId, path, HttpMethodConstants.GET.name(), module, OperationLogType.DELETE.name());
saveLog(List.of(schedule), userId, path, HttpMethodConstants.GET.name(), logModule, OperationLogType.DELETE.name());
}
private static String getLogModule(String moduleType, String module) {
return switch (ScheduleTagType.valueOf(moduleType)) {
case ScheduleTagType.API_IMPORT -> StringUtils.join(module, "_TIME_API_IMPORT");
case ScheduleTagType.API_SCENARIO -> StringUtils.join(module, "_TIME_API_SCENARIO");
default -> throw new MSException(Translator.get("module_type_error"));
};
}
private Schedule checkScheduleExit(String id) {
@ -206,23 +215,25 @@ public class TaskCenterService {
return schedule;
}
public void enable(String id, String userId, String path, String module) {
public void enable(String id,String moduleType, String userId, String path, String module) {
Schedule schedule = checkScheduleExit(id);
schedule.setEnable(!schedule.getEnable());
scheduleService.editSchedule(schedule);
scheduleService.addOrUpdateCronJob(schedule, new JobKey(schedule.getKey(), schedule.getJob()),
new TriggerKey(schedule.getKey(), schedule.getJob()), BaseScheduleJob.class);
apiScheduleNoticeService.sendScheduleNotice(schedule, userId);
saveLog(List.of(schedule), userId, path, HttpMethodConstants.GET.name(), module, OperationLogType.UPDATE.name());
String logModule = getLogModule(moduleType, module);
saveLog(List.of(schedule), userId, path, HttpMethodConstants.GET.name(), logModule, OperationLogType.UPDATE.name());
}
public void update(String id, String cron, String userId, String path, String module) {
public void update(String id,String moduleType, String cron, String userId, String path, String module) {
Schedule schedule = checkScheduleExit(id);
schedule.setValue(cron);
scheduleService.editSchedule(schedule);
scheduleService.addOrUpdateCronJob(schedule, new JobKey(schedule.getKey(), schedule.getJob()),
new TriggerKey(schedule.getKey(), schedule.getJob()), schedule.getJob().getClass());
saveLog(List.of(schedule), userId, path, HttpMethodConstants.POST.name(), module, OperationLogType.UPDATE.name());
String logModule = getLogModule(moduleType, module);
saveLog(List.of(schedule), userId, path, HttpMethodConstants.POST.name(), logModule, OperationLogType.UPDATE.name());
}
private void saveLog(List<Schedule> scheduleList, String userId, String path, String method, String module, String operationType) {
@ -293,7 +304,8 @@ public class TaskCenterService {
});
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
apiScheduleNoticeService.batchSendNotice(projectId, scheduleList, userMapper.selectByPrimaryKey(userId), enable ? NoticeConstants.Event.OPEN : NoticeConstants.Event.CLOSE);
saveLog(scheduleList, userId, path, HttpMethodConstants.POST.name(), module, OperationLogType.UPDATE.name());
String logModule = getLogModule(request.getScheduleTagType(), module);
saveLog(scheduleList, userId, path, HttpMethodConstants.POST.name(), logModule, OperationLogType.UPDATE.name());
}
public void batchEnableProject(TaskCenterScheduleBatchRequest request, String userId, String projectId, String path, String module, boolean enable) {

View File

@ -164,10 +164,6 @@ class TaskCenterScheduleControllerTests extends BaseTest {
this.requestGet("/task/center/system/schedule/delete/" + "API_IMPORT/" + scheduleId);
Schedule schedule = scheduleMapper.selectByPrimaryKey(scheduleId);
Assertions.assertNull(schedule);
if (ScheduleTagType.API_IMPORT.getNames().contains(oldSchedule.getType())) {
int count = extSwaggerMapper.selectByPrimaryKey(oldSchedule.getResourceId());
Assertions.assertTrue(count > 0);
}
this.requestGet("/task/center/org/schedule/delete/" + "API_IMPORT/" + "4");
this.requestGet("/task/center/project/schedule/delete/" + "API_SCENARIO/" + "2");
this.requestGet("/task/center/system/schedule/delete/" + "API_SCENARIO/" + "schedule-121", ERROR_REQUEST_MATCHER);