Skip to content

Commit

Permalink
Merge pull request #363 from mmagi/feat_bot
Browse files Browse the repository at this point in the history
fix: fix known bugs in feat_bot
ZhouYixun authored Jun 8, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents a3941c0 + a840cac commit 90d3862
Showing 6 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
*/
package org.cloud.sonic.controller.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
@@ -93,7 +94,7 @@ 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))) {
if (alertRobotsService.remove(Wrappers.lambdaQuery(AlertRobots.class).eq(AlertRobots::getId, id).eq(AlertRobots::getProjectId, projectId))) {
return new RespModel<>(RespEnum.DELETE_OK);
} else {
return new RespModel<>(RespEnum.DELETE_FAIL);
Original file line number Diff line number Diff line change
@@ -73,12 +73,4 @@ default List<AlertRobots> computeAgentRobots(int agentId) {
return listAgentRobotsFromIds(ids);
}

@Select("""
select * from alert_robots r where scene = 'summary'
and (
r.project_id = ${projectId} or
(r.project_id is null and 1 = (select global_robot from projects where id = ${projectId})
)
""")
List<AlertRobots> computeSummaryRobots(@Param("projectId") int projectId);
}
Original file line number Diff line number Diff line change
@@ -27,16 +27,19 @@
import org.cloud.sonic.controller.services.impl.base.SonicServiceImpl;
import org.cloud.sonic.controller.tools.robot.Message;
import org.cloud.sonic.controller.tools.robot.RobotFactory;
import org.cloud.sonic.controller.tools.robot.RobotMessenger;
import org.cloud.sonic.controller.tools.robot.message.DeviceMessage;
import org.cloud.sonic.controller.tools.robot.message.ProjectSummaryMessage;
import org.cloud.sonic.controller.tools.robot.message.TestSuiteMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
@@ -85,7 +88,7 @@ public void sendResultFinishReport(int suitId, String suiteName, int pass, int w

@Override
public void sendProjectReportMessage(int projectId, String projectName, Date startDate, Date endDate, boolean isWeekly, int pass, int warn, int fail) {
var robots = baseMapper.computeSummaryRobots(projectId);
var robots = findRobots(projectId, SCENE_SUMMARY).list();
if (robots.isEmpty()) return;
var total = pass + warn + fail;
var rate = total > 0 ? BigDecimal.valueOf(((float) pass / total) * 100).setScale(2, RoundingMode.HALF_UP).doubleValue() : 0;
@@ -98,7 +101,7 @@ public void sendProjectReportMessage(int projectId, String projectName, Date sta
public void sendErrorDevice(int agentId, int errorType, int tem, String udId) {
var robots = baseMapper.computeAgentRobots(agentId);
if (robots.isEmpty()) return;
var msg = new DeviceMessage(errorType, tem, udId);
var msg = new DeviceMessage(errorType, new BigDecimal(BigInteger.valueOf(tem), 1), udId);
send(robots, msg);
}

@@ -118,11 +121,18 @@ private void send(List<AlertRobots> robots, Message message) {
@Override
public String getDefaultNoticeTemplate(int type, String scene) {
var messenger = robotFactory.getRobotMessenger(type);
return switch (scene) {
case SCENE_AGENT -> messenger.getDefaultDeviceMessageTemplate().getExpressionString();
case SCENE_SUMMARY -> messenger.getDefaultProjectSummaryTemplate().getExpressionString();
case SCENE_TESTSUITE -> messenger.getDefaultTestSuiteTemplate().getExpressionString();
default -> "";
var template = switch (scene) {
case SCENE_AGENT -> messenger.getDefaultDeviceMessageTemplate();
case SCENE_SUMMARY -> messenger.getDefaultProjectSummaryTemplate();
case SCENE_TESTSUITE -> messenger.getDefaultTestSuiteTemplate();
default -> null;
};
if (null == template) {
return "";
} else if (template instanceof SpelExpression) {
return RobotMessenger.templateParserContext.getExpressionPrefix() + template.getExpressionString() + RobotMessenger.templateParserContext.getExpressionSuffix();
} else {
return template.getExpressionString();
}
}
}
Original file line number Diff line number Diff line change
@@ -20,9 +20,11 @@
import lombok.AllArgsConstructor;
import org.cloud.sonic.controller.tools.robot.Message;

import java.math.BigDecimal;

@AllArgsConstructor
public class DeviceMessage extends Message {
public int errorType;
public int tem;
public BigDecimal tem;
public String udId;
}
Original file line number Diff line number Diff line change
@@ -5,9 +5,11 @@
import org.cloud.sonic.controller.tools.robot.RobotMessenger;
import org.springframework.expression.Expression;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service("WebhookImpl")
public class WebhookImpl implements RobotMessenger {
@Override
public void sendMessage(RestTemplate restTemplate, String token, String secret, Expression messageTemplate, Message message) {
Original file line number Diff line number Diff line change
@@ -30,14 +30,15 @@
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Date;

@SpringBootTest(classes = ControllerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class RobotMsgToolTest {

private void testMessage(RobotMessenger bot, String token, String secret) {
for (var msg : new Message[]{new DeviceMessage(0, 0, "test"), new ProjectSummaryMessage(0, "test", new Date(System.currentTimeMillis() - 7 * 24 * 3600 * 1000), new Date(), 1, 3, 4, 1.4, 3, "https://sonic/?#=+", true), new ProjectSummaryMessage(0, "test", new Date(System.currentTimeMillis() - 24 * 3600 * 1000), new Date(), 1, 3, 4, 1.4, 3, "https://sonic/?#=+", false), new TestSuiteMessage("asf", 0, 1, 4, 5, 3, "https://sonic/?#=+"),}) {
for (var msg : new Message[]{new DeviceMessage(0, new BigDecimal(0), "test"), new ProjectSummaryMessage(0, "test", new Date(System.currentTimeMillis() - 7 * 24 * 3600 * 1000), new Date(), 1, 3, 4, 1.4, 3, "https://sonic/?#=+", true), new ProjectSummaryMessage(0, "test", new Date(System.currentTimeMillis() - 24 * 3600 * 1000), new Date(), 1, 3, 4, 1.4, 3, "https://sonic/?#=+", false), new TestSuiteMessage("asf", 0, 1, 4, 5, 3, "https://sonic/?#=+"),}) {
bot.sendMessage(new RestTemplate(), token, secret, "", msg);
}
}

0 comments on commit 90d3862

Please sign in to comment.