Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: alert rule template #812

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.holoinsight.server.home.biz.plugin.MarketplaceProductHandler;
import io.holoinsight.server.home.biz.plugin.MetricInfoCheckService;
import io.holoinsight.server.home.biz.plugin.MetricInfoCheckServiceImpl;
import io.holoinsight.server.home.biz.service.AlertRuleService;
import io.holoinsight.server.home.biz.service.EnvironmentService;
import io.holoinsight.server.home.biz.service.TenantInitService;
import io.holoinsight.server.home.biz.service.UserinfoVerificationService;
Expand Down Expand Up @@ -113,7 +114,7 @@ public ProductCtlService productCtlService() {
}

@Bean
public AlertRuleServiceImpl alertRuleService() {
public AlertRuleService alertRuleService() {
return new AlertRuleServiceImpl();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface RequestContextAdapter {
<T> void queryWrapperWorkspaceAdapt(QueryWrapper<T> queryWrapper, String workspace);

String getWorkspace(boolean cross);

String getLoginName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public String getWorkspace(boolean cross) {
MonitorScope ms = RequestContext.getContext().ms;
return ms.getWorkspace();
}

@Override
public String getLoginName() {
return StringUtils.EMPTY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package io.holoinsight.server.home.common.service.query;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -54,5 +55,49 @@ public void distinct() {
}
this.values = distinctValues;
}

public void sort(String order, String... priorities) {
if (CollectionUtils.isEmpty(headers) || CollectionUtils.isEmpty(values)) {
return;
}
int index = -1;
for (String priority : priorities) {
for (int i = 0; i < headers.size(); i++) {
if (StringUtils.equals(headers.get(i), priority) && validData(i)) {
index = i;
break;
}
}
}
if (index < 0) {
return;
}
int finalIndex = index;
values.sort((o1, o2) -> {
Double d1 = Double.parseDouble(String.valueOf(o1[finalIndex]));
Double d2 = Double.parseDouble(String.valueOf(o2[finalIndex]));
if (StringUtils.equalsIgnoreCase(order, "asc")) {
return d1.compareTo(d2);
} else {
return d2.compareTo(d1);
}
});
}

private boolean validData(int i) {
for (Object[] value : values) {
if (value.length <= i) {
return false;
}
if (!(value[i] instanceof Number)) {
return false;
}
Double d = ((Number) value[i]).doubleValue();
if (!d.equals(0d)) {
return true;
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@
import io.holoinsight.server.home.dal.model.AlarmRule;
import io.holoinsight.server.home.facade.AlarmRuleDTO;
import io.holoinsight.server.home.facade.AlertRuleExtra;
import io.holoinsight.server.home.facade.Rule;
import io.holoinsight.server.home.facade.page.MonitorPageRequest;
import io.holoinsight.server.home.facade.page.MonitorPageResult;
import io.holoinsight.server.home.facade.trigger.DataSource;
import io.holoinsight.server.home.facade.trigger.Filter;
import io.holoinsight.server.home.facade.trigger.Trigger;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand All @@ -35,6 +42,7 @@
* @author wangsiyuan
* @date 2022/4/1 10:44 上午
*/
@Slf4j
public class AlertRuleServiceImpl extends ServiceImpl<AlarmRuleMapper, AlarmRule>
implements AlertRuleService {

Expand All @@ -49,10 +57,7 @@ public class AlertRuleServiceImpl extends ServiceImpl<AlarmRuleMapper, AlarmRule

@Override
public Long save(AlarmRuleDTO alarmRuleDTO) {
String md5 = MD5Hash.getMD5(J.toJson(alarmRuleDTO.getRule()));
AlertRuleExtra extra =
null != alarmRuleDTO.getExtra() ? alarmRuleDTO.getExtra() : new AlertRuleExtra();
extra.md5 = md5;
genMd5(alarmRuleDTO);
AlarmRule alarmRule = alarmRuleConverter.dtoToDO(alarmRuleDTO);
this.save(alarmRule);
EventBusHolder.post(alarmRuleConverter.doToDTO(alarmRule));
Expand All @@ -61,24 +66,12 @@ public Long save(AlarmRuleDTO alarmRuleDTO) {

@Override
public Boolean updateById(AlarmRuleDTO alarmRuleDTO) {

AlertRuleExtra extra =
null != alarmRuleDTO.getExtra() ? alarmRuleDTO.getExtra() : new AlertRuleExtra();
extra.md5 = genMd5(alarmRuleDTO);
genMd5(alarmRuleDTO);
AlarmRule alarmRule = alarmRuleConverter.dtoToDO(alarmRuleDTO);
EventBusHolder.post(alarmRuleDTO);
return this.updateById(alarmRule);
}

public String genMd5(AlarmRuleDTO alarmRuleDTO) {

AlarmRuleDTO alarmRuleDTO1 = new AlarmRuleDTO();
BeanUtils.copyProperties(alarmRuleDTO, alarmRuleDTO1, "id", "gmtCreate", "gmtModified",
"creator", "modifier", "status", "blockId", "tenant", "workspace", "sourceType", "sourceId",
"extra", "envType");
return MD5Hash.getMD5(J.toJson(alarmRuleDTO1));
}

@Override
public Boolean deleteById(Long id) {
AlarmRule alarmRule = getById(id);
Expand Down Expand Up @@ -268,4 +261,59 @@ public List<AlarmRuleDTO> getListByKeyword(String keyword, String tenant, String
wrapper.last("LIMIT 10");
return alarmRuleConverter.dosToDTOs(baseMapper.selectList(wrapper));
}

public static void genMd5(AlarmRuleDTO alarmRuleDTO) {

AlarmRuleDTO md5Rule = new AlarmRuleDTO();
BeanUtils.copyProperties(alarmRuleDTO, md5Rule, "id", "gmtCreate", "gmtModified", "creator",
"modifier", "status", "isMerge", "mergeType", "noticeType", "alarmContent", "tenant",
"workspace", "sourceType", "sourceId", "envType", "alertNotificationTemplateId");
if (md5Rule.getExtra() != null) {
md5Rule.getExtra().md5 = StringUtils.EMPTY;
}
if (!CollectionUtils.isEmpty(md5Rule.getRule())) {
Rule rule = J.fromJson(J.toJson(md5Rule.getRule()), Rule.class);
if (!CollectionUtils.isEmpty(rule.getTriggers())) {
for (Trigger trigger : rule.getTriggers()) {
markMetric(trigger, alarmRuleDTO.getSourceType());
}
}
md5Rule.setRule(J.toMap(J.toJson(rule)));
}
String md5 = MD5Hash.getMD5(J.toJson(md5Rule));
log.info("generate md5 {} for rule {}", md5, J.toJson(md5Rule));
if (alarmRuleDTO.getExtra() == null) {
alarmRuleDTO.setExtra(new AlertRuleExtra());
}
alarmRuleDTO.getExtra().md5 = md5;
}

private static void markMetric(Trigger trigger, String sourceType) {
if (CollectionUtils.isEmpty(trigger.getDatasources())) {
return;
}
if (StringUtils.isEmpty(sourceType) || !sourceType.startsWith("template")) {
return;
}
for (DataSource dataSource : trigger.getDatasources()) {
if (StringUtils.isEmpty(dataSource.getMetric())) {
continue;
}
String[] metricArr = dataSource.getMetric().split("_");
String suffix = metricArr[metricArr.length - 1];
if (StringUtils.isNumeric(suffix) || "${appId}".equals(suffix)) {
String[] newMetricArr = new String[metricArr.length - 1];
System.arraycopy(metricArr, 0, newMetricArr, 0, metricArr.length - 1);
dataSource.setMetric(String.join("_", Arrays.asList(newMetricArr)));
}
List<Filter> filters = dataSource.getFilters();
if (!CollectionUtils.isEmpty(filters)) {
for (Filter filter : filters) {
if (StringUtils.equals(filter.getName(), "appId")) {
filter.setValue(StringUtils.EMPTY);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public MonitorPageResult<UserOpLog> getListByPage(

wrapper.orderByDesc("gmt_create");

wrapper.select(UserOpLog.class, info -> !info.getColumn().equals("creator"));

Page<UserOpLog> page =
new Page<>(userOpLogRequest.getPageNum(), userOpLogRequest.getPageSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.holoinsight.server.home.biz.service.TenantInitService;
import io.holoinsight.server.home.biz.service.UserFavoriteService;
import io.holoinsight.server.home.biz.service.UserOpLogService;
import io.holoinsight.server.home.common.service.RequestContextAdapter;
import io.holoinsight.server.home.common.service.SpringContext;
import io.holoinsight.server.home.common.util.MonitorException;
import io.holoinsight.server.home.common.util.ResultCodeEnum;
Expand Down Expand Up @@ -92,6 +93,9 @@ public class UserFavoriteFacadeImpl extends BaseFacade {
@Autowired
private ParameterSecurityService parameterSecurityService;

@Autowired
private RequestContextAdapter requestContextAdapter;

@PostMapping("/create")
@ResponseBody
@MonitorScopeAuth(targetType = AuthTargetType.TENANT, needPower = PowerConstants.EDIT)
Expand Down Expand Up @@ -323,6 +327,11 @@ public void doManage() {
if (null != ms && !StringUtil.isBlank(ms.workspace)) {
userFavoriteRequest.getTarget().setWorkspace(ms.workspace);
}
if (StringUtils.isEmpty(userFavoriteRequest.getTarget().getUserLoginName())) {
if (StringUtils.isNotEmpty(requestContextAdapter.getLoginName())) {
userFavoriteRequest.getTarget().setUserLoginName(requestContextAdapter.getLoginName());
}
}
JsonResult.createSuccessResult(result,
userFavoriteService.getListByPage(userFavoriteRequest));
}
Expand Down
Loading