diff --git a/config/sonic-server-controller-dev.yml b/config/sonic-server-controller-dev.yml index 3ee5b156..ca201930 100644 --- a/config/sonic-server-controller-dev.yml +++ b/config/sonic-server-controller-dev.yml @@ -9,7 +9,7 @@ spring: robot: client: - host: "http://localhost:8080" + host: "http://localhost:3000" img: success: "" warning: "" diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/controller/AgentsController.java b/sonic-server-controller/src/main/java/com/sonic/controller/controller/AgentsController.java index 6e0e06b3..93e99566 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/controller/AgentsController.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/controller/AgentsController.java @@ -39,6 +39,13 @@ public RespModel save(@RequestBody JSONObject jsonObject) { return new RespModel(RespEnum.HANDLE_OK); } + @WebAspect + @PutMapping("/updateName") + public RespModel updateName(@RequestBody JSONObject jsonObject) { + agentsService.updateName(jsonObject.getInteger("id"), jsonObject.getString("name")); + return new RespModel(RespEnum.HANDLE_OK); + } + @WebAspect @GetMapping("/offLine") public RespModel offLine(@RequestParam(name = "id") int id) { diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/controller/ResultsController.java b/sonic-server-controller/src/main/java/com/sonic/controller/controller/ResultsController.java index b60a8c88..c9ce7f06 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/controller/ResultsController.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/controller/ResultsController.java @@ -100,4 +100,20 @@ public RespModel chart(@RequestParam(name = "projectId") int project @RequestParam(name = "endTime") String endTime) { return new RespModel(RespEnum.SEARCH_OK, resultsService.chart(startTime, endTime, projectId)); } + + @WebAspect + @ApiOperation(value = "发送日报", notes = "发送所有项目日报") + @GetMapping("/sendDayReport") + public RespModel sendDayReport() { + resultsService.sendDayReport(); + return new RespModel(RespEnum.HANDLE_OK); + } + + @WebAspect + @ApiOperation(value = "发送周报", notes = "发送所有项目周报") + @GetMapping("/sendWeekReport") + public RespModel sendWeekReport() { + resultsService.sendWeekReport(); + return new RespModel(RespEnum.HANDLE_OK); + } } diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/dao/AgentsRepository.java b/sonic-server-controller/src/main/java/com/sonic/controller/dao/AgentsRepository.java index d2e2044b..b63e085c 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/dao/AgentsRepository.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/dao/AgentsRepository.java @@ -12,12 +12,5 @@ * @date 2021/8/16 20:29 */ public interface AgentsRepository extends JpaRepository { - Agents findByHost(String host); - - @Query(value = "select count(*) from agents where status?=1", nativeQuery = true) - int findCountByStatus(int status); - - Agents findTopByName(String name); - Agents findBySecretKey(String key); } diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/dao/ResultsRepository.java b/sonic-server-controller/src/main/java/com/sonic/controller/dao/ResultsRepository.java index 1fed05fa..668e0ecb 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/dao/ResultsRepository.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/dao/ResultsRepository.java @@ -28,6 +28,9 @@ public interface ResultsRepository extends JpaRepository { @Query(value = "select status,count(*) as total from results where end_time>?1 and end_time<=?2 and project_id=?3 group by status", nativeQuery = true) List findDayStatus(String startTime, String endTime, int projectId); + @Query(value = "select count(*) as total from results where end_time>?1 and end_time<=?2 and project_id=?3", nativeQuery = true) + int findRunCount(String startTime, String endTime, int projectId); + @Transactional void deleteByProjectId(int projectId); diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/models/Agents.java b/sonic-server-controller/src/main/java/com/sonic/controller/models/Agents.java index 872c18d6..13ec9cce 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/models/Agents.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/models/Agents.java @@ -29,7 +29,7 @@ public class Agents { @NotNull @ApiModelProperty(value = "Agent端所在host", example = "127.0.0.1") String host; - @Positive + @NotNull @ApiModelProperty(value = "Agent端暴露web端口", example = "1234") int port; @ApiModelProperty(value = "Agent端状态", example = "1") diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/models/Users.java b/sonic-server-controller/src/main/java/com/sonic/controller/models/Users.java index 7812e59c..64416b9a 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/models/Users.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/models/Users.java @@ -23,9 +23,6 @@ public class Users { @NotNull @ApiModelProperty(value = "用户密码", required = true, example = "123456") String password; - @NotNull - @ApiModelProperty(value = "所属部门", required = true, example = "中后台-测试部") - String department; @Positive @ApiModelProperty(value = "角色", required = true, example = "1") int role; @@ -56,12 +53,12 @@ public void setPassword(String password) { this.password = password; } - public String getDepartment() { - return department; + public int getRole() { + return role; } - public void setDepartment(String department) { - this.department = department; + public void setRole(int role) { + this.role = role; } @Override @@ -70,7 +67,7 @@ public String toString() { "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + - ", department='" + department + '\'' + + ", role=" + role + '}'; } } \ No newline at end of file diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/AgentsService.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/AgentsService.java index c9ed963d..916eb967 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/services/AgentsService.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/AgentsService.java @@ -13,6 +13,8 @@ public interface AgentsService { List findAgents(); + void updateName(int id, String name); + void save(JSONObject agents); boolean offLine(int id); diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/ResultsService.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/ResultsService.java index f95000fc..cd7bb02e 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/services/ResultsService.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/ResultsService.java @@ -29,4 +29,8 @@ public interface ResultsService { void subResultCount(int id); JSONObject chart(String startTime, String endTime, int projectId); + + void sendDayReport(); + + void sendWeekReport(); } diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/AgentsServiceImpl.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/AgentsServiceImpl.java index 24478a93..40c7b6af 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/AgentsServiceImpl.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/AgentsServiceImpl.java @@ -8,11 +8,13 @@ import com.sonic.controller.models.interfaces.AgentStatus; import com.sonic.controller.models.interfaces.DeviceStatus; import com.sonic.controller.services.AgentsService; +import org.aspectj.weaver.loadtime.Agent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.List; +import java.util.UUID; @Service public class AgentsServiceImpl implements AgentsService { @@ -26,6 +28,27 @@ public List findAgents() { return agentsRepository.findAll(); } + @Override + public void updateName(int id, String name) { + if (id == 0) { + Agents agents = new Agents(); + agents.setName(name); + agents.setHost("未知"); + agents.setStatus(AgentStatus.OFFLINE); + agents.setVersion("未知"); + agents.setPort(0); + agents.setSystemType("未知"); + agents.setSecretKey(UUID.randomUUID().toString()); + agentsRepository.save(agents); + } else { + if (agentsRepository.existsById(id)) { + Agents a = agentsRepository.findById(id).get(); + a.setName(name); + agentsRepository.save(a); + } + } + } + public void resetDevice(int id) { List devicesList = devicesRepository.findByAgentId(id); for (Devices devices : devicesList) { diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/ResultsServiceImpl.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/ResultsServiceImpl.java index e023071a..8de11ed8 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/ResultsServiceImpl.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/ResultsServiceImpl.java @@ -195,6 +195,71 @@ public JSONObject chart(String startTime, String endTime, int projectId) { return result; } + @Override + public void sendDayReport() { + long timeMillis = Calendar.getInstance().getTimeInMillis(); + SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + List projectsList = projectsService.findAll(); + for (Projects projects : projectsList) { + Date yesterday = new Date(timeMillis - 86400000); + Date today = new Date(timeMillis); + List status = resultsRepository.findDayStatus(sf.format(yesterday), sf.format(today), projects.getId()); + int suc = 0; + int warn = 0; + int fail = 0; + for (JSONObject j : status) { + switch (j.getInteger("status")) { + case 1: + suc += j.getInteger("total"); + break; + case 2: + warn += j.getInteger("total"); + break; + case 3: + fail += j.getInteger("total"); + break; + } + } + if (projects.getRobotToken().length() > 0 && projects.getRobotSecret().length() > 0) { + robotMsgTool.sendDayReportMessage(projects.getRobotToken(), projects.getRobotSecret(), projects.getId() + , projects.getProjectName(), sf.format(yesterday), sf.format(today), suc, warn, fail); + } + } + } + + @Override + public void sendWeekReport() { + long timeMillis = Calendar.getInstance().getTimeInMillis(); + SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + List projectsList = projectsService.findAll(); + for (Projects projects : projectsList) { + Date lastWeek = new Date(timeMillis - 86400000 * 7L); + Date today = new Date(timeMillis); + List status = resultsRepository.findDayStatus(sf.format(lastWeek), sf.format(today), projects.getId()); + int count = resultsRepository.findRunCount(sf.format(lastWeek), sf.format(today), projects.getId()); + int suc = 0; + int warn = 0; + int fail = 0; + for (JSONObject j : status) { + switch (j.getInteger("status")) { + case 1: + suc += j.getInteger("total"); + break; + case 2: + warn += j.getInteger("total"); + break; + case 3: + fail += j.getInteger("total"); + break; + } + } + if (projects.getRobotToken().length() > 0 && projects.getRobotSecret().length() > 0) { + robotMsgTool.sendWeekReportMessage(projects.getRobotToken(), projects.getRobotSecret(), projects.getId() + , projects.getProjectName(), sf.format(lastWeek), sf.format(today), suc, warn, fail, count); + } + } + } + public static List getBetweenDate(String begin, String end) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); List betweenList = new ArrayList(); diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/tools/RobotMsgTool.java b/sonic-server-controller/src/main/java/com/sonic/controller/tools/RobotMsgTool.java index 89b9e080..101ff074 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/tools/RobotMsgTool.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/tools/RobotMsgTool.java @@ -180,10 +180,44 @@ public void sendDayReportMessage(String token, String secret, int projectId, Str "> ###### 失败数:" + failColorString + " \n" + "> ###### 测试通过率:" + (total > 0 ? new BigDecimal((float) passCount / total).setScale(2, RoundingMode.HALF_UP).doubleValue() : 0) + "% \n" + - "> ###### 详细统计:[点击查看](http://" + clientHost + "/Home/" + projectId); + "> ###### 详细统计:[点击查看](" + clientHost + "/Home/" + projectId + ")"); markdown.put("title", "Sonic云真机测试平台日报"); jsonObject.put("msgtype", "markdown"); jsonObject.put("markdown", markdown); signAndSend(token, secret, jsonObject); } + + public void sendWeekReportMessage(String token, String secret, int projectId, String projectName, + String yesterday, String today, int passCount, int warnCount, int failCount, int count) { + JSONObject jsonObject = new JSONObject(); + JSONObject markdown = new JSONObject(); + //根据三个数量来决定markdown的字体颜色 + String failColorString; + if (failCount == 0) { + failColorString = "" + failCount + ""; + } else { + failColorString = "" + failCount + ""; + } + String warnColorString; + if (warnCount == 0) { + warnColorString = "" + warnCount + ""; + } else { + warnColorString = "" + warnCount + ""; + } + int total = passCount + warnCount + failCount; + markdown.put("text", "### Sonic云真机测试平台周报 \n" + + "> ###### 项目:" + projectName + " \n" + + "> ###### 时间:" + yesterday + " ~ " + today + " \n" + + "> ###### 共测试:" + count + " 次\n" + + "> ###### 通过数:" + passCount + " \n" + + "> ###### 异常数:" + warnColorString + " \n" + + "> ###### 失败数:" + failColorString + " \n" + + "> ###### 测试通过率:" + (total > 0 ? + new BigDecimal((float) passCount / total).setScale(2, RoundingMode.HALF_UP).doubleValue() : 0) + "% \n" + + "> ###### 详细统计:[点击查看](" + clientHost + "/Home/" + projectId + ")"); + markdown.put("title", "Sonic云真机测试平台周报"); + jsonObject.put("msgtype", "markdown"); + jsonObject.put("markdown", markdown); + signAndSend(token, secret, jsonObject); + } } diff --git a/sonic-server-transport/src/main/java/com/sonic/transport/receiver/TestDataReceiver.java b/sonic-server-transport/src/main/java/com/sonic/transport/receiver/TestDataReceiver.java index a4da8738..d6720476 100644 --- a/sonic-server-transport/src/main/java/com/sonic/transport/receiver/TestDataReceiver.java +++ b/sonic-server-transport/src/main/java/com/sonic/transport/receiver/TestDataReceiver.java @@ -53,10 +53,10 @@ public void process(JSONObject jsonMsg, Channel channel, Message message) throws case "deviceDetail": controllerResp = controllerFeignClient.deviceStatus(jsonMsg); break; - case "elapsed": - jsonMsg.remove("msg"); - controllerResp = controllerFeignClient.saveElapsed(jsonMsg); - break; +// case "elapsed": +// jsonMsg.remove("msg"); +// controllerResp = controllerFeignClient.saveElapsed(jsonMsg); +// break; case "step": case "perform": case "record":