Skip to content

Commit

Permalink
Merge pull request #45 from ZhouYixun/dev
Browse files Browse the repository at this point in the history
save_robot_agent_user
  • Loading branch information
ZhouYixun authored Oct 13, 2021
2 parents e91d498 + be382cb commit 7ac6181
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 22 deletions.
2 changes: 1 addition & 1 deletion config/sonic-server-controller-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spring:

robot:
client:
host: "http://localhost:8080"
host: "http://localhost:3000"
img:
success: ""
warning: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,20 @@ public RespModel<JSONObject> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,5 @@
* @date 2021/8/16 20:29
*/
public interface AgentsRepository extends JpaRepository<Agents, Integer> {
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public interface ResultsRepository extends JpaRepository<Results, Integer> {
@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<JSONObject> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -70,7 +67,7 @@ public String toString() {
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", department='" + department + '\'' +
", role=" + role +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
public interface AgentsService {
List<Agents> findAgents();

void updateName(int id, String name);

void save(JSONObject agents);

boolean offLine(int id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public interface ResultsService {
void subResultCount(int id);

JSONObject chart(String startTime, String endTime, int projectId);

void sendDayReport();

void sendWeekReport();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,6 +28,27 @@ public List<Agents> 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<Devices> devicesList = devicesRepository.findByAgentId(id);
for (Devices devices : devicesList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Projects> projectsList = projectsService.findAll();
for (Projects projects : projectsList) {
Date yesterday = new Date(timeMillis - 86400000);
Date today = new Date(timeMillis);
List<JSONObject> 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<Projects> projectsList = projectsService.findAll();
for (Projects projects : projectsList) {
Date lastWeek = new Date(timeMillis - 86400000 * 7L);
Date today = new Date(timeMillis);
List<JSONObject> 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<String> getBetweenDate(String begin, String end) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
List<String> betweenList = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<font color=#67C23A>" + failCount + "</font>";
} else {
failColorString = "<font color=#F56C6C>" + failCount + "</font>";
}
String warnColorString;
if (warnCount == 0) {
warnColorString = "<font color=#67C23A>" + warnCount + "</font>";
} else {
warnColorString = "<font color=#E6A23C>" + warnCount + "</font>";
}
int total = passCount + warnCount + failCount;
markdown.put("text", "### Sonic云真机测试平台周报 \n" +
"> ###### 项目:" + projectName + " \n" +
"> ###### 时间:" + yesterday + " ~ " + today + " \n" +
"> ###### 共测试:" + count + " 次\n" +
"> ###### 通过数:<font color=#67C23A>" + passCount + "</font> \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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 7ac6181

Please sign in to comment.