Skip to content

Commit

Permalink
Merge pull request #345 from mmagi/feat_bot
Browse files Browse the repository at this point in the history
feat: configurable alert robot with SpEL template
  • Loading branch information
ZhouYixun authored Jun 5, 2023
2 parents 29b79e1 + e99e4de commit 203292a
Show file tree
Hide file tree
Showing 39 changed files with 1,510 additions and 1,389 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@

import com.gitee.sunchenbin.mybatis.actable.manager.handler.StartUpHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

@Configuration
@Order(1)
public class ActableConfig {

@Autowired
private StartUpHandler startUpHandler;

@Bean
public void generate() {
public ActableConfig(@Autowired StartUpHandler startUpHandler) {
startUpHandler.startHandler();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* sonic-server Sonic Cloud Real Machine Platform.
* Copyright (C) 2022 SonicCloudOrg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.cloud.sonic.controller.config.mybatis;

import lombok.extern.slf4j.Slf4j;
import org.cloud.sonic.controller.mapper.AlertRobotsMigrateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
* @deprecated 保留此类一段时间,在ActableConfig完成之后执行,
* 如果发现旧的robot_token配置数据,则自动迁移到新的表结构中,并清空robot_token,
* 表结构保留原字段需要暂时保留以免ActableConfig后数据丢失,
* 后续完成过渡后可直接删除此类。
* 此过程需在ActableConfig完成后才可进行,
* Order等注解某些打包方式可能出现不生效问题,通过直接依赖config确保执行顺序
*/
@Configuration
@Slf4j
@Deprecated
public class RobotConfigMigrate {
public RobotConfigMigrate(
@Autowired AlertRobotsMigrateMapper robotsMapper,
@Autowired ActableConfig config
) {
if (null == config) return;
int n;
if ((n = robotsMapper.migrateProjectRobot()) > 0) {
log.warn("legacy project robot found! migrated to {} alert robots", n);
robotsMapper.clearProjectRobot();
}
if ((n = robotsMapper.migrateAgentRobot()) > 0) {
log.warn("legacy project robot found! migrated to {} alert robots", n);
robotsMapper.clearAgentRobot();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.cloud.sonic.common.config.WebAspect;
import org.cloud.sonic.common.http.RespEnum;
Expand Down Expand Up @@ -75,11 +72,12 @@ public RespModel<List<AgentsDTO>> findAgents() {
@WebAspect
@Operation(summary = "修改agent信息", description = "修改agent信息")
@PutMapping("/update")
public RespModel<String> update(@RequestBody JSONObject jsonObject) {
agentsService.update(jsonObject.getInteger("id"),
jsonObject.getString("name"), jsonObject.getInteger("highTemp"),
jsonObject.getInteger("highTempTime"), jsonObject.getInteger("robotType"),
jsonObject.getString("robotToken"), jsonObject.getString("robotSecret"));
public RespModel<String> update(@RequestBody AgentsDTO jsonObject) {
agentsService.update(jsonObject.getId(),
jsonObject.getName(), jsonObject.getHighTemp(),
jsonObject.getHighTempTime(), jsonObject.getRobotType(),
jsonObject.getRobotToken(), jsonObject.getRobotToken(),
jsonObject.getAlertRobotIds());
return new RespModel<>(RespEnum.HANDLE_OK);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* sonic-server Sonic Cloud Real Machine Platform.
* Copyright (C) 2022 SonicCloudOrg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.cloud.sonic.controller.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.cloud.sonic.common.config.WebAspect;
import org.cloud.sonic.common.config.WhiteUrl;
import org.cloud.sonic.common.http.RespEnum;
import org.cloud.sonic.common.http.RespModel;
import org.cloud.sonic.controller.models.base.CommentPage;
import org.cloud.sonic.controller.models.domain.AlertRobots;
import org.cloud.sonic.controller.models.dto.AlertRobotsDTO;
import org.cloud.sonic.controller.services.AlertRobotsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "告警通知机器人相关")
@RestController
@RequestMapping("/alertRobotsAdmin")
public class AlertRobotsAdminController {

@Autowired
private AlertRobotsService alertRobotsService;

@WebAspect
@Operation(summary = "更新机器人参数", description = "新增或更新对应的机器人")
@PutMapping
public RespModel<String> save(@Validated @RequestBody AlertRobotsDTO alertRobotsDTO) {
alertRobotsService.saveOrUpdate(alertRobotsDTO.convertTo());
return new RespModel<>(RespEnum.UPDATE_OK);
}

@WebAspect
@Operation(summary = "查找机器人参数", description = "查找所有机器人参数列表")
@GetMapping("/list")
@Parameters(value = {
@Parameter(name = "scene", allowEmptyValue = true, description = "使用场景"),
@Parameter(name = "page", description = "页码"),
@Parameter(name = "pageSize", description = "页尺寸")
})
public RespModel<CommentPage<AlertRobots>> listAll(
@RequestParam(name = "scene", required = false) String scene,
@RequestParam(name = "page") int page,
@RequestParam(name = "pageSize", defaultValue = "20") int pageSize
) {
return new RespModel<>(RespEnum.SEARCH_OK, alertRobotsService.findRobots(new Page<>(page, pageSize), null, scene));
}

@WebAspect
@Operation(summary = "查找机器人参数", description = "查找所有机器人参数列表")
@GetMapping("/listAll")
@Parameters(value = {
@Parameter(name = "scene", allowEmptyValue = true, description = "使用场景")
})
public RespModel<List<AlertRobots>> listAll(
@RequestParam(name = "scene", required = false) String scene
) {
return new RespModel<>(RespEnum.SEARCH_OK, alertRobotsService.findAllRobots(null, scene));
}

@WebAspect
@Operation(summary = "删除机器人参数", description = "删除对应id的机器人参数")
@Parameter(name = "id", description = "id")
@DeleteMapping
public RespModel<String> delete(@RequestParam(name = "id") int id) {
if (alertRobotsService.removeById(id)) {
return new RespModel<>(RespEnum.DELETE_OK);
} else {
return new RespModel<>(RespEnum.DELETE_FAIL);
}
}

@WebAspect
@Operation(summary = "获取机器人对类型机器人在相应使用场景下的默认模板", description = "获取机器人对类型机器人在相应使用场景下的默认模板")
@Parameter(name = "type", description = "type")
@Parameter(name = "scene", description = "scene")
@GetMapping("/findDefaultTemplate")
@WhiteUrl
public RespModel<String> getDefaultNoticeTemplate(@RequestParam(name = "type") int type, @RequestParam(name = "scene") String scene) {
return new RespModel<>(RespEnum.SEARCH_OK, alertRobotsService.getDefaultNoticeTemplate(type, scene));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* sonic-server Sonic Cloud Real Machine Platform.
* Copyright (C) 2022 SonicCloudOrg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.cloud.sonic.controller.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.cloud.sonic.common.config.WebAspect;
import org.cloud.sonic.common.config.WhiteUrl;
import org.cloud.sonic.common.http.RespEnum;
import org.cloud.sonic.common.http.RespModel;
import org.cloud.sonic.controller.models.base.CommentPage;
import org.cloud.sonic.controller.models.domain.AlertRobots;
import org.cloud.sonic.controller.models.dto.AlertRobotsDTO;
import org.cloud.sonic.controller.services.AlertRobotsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "告警通知机器人相关")
@RestController
@RequestMapping("/alertRobots")
public class AlertRobotsController {

@Autowired
private AlertRobotsService alertRobotsService;

@WebAspect
@Operation(summary = "更新机器人参数", description = "新增或更新对应的机器人")
@PutMapping
public RespModel<String> save(@Validated @RequestBody AlertRobotsDTO alertRobotsDTO) {
alertRobotsService.saveOrUpdate(alertRobotsDTO.convertTo());
return new RespModel<>(RespEnum.UPDATE_OK);
}

@WebAspect
@Operation(summary = "查找项目机器人参数", description = "查找对应项目id的机器人参数列表")
@GetMapping("/list")
@Parameters(value = {
@Parameter(name = "projectId", description = "项目id"),
@Parameter(name = "scene", allowEmptyValue = true, description = "使用场景"),
@Parameter(name = "page", description = "页码"),
@Parameter(name = "pageSize", description = "页尺寸")
})
public RespModel<CommentPage<AlertRobots>> list(
@RequestParam(name = "projectId") int projectId,
@RequestParam(name = "scene", required = false) String scene,
@RequestParam(name = "page") int page,
@RequestParam(name = "pageSize", defaultValue = "20") int pageSize
) {
return new RespModel<>(RespEnum.SEARCH_OK, alertRobotsService.findRobots(new Page<>(page, pageSize), projectId, scene));
}

@WebAspect
@Operation(summary = "查找项目机器人参数", description = "查找对应项目id的机器人参数列表")
@GetMapping("/listAll")
@Parameters(value = {
@Parameter(name = "projectId", allowEmptyValue = true, description = "项目id"),
@Parameter(name = "scene", allowEmptyValue = true, description = "使用场景")
})
public RespModel<List<AlertRobots>> listAll(
@RequestParam(name = "projectId", required = false, defaultValue = "-1") int projectId,
@RequestParam(name = "scene", required = false) String scene
) {
return new RespModel<>(RespEnum.SEARCH_OK, alertRobotsService.findAllRobots(projectId, scene));
}

@WebAspect
@Operation(summary = "删除机器人参数", description = "删除对应id的机器人参数")
@Parameter(name = "id", description = "id")
@DeleteMapping
public RespModel<String> delete(
@Parameter(name = "projectId", description = "项目id") int projectId,
@RequestParam(name = "id") int id
) {
if (alertRobotsService.removeByMap(Map.of("id", id, "projectId", projectId))) {
return new RespModel<>(RespEnum.DELETE_OK);
} else {
return new RespModel<>(RespEnum.DELETE_FAIL);
}
}

@WebAspect
@Operation(summary = "获取机器人对类型机器人在相应使用场景下的默认模板", description = "获取机器人对类型机器人在相应使用场景下的默认模板")
@Parameter(name = "type", description = "type")
@Parameter(name = "scene", description = "scene")
@GetMapping("/findDefaultTemplate")
@WhiteUrl
public RespModel<String> getDefaultNoticeTemplate(@RequestParam(name = "type") int type, @RequestParam(name = "scene") String scene) {
return new RespModel<>(RespEnum.SEARCH_OK, alertRobotsService.getDefaultNoticeTemplate(type, scene));
}
}
Loading

0 comments on commit 203292a

Please sign in to comment.