1、任务启动时就发送指令

2、新增局点多级数据库表查询
3、静态任务下发指令补充指令展示id
This commit is contained in:
lifulian
2024-06-08 15:01:38 +08:00
committed by PushM
parent c57bdf6103
commit 49b9adf086
9 changed files with 267 additions and 47 deletions

View File

@@ -51,8 +51,8 @@ public class AuditAdvice implements ResponseBodyAdvice<ResponseResult> {
@Override @Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// return false; return false;
return true; // return true;
} }
@Override @Override

View File

@@ -66,12 +66,23 @@ public class CommandService {
return commandInfo.getUUID(); return commandInfo.getUUID();
} }
public void createCommands(List<TaskCommandInfo> taskCommandInfos) { public List<String> createCommands(List<TaskCommandInfo> taskCommandInfos) {
List<String> commandUUIDs = ListUtils.newArrayListWithExpectedSize(taskCommandInfos.size());
Function<CommandMapper, Function<List<TaskCommandInfo>, Boolean>> function = mapper -> list -> { Function<CommandMapper, Function<List<TaskCommandInfo>, Boolean>> function = mapper -> list -> {
List<TaskCommandInfo> taskCommandInfoBatch = ListUtils.newArrayListWithExpectedSize(BatchSize); List<TaskCommandInfo> taskCommandInfoBatch = ListUtils.newArrayListWithExpectedSize(BatchSize);
for (TaskCommandInfo info : list) { for (TaskCommandInfo info : list) {
info.setUUID(UUID.randomUUID().toString()); info.setUUID(UUID.randomUUID().toString());
commandUUIDs.add(info.getUUID());
info.setDisplayId(
"ZL-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
+ "-"
+ String.format("%06d", counter.generateId("command"))
);
taskCommandInfoBatch.add(info); taskCommandInfoBatch.add(info);
if (taskCommandInfoBatch.size() < BatchSize) { if (taskCommandInfoBatch.size() < BatchSize) {
continue; continue;
} }
@@ -91,6 +102,8 @@ public class CommandService {
}; };
sqlSessionWrapper.startBatchSession(CommandMapper.class, function, taskCommandInfos); sqlSessionWrapper.startBatchSession(CommandMapper.class, function, taskCommandInfos);
return commandUUIDs;
} }
public List<TaskCommandInfo> queryCommandInfos(Long taskId, public List<TaskCommandInfo> queryCommandInfos(Long taskId,

View File

@@ -0,0 +1,40 @@
package com.realtime.protection.server.nodetree;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//李福连
@Data
@AllArgsConstructor
@NoArgsConstructor
public class NodeEntity {
int id;
int parent_id;
String code;
String dict_key;
String dict_value;
int sort;
String remark;
int is_deleted;
// 子节点列表
List<NodeEntity> children = new ArrayList<>();
public NodeEntity(int id, int parent_id, String code, String dict_key, String dict_value, int sort, String remark, int is_deleted) {
this.id = id;
this.parent_id = parent_id;
this.code = code;
this.dict_key = dict_key;
this.dict_value = dict_value;
this.sort = sort;
this.remark = remark;
this.is_deleted = is_deleted;
}
}

View File

@@ -0,0 +1,40 @@
package com.realtime.protection.server.nodetree;
import com.realtime.protection.configuration.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
//李福连
@RestController
@RequestMapping("/nodeTree")
@Slf4j
public class NodeTreeController {
@Autowired
NodeTreeService nodeTreeService;
@GetMapping("/get")
public ResponseResult tupoInfo(String business) throws IOException {
List<NodeEntity> roots = nodeTreeService.get();
if (!roots.isEmpty()) {
return ResponseResult.ok()
.setData("data", roots);
}
return ResponseResult.error()
.setMessage("没有查到");
}
}

View File

@@ -0,0 +1,15 @@
package com.realtime.protection.server.nodetree;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
//李福连
@Mapper
public interface NodeTreeMapper {
@Select("select * from realtime_protection.t_judian_dict where is_deleted=0")
List<NodeEntity> get();
}

View File

@@ -0,0 +1,52 @@
package com.realtime.protection.server.nodetree;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//李福连
@Service
public class NodeTreeService {
@Autowired
NodeTreeMapper nodeTreeMapper;
public List<NodeEntity> get() {
List<NodeEntity> nodes= nodeTreeMapper.get();
// 用于存储每个节点的id和节点对象的映射
Map<Integer, NodeEntity> nodeMap = new HashMap<>();
// 用于存储所有根节点
List<NodeEntity> roots = new ArrayList<>();
// 将所有节点放入map中以便根据id快速查找
for (NodeEntity node : nodes) {
nodeMap.put(node.id, node);
}
// 建立树结构
for (NodeEntity node : nodes) {
if (node.parent_id == 0) {
// 如果parent_id为0说明这是一个根节点
roots.add(node);
} else {
// 根据parent_id找到父节点然后将当前节点加入父节点的children列表中
NodeEntity parentNode = nodeMap.get(node.parent_id);
if (parentNode != null) {
parentNode.children.add(node);
}
}
}
return roots;
}
}

View File

@@ -101,7 +101,7 @@ public class StateChangeService {
/** /**
* 将任务切换为结束状态 * 将任务切换为结束状态
*/ */
@Scheduled(cron = "0 0/10 * * * ?") @Scheduled(cron = "0/10 * * * * ?")
@Async @Async
protected void finishTasks() { protected void finishTasks() {
List<Long> finishedTaskIds = taskService.getFinishedTasks(); List<Long> finishedTaskIds = taskService.getFinishedTasks();
@@ -117,4 +117,25 @@ public class StateChangeService {
} }
} }
// /**
// * 将任务切换为开始状态
// */
// @Scheduled(cron = "0/10 * * * * ?")
// @Async
// protected void startTasks() {
// List<Long> startedTaskIds = taskService.getFinishedTasks();
// log.debug("成功扫描出所有需要变为开始状态的任务:{}", startedTaskIds);
//
// for (Long taskId : startedTaskIds) {
// try {
// changeState(StateEnum.RUNNING.getStateNum(), taskId, true);
// } catch (Exception e) {
// log.warn(String.format("任务%d从%s状态变为运行中RUNNING状态遭遇异常%s",
// taskId, taskService.queryTaskStatus(taskId), e.getMessage()));
// }
//
// }
// }
} }

View File

@@ -8,15 +8,19 @@ import com.realtime.protection.configuration.utils.enums.TaskTypeEnum;
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum; import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum;
import com.realtime.protection.server.command.CommandService; import com.realtime.protection.server.command.CommandService;
import com.realtime.protection.server.task.TaskService; import com.realtime.protection.server.task.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.*; import org.springframework.web.reactive.function.client.*;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@Slf4j
public class StateHandler { public class StateHandler {
@@ -25,6 +29,12 @@ public class StateHandler {
// .baseUrl("http://10.58.72.151:8088") // .baseUrl("http://10.58.72.151:8088")
.build(); .build();
//http://10.26.22.123:17011/rule
private final WebClient client_commandDistribute = WebClient.builder()
.baseUrl("http://10.26.22.123:17011")
// .baseUrl("http://10.58.72.151:8088")
.build();
protected Boolean handleStart(TaskService taskService, CommandService commandService, Long taskId) { protected Boolean handleStart(TaskService taskService, CommandService commandService, Long taskId) {
Task task = taskService.queryTask(taskId); Task task = taskService.queryTask(taskId);
@@ -121,10 +131,48 @@ public class StateHandler {
taskService.updateStaticRuleAuditStatusInTask(task.getTaskId(), AuditStatusEnum.USING); taskService.updateStaticRuleAuditStatusInTask(task.getTaskId(), AuditStatusEnum.USING);
// taskService.changeTaskAuditStatus(task.getTaskId(), AuditStatusEnum.USING.getNum()); // taskService.changeTaskAuditStatus(task.getTaskId(), AuditStatusEnum.USING.getNum());
commandService.createCommands(staticTaskCommandInfos); List<String> commandUUIDs= commandService.createCommands(staticTaskCommandInfos);
// 将command新建信号发送到c3下发程序
sendCommandDistributeSignal(commandUUIDs);
return true; return true;
} }
private Boolean sendCommandDistributeSignal(List<String> commandUUIDs) {
List<Map<String, String>> commandIDMaps = new ArrayList<>();
for (String commandUUID : commandUUIDs) {
commandIDMaps.add(Map.of("COMMAND_ID", commandUUID));
}
AtomicReference<Boolean> success = new AtomicReference<>(false);
Mono<Map> mono = client_commandDistribute.post()
.uri("/rule")
.bodyValue(commandIDMaps)
.exchangeToMono(res -> {
if (res.statusCode().equals(HttpStatus.OK)) {
return res.bodyToMono(Map.class);
}
return res.createError();
})
.doOnError(WebClientResponseException.class, res -> success.set(false));
Map<String, String> response = mono.block(Duration.ofSeconds(5));
if (response == null) {
return false;
}
response.forEach((commandUUID, responseCode) -> {
if (!responseCode.equals("0")) {
log.error("指令首次下发失败, 指令uuid: " + commandUUID + ", responseCode: " + responseCode);
}
});
success.set(true);
return success.get();
}
private Boolean sendFilters(TaskService taskService, Task task) { private Boolean sendFilters(TaskService taskService, Task task) {
List<DynamicTaskInfo> dynamicTaskInfos = taskService.getDynamicTaskInfos(task.getTaskId()); List<DynamicTaskInfo> dynamicTaskInfos = taskService.getDynamicTaskInfos(task.getTaskId());

View File

@@ -236,58 +236,49 @@ class TaskServiceTest extends ProtectionApplicationTests {
@Test @Test
void testStartStaticTask() throws DorisStartException { void testStartStaticTask() throws DorisStartException {
for (int n = 10; n < 20; n++) {
List<Integer> staticRuleIds = new ArrayList<>();
for (int i = 10; i < 15; i++) {
StaticRuleObject staticRuleTest = new StaticRuleObject();
staticRuleTest.setStaticRuleName(n+"test_staticrule" + i);
staticRuleTest.setStaticRuleCreateUsername("NSADD管理员");
staticRuleTest.setStaticRuleCreateDepart("组织树");
staticRuleTest.setStaticRuleCreateUserId(22222222);
staticRuleTest.setAuditStatus(0);
staticRuleTest.setStaticRuleSip("32.2.3." + i); StaticRuleObject staticRuleTest = new StaticRuleObject();
staticRuleTest.setStaticRuleDip(n + ".2.3.2"); staticRuleTest.setStaticRuleName("test_staticrule");
staticRuleService.newStaticRuleObject(staticRuleTest); staticRuleTest.setStaticRuleCreateUsername("NSADD管理员");
staticRuleIds.add(staticRuleTest.getStaticRuleId()); staticRuleTest.setStaticRuleCreateDepart("组织树");
staticRuleService.updateAuditStatus(staticRuleTest.getStaticRuleId(), 2, "xxx管理员", 1111111, "组织树"); staticRuleTest.setStaticRuleCreateUserId(22222222);
staticRuleTest.setAuditStatus(0);
} staticRuleTest.setStaticRuleSip("32.2.3.1");
staticRuleTest.setStaticRuleDip("2.2.3.2");
staticRuleService.newStaticRuleObject(staticRuleTest);
// staticRuleIds.add(staticRuleTest.getStaticRuleId());
staticRuleService.updateAuditStatus(staticRuleTest.getStaticRuleId(), 2, "xxx管理员", 1111111, "组织树");
Task task = new Task();
task.setTaskName("静态task测试"+n);
LocalDateTime taskStartTime = LocalDateTime.now().plusMinutes(1);
LocalDateTime taskEndTime = LocalDateTime.now().plusYears(5);
task.setTaskStartTime(taskStartTime);
task.setTaskEndTime(taskEndTime);
task.setTaskAct("23");
task.setTaskType(1);
task.setTaskRange("1007");
task.setTaskCreateUserId(111111);
task.setTaskCreateUsername("xxx管理员");
task.setTaskCreateDepart("xxx");
task.setStaticRuleIds(staticRuleIds);
List<ProtectObject> protectObject = protectObjectService.queryProtectObjects(
null, null, null, null,
null, null, null, null,
null, null, 1, 1);
Long taskId = taskService.newTask(task); Task task = new Task();
task.setTaskName("静态task测试s");
// LocalDateTime taskStartTime = LocalDateTime.now().plusMinutes(1);
taskService.changeTaskAuditStatus(taskId, 2); LocalDateTime taskEndTime = LocalDateTime.now().plusYears(1);
stateChangeService.changeState(2, taskId, false); task.setTaskStartTime(taskStartTime);
task.setTaskEndTime(taskEndTime);
task.setTaskAct("23");
task.setTaskType(1);
task.setTaskRange("1007");
task.setTaskCreateUserId(111111);
task.setTaskCreateUsername("xxx管理员");
task.setTaskCreateDepart("xxx");
task.setStaticRuleIds(List.of(staticRuleTest.getStaticRuleId()));
Long taskId = taskService.newTask(task);
taskService.changeTaskAuditStatus(taskId, 2);
stateChangeService.changeState(2, taskId, false);
// System.out.println(commandService.queryCommandInfos(taskId, null, null, null, null, 1, 5)); // System.out.println(commandService.queryCommandInfos(taskId, null, null, null, null, 1, 5));
}
} }
@Test @Test