Skip to content

Commit

Permalink
[Feature]Add third report of TenCloud (#1113)
Browse files Browse the repository at this point in the history
Signed-off-by: Logic <zqr10159@126.com>
Signed-off-by: tomsun28 <tomsun28@outlook.com>
Co-authored-by: tomsun28 <tomsun28@outlook.com>
  • Loading branch information
zqr10159 and tomsun28 authored Jul 20, 2023
1 parent a5d9698 commit 04eeb10
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.dromara.hertzbeat.alert.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.hertzbeat.alert.service.AlertService;
import org.dromara.hertzbeat.alert.service.impl.AlertConvertTenCloudServiceImpl;
import org.dromara.hertzbeat.common.entity.dto.AlertReport;
import org.dromara.hertzbeat.common.entity.dto.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
* @author zqr10159
* 第三方告警上报接口
*/
@Tag(name = "Extern Alarm Manage API | 第三方告警管理API")
@RestController
@RequestMapping(path = "/api/alerts/report", produces = {APPLICATION_JSON_VALUE})
public class AlertReportController {

@Autowired
AlertConvertTenCloudServiceImpl alertConvertTenCloudService;

@Autowired
private AlertService alertService;

@PostMapping("/tencloud")
@Operation(summary = "Interface for reporting external alarm information of tencloud | 对外上报告警信息 接口",
description = "对外 新增一个腾讯云告警")
public ResponseEntity<Message<Void>> addNewAlertReportTencent(@Valid @RequestBody String alertReport) {
AlertReport convert = alertConvertTenCloudService.convert(alertReport);
alertService.addNewAlertReport(convert);
return ResponseEntity.ok(new Message<>("Add report success"));
}

@PostMapping
@Operation(summary = "Interface for reporting external alarm information | 对外上报告警信息 接口",
description = "对外 新增一个告警")
public ResponseEntity<Message<Void>> addNewAlertReport(@Valid @RequestBody AlertReport alertReport) {
// 校验请求数据 TODO
alertService.addNewAlertReport(alertReport);
return ResponseEntity.ok(new Message<>("Add report success"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.dromara.hertzbeat.alert.dto.AlertSummary;
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.alert.service.AlertService;
import org.dromara.hertzbeat.common.entity.dto.AlertReport;
import org.dromara.hertzbeat.common.entity.dto.Message;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -35,7 +34,6 @@

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -144,13 +142,5 @@ public ResponseEntity<Message<AlertSummary>> getAlertsSummary() {
Message<AlertSummary> message = new Message<>(alertSummary);
return ResponseEntity.ok(message);
}

@PostMapping("/report")
@Operation(summary = "Interface for reporting external alarm information | 对外上报告警信息 接口",
description = "对外 新增一个告警")
public ResponseEntity<Message<Void>> addNewAlertReport(@Valid @RequestBody AlertReport alertReport) {
// 校验请求数据 TODO
alertService.addNewAlertReport(alertReport);
return ResponseEntity.ok(new Message<>("Add report success"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.dromara.hertzbeat.alert.dto;


import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* @author zqr10159
* 腾讯云告警实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TenCloudAlertReport implements Serializable {
@JsonProperty("sessionID")
private String sessionId;
private String alarmStatus;
private String alarmType;
private AlarmObjInfo alarmObjInfo;
private AlarmPolicyInfo alarmPolicyInfo;
private String firstOccurTime;
private int durationTime;
private String recoverTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class AlarmObjInfo {
private String region;
private String namespace;
@JsonProperty("appID")
private String appId;
private String uin;
private Dimensions dimensions;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Dimensions {
@JsonProperty("unInstanceID")
private String unInstanceId;
@JsonProperty("objID")
private String objId;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class AlarmPolicyInfo {
@JsonProperty("policyID")
private String policyId;
private String policyType;
private String policyName;
@JsonProperty("policyTypeCName")
private String policyTypeCname;
private Conditions conditions;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Conditions {
private String metricName;
private String metricShowName;
private String calcType;
private String calcValue;
private String calcUnit;
private String currentValue;
private String historyValue;
private String unit;
private String period;
private String periodNum;
private String alarmNotifyType;
private long alarmNotifyPeriod;
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.dromara.hertzbeat.alert.service;

/**
* Alert Convert Interface.
* @author zqr10159
* @param <T> the type of object to convert the JSON into
*/
public interface AlertConvertService<T> {
/**
* Alert Convert Method.
*
* @param json the JSON string to convert
* @return the converted object of type T
*/
T convert(String json);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.dromara.hertzbeat.alert.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.alert.dto.TenCloudAlertReport;
import org.dromara.hertzbeat.alert.service.AlertConvertService;
import org.dromara.hertzbeat.common.entity.dto.AlertReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;

/**
* @author zqr10159
* 腾讯云告警转化类
*/
@Component
@Slf4j
public class AlertConvertTenCloudServiceImpl implements AlertConvertService<AlertReport> {
@Autowired
private ObjectMapper objectMapper;
@Override
public AlertReport convert(String json) {
TenCloudAlertReport tenCloudAlertReport;
AlertReport alert = null;
try {
tenCloudAlertReport = objectMapper.readValue(json, TenCloudAlertReport.class);
StringBuilder contentBuilder = new StringBuilder();
String content = contentBuilder.append("[").append("告警对象:地区")
.append(tenCloudAlertReport.getAlarmObjInfo().getRegion()).append("|")
.append(tenCloudAlertReport.getAlarmObjInfo().getNamespace()).append("]")
.append("[").append("告警内容:")
.append(tenCloudAlertReport.getAlarmPolicyInfo().getPolicyTypeCname()).append("|")
.append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getMetricShowName()).append("|")
.append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getMetricName())
.append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCalcType())
.append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCalcValue())
.append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCalcUnit()).append("]")
.append("[").append("当前数据")
.append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCurrentValue())
.append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCalcUnit()).append("]").toString();

HashMap<String, String> tagMap = new HashMap<>(1);
tagMap.put("app", "TenCloud");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long occurTime = sdf.parse(tenCloudAlertReport.getFirstOccurTime()).getTime();
alert = AlertReport.builder().content(content)
.alertName("TenCloud|腾讯云")
.alertTime(occurTime)
.alertDuration(tenCloudAlertReport.getDurationTime())
.priority(1)
.reportType(1)
.labels(tagMap)
.annotations(tagMap).build();

} catch (JsonProcessingException|ParseException e) {
log.error("解析腾讯云告警内容失败!");
}
return alert;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.dromara.hertzbeat.alert.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.dromara.hertzbeat.alert.reduce.AlarmCommonReduce;
import org.dromara.hertzbeat.alert.dao.AlertDao;
import org.dromara.hertzbeat.alert.dto.AlertPriorityNum;
Expand Down Expand Up @@ -167,4 +168,6 @@ private Alert buildAlertData(AlertReport alertReport){
.build();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.dromara.hertzbeat.alert.controller;

import org.dromara.hertzbeat.alert.dto.TenCloudAlertReport;
import org.dromara.hertzbeat.alert.service.AlertService;
import org.dromara.hertzbeat.alert.service.impl.AlertConvertTenCloudServiceImpl;
import org.dromara.hertzbeat.common.constants.CommonConstants;
import org.dromara.hertzbeat.common.entity.dto.AlertReport;
import org.dromara.hertzbeat.common.util.JsonUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* unit test for {@link AlertReportController }
* @author tom
*/
@ExtendWith(MockitoExtension.class)
class AlertReportControllerTest {

private MockMvc mockMvc;

@InjectMocks
private AlertReportController alertReportController;

@Mock
private AlertService alertService;

@Mock
AlertConvertTenCloudServiceImpl alertConvertTenCloudService;

@BeforeEach
void setUp() {
this.mockMvc = MockMvcBuilders.standaloneSetup(alertReportController).build();
}

@Test
void addNewAlertReportTencent() throws Exception {
TenCloudAlertReport report = TenCloudAlertReport.builder()
.sessionId("xxxxxxxx")
.alarmStatus("1")
.alarmType("metric")
.build();
mockMvc.perform(
MockMvcRequestBuilders
.post("/api/alerts/report/tencloud")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(report))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(content().json("{\"data\":null,\"msg\":\"Add report success\",\"code\":0}"))
.andReturn();
}

@Test
void addNewAlertReport() throws Exception {
mockMvc.perform(
MockMvcRequestBuilders
.post("/api/alerts/report")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(AlertReport.builder().build()))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(content().json("{\"data\":null,\"msg\":\"Add report success\",\"code\":0}"))
.andReturn();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,4 @@ void getAlertsSummary() throws Exception {
.andExpect(content().json("{\"data\":{\"total\":0,\"dealNum\":0,\"rate\":0.0,\"priorityWarningNum\":0,\"priorityCriticalNum\":0,\"priorityEmergencyNum\":0},\"msg\":null,\"code\":0}"))
.andReturn();
}

@Test
void addNewAlertReport() throws Exception {

mockMvc.perform(
MockMvcRequestBuilders
.post("/api/alerts/report")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(AlertReport.builder().build()))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(content().json("{\"data\":null,\"msg\":\"Add report success\",\"code\":0}"))
.andReturn();
}
}
1 change: 1 addition & 0 deletions manager/src/main/resources/sureness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ resourceRole:
# 需要被过滤保护的资源,不认证鉴权直接访问
# /api/v1/source3===get 表示 /api/v1/source3===get 可以被任何人访问 无需登录认证鉴权
excludedResource:
- /api/alerts/**===*
- /api/account/auth/**===*
- /api/i18n/**===get
- /api/metrics===get
Expand Down

0 comments on commit 04eeb10

Please sign in to comment.