From 4f4b57d23a24207cbd796c13310e8e21f15a8f29 Mon Sep 17 00:00:00 2001 From: Black Pan Date: Tue, 10 Sep 2024 09:52:55 +0800 Subject: [PATCH] refactor: improve code layering of ozhera-monitor module (#433) Co-authored-by: wodiwudi <40229449+wodiwudi@users.noreply.github.com> --- .../monitor/service/AlarmStrategyService.java | 461 +----- .../monitor/service/AlertGroupService.java | 238 +--- .../monitor/service/AlertHistoryService.java | 78 +- .../mone/monitor/service/AppAlarmService.java | 1127 +-------------- .../service/AppGrafanaMappingService.java | 389 +---- .../service/AppMonitorConfigService.java | 75 +- .../monitor/service/AppMonitorService.java | 1174 +--------------- .../monitor/service/ComputeTimerService.java | 209 +-- .../service/GrafanaBlackListService.java | 79 +- .../mone/monitor/service/GrafanaService.java | 93 +- .../monitor/service/HeraBaseInfoService.java | 450 +----- .../monitor/service/HeraDashboardService.java | 413 +----- .../monitor/service/QualityMarketService.java | 110 +- .../monitor/service/ResourceUsageService.java | 52 +- .../service/RulePromQLTemplateService.java | 95 +- .../monitor/service/ServiceMarketService.java | 139 +- .../impl}/AlarmPresetMetricsServiceImpl.java | 2 +- .../impl}/AppAlarmServiceExtensionImpl.java | 2 +- ...AppGrafanaMappingServiceExtensionImpl.java | 2 +- .../impl/AppMonitorServiceExtensionImpl.java | 146 ++ .../ComputeTimerServiceExtensionImpl.java | 2 +- .../impl/GrafanaServiceExtensionImpl.java | 2 +- .../{ => api/impl}/IAMServiceImpl.java | 2 +- .../impl}/MetricsLabelKindServiceImpl.java | 2 +- .../{ => api}/impl/PrometheusServiceImpl.java | 2 +- .../impl}/ReqErrorMetricsServiceImpl.java | 2 +- .../impl}/ReqSlowMetricsServiceImpl.java | 2 +- .../{ => api}/impl/ServiceMarketImpl.java | 2 +- .../{ => api/impl}/TeslaServiceImpl.java | 2 +- .../impl/AlarmStrategyServiceImpl.java | 496 +++++++ .../service/impl/AlertGroupServiceImpl.java | 321 +++++ .../service/impl/AlertHistoryServiceImpl.java | 106 ++ .../service/impl/AppAlarmServiceImpl.java | 1108 +++++++++++++++ .../impl/AppGrafanaMappingServiceImpl.java | 427 ++++++ .../impl/AppMonitorConfigServiceImpl.java | 95 ++ .../service/impl/AppMonitorServiceImpl.java | 1245 +++++++++++++++-- .../service/impl/ComputeTimerServiceImpl.java | 246 ++++ .../service/impl/GrafanaApiServiceImpl.java | 53 + .../impl/GrafanaBlackListServiceImpl.java | 92 ++ .../service/impl/GrafanaServiceImpl.java | 104 +- .../service/impl/HeraBaseInfoServiceImpl.java | 464 ++++++ .../impl/HeraDashboardServiceImpl.java | 479 +++++++ .../impl/QualityMarketServiceImpl.java | 126 ++ .../impl/ResourceUsageServiceImpl.java | 106 ++ .../impl/RulePromQLTemplateServiceImpl.java | 130 ++ .../impl/ServiceMarketServiceImpl.java | 161 +++ 46 files changed, 6204 insertions(+), 4907 deletions(-) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api/impl}/AlarmPresetMetricsServiceImpl.java (98%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api/impl}/AppAlarmServiceExtensionImpl.java (96%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api/impl}/AppGrafanaMappingServiceExtensionImpl.java (96%) create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppMonitorServiceExtensionImpl.java rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api/impl}/ComputeTimerServiceExtensionImpl.java (99%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api}/impl/GrafanaServiceExtensionImpl.java (99%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api/impl}/IAMServiceImpl.java (96%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api/impl}/MetricsLabelKindServiceImpl.java (97%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api}/impl/PrometheusServiceImpl.java (99%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api/impl}/ReqErrorMetricsServiceImpl.java (97%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api/impl}/ReqSlowMetricsServiceImpl.java (97%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api}/impl/ServiceMarketImpl.java (97%) rename ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/{ => api/impl}/TeslaServiceImpl.java (96%) create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlarmStrategyServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlertGroupServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlertHistoryServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppAlarmServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppGrafanaMappingServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppMonitorConfigServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ComputeTimerServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaApiServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaBlackListServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/HeraBaseInfoServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/HeraDashboardServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/QualityMarketServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ResourceUsageServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/RulePromQLTemplateServiceImpl.java create mode 100644 ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ServiceMarketServiceImpl.java diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlarmStrategyService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlarmStrategyService.java index 169940577..10b00e349 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlarmStrategyService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlarmStrategyService.java @@ -13,458 +13,53 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.google.common.collect.Lists; -import com.google.gson.Gson; -import com.google.gson.JsonObject; import com.xiaomi.mone.monitor.bo.AlarmStrategyInfo; import com.xiaomi.mone.monitor.bo.AlarmStrategyParam; -import com.xiaomi.mone.monitor.bo.AlarmStrategyType; -import com.xiaomi.mone.monitor.bo.AppViewType; -import com.xiaomi.mone.monitor.dao.AppAlarmRuleDao; -import com.xiaomi.mone.monitor.dao.AppAlarmRuleTemplateDao; -import com.xiaomi.mone.monitor.dao.AppAlarmStrategyDao; -import com.xiaomi.mone.monitor.dao.AppMonitorDao; import com.xiaomi.mone.monitor.dao.model.AlarmStrategy; -import com.xiaomi.mone.monitor.dao.model.AppAlarmRule; import com.xiaomi.mone.monitor.dao.model.AppMonitor; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; import com.xiaomi.mone.monitor.service.model.PageData; import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleRequest; -import com.xiaomi.mone.monitor.service.prometheus.PrometheusService; -import com.xiaomi.mone.monitor.service.user.LocalUser; -import com.xiaomi.mone.monitor.service.user.MoneUserDetailService; -import com.xiaomi.mone.monitor.service.user.UseDetailInfo; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; /** - * * @author zhanggaofeng1 */ -@Slf4j -@Service -public class AlarmStrategyService { - - @Autowired - private AppAlarmStrategyDao appAlarmStrategyDao; - @Autowired - private AppAlarmRuleDao appAlarmRuleDao; - @Autowired - private AppAlarmService appAlarmService; - @Autowired - private AppMonitorDao appMonitorDao; - @Autowired - private PrometheusService prometheusService; - @Autowired - private AppAlarmRuleTemplateDao appAlarmRuleTemplateDao; - @Autowired - private MoneUserDetailService moneUserDetailService; - - private static final List orderSorts = Lists.newArrayList("asc","ASC","desc","DESC"); - private static final List orderColumn = Lists.newArrayList("update_time","strategy_name"); - - public AlarmStrategy getById(Integer id){ - return appAlarmStrategyDao.getById(id); - } - - public boolean updateById(AlarmStrategy strategy){ - return appAlarmStrategyDao.updateById(strategy); - } - +public interface AlarmStrategyService { + + + AlarmStrategy getById(Integer id); + + boolean updateById(AlarmStrategy strategy); + /** * 创建策略 + * * @param param * @param app * @return */ - public AlarmStrategy create(AlarmRuleRequest param, AppMonitor app) { - if(StringUtils.isBlank(param.getStrategyName()) || param.getStrategyType() == null) { - throw new IllegalArgumentException("strategy name or type is null"); - } - AlarmStrategy strategy = new AlarmStrategy(); - strategy.setCreater(param.getUser()); - strategy.setStrategyName(param.getStrategyName()); - strategy.setStrategyType(param.getStrategyType()); - strategy.setAppId(app.getProjectId()); - strategy.setIamId(app.getIamTreeId()); - strategy.setAppName(param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode()) ? param.getAppAlias() : app.getProjectName()); - strategy.setDesc(param.getStrategyDesc()); - strategy.setStatus(0); - if (StringUtils.isNotBlank(param.getAlertTeam())) { - strategy.setAlertTeam(param.getAlertTeam()); - } else if (!CollectionUtils.isEmpty(param.getAlarmRules())) { - strategy.setAlertTeam(param.getAlarmRules().get(0).getAlertTeam()); - } - Map deptMap = LocalUser.getDepts(); - if (deptMap.containsKey(3)) { - strategy.setGroup3(deptMap.get(3).getDeptName()); - } - if (deptMap.containsKey(4)) { - strategy.setGroup4(deptMap.get(4).getDeptName()); - } - if (deptMap.containsKey(5)) { - strategy.setGroup5(deptMap.get(5).getDeptName()); - } - - JsonObject envs = new JsonObject(); - if(!CollectionUtils.isEmpty(param.getIncludeEnvs())){ - envs.addProperty("includeEnvs",String.join(",", param.getIncludeEnvs())); - } - if(!CollectionUtils.isEmpty(param.getExceptEnvs())){ - envs.addProperty("exceptEnvs",String.join(",", param.getExceptEnvs())); - } - - if(!CollectionUtils.isEmpty(param.getIncludeZones())){ - envs.addProperty("includeZones",String.join(",", param.getIncludeZones())); - } - if(!CollectionUtils.isEmpty(param.getExceptZones())){ - envs.addProperty("exceptZones",String.join(",", param.getExceptZones())); - } - - if(!CollectionUtils.isEmpty(param.getIncludeContainerName())){ - envs.addProperty("includeContainerName",String.join(",", param.getIncludeContainerName())); - } - if(!CollectionUtils.isEmpty(param.getExceptContainerName())){ - envs.addProperty("exceptContainerName",String.join(",", param.getExceptContainerName())); - } - - if(!CollectionUtils.isEmpty(param.getIncludeModules())){ - envs.addProperty("includeModules",String.join(",", param.getIncludeModules())); - } - if(!CollectionUtils.isEmpty(param.getExceptModules())){ - envs.addProperty("exceptModules",String.join(",", param.getExceptModules())); - } - - if(!CollectionUtils.isEmpty(param.getIncludeFunctions())){ - envs.addProperty("includeFunctions",String.join(",", param.getIncludeFunctions())); - } - if(!CollectionUtils.isEmpty(param.getExceptFunctions())){ - envs.addProperty("exceptFunctions",String.join(",", param.getExceptFunctions())); - } - strategy.setEnvs(envs.toString()); - - if(!CollectionUtils.isEmpty(param.getAlertMembers())){ - strategy.setAlertMembers(String.join(",", param.getAlertMembers())); - } - - if(!CollectionUtils.isEmpty(param.getAtMembers())){ - strategy.setAtMembers(String.join(",", param.getAtMembers())); - } - - - if (!appAlarmStrategyDao.insert(strategy)) { - return null; - } - - log.info("插入规则策略成功:strategy={}",strategy); - - return strategy; - } - - public Result updateByParam(AlarmRuleRequest param) { - - AlarmStrategy strategy = new AlarmStrategy(); - - strategy.setId(param.getStrategyId()); - strategy.setStrategyName(param.getStrategyName()); - strategy.setDesc(param.getStrategyDesc()); - - if (StringUtils.isNotBlank(param.getAlertTeam())) { - strategy.setAlertTeam(param.getAlertTeam()); - } - - JsonObject envs = new JsonObject(); - if(!CollectionUtils.isEmpty(param.getIncludeEnvs())){ - envs.addProperty("includeEnvs",String.join(",", param.getIncludeEnvs())); - } - if(!CollectionUtils.isEmpty(param.getExceptEnvs())){ - envs.addProperty("exceptEnvs",String.join(",", param.getExceptEnvs())); - } - - if(!CollectionUtils.isEmpty(param.getIncludeZones())){ - envs.addProperty("includeZones",String.join(",", param.getIncludeZones())); - } - if(!CollectionUtils.isEmpty(param.getExceptZones())){ - envs.addProperty("exceptZones",String.join(",", param.getExceptZones())); - } - - if(!CollectionUtils.isEmpty(param.getIncludeContainerName())){ - envs.addProperty("includeContainerName",String.join(",", param.getIncludeContainerName())); - } - if(!CollectionUtils.isEmpty(param.getExceptContainerName())){ - envs.addProperty("exceptContainerName",String.join(",", param.getExceptContainerName())); - } - - if(!CollectionUtils.isEmpty(param.getIncludeModules())){ - envs.addProperty("includeModules",String.join(",", param.getIncludeModules())); - } - if(!CollectionUtils.isEmpty(param.getExceptModules())){ - envs.addProperty("exceptModules",String.join(",", param.getExceptModules())); - } - - if(!CollectionUtils.isEmpty(param.getIncludeFunctions())){ - envs.addProperty("includeFunctions",String.join(",", param.getIncludeFunctions())); - } - if(!CollectionUtils.isEmpty(param.getExceptFunctions())){ - envs.addProperty("exceptFunctions",String.join(",", param.getExceptFunctions())); - } - - strategy.setEnvs(envs.toString()); - - strategy.setAlertMembers(String.join(",", param.getAlertMembers() == null ? new ArrayList<>() : param.getAlertMembers())); - strategy.setAtMembers(String.join(",", param.getAtMembers() == null ? new ArrayList<>() : param.getAtMembers())); - - if (!appAlarmStrategyDao.updateById(strategy)) { - return Result.fail(ErrorCode.unknownError); - } - - log.info("更新规则策略成功:strategy={}",strategy); - return Result.success(strategy); - } - - public Result enabled(String user, AlarmStrategyParam param) { - AlarmStrategy strategy = appAlarmStrategyDao.getById(param.getId()); - if (strategy == null) { - return Result.fail(ErrorCode.nonExistentStrategy); - } - AppMonitor app = AlarmStrategyType.TESLA.getCode().equals(strategy.getStrategyType()) ? appMonitorDao.getByIamTreeId(strategy.getIamId()) : appMonitorDao.getMyApp(strategy.getAppId(),strategy.getIamId(), user, AppViewType.MyApp); - if (app == null) { - return Result.fail(ErrorCode.NoOperPermission); - } - Integer ruleStat = param.getStatus() == 0 ? 1 : 0; - Result result = appAlarmService.enabledRules(strategy.getIamId(), strategy.getId(), ruleStat, user); - if (result.getCode() != ErrorCode.success.getCode()) { - return result; - } - strategy = new AlarmStrategy(); - strategy.setId(param.getId()); - strategy.setStatus(param.getStatus()); - if (!appAlarmStrategyDao.updateById(strategy)) { - return Result.fail(ErrorCode.unknownError); - } - log.info("enabled规则策略成功:strategy={}",strategy); - return Result.success(null); - } - - public Result batchDeleteStrategy(String user, List strategyIds){ - - if(CollectionUtils.isEmpty(strategyIds)){ - log.error("batchDeleteStrategy param error!user:{} strategyIds:{}",user,strategyIds); - return Result.success(); - } - - for(Integer id : strategyIds){ - try { - Result result = deleteById(user, id); - if(!result.isSuccess()){ - log.error("deleteById fail! user : {}, id : {} ,result : {}",user,id,new Gson().toJson(result)); - } - } catch (Exception e) { - log.error("deleteById error!user:" + user + ",strategyId:" + id + ",exception:" + e.getMessage(),e); - } - } - - return Result.success(); - - } - - public Result deleteById(String user, Integer strategyId) { - if (strategyId == null) { - return Result.fail(ErrorCode.invalidParamError); - } - AlarmStrategy strategy = appAlarmStrategyDao.getById(strategyId); - if (strategy == null) { - return Result.fail(ErrorCode.nonExistentStrategy); - } - log.info("AlarmStrategyService.deleteById strategy : {}",new Gson().toJson(strategy)); - AppMonitor app = AlarmStrategyType.TESLA.getCode().equals(strategy.getStrategyType()) ? appMonitorDao.getByIamTreeId(strategy.getIamId()) : appMonitorDao.getMyApp(strategy.getAppId(),strategy.getIamId(), user, AppViewType.MyApp); - if (app == null) { - return Result.fail(ErrorCode.NoOperPermission); - } - Result result = appAlarmService.deleteRulesByIamId(strategy.getIamId() ,strategyId, user); - if (result.getCode() != ErrorCode.success.getCode()) { - return result; - } - if (!appAlarmStrategyDao.deleteById(strategyId)) { - return Result.fail(ErrorCode.unknownError); - } - log.info("删除规则策略成功:strategy={}",strategy); - return Result.success(null); - } - - public Result deleteByStrategyId(String user, Integer strategyId) { - if (strategyId == null) { - return Result.fail(ErrorCode.invalidParamError); - } - AlarmStrategy strategy = appAlarmStrategyDao.getById(strategyId); - if (strategy == null) { - return Result.fail(ErrorCode.nonExistentStrategy); - } - log.info("AlarmStrategyService.deleteById strategy : {}",new Gson().toJson(strategy)); - - Result result = appAlarmService.deleteRulesByIamId(strategy.getIamId() ,strategyId, user); - if (result.getCode() != ErrorCode.success.getCode()) { - return result; - } - if (!appAlarmStrategyDao.deleteById(strategyId)) { - return Result.fail(ErrorCode.unknownError); - } - log.info("删除规则策略成功:strategy={}",strategy); - return Result.success(null); - } - - public Result detailed(String user, AlarmStrategyParam param) { - AlarmStrategyInfo info = appAlarmStrategyDao.getInfoById(param.getId()); - if (info != null) { - setRuleListData(info); - } - return Result.success(info); - } - - private void setRuleListData(AlarmStrategyInfo info) { - if (info == null) { - return; - } - List ruleList = appAlarmRuleDao.selectByStrategyId(info.getId()); - info.setAlarmRules(ruleList); - if (StringUtils.isBlank(info.getAlertTeam()) && !CollectionUtils.isEmpty(ruleList)) { - info.setAlertTeam(ruleList.get(0).getAlertTeam()); - } - } - - public Result>> search(String user, AlarmStrategyParam param) { - - if(!paramCheck(param)){ - return Result.fail(ErrorCode.invalidParamError); - } - AlarmStrategy strategy = new AlarmStrategy(); - strategy.setStrategyName(param.getStrategyName()); - strategy.setStrategyType(param.getStrategyType()); - strategy.setAppId(param.getAppId()); - strategy.setAppName(param.getAppName()); - strategy.setStatus(param.getStatus()); - Map deptMap = LocalUser.getDepts(); - if (deptMap.containsKey(3)) { - strategy.setGroup3(deptMap.get(3).getDeptName()); - } - if (deptMap.containsKey(4)) { - strategy.setGroup4(deptMap.get(4).getDeptName()); - } - if (deptMap.containsKey(5)) { - strategy.setGroup5(deptMap.get(5).getDeptName()); - } - PageData> pageData = appAlarmStrategyDao.searchByCond(user, param.isOwner(),strategy, param.getPage(), param.getPageSize(),param.getSortBy(),param.getSortOrder()); - ruleDataHandler(pageData.getList()); - return Result.success(pageData); - } - - private boolean paramCheck(AlarmStrategyParam param){ - if(!orderColumn.contains(param.getSortBy())){ - log.info("param sortBy is error!"); - log.error("param sortBy is error!"); - return false; - } - if(!orderSorts.contains(param.getSortOrder())){ - log.info("param sortOrder is error!"); - log.error("param sortOrder is error!"); - return false; - } - return true; - } - - private List listStrategyByAppIdAndIamId(Integer appId,Integer iamId){ - - AlarmStrategy strategy = new AlarmStrategy(); - strategy.setStatus(0); - strategy.setAppId(appId); - strategy.setIamId(iamId); - PageData> listPageData = appAlarmStrategyDao.searchByCondNoUser(strategy, 1, 5000, null, null); - if(listPageData == null){ - return null; - } - return listPageData.getList(); - } - - public void deleteByAppIdAndIamId(Integer appId,Integer iamId){ - List alarmStrategyInfos = listStrategyByAppIdAndIamId(appId, iamId); - if(CollectionUtils.isEmpty(alarmStrategyInfos)){ - log.info("AlarmStrategyService#deleteByAppIdAndIamId no data found!appId:{},iamId:{}",appId,iamId); - return; - } - - for(AlarmStrategyInfo strategy : alarmStrategyInfos){ - - Result result = appAlarmService.deleteRulesByIamId(strategy.getIamId() ,strategy.getId(), null); - if (result.getCode() != ErrorCode.success.getCode()) { - log.error("deleteByAppIdAndIamId delete alarmRules fail! strategy : {}",strategy.toString()); - continue; - } - if (!appAlarmStrategyDao.deleteById(strategy.getId())) { - log.error("deleteByAppIdAndIamId delete strategy fail! strategy : {}",strategy.toString()); - continue; - } - } - } - - public Result dubboSearch(String user, AlarmStrategyParam param) { - StringBuilder metricStr = new StringBuilder(); - metricStr.append("sum(sum_over_time(staging_").append(param.getAppId()).append("_").append(param.getAppName()) - .append("_jaeger_dubboBisTotalCount_total{}[24h])) by (serviceName)"); - return prometheusService.queryByMetric(metricStr.toString()); - } - - private void ruleDataHandler(List infoList) { - if (infoList == null || infoList.isEmpty()) { - return; - } - Map infoMap = listToMap(infoList); - List ruleList = appAlarmRuleDao.selectByStrategyIdList(infoMap.keySet().stream().collect(Collectors.toList())); - if (ruleList == null) { - return; - } - AlarmStrategyInfo info = null; - for (AppAlarmRule rule : ruleList) { - if (rule.getStrategyId() == null) { - continue; - } - info = infoMap.get(rule.getStrategyId()); - if (info == null) { - continue; - } - if (info.getAlarmRules() == null) { - info.setAlarmRules(new ArrayList<>()); - } - info.getAlarmRules().add(rule); - if (StringUtils.isBlank(info.getAlertTeam())) { - info.setAlertTeam(rule.getAlertTeam()); - } - } - } - - private Map listToMap(List infoList) { - if (CollectionUtils.isEmpty(infoList)) { - return null; - } - Map infoMap = new HashMap<>(); - for (AlarmStrategyInfo info : infoList) { - if (infoMap.containsKey(info.getId()) && !info.isOwner()) { - continue; - } - infoMap.put(info.getId(), info); - } - return infoMap; - } - + AlarmStrategy create(AlarmRuleRequest param, AppMonitor app); + + Result updateByParam(AlarmRuleRequest param); + + Result enabled(String user, AlarmStrategyParam param); + + Result batchDeleteStrategy(String user, List strategyIds); + + Result deleteById(String user, Integer strategyId); + + Result deleteByStrategyId(String user, Integer strategyId); + + Result>> search(String user, AlarmStrategyParam param); + + void deleteByAppIdAndIamId(Integer appId, Integer iamId); + + Result dubboSearch(String user, AlarmStrategyParam param); + + Result detailed(String user, AlarmStrategyParam param); } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlertGroupService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlertGroupService.java index c292ecb64..cd39f8a9d 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlertGroupService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlertGroupService.java @@ -13,279 +13,113 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.alibaba.fastjson.JSONObject; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import com.xiaomi.mone.monitor.bo.AlertGroupInfo; import com.xiaomi.mone.monitor.bo.AlertGroupParam; import com.xiaomi.mone.monitor.bo.UserInfo; -import com.xiaomi.mone.monitor.dao.AlertGroupDao; -import com.xiaomi.mone.monitor.dao.model.AlertGroup; -import com.xiaomi.mone.monitor.dao.model.AlertGroupMember; -import com.xiaomi.mone.monitor.result.ErrorCode; -import com.xiaomi.mone.monitor.result.ExceptionCode; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.alertmanager.AlertServiceAdapt; -import com.xiaomi.mone.monitor.service.helper.AlertHelper; import com.xiaomi.mone.monitor.service.model.PageData; -import com.xiaomi.mone.monitor.service.user.UserConfigService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; /** * 报警组service */ -@Slf4j -@Service -public class AlertGroupService { - - @Autowired - private AlertServiceAdapt alertServiceAdapt; - @Autowired - private AlertHelper alertHelper; - @Autowired - private AlertGroupDao alertGroupDao; - @Autowired - UserConfigService userConfigService; +public interface AlertGroupService { + + /** * 数据清洗专用 + * * @return */ - public Result initAlertGroupData() { - Result pageDataResult = alertServiceAdapt.getAlertGroupPageData(null, null, 1, 1000); - if (pageDataResult == null || pageDataResult.getData() == null || pageDataResult.getData().getList() == null) { - return Result.success(null); - } - JsonElement ele = (JsonElement)pageDataResult.getData().getList(); - JsonArray arr = ele.getAsJsonArray(); - arr.forEach(subEle -> { - AlertGroup ag = alertHelper.buildAlertGroup(subEle.getAsJsonObject()); - if (alertGroupDao.getByRelId("alert", ag.getRelId()) != null) { - return; - } - alertGroupDao.insert(ag); - }); - return Result.success(null); - } - + Result initAlertGroupData(); + /** * 用户搜索 + * * @param user * @param param * @return */ - public Result>> userSearch(String user, AlertGroupParam param) { - return alertServiceAdapt.searchUser(user, param.getName(), param.getPage(), param.getPageSize()); - } - + Result>> userSearch(String user, AlertGroupParam param); + /** * 告警组搜索 + * * @param user * @param param * @return */ - public Result>> alertGroupSearch(String user, AlertGroupParam param) { - PageData> pageData = new PageData<>(); - pageData.setPage(param.getPage()); - pageData.setPageSize(param.getPageSize()); - pageData.setTotal(0L); - boolean admin = userConfigService.isAdmin(user); - PageData> pageAgList = alertGroupDao.searchByCond(admin,user, param.getName(), param.getType(), param.getPage(), param.getPageSize()); - if (pageAgList != null && pageAgList.getList() != null) { - pageData.setTotal(pageAgList.getTotal()); - pageData.setList(alertHelper.buildAlertGroupInfoList(admin,user, pageAgList.getList())); - } - return Result.success(pageData); - } - + Result>> alertGroupSearch(String user, AlertGroupParam param); + /** * 告警组查询通过id列表 + * * @param user * @param ids * @return */ - public Result> queryByIds(String user, List ids) { - List agList = alertGroupDao.getByIds(ids, true); - boolean admin = userConfigService.isAdmin(user); - return Result.success(alertHelper.buildAlertGroupInfoList(admin,user, agList)); - } - + Result> queryByIds(String user, List ids); + /** * 告警组同步 + * * @param user * @param type * @return */ - public Result sync(String user, String type) { - Result pageDataResult = alertServiceAdapt.getAlertGroupPageData(user, null, 1, 100); - if (pageDataResult == null || !pageDataResult.isSuccess()) { - Result.fail(ErrorCode.unknownError); - } - if (pageDataResult.getData() == null || pageDataResult.getData().getList() == null) { - return Result.success(null); - } - JsonArray list = (JsonArray)pageDataResult.getData().getList(); - if (list.isEmpty()) { - return Result.success(null); - } - Map relIdMap = new HashMap<>(); - for (int idx = 0; idx < list.size(); idx++) { - AlertGroup ag = alertHelper.buildAlertGroup((JsonObject)list.get(idx)); - ag.setType(type); - relIdMap.put(ag.getRelId(), ag); - } - List dbAgList = alertGroupDao.getByRelIds(type, new ArrayList<>(relIdMap.keySet())); - if (!CollectionUtils.isEmpty(dbAgList)) { - dbAgList.stream().filter(g -> g.getRelId() != null).forEach(g -> relIdMap.remove(g.getRelId())); - } - relIdMap.values().forEach(g -> alertGroupDao.insert(g)); - return Result.success(null); - } - + Result sync(String user, String type); + /** * 告警组搜索 + * * @param user * @param param * @return */ - public Result> alertGroupSearchByIds(String user, AlertGroupParam param) { - Map mapData = new HashMap<>(); - AlertGroupInfo groupInfo = null; - AlertGroup am = null; - boolean admin = userConfigService.isAdmin(user); - for (Integer relId : param.getRelIds()) { - am = alertGroupDao.getByRelId(param.getType(), relId); - groupInfo = alertHelper.buildAlertGroupInfo(admin,user, am); - if (groupInfo == null) { - continue; - } - mapData.put(relId.longValue(), groupInfo); - } - return Result.success(mapData); - } - + Result> alertGroupSearchByIds(String user, AlertGroupParam param); + /** * 告警组详情 + * * @param user * @param param * @return */ - public Result alertGroupDetailed(String user, AlertGroupParam param) { - AlertGroup ag = alertGroupDao.getById(param.getId()); - boolean isAdmin = userConfigService.isAdmin(user); - return Result.success(alertHelper.buildAlertGroupInfo(isAdmin,user, ag)); - } - + Result alertGroupDetailed(String user, AlertGroupParam param); + /** * 告警组创建 + * * @param user * @param param * @return */ - public Result alertGroupCreate(String user, AlertGroupParam param) { - Result result = alertServiceAdapt.createAlertGroup(user, param.getName(), param.getNote(), param.getChatId(), param.getMemberIds(),param.getDutyInfo()); - if (result == null) { - return Result.fail(ErrorCode.unknownError); - } - if (result.getCode() != ErrorCode.success.getCode()) { - return Result.fail(new ExceptionCode(result.getCode(),result.getMessage())); - } - param.setId(result.getData().get("id").getAsLong()); - Result resultData = alertServiceAdapt.getAlertGroup(user, param.getId()); - if (resultData == null || resultData.getData() == null) { - return Result.fail(ErrorCode.unknownError); - } - AlertGroup ag = alertHelper.buildAlertGroup(resultData.getData()); - ag.setType(param.getType()); - if (!alertGroupDao.insert(ag)) { - return Result.fail(ErrorCode.OperFailed); - } - boolean isAdmin = userConfigService.isAdmin(user); - return Result.success(alertHelper.buildAlertGroupInfo(isAdmin,user, ag)); - } - + Result alertGroupCreate(String user, AlertGroupParam param); + /** * 告警组编辑 + * * @param user * @param param * @return */ - public Result alertGroupEdit(String user, AlertGroupParam param) { - AlertGroup ag = alertGroupDao.getById(param.getId()); - if (ag == null) { - return Result.fail(ErrorCode.NoOperPermission); - } - Result result = alertServiceAdapt.editAlertGroup(user, ag.getRelId(), param.getName(), param.getNote(), param.getChatId(), param.getMemberIds(),param.getDutyInfo()); - if (result == null) { - return Result.fail(ErrorCode.unknownError); - } - if (result.getCode() != ErrorCode.success.getCode()) { - return result; - } - Result resultData = alertServiceAdapt.getAlertGroup(user, ag.getRelId()); - if (resultData == null || resultData.getData() == null) { - return Result.fail(ErrorCode.unknownError); - } - AlertGroup newAg = alertHelper.buildAlertGroup(resultData.getData()); - newAg.setId(ag.getId()); - List addMembers = alertHelper.getDiffAgMember(ag.getMembers(), newAg.getMembers()); - List delMembers = alertHelper.getDiffAgMember(newAg.getMembers(), ag.getMembers()); - if (!alertGroupDao.updateById(newAg, addMembers, delMembers)) { - return Result.fail(ErrorCode.OperFailed); - } - return Result.success(null); - } - + Result alertGroupEdit(String user, AlertGroupParam param); + /** * 告警组删除 + * * @param user * @param param * @return */ - public Result alertGroupDelete(String user, AlertGroupParam param) { - AlertGroup ag = alertGroupDao.getById(param.getId()); - if (ag == null) { - return Result.fail(ErrorCode.NoOperPermission); - } - Result resultData = alertServiceAdapt.getAlertGroup(user, ag.getRelId()); - if (resultData == null || resultData.getData() == null) { - return Result.fail(ErrorCode.unknownError); - } - if (resultData.getData().has("used") && resultData.getData().get("used").getAsInt() != 0) { - return Result.fail(ErrorCode.ALERT_GROUP_USED_FAIL); - } - Result result = alertServiceAdapt.deleteAlertGroup(user, ag.getRelId()); - if (result == null) { - return Result.fail(ErrorCode.unknownError); - } - if (result.getCode() != ErrorCode.success.getCode()) { - return result; - } - alertGroupDao.delete(ag); - return Result.success(null); - } - - public Result dutyInfoList(String user, AlertGroupParam param){ - Result result = alertServiceAdapt.dutyInfoList(user, param.getId(), param.getStart(), param.getEnd()); - if(result.isSuccess() && result.getData() != null){ - JsonArray asJsonArray = result.getData().getAsJsonArray(); - List list = JSONObject.parseArray(new Gson().toJson(asJsonArray),Map.class); - return Result.success(list); - } - log.info("dutyInfoList param:{}, result:{}",param.toString(),new Gson().toJson(result)); - return result; - } - + Result alertGroupDelete(String user, AlertGroupParam param); + + Result dutyInfoList(String user, AlertGroupParam param); + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlertHistoryService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlertHistoryService.java index 4d30b00cf..f2318479d 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlertHistoryService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlertHistoryService.java @@ -13,86 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import com.xiaomi.mone.monitor.bo.AlertHistory; import com.xiaomi.mone.monitor.bo.AlertHistoryDetailed; import com.xiaomi.mone.monitor.bo.AlertHistoryParam; -import com.xiaomi.mone.monitor.dao.AppMonitorDao; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.helper.AlertHelper; import com.xiaomi.mone.monitor.service.model.PageData; -import com.xiaomi.mone.monitor.service.prometheus.AlarmService; -import com.xiaomi.mone.monitor.utils.CommonUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; -import java.util.HashSet; import java.util.List; -import java.util.Set; /** - * * @author zhanggaofeng1 */ -@Slf4j -@Service -public class AlertHistoryService { - - @Autowired - private AlarmService alarmService; - @Autowired - private AlertHelper alertHelper; - @Autowired - private AppMonitorDao appMonitorDao; - - public Result>> metricList(String user, AlertHistoryParam param) { - PageData> pageData = new PageData(); - pageData.setPage(param.getPage()); - pageData.setPageSize(param.getPageSize()); - pageData.setTotal(0L); - Set treeIdSet = null; - if (param.getIamTreeId() != null) { - treeIdSet = new HashSet<>(); - treeIdSet.add(param.getIamTreeId()); - } else { - treeIdSet = appMonitorDao.selectTreeIdByOwnerOrCareUser(user); - } - if (CollectionUtils.isEmpty(treeIdSet)) { - return Result.success(pageData); - } - JsonObject labels = alertHelper.buildLabels(param); - Long startTime = CommonUtil.toSeconds(param.getStartTime()); - Long endTime = CommonUtil.toSeconds(param.getEndTime()); - Result pageResult = alarmService.queryLatestEvents(treeIdSet, param.getAlertStat(), param.getAlertLevel(), startTime, endTime, param.getPage(), param.getPageSize(), labels); - if (pageResult.getData() != null) { - pageData.setTotal(pageResult.getData().getTotal()); - pageData.setList(alertHelper.buildAlertHistoryList((JsonElement) pageResult.getData().getList())); - } - return Result.success(pageData); - } - - public Result metricDetailed(String user, AlertHistoryParam param) { - Result dataResult = alarmService.getEventById(user, param.getIamTreeId(), param.getId()); - if (dataResult == null || dataResult.getData() == null) { - return Result.fail(ErrorCode.ALERT_NOT_FOUND); - } - return Result.success(alertHelper.buildAlertHistoryDetailed(dataResult.getData())); - } - - public Result metricResolved(String user, AlertHistoryParam param) { - Long startTime = CommonUtil.toSeconds(param.getStartTime()); - Long endTime = CommonUtil.toSeconds(param.getEndTime()); - Result dataResult = alarmService.resolvedEvent(user, param.getIamTreeId(), param.getAlertName(), param.getComment(), startTime, endTime); - if (dataResult == null || dataResult.getCode() != ErrorCode.success.getCode()) { - return Result.fail(ErrorCode.unknownError); - } - return Result.success(null); - } +public interface AlertHistoryService { + + + Result>> metricList(String user, AlertHistoryParam param); + + Result metricDetailed(String user, AlertHistoryParam param); + + Result metricResolved(String user, AlertHistoryParam param); + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppAlarmService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppAlarmService.java index 13d2670f7..5cd100e01 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppAlarmService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppAlarmService.java @@ -13,1088 +13,69 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.alibaba.nacos.api.config.annotation.NacosValue; -import com.google.common.collect.Lists; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.xiaomi.mone.monitor.bo.AlarmCheckDataCount; -import com.xiaomi.mone.monitor.bo.AlarmRuleMetricType; -import com.xiaomi.mone.monitor.bo.AlarmRuleTemplateType; -import com.xiaomi.mone.monitor.bo.AlarmRuleType; -import com.xiaomi.mone.monitor.bo.AlarmSendInterval; -import com.xiaomi.mone.monitor.bo.AlarmStrategyType; -import com.xiaomi.mone.monitor.bo.AppViewType; -import com.xiaomi.mone.monitor.bo.RuleStatusType; -import com.xiaomi.mone.monitor.dao.AppAlarmRuleDao; -import com.xiaomi.mone.monitor.dao.AppAlarmRuleTemplateDao; -import com.xiaomi.mone.monitor.dao.AppMonitorDao; import com.xiaomi.mone.monitor.dao.model.AlarmStrategy; import com.xiaomi.mone.monitor.dao.model.AppAlarmRule; -import com.xiaomi.mone.monitor.dao.model.AppAlarmRuleTemplate; import com.xiaomi.mone.monitor.dao.model.AppMonitor; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.aop.context.HeraRequestMappingContext; -import com.xiaomi.mone.monitor.service.api.AlarmPresetMetricsService; -import com.xiaomi.mone.monitor.service.api.AppAlarmServiceExtension; -import com.xiaomi.mone.monitor.service.model.PageData; -import com.xiaomi.mone.monitor.service.model.prometheus.*; -import com.xiaomi.mone.monitor.service.prometheus.AlarmService; -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.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; +import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleData; +import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleRequest; +import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleTemplateRequest; +import com.xiaomi.mone.monitor.service.model.prometheus.AppAlarmRuleTemplateQuery; -import java.util.*; -import java.util.stream.Collectors; +import java.util.List; /** * @author gaoxihui */ -@Slf4j -@Service -public class AppAlarmService { - - @Autowired - AppAlarmRuleDao appAlarmRuleDao; - - @Autowired - AppAlarmRuleTemplateDao appAlarmRuleTemplateDao; - - @Autowired - AppMonitorService appMonitorService; - - @Autowired - AlarmService alarmService; - - @Autowired - private AppMonitorDao appMonitorDao; - - @Autowired - private AlarmStrategyService alarmStrategyService; - - @Autowired - private AlarmPresetMetricsService alarmPresetMetricsService; - - @Autowired - private AppAlarmServiceExtension appAlarmServiceExtension; - - - @NacosValue("${rule.evaluation.interval:20}") - private Integer evaluationInterval; - - @NacosValue("${rule.evaluation.unit:s}") - private String evaluationUnit; - - @Value("${prometheus.alarm.env:staging}") - private String prometheusAlarmEnv; - - @Value("${server.type}") - private String env; - - - @Value("${alert.manager.env:staging}") - private String alertManagerEnv; - - - @Deprecated - public void alarmRuleSwitchPlat(AppAlarmRule oldRule, Integer newProjectId, Integer newIamId, String oldProjectName, String newProjectName) { - - Result alarmRuleRemote = alarmService.getAlarmRuleRemote(oldRule.getAlarmId(), oldRule.getProjectId(), oldRule.getCreater()); - if (!alarmRuleRemote.isSuccess()) { - log.error("appPlatMove update get remote rule fail!oldRule:{},newProjectId:{},newIamId:{},newProjectName:{}", oldRule, newProjectId, newIamId, newProjectName); - return; - } - JsonElement remoteRule = alarmRuleRemote.getData(); - - if (remoteRule == null) { - log.error("appPlatMove update no remote rule found!oldRule:{},newProjectId:{},newIamId:{},newProjectName:{}", oldRule, newProjectId, newIamId, newProjectName); - return; - } - - - JsonObject asJsonObject = remoteRule.getAsJsonObject(); - - /** - * 表达式中的projectId、projectName替换 - */ - String expr = asJsonObject.get("expr").getAsString(); - - String oldApplication = oldRule.getProjectId() + "_" + oldProjectName.replaceAll("-", "_"); - String newApplication = newProjectId + "_" + newProjectName.replaceAll("-", "_"); - String newExpr = expr.replace(oldApplication, newApplication); - asJsonObject.remove("expr"); - asJsonObject.addProperty("expr", newExpr); - - - /** - * 替换iamId - */ - asJsonObject.remove("tree_id"); - asJsonObject.addProperty("tree_id", newIamId); - - - /** - * labels内容替换 - */ - - JsonObject labels = asJsonObject.getAsJsonObject("labels"); - labels.remove("project_id"); - labels.addProperty("project_id", newProjectId); - labels.remove("project_name"); - labels.addProperty("project_name", newProjectName); - labels.remove("app_iam_id"); - labels.addProperty("app_iam_id", newIamId); - - Result result = alarmService.updateAlarm(oldRule.getAlarmId(), oldRule.getIamId(), oldRule.getCreater(), asJsonObject.toString()); - - log.info("alarmRuleSwitchPlat updateAlarm request body:{},response{}", asJsonObject.toString(), new Gson().toJson(result)); - if (result.isSuccess()) { - - } - - } - - public Result queryFunctionList(Integer projectId) { - return appAlarmServiceExtension.queryFunctionList(projectId); - } - - - public Result queryRulesByAppName(String appName, String userName, Integer page, Integer pageSize) { - - if (page == null) { - page = 1; - } - if (pageSize == null) { - pageSize = 10; - } - PageData pd = new PageData(); - pd.setPage(page); - pd.setPageSize(pageSize); - - Long aLong = appAlarmRuleDao.countAlarmRuleByAppName(userName, appName); - pd.setTotal(aLong); - - List resultList = appAlarmRuleDao.queryRulesByAppName(userName, appName, page, pageSize); - if (!CollectionUtils.isEmpty(resultList)) { - for (AppWithAlarmRules appWithAlarmRule : resultList) { - List alarmRules = appWithAlarmRule.getAlarmRules(); - - appWithAlarmRule.setMetricMap(alarmPresetMetricsService.getEnumMap()); - appWithAlarmRule.setCheckDataMap(AlarmCheckDataCount.getEnumMap()); - appWithAlarmRule.setSendIntervalMap(AlarmSendInterval.getEnumMap()); - - - AppAlarmRule rule = new AppAlarmRule(); - rule.setIamId(appWithAlarmRule.getIamId()); - rule.setStatus(0); - List rules = appAlarmRuleDao.query(rule, 0, Integer.MAX_VALUE); - appWithAlarmRule.setAlarmRules(rules); - if (!CollectionUtils.isEmpty(rules)) { - AppAlarmRule rule1 = rules.get(0); - appWithAlarmRule.setCreater(rule1.getCreater()); - appWithAlarmRule.setLastUpdateTime(rule1.getUpdateTime()); - appWithAlarmRule.setRuleStatus(rule1.getRuleStatus()); - } - } - - } - -// Result>> appsResult = appMonitorService.listMyApp(appName, userName, page, pageSize); -// if(appsResult.getData() == null || CollectionUtils.isEmpty(appsResult.getData().getList())){ -// pd.setTotal(0l); -// return Result.success(pd); -// } -// -// pd.setTotal(appsResult.getData().getTotal()); -// -// List list = appsResult.getData().getList(); -// -// List resultList = new ArrayList<>(); -// -// for(AppMonitor app : list){ -// -// log.info("queryRulesByAppName : app id :{},appname:{},iamTreeId:{}",app.getId(),app.getProjectName(),app.getIamTreeId()); -// AppWithAlarmRules appAlarmRuleList = new AppWithAlarmRules(); -// appAlarmRuleList.setAppName(app.getProjectName()); -// appAlarmRuleList.setIamId(app.getIamTreeId()); -// appAlarmRuleList.setProjectId(app.getProjectId()); -// -// AppAlarmRule rule = new AppAlarmRule(); -// rule.setIamId(app.getIamTreeId()); -// rule.setStatus(0); -// List rules = appAlarmRuleDao.query(rule, 0, Integer.MAX_VALUE); -// -// appAlarmRuleList.setAlarmRules(rules); -// if(!CollectionUtils.isEmpty(rules)){ -// AppAlarmRule rule1 = rules.get(0); -// appAlarmRuleList.setCreater(rule1.getCreater()); -// appAlarmRuleList.setLastUpdateTime(rule1.getUpdateTime()); -// appAlarmRuleList.setRuleStatus(rule1.getRuleStatus()); -// appAlarmRuleList.setMetricMap(AlarmPresetMetrics.getEnumMap()); -// appAlarmRuleList.setCheckDataMap(AlarmCheckDataCount.getEnumMap()); -// appAlarmRuleList.setSendIntervalMap(AlarmSendInterval.getEnumMap()); -// -// } -// resultList.add(appAlarmRuleList); -// } - - pd.setList(resultList); - - return Result.success(pd); - - } - - public Result queryNoRulesConfig(String appName, String userName, Integer page, Integer pageSize) { - - if (page == null) { - page = 1; - } - if (pageSize == null) { - pageSize = 10; - } - PageData pd = new PageData(); - pd.setPage(page); - pd.setPageSize(pageSize); - - Long aLong = appAlarmRuleDao.countAppNoAlarmRulesConfig(userName, appName); - pd.setTotal(aLong); - - List resultList = appAlarmRuleDao.queryAppNoAlarmRulesConfig(userName, appName, page, pageSize); - - pd.setList(resultList); - - return Result.success(pd); - - } - - public Result queryRulesByIamId(Integer iamId, String userName){ - return appAlarmServiceExtension.queryRulesByIamId(iamId, userName); - } - - public Integer getAlarmConfigNumByTeslaGroup(String group){ - AppAlarmRule rule = new AppAlarmRule(); - rule.setLabels(group); - rule.setStatus(0); - Long aLong = appAlarmRuleDao.countByExample(rule); - return aLong == null ? 0 : aLong.intValue(); - } - - -// public Result queryRemoteRules(Integer iamId){ -// -// Map labels = new HashMap(); -// labels.put("app_iam_id",String.valueOf(iamId)); -// -// Result pageDataResult = alarmService.queryRuels(iamId, null, null, null, prometheusAlarmEnv, null, null, labels); -// JsonElement data = (JsonElement) pageDataResult.getData().getList(); -// if(data != null){ -// AlarmRuleDataRemote[] list = new Gson().fromJson(data, AlarmRuleDataRemote[].class); -// if(list.length == 0){ -// log.info("deleteRemoteRules no data found!iamId:{}",iamId); -// return; -// } -// } -// } - - public Result addRulesWithStrategy(AlarmRuleRequest param){ - - /** - * 校验当前操作人是否具有权限 - */ - AppMonitor app = null; - if(param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())){ - app = appMonitorDao.getByIamTreeId(param.getIamId()); - }else{ - app = appMonitorDao.getMyApp(param.getProjectId(), param.getIamId(), param.getUser(), AppViewType.MyApp); - } - - if (app == null) { - log.error("不存在projectId={}的项目", param.getProjectId()); - return Result.fail(ErrorCode.NoOperPermission); - } - - /** - * 创建策略 - */ - AlarmStrategy strategy = alarmStrategyService.create(param,app); - if (strategy == null) { - log.error("规则策略创建失败; strategyResult={}", strategy); - return Result.fail(ErrorCode.unknownError); - } - - Integer strategyId = strategy.getId(); - HeraRequestMappingContext.set("strategyId", strategyId); - - param.setStrategyId(strategyId); - - return addRules(param,app); - - } - - public Result batchAddRulesWithStrategy(AlarmRuleRequest param){ - - - List projectsAlarmInfo = param.getProjectsAlarmInfo(); - if(projectsAlarmInfo == null){ - projectsAlarmInfo = Lists.newArrayList(); - } - if(param.getIamId() != null && param.getProjectId() != null){ - ProjectAlarmInfo projectAlarmInfo = new ProjectAlarmInfo(); - projectAlarmInfo.setIamId(param.getIamId()); - projectAlarmInfo.setIamType(param.getIamType()); - projectAlarmInfo.setProjectId(param.getProjectId()); - projectsAlarmInfo.add(projectAlarmInfo); - } - - if(CollectionUtils.isEmpty(projectsAlarmInfo)){ - log.info("batchAddRulesWithStrategy no projectsAlarmInfo found!"); - return Result.fail(ErrorCode.invalidParamError); - } - - for(ProjectAlarmInfo t : projectsAlarmInfo){ - - param.setProjectId(t.getProjectId()); - param.setIamType(t.getIamType()); - param.setIamId(t.getIamId()); - - /** - * check permission for current user. - */ - AppMonitor app = null; - if(param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())){ - app = appMonitorDao.getByIamTreeId(param.getIamId()); - }else{ - app = appMonitorDao.getMyApp(param.getProjectId(), param.getIamId(), param.getUser(), AppViewType.MyApp); - } - - if (app == null) { - log.error("batchAddRulesWithStrategy# current user has not permission for projectId={} param:{}", param.getProjectId(),param.toString()); - continue; - } - - /** - * create strategy - */ - AlarmStrategy strategy = alarmStrategyService.create(param,app); - if (strategy == null) { - log.error("batchAddRulesWithStrategy remote fail; param:{},strategyResult={}", param.toString(),strategy.toString()); - continue; - } - - Integer strategyId = strategy.getId(); - HeraRequestMappingContext.set("strategyId", strategyId); - - param.setStrategyId(strategyId); - - Result result = addRules(param, app); - if(!result.isSuccess()){ - log.error("batchAddRulesWithStrategy#local create strategy fail! param:{},result:{}",param.toString(),new Gson().toJson(result)); - } - - } - - return Result.success(); - } - - - public Result addRules(AlarmRuleRequest param,AppMonitor app){ - - for(AlarmRuleData ruleData : param.getAlarmRules()){ - - ruleData.setIncludeEnvs(param.getIncludeEnvs()); - ruleData.setExceptEnvs(param.getExceptEnvs()); - ruleData.setIncludeZones(param.getIncludeZones()); - ruleData.setExceptZones(param.getExceptZones()); - ruleData.setIncludeContainerName(param.getIncludeContainerName()); - ruleData.setExceptContainerName(param.getExceptContainerName()); - ruleData.setAlertMembers(param.getAlertMembers()); - ruleData.setAtMembers(param.getAtMembers()); - - ruleData.setIncludeFunctions(param.getIncludeFunctions()); - ruleData.setExceptFunctions(param.getExceptFunctions()); - ruleData.setIncludeModules(param.getIncludeModules()); - ruleData.setExceptModules(param.getExceptModules()); - - ruleData.convertLabels(); - AppAlarmRule rule = new AppAlarmRule(); - BeanUtils.copyProperties(ruleData,rule); - StringBuilder cname = new StringBuilder(); - cname.append(param.getIamId() != null ? param.getIamId() : param.getProjectId()); - if (param.getStrategyType().intValue() == AlarmStrategyType.PAOMQL.getCode()) { - if(rule.getMetricType() == null){ - rule.setMetricType(AlarmRuleMetricType.customer_promql.getCode()); - } - - if(StringUtils.isBlank(ruleData.getAlert())){ - cname.append("-").append(AlarmRuleMetricType.customer_promql.getDesc()); - }else{ - cname.append("-").append(ruleData.getAlert()); - } - - cname.append("-").append(System.currentTimeMillis()); - rule.setCname(cname.toString()); - rule.setAlert(StringUtils.isBlank(rule.getAlert()) ? cname.toString() : rule.getAlert()); - } else { - if(rule.getMetricType() == null){ - rule.setMetricType(AlarmRuleMetricType.preset.getCode()); - } - cname.append("-").append(ruleData.getAlert()); - cname.append("-").append(System.currentTimeMillis()); - rule.setCname(cname.toString()); - } - - rule.setRuleType(AlarmRuleType.app_config.getCode()); - rule.setProjectId(param.getProjectId()); - - rule.setIamId(param.getIamId()); - int alarmForTime = evaluationInterval * ruleData.getDataCount(); - String alarmForTimeS = alarmForTime + evaluationUnit; - rule.setForTime(alarmForTimeS); - rule.setRuleGroup("group" + param.getIamId()); - rule.setEnv(prometheusAlarmEnv); - rule.setStatus(0); - rule.setCreater(param.getUser()); - rule.setRuleStatus(RuleStatusType.active.getCode()); - - String remark = null; - if(rule.getMetricType() == AlarmRuleMetricType.customer_promql.getCode()){ - remark = StringUtils.isBlank(rule.getRemark()) ? param.getStrategyDesc() : rule.getRemark(); - }else{ - remark = param.getStrategyDesc(); - } - rule.setRemark(remark); - - rule.setCreateTime(new Date()); - rule.setUpdateTime(new Date()); - rule.setStrategyId(param.getStrategyId()); - - Result result = alarmService.addRule(app, rule, param.getUser(),ruleData); - if(result.getCode() != 0){ - log.error("AppAlarmService.addRules error! remote add ruleData fail!ruleData:{}",rule.toString()); - return Result.fail(ErrorCode.unknownError); - } - - Integer alarmId = appAlarmServiceExtension.getAlarmIdByResult(result); - rule.setAlarmId(alarmId); - - - int i = appAlarmRuleDao.create(rule); - if(i < 0){ - log.error("AppAlarmService.addRules error! add ruleData data fail!ruleData:{}",rule.toString()); - return Result.fail(ErrorCode.unknownError); - } - - } - return Result.success(null); - - } - - private static String getLabelJsonStr(AlarmRuleData ruleData){ - - JsonObject labels = new JsonObject(); - labels.addProperty("includeMethods",ruleData.getIncludeMethods()); - labels.addProperty("exceptMethods",ruleData.getExceptMethods()); - labels.addProperty("includeErrorCodes",ruleData.getIncludeErrorCodes()); - labels.addProperty("exceptErrorCodes",ruleData.getExceptErrorCodes()); - labels.addProperty("includeDubboServices",ruleData.getIncludeDubboServices()); - labels.addProperty("exceptDubboServices",ruleData.getExceptDubboServices()); - labels.addProperty("teslaApiGroup",ruleData.getTeslaGroup()); - - log.debug("labels : {}" , labels.toString()); - - return labels.toString(); - - } - - - @Deprecated - public Result editRules(List rules, AlarmRuleRequest param,String user, String userName){ - - Result result = alarmStrategyService.deleteById(user, param.getStrategyId()); - if(result.getCode() != ErrorCode.success.getCode()){ - return result; - } - - /** - * 校验当前操作人是否具有权限 - */ - AppMonitor app = null; - if(param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())){ - app = appMonitorDao.getByIamTreeId(param.getIamId()); - }else{ - app = appMonitorDao.getMyApp(param.getProjectId(), param.getIamId(), user, AppViewType.MyApp); - } - - if (app == null) { - log.error("不存在projectId={}的项目", param.getProjectId()); - return Result.fail(ErrorCode.NoOperPermission); - } - - /** - * 创建策略 - */ - AlarmStrategy strategy = alarmStrategyService.create(param,app); - if (strategy == null) { - log.error("规则策略创建失败; strategyResult={}", strategy); - return Result.fail(ErrorCode.unknownError); - } - - Integer strategyId = strategy.getId(); - HeraRequestMappingContext.set("strategyId", strategyId); - - param.setStrategyId(strategyId); - - return addRules(param,app); - - } - - public Result delAlarmRules(List ids,String user){ - - if(CollectionUtils.isEmpty(ids)){ - return Result.fail(ErrorCode.invalidParamError); - } - - for(Integer id : ids){ - - AppAlarmRule rule = appAlarmRuleDao.getById(id); - if(rule == null){ - return Result.fail(ErrorCode.nonExistentAlarmRule); - } - - Result result = alarmService.deleteRule(rule.getAlarmId(), rule.getIamId(), user); - if(!result.isSuccess()){ - return Result.fail(ErrorCode.DeleteJobFail); - } - - int i = appAlarmRuleDao.delById(id); - if(i < 1){ - log.error("fail to delete rule in db,id:{}",id); - return Result.fail(ErrorCode.FAIL_TO_DELETE_RULE_IN_DB); - } - } - - return Result.success(); - - } - - public Result editAlarmRuleSingle(AlarmRuleData ruleData,String user){ - - if(ruleData == null){ - log.error("editAlarmRuleSingle invalid ruleData : {} ",ruleData); - return Result.fail(ErrorCode.invalidParamError); - } - - /** - * 检查策略是否存在 - */ - AlarmStrategy alarmStrategy = alarmStrategyService.getById(ruleData.getStrategyId()); - if(alarmStrategy == null){ - log.error("editAlarmRuleSingle strategy is not exist!ruleData:{}",ruleData); - return Result.fail(ErrorCode.nonExistentStrategy); - } - - - /** - * 校验当前操作人是否具有权限 - */ - AppMonitor app = null; - if(alarmStrategy.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())){ - app = appMonitorDao.getByIamTreeId(ruleData.getIamId()); - }else{ - app = appMonitorDao.getMyApp(ruleData.getProjectId(), ruleData.getIamId(), user, AppViewType.MyApp); - } - - if (app == null) { - log.error("不存在 owner 为 user : {}, projectId={}的项目", user,ruleData.getProjectId()); - return Result.fail(ErrorCode.NoOperPermission); - } - - ruleData.setAlertMembers(StringUtils.isBlank(alarmStrategy.getAlertMembers()) ? null : Arrays.asList(alarmStrategy.getAlertMembers().split(","))); - ruleData.setAtMembers(StringUtils.isBlank(alarmStrategy.getAtMembers()) ? null : Arrays.asList(alarmStrategy.getAtMembers().split(","))); - - return editAlarmRule(ruleData,alarmStrategy,app,user); - - } - - public Result editRulesByStrategy(AlarmRuleRequest param){ - - /** - * 参数校验 - */ - List alarmRuleDatas = param.getAlarmRules(); - if(CollectionUtils.isEmpty(alarmRuleDatas)){ - log.error("editRules no rule data found!param:{}",param); - return Result.fail(ErrorCode.invalidParamError); - } - - /** - * 校验当前操作人是否具有权限 - */ - AppMonitor app = null; - if(param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())){ - app = appMonitorDao.getByIamTreeId(param.getIamId()); - }else{ - app = appMonitorDao.getMyApp(param.getProjectId(), param.getIamId(), param.getUser(), AppViewType.MyApp); - } - - if (app == null) { - log.error("不存在projectId={}的项目", param.getProjectId()); - return Result.fail(ErrorCode.NoOperPermission); - } - - - /** - * 检查策略是否存在 - */ - AlarmStrategy alarmStrategy = alarmStrategyService.getById(param.getStrategyId()); - if(alarmStrategy == null){ - log.error("the strategy is not exist!param:{}",param.toString()); - return Result.fail(ErrorCode.nonExistentStrategy); - } - - - /** - * 过滤参数中新增的规则(rule的id为null),进行创建 - */ - List addRules = alarmRuleDatas.stream().filter(t -> t.getId() == null).collect(Collectors.toList()); - - if(!CollectionUtils.isEmpty(addRules)){ - - //指定要添加的报警规则列表 - param.setAlarmRules(addRules); - Result result1 = addRules(param, app); - if(result1.getCode() != 0){ - return result1; - } - } - - - /** - * 检查报警组、通知人列表、选择的环境是否有更新,有更新则级联更新历史报警规则 - */ - if(!nullToEmpty(alarmStrategy.getAlertTeam()).equals(nullToEmpty(param.getAlertTeam())) - ||!nullToEmpty(alarmStrategy.getAlertMembers()).equals(nullToEmpty(String.join(",", param.getAlertMembers()))) - ||!nullToEmpty(alarmStrategy.getAtMembers()).equals(nullToEmpty(String.join(",", param.getAtMembers() == null ? new ArrayList<>() : param.getAtMembers() ))) - ||!nullToEmpty(alarmStrategy.getEnvs()).equals(nullToEmpty(param.convertEnvs())) - ||!nullToEmpty(alarmStrategy.getDesc()).equals(nullToEmpty(param.getStrategyDesc()))){ - - alarmStrategy.setDesc(param.getStrategyDesc()); - - List updateRules = alarmRuleDatas.stream().filter(t -> t.getId() != null).collect(Collectors.toList()); - - if(!CollectionUtils.isEmpty(updateRules)){ - - AppMonitor appMonitor = app; - - Optional failResult = Optional.empty(); - - updateRules.forEach(ruleData -> { - - ruleData.setIncludeEnvs(param.getIncludeEnvs()); - ruleData.setExceptEnvs(param.getExceptEnvs()); - ruleData.setIncludeZones(param.getIncludeZones()); - ruleData.setExceptZones(param.getExceptZones()); - ruleData.setIncludeContainerName(param.getIncludeContainerName()); - ruleData.setExceptContainerName(param.getExceptContainerName()); - ruleData.setIncludeModules(param.getIncludeModules()); - ruleData.setExceptModules(param.getExceptModules()); - ruleData.setIncludeFunctions(param.getIncludeFunctions()); - ruleData.setExceptFunctions(param.getExceptFunctions()); - - ruleData.setAlertMembers(param.getAlertMembers()); - ruleData.setAtMembers(param.getAtMembers()); - - Result result = editAlarmRule(ruleData, alarmStrategy, appMonitor, param.getUser()); - if(!result.isSuccess()){ - failResult.orElse(result); - return; - } - - }); - - if(failResult.isPresent()){ - return failResult.get(); - } - - } - - } - - /** - * 更新策略信息 - */ - Result strategyUpdateResult = alarmStrategyService.updateByParam(param); - if(strategyUpdateResult.getCode() != ErrorCode.success.getCode()){ - log.error("update strategy in db fail!param:{}", param.toString()); - return Result.fail(ErrorCode.ALARM_STRATEGY_INFO_UPDATE_FAIL); - } - - return Result.success(null); - } - - public Result editAlarmRule(AlarmRuleData ruleData,AlarmStrategy alarmStrategy,AppMonitor app,String user){ - - AppAlarmRule rule = appAlarmRuleDao.getById(ruleData.getId()); - if (rule == null) { - log.info("edit alarm rule,no data found in db!ruleData:{}",ruleData); - return Result.fail(ErrorCode.nonExistentAlarmRule); - } - - ruleData.convertLabels(); - - - rule.setOp(ruleData.getOp()); - rule.setValue(ruleData.getValue()); - - int alarmForTime = evaluationInterval * ruleData.getDataCount(); - String alarmForTimeS = alarmForTime + evaluationUnit; - rule.setForTime(alarmForTimeS); - rule.setDataCount(ruleData.getDataCount()); - - rule.setPriority(ruleData.getPriority()); - rule.setSendInterval(ruleData.getSendInterval()); - - rule.setAlertTeam(ruleData.getAlertTeam()); - - rule.setLabels(ruleData.getLabels()); - - /** - * 兼容自定义报警单条填写描述信息 - */ - String remark = null; - if(rule.getMetricType() == AlarmRuleMetricType.customer_promql.getCode()){ - remark = StringUtils.isNotBlank(ruleData.getRemark()) ? ruleData.getRemark(): alarmStrategy.getDesc(); - }else{ - remark = alarmStrategy.getDesc(); - } - rule.setRemark(remark); - - rule.setUpdateTime(new Date()); - - Result result = alarmService.editRule(rule,ruleData,app,user); - - if(result.getCode() != 0){ - log.error("AppAlarmService.editRules error! remote add ruleData fail!ruleData:{}",rule.toString()); - return Result.fail(ErrorCode.UpdateJobFail); - } - - log.info("appAlarmRuleDao.updateByIdSelective rule:{}",new Gson().toJson(rule)); - int i = appAlarmRuleDao.updateByIdSelective(rule); - if(i < 1){ - log.error("AppAlarmService.editRules save db fail!ruleData:{}",rule.toString()); - return Result.fail(ErrorCode.ALARM_RULE_INFO_UPDATE_FAIL); - } - - return Result.success(""); - } - - private String nullToEmpty(String s){ - if(s == null){ - return ""; - } - return s; - } - - public Result deleteRulesByIamId(Integer iamId, Integer strategyId, String user){ - AppAlarmRule rulequery = new AppAlarmRule(); - rulequery.setStatus(0); - if(iamId != null){ - rulequery.setIamId(iamId); - } - rulequery.setStrategyId(strategyId); - List delRules = appAlarmRuleDao.query(rulequery, 0, Integer.MAX_VALUE); - if(CollectionUtils.isEmpty(delRules)){ - log.info("AppAlarmService.deleteRulesByIamId no data found! iamId : {},user:{}",iamId,user); - return Result.success(null); - } - - /** - * 同时删除远程接口的数据,和本地数据 - */ - for(AppAlarmRule rule : delRules){ - Result result = alarmService.deleteRule(rule.getAlarmId(), iamId, user); - if(result.getCode()==0){ - int delete = appAlarmRuleDao.delete(rule); - if(delete < 1){ - log.error("AppAlarmService.deleteRulesByIamId delete local database fail!iamId : {},user:{}",iamId,user); - return Result.fail(ErrorCode.unknownError); - } - }else{ - - if(result.getCode() == 404){ - log.error("AppAlarmService.deleteRulesByIamId delete remote data,no data found!iamId : {},user:{}",iamId,user); - - int delete = appAlarmRuleDao.delete(rule); - if(delete < 1){ - log.error("AppAlarmService.deleteRulesByIamId delete local database fail!iamId : {},user:{}",iamId,user); - return Result.fail(ErrorCode.unknownError); - } - Result.success(""); - } - log.error("AppAlarmService.deleteRulesByIamId delete remote data fail!iamId : {},user:{}",iamId,user); - Result.fail(ErrorCode.unknownError); - } - } - - return Result.success(""); - } - - - public Result enabledRules(Integer iamId, Integer strategyId, Integer pauseStatus,String user){ - - - AppAlarmRule rulequery = new AppAlarmRule(); - rulequery.setStatus(0); - if(iamId != null){ - rulequery.setIamId(iamId); - } - rulequery.setStrategyId(strategyId); - List delRules = appAlarmRuleDao.query(rulequery, 0, Integer.MAX_VALUE); - if(CollectionUtils.isEmpty(delRules)){ - log.info("AppAlarmService.enabledRules no data found! iamId : {},user:{}",iamId,user); - return Result.success(null); - } - - /** - * 同时暂停远程接口的数据,和本地数据 - */ - for(AppAlarmRule rule : delRules){ - Result result = alarmService.enabledRule(rule.getAlarmId(),pauseStatus,iamId, user); - if(result.getCode()==0){ - AppAlarmRule condition = new AppAlarmRule(); - condition.setIamId(rule.getIamId()); - condition.setStatus(0); - AppAlarmRule value = new AppAlarmRule(); - value.setRuleStatus(pauseStatus); - try { - int update = appAlarmRuleDao.update(condition, value); - if(update < 1){ - log.info("AppAlarmService.enabledRules update data failed!"); - return Result.fail(ErrorCode.unknownError); - } - } catch (Exception e) { - log.error("AppAlarmService.enabledRules error!{}",e.getMessage(),e); - return Result.fail(ErrorCode.unknownError); - } - }else{ - log.error("AppAlarmService.enabledRules update remote data fail!iamId : {},user:{}",iamId,user); - Result.fail(ErrorCode.unknownError); - } - } - - - return Result.success(""); - } - - public Result queryTemplate(AppAlarmRuleTemplateQuery query){ - if(StringUtils.isNotBlank(query.getCreater())){ - query.setCreater(query.getCreater()); - } - - Integer page = query.getPage(); - if(page == null || page.intValue() ==0){ - page = 1; - } - - Integer pageSize = query.getPageSize(); - if(pageSize == null){ - pageSize = 10; - } - - Integer start = (page-1) * pageSize; - Long dataTotal = appAlarmRuleTemplateDao.getDataTotal(query); - - PageData pd = new PageData(); - pd.setPage(page); - pd.setPageSize(pageSize); - pd.setTotal(dataTotal); - - List resultList = new ArrayList<>(); - - /** - * 模版查询 - */ - List list = appAlarmRuleTemplateDao.query(query, start, pageSize); - if(CollectionUtils.isEmpty(list)){ - pd.setList(resultList); - } - - /** - * 遍历附加规则 - */ - AppAlarmRule rule = new AppAlarmRule(); - rule.setRuleType(AlarmRuleType.template.getCode()); - rule.setStatus(0); - for(AppAlarmRuleTemplate template : list){ - rule.setTemplateId(template.getId()); - List rules = appAlarmRuleDao.query(rule, 0, Integer.MAX_VALUE); - - AlarmTemplateResponse response = new AlarmTemplateResponse(); - response.setTemplate(template); - response.setAlarmRules(rules); - resultList.add(response); - } - - pd.setList(resultList); - - return Result.success(pd); - } - - public Result getTemplateById(Integer id){ - - - /** - * 模版查询 - */ - AppAlarmRuleTemplate template = appAlarmRuleTemplateDao.getById(id); - if(template == null){ - log.info("getTemplateById no data found id : {}",id); - return Result.success(null); - } - - /** - * 遍历附加规则 - */ - AppAlarmRule rule = new AppAlarmRule(); - rule.setRuleType(AlarmRuleType.template.getCode()); - rule.setStatus(0); - rule.setTemplateId(template.getId()); - List rules = appAlarmRuleDao.query(rule, 0, Integer.MAX_VALUE); - - AlarmTemplateResponse response = new AlarmTemplateResponse(); - response.setTemplate(template); - response.setAlarmRules(rules); - return Result.success(response); - } - - public Result getTemplateByCreater(String user){ - - AppAlarmRuleTemplateQuery query = new AppAlarmRuleTemplateQuery(); - query.setCreater(user); - List list = appAlarmRuleTemplateDao.query(query, 0, Integer.MAX_VALUE); - return Result.success(list); - } - - public Result addTemplate(AlarmRuleTemplateRequest request,String user){ - - AppAlarmRuleTemplate alarmRuleTemplate = request.getTemplate(); - alarmRuleTemplate.setCreater(user); - alarmRuleTemplate.setType(AlarmRuleTemplateType.user.getCode()); - int i = appAlarmRuleTemplateDao.create(alarmRuleTemplate); - if(i < 1){ - log.error("AppAlarmService.addTemplate,insert template data failed!"); - return Result.fail(ErrorCode.unknownError); - } - - List alarmRules = request.getAlarmRules(); - - boolean b = saveTemplateRules(alarmRules, alarmRuleTemplate.getId(), user); - if(!b){ - return Result.fail(ErrorCode.unknownError); - } - - return Result.success(""); - - } - - public Result editTemplate(AlarmRuleTemplateRequest request,String user){ - - AppAlarmRuleTemplate alarmRuleTemplate = request.getTemplate(); - - AppAlarmRuleTemplate update = new AppAlarmRuleTemplate(); - update.setId(alarmRuleTemplate.getId()); - update.setRemark(alarmRuleTemplate.getRemark()); - update.setName(alarmRuleTemplate.getName()); - update.setStrategyType(alarmRuleTemplate.getStrategyType()); - int i = appAlarmRuleTemplateDao.update(update); - if(i < 1){ - log.error("AppAlarmService.editTemplate,update template data failed!"); - return Result.fail(ErrorCode.unknownError); - } - - - AppAlarmRule rule = new AppAlarmRule(); - rule.setTemplateId(alarmRuleTemplate.getId()); - rule.setRuleType(0); - appAlarmRuleDao.delete(rule); - - List alarmRules = request.getAlarmRules(); - - boolean b = saveTemplateRules(alarmRules, alarmRuleTemplate.getId(), user); - if(!b){ - return Result.fail(ErrorCode.unknownError); - } - - return Result.success(""); - - } - - public Result deleteTemplate(Integer templateId){ - - int i = appAlarmRuleTemplateDao.deleteById(templateId); - if(i < 1){ - log.error("AppAlarmService.deleteTemplate,delete template data failed!"); - return Result.fail(ErrorCode.unknownError); - } - - AppAlarmRule rule = new AppAlarmRule(); - rule.setTemplateId(templateId); - rule.setRuleType(0); - int delete = appAlarmRuleDao.delete(rule); - if(delete < 1){ - log.info("AppAlarmService.deleteTemplate,delete template rules no data found!"); - } - - return Result.success(""); - - } - - /** - * 保存模版规则 - * @param alarmRules - * @param templateId - * @return - */ - private boolean saveTemplateRules(List alarmRules,Integer templateId,String user){ - - /** - * 创建新的关联规则 - */ - if(CollectionUtils.isEmpty(alarmRules)){ - log.info("AppAlarmService.saveTemplateRules,alarmRules is empty!templateId : {}",templateId); - return true; - } - Iterator iterator = alarmRules.iterator(); - while (iterator.hasNext()){ - AppAlarmRule next = iterator.next(); - next.setCreater(user); - - next.setTemplateId(templateId); - //没有和任何应用关联配置,单纯隶属于一个模版的模版规则 - next.setRuleType(AlarmRuleType.template.getCode()); - //因为是模版,模版的数据不关联任何应用,默认不生效 - next.setRuleStatus(0); - next.setForTime(next.getDataCount() * 30 + "s"); - next.setStatus(0); - next.setCreateTime(new Date()); - next.setUpdateTime(new Date()); - } - - int i1 = appAlarmRuleDao.batchInsert(alarmRules); - if (i1 < 1) { - log.error("AppAlarmService.saveTemplateRules,insert rules data failed! templateId:{},alarmRules:{}", - templateId,alarmRules); - return false; - } - return true; - } - - +public interface AppAlarmService { + + + void alarmRuleSwitchPlat(AppAlarmRule oldRule, Integer newProjectId, Integer newIamId, String oldProjectName, + String newProjectName); + + Result queryFunctionList(Integer projectId); + + Result queryRulesByAppName(String appName, String userName, Integer page, Integer pageSize); + + Result queryNoRulesConfig(String appName, String userName, Integer page, Integer pageSize); + + Result queryRulesByIamId(Integer iamId, String userName); + + Integer getAlarmConfigNumByTeslaGroup(String group); + + Result addRulesWithStrategy(AlarmRuleRequest param); + + Result batchAddRulesWithStrategy(AlarmRuleRequest param); + + Result addRules(AlarmRuleRequest param, AppMonitor app); + + Result editRules(List rules, AlarmRuleRequest param, String user, String userName); + + Result delAlarmRules(List ids, String user); + + Result editAlarmRuleSingle(AlarmRuleData ruleData, String user); + + Result editRulesByStrategy(AlarmRuleRequest param); + + Result editAlarmRule(AlarmRuleData ruleData, AlarmStrategy alarmStrategy, AppMonitor app, String user); + + Result deleteRulesByIamId(Integer iamId, Integer strategyId, String user); + + Result enabledRules(Integer iamId, Integer strategyId, Integer pauseStatus, String user); + + Result queryTemplate(AppAlarmRuleTemplateQuery query); + + Result getTemplateById(Integer id); + + Result getTemplateByCreater(String user); + + Result addTemplate(AlarmRuleTemplateRequest request, String user); + + Result editTemplate(AlarmRuleTemplateRequest request, String user); + + Result deleteTemplate(Integer templateId); + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppGrafanaMappingService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppGrafanaMappingService.java index 26d2580f1..4b4ba9206 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppGrafanaMappingService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppGrafanaMappingService.java @@ -13,383 +13,36 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; import com.xiaomi.mone.app.api.model.HeraAppBaseInfoModel; -import com.xiaomi.mone.monitor.bo.AppLanguage; -import com.xiaomi.mone.monitor.bo.AppType; -import com.xiaomi.mone.monitor.bo.PlatFormType; -import com.xiaomi.mone.monitor.dao.AppGrafanaMappingDao; -import com.xiaomi.mone.monitor.dao.AppMonitorDao; -import com.xiaomi.mone.monitor.dao.GrafanaTemplateDao; import com.xiaomi.mone.monitor.dao.model.AppGrafanaMapping; -import com.xiaomi.mone.monitor.dao.model.AppMonitor; -import com.xiaomi.mone.monitor.dao.model.GrafanaTemplate; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.api.AppGrafanaMappingServiceExtension; -import com.xiaomi.mone.monitor.service.extension.PlatFormTypeExtensionService; -import com.xiaomi.mone.monitor.service.model.GrafanaResponse; -import com.xiaomi.mone.monitor.service.model.MutiGrafanaResponse; -import com.xiaomi.mone.monitor.service.serverless.ServerLessService; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; - -import java.util.*; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; /** * @author gaoxihui * @date 2021/7/8 11:05 PM */ -@Slf4j -@Service -public class AppGrafanaMappingService { - - @Autowired - AppGrafanaMappingDao appGrafanaMappingDao; - - @Autowired - GrafanaService grafanaService; - - @Autowired - AppMonitorDao appMonitorDao; - - @Autowired - GrafanaTemplateDao grafanaTemplateDao; - - @Autowired - HeraBaseInfoService heraBaseInfoService; - - @Autowired - PlatFormTypeExtensionService platFormTypeExtensionService; - - @Autowired - private ServerLessService serverLessService; - - @Autowired - private AppGrafanaMappingServiceExtension appGrafanaMappingServiceExtension; - - - @Value("${server.type}") - private String serverType; - - @Value("${grafana.domain}") - private String grafanaDomain; - - ExecutorService executor = null; - - - public static final String OLD_ST_GRAFANA_DOMAIN = "http://grafana.mione"; - public static final String OLD_ONLINE_GRAFANA_DOMAIN = "http://grafana.be"; - - public AppGrafanaMappingService() { - executor = Executors.newSingleThreadExecutor(); - } - - public void exeReloadTemplateBase(Integer pSize) { - - log.info("exeReloadTemplateBase will be running!! pSize:" + pSize); - Future result = executor.submit(() -> { - - log.info("exeReloadTemplateBase start running!! pSize:" + pSize); - - try { - HeraAppBaseInfoModel baseInfoCondition = new HeraAppBaseInfoModel(); - Long total = heraBaseInfoService.count(baseInfoCondition); - log.info("AppGrafanaMappingService.exeReloadTemplateBase data totalNum ====== {}", total); - if (total == null || total.intValue() == 0) { - log.info("AppGrafanaMappingService.exeReloadTemplateBase no data found!!!"); - return "No data found!!"; - } - - int pageCount = total.intValue() / pSize + (total.intValue() % pSize > 0 ? 1 : 0); - - for (int i = 0; i < pageCount; i++) { - int offset = i + 1; - List list = heraBaseInfoService.query(baseInfoCondition, offset, pSize); - for (HeraAppBaseInfoModel heraAppBaseInfo : list) { - - try { - this.createTmpByAppBaseInfo(heraAppBaseInfo); - } catch (Exception e1) { - log.error("grafanaMappingService.exeReloadTemplateBase error appName:{} error : {}", heraAppBaseInfo.getAppName(), e1.getMessage(), e1); - continue; - } - - } - } - } catch (Exception e) { - log.error("AppGrafanaMappingService.exeReloadTemplateBase error : {}", e.getMessage(), e); - return ErrorCode.unknownError.getMessage(); - } - - return ErrorCode.success.getMessage(); - }); - } - - public void reloadTmpByAppId(Integer id) { - HeraAppBaseInfoModel baseInfo = heraBaseInfoService.getById(id); - if (baseInfo == null) { - log.error("reloadTmpByAppId no data found id:{}", id); - return; - } - createTmpByAppBaseInfo(baseInfo); - } - - public void createTmpByAppBaseInfo(HeraAppBaseInfoModel baseInfo) { - GrafanaTemplate template = new GrafanaTemplate(); - Integer appType = baseInfo.getAppType(); - /** - * 默认使用业务类型; - * mesh类型应用与业务类型公用模版 - */ - if(appType == null || AppType.mesh.getCode().equals(appType)){ - appType = AppType.businessType.getCode(); - } - template.setAppType(appType); - template.setPlatform(baseInfo.getPlatformType()); - - try { - Integer langUageCode = AppLanguage.getCodeByMessage(baseInfo.getAppLanguage()); - //无语言配置,默认使用java语言模版 - if (langUageCode == null) { - langUageCode = AppLanguage.java.getCode(); - log.error("the app base info no language set! set default language java. baseInfo:{}", new Gson().toJson(baseInfo)); - } - template.setLanguage(langUageCode); - - appGrafanaMappingServiceExtension.setPlatFormByLanguage(template,baseInfo.getAppLanguage()); - - List search = grafanaTemplateDao.search(template); - if (CollectionUtils.isEmpty(search)) { - log.error("createTmpByAppBaseInfo,no template config found! baseInfo:{}", new Gson().toJson(baseInfo)); - return; - } - - Optional platformType = Optional.ofNullable(baseInfo.getPlatformType()); - //找不到平台类型,默认放在open下 - String grafanaDirByCode = platformType.isPresent() ? platFormTypeExtensionService.getGrafanaDirByTypeCode(platformType.get()) : PlatFormType.open.getGrafanaDir(); - - - List funcList = new ArrayList<>(); - - if(AppType.serverless.getCode().equals(baseInfo.getAppType())){ - funcList = serverLessService.getFaasFunctionList(Integer.valueOf(baseInfo.getBindId())); - } - - MutiGrafanaResponse mutiGrafanaResponse = grafanaService.requestGrafanaTemplate(serverType, baseInfo.getBindId() + "_" + baseInfo.getAppName(), grafanaDirByCode, search.get(0), funcList); - - log.info("createTmpByAppBaseInfo response info : {}", mutiGrafanaResponse); - - appGrafanaMappingServiceExtension.dealRequestGrafanaTemplateCode(mutiGrafanaResponse.getCode(), baseInfo.getBindId(), baseInfo.getAppName()); - - if(mutiGrafanaResponse.getCode() != 0 || mutiGrafanaResponse.getData() == null){ - log.info("create grafana fail! baseInfo:{},response:{}",baseInfo.toString(),new Gson().toJson(mutiGrafanaResponse)); - return; - } - log.info("grafanaMappingService.createTmpByAppBaseInfo success appName : {}, version:{},area : {}, returnUrl :{}" - , baseInfo.getAppName(), mutiGrafanaResponse.getData().get(0).getMimonitor_version(), grafanaDirByCode, mutiGrafanaResponse); - - String url = new StringBuffer().append(grafanaDomain).append(mutiGrafanaResponse.getUrl()).toString(); - - AppGrafanaMapping grafanaMapping = new AppGrafanaMapping(); - grafanaMapping.setGrafanaUrl(url); - grafanaMapping.setAppName(baseInfo.getBindId() + "_" + baseInfo.getAppName()); - grafanaMapping.setMioneEnv(grafanaDirByCode); - int i = this.saveOrUpdate(grafanaMapping); - if (i > 0) { - log.info("createTmpByAppBaseInfo sucess!grafanaMapping:{}", grafanaMapping.toString()); - } else { - log.info("createTmpByAppBaseInfo fail!grafanaMapping:{}", grafanaMapping.toString()); - } - } catch (JsonSyntaxException e) { - log.error("createTmpByAppBaseInfo error!{}", e.getMessage(), e); - } - - } - - @Deprecated - public String createGrafanaUrlByAppName(String appName, String area) { - if (StringUtils.isEmpty(appName) || StringUtils.isEmpty(area)) { - log.error("GrafanaMappingController.createGrafanaUrlByAppName error! param appName or area is empty!"); - return ErrorCode.invalidParamError.getMessage(); - } - - try { - - String grafanaReqResult = grafanaService.requestGrafana(serverType, appName, area); - log.info("GrafanaMappingController.createGrafanaUrlByAppName requestGrafana serverType:{} ,appName : {},area : {}, result : {}", serverType, appName, area, grafanaReqResult); - - GrafanaResponse grafanaResponse = new Gson().fromJson(grafanaReqResult, GrafanaResponse.class); - String url = new StringBuffer().append(grafanaDomain).append(grafanaResponse.getUrl()).toString(); - - AppGrafanaMapping appGrafanaMapping = new AppGrafanaMapping(); - appGrafanaMapping.setMioneEnv(area); - appGrafanaMapping.setAppName(appName); - appGrafanaMapping.setGrafanaUrl(url); - Integer ret = this.saveOrUpdate(appGrafanaMapping); - - if (ret > 0) { - log.info("GrafanaMappingController.createGrafanaUrlByAppName save data success name : {}, area : {}, url : {} ", appName, area, url); - return ErrorCode.success.getMessage(); - } else { - log.info("GrafanaMappingController.createGrafanaUrlByAppName save data failed name : {}, area : {}, url : {} ", appName, area, url); - return "data save failed!!"; - } - - } catch (Exception e) { - log.error("GrafanaMappingController.createGrafanaUrlByAppName error:{}", e.getMessage(), e); - return ErrorCode.unknownError.getMessage(); - } - } - - - public Result getGrafanaUrlByAppName(String appName) { - - if (StringUtils.isEmpty(appName)) { - log.error("AppGrafanaMappingService#getGrafanaUrlByAppName param is empty"); - return Result.fail(ErrorCode.invalidParamError); - } - - int i = appName.indexOf("_"); - String bindId = appName.substring(0, i); - String appNameBase = appName.substring(i + 1); - - String appType = null; - - - try { - AppGrafanaMapping mapping = appGrafanaMappingDao.getByAppName(appName); - if (mapping == null) { - log.info("AppGrafanaMappingService#getGrafanaUrlByAppName can not find data for appName : {}", appName); - return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), null); - } - log.info("AppGrafanaMappingService#getGrafanaUrlByAppName by appName : {} ,return : {}", appName, mapping.getGrafanaUrl()); - - - HeraAppBaseInfoModel heraAppBaseInfo = new HeraAppBaseInfoModel(); - heraAppBaseInfo.setBindId(bindId); - heraAppBaseInfo.setAppName(appNameBase); - - List query = heraBaseInfoService.query(heraAppBaseInfo, null, null); - if (CollectionUtils.isEmpty(query) || query.get(0).getAppType() == null) { - log.error("no appType found for appName:{},has set default type by businessType type", appName); - appType = AppType.businessType.getMessage(); - } else { - AppType anEnum = AppType.getEnum(query.get(0).getAppType()); - if (anEnum == null) { - log.error("error AppType appName:{},has set default type by businessType type", appName); - appType = AppType.businessType.getMessage(); - }else { - appType = anEnum.getMessage(); - } - - // mesh类型和业务类型应用使用相同的模版 - if(appType.equals(AppType.mesh.getMessage())){ - appType = AppType.businessType.getMessage(); - } - - } - Map map = new HashMap(); - map.put("appType", appType); - map.put("url", transferGrafanaUrl(mapping.getGrafanaUrl())); - - return Result.success(map); - } catch (Exception e) { - log.error("AppGrafanaMappingService#getGrafanaUrlByAppName error" + e.getMessage(), e); - return Result.fail(ErrorCode.unknownError); - } - - } - - @Deprecated - public Result getGrafanaUrlByAppId(Integer appId) { - - if (appId == null) { - log.error("AppGrafanaMappingService#getGrafanaUrlByAppId param is empty"); - return Result.fail(ErrorCode.invalidParamError); - } - - try { - AppMonitor appMonitor = appMonitorDao.getByAppId(appId); - if (appMonitor == null) { - return Result.success(null); - } - - String grafanaName = new StringBuilder().append(appMonitor.getProjectId()).append("_").append(appMonitor.getProjectName()).toString(); - AppGrafanaMapping mapping = appGrafanaMappingDao.getByAppName(grafanaName); - if (mapping == null) { - log.info("AppGrafanaMappingService#getGrafanaUrlByAppId can not find data for appName : {}", grafanaName); - return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), null); - } - log.info("AppGrafanaMappingService#getGrafanaUrlByAppId by appName : {} ,return : {}", grafanaName, mapping.getGrafanaUrl()); - return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), mapping.getGrafanaUrl()); - } catch (Exception e) { - log.error("AppGrafanaMappingService#getGrafanaUrlByAppId error" + e.getMessage(), e); - return Result.fail(ErrorCode.unknownError); - } - - } - - public Integer save(AppGrafanaMapping appGrafanaMapping) { - try { - return appGrafanaMappingDao.generateGrafanaMapping(appGrafanaMapping); - } catch (Exception e) { - log.error("AppGrafanaMappingService#save error" + e.getMessage(), e); - } - return 0; - } - - public Integer saveOrUpdate(AppGrafanaMapping appGrafanaMapping) { - try { - AppGrafanaMapping byAppName = appGrafanaMappingDao.getByAppName(appGrafanaMapping.getAppName()); - if (byAppName == null) { - return appGrafanaMappingDao.generateGrafanaMapping(appGrafanaMapping); - } - - appGrafanaMapping.setId(byAppName.getId()); - appGrafanaMapping.setCreateTime(byAppName.getCreateTime()); - return appGrafanaMappingDao.updateByPrimaryKey(appGrafanaMapping); - - } catch (Exception e) { - log.error("AppGrafanaMappingService#saveOrUpdate error" + e.getMessage(), e); - } - return 0; - } - - private String transferGrafanaUrl(String grafanaUrl) { - if (!serverType.equals("online")) { - //测试环境由于grafana升级https 域名发生变换 - if (grafanaUrl.startsWith(OLD_ST_GRAFANA_DOMAIN)) { - String[] grafanaUrlArr = grafanaUrl.split(OLD_ST_GRAFANA_DOMAIN); - if (grafanaUrlArr.length < 2) { - return grafanaUrl; - } else { - return grafanaDomain + grafanaUrlArr[1]; - } - } - return grafanaUrl; - } else { - //线上环境更换域名,替换名称 - if (grafanaUrl.startsWith(OLD_ONLINE_GRAFANA_DOMAIN)) { - String[] grafanaUrlArr = grafanaUrl.split(OLD_ONLINE_GRAFANA_DOMAIN); - if (grafanaUrlArr.length < 2) { - return grafanaUrl; - } else { - return grafanaDomain + grafanaUrlArr[1]; - } - } - return grafanaUrl; - } - } +public interface AppGrafanaMappingService { + + + void exeReloadTemplateBase(Integer pSize); + + void reloadTmpByAppId(Integer id); + + void createTmpByAppBaseInfo(HeraAppBaseInfoModel baseInfo); + + String createGrafanaUrlByAppName(String appName, String area); + + Result getGrafanaUrlByAppName(String appName); + + Result getGrafanaUrlByAppId(Integer appId); + + Integer save(AppGrafanaMapping appGrafanaMapping); + + Integer saveOrUpdate(AppGrafanaMapping appGrafanaMapping); + + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppMonitorConfigService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppMonitorConfigService.java index f7d0af1de..1bfea786c 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppMonitorConfigService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppMonitorConfigService.java @@ -13,76 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.xiaomi.mone.monitor.dao.AppMonitorConfigDao; import com.xiaomi.mone.monitor.dao.model.AppMonitorConfig; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; import com.xiaomi.mone.monitor.service.model.PageData; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.Date; /** * @author gaoxihui * @date 2021/8/19 4:07 PM */ -@Slf4j -@Service -public class AppMonitorConfigService { - - @Autowired - AppMonitorConfigDao dao; - - public Result createConfig(AppMonitorConfig config){ - config.setCreateTime(new Date()); - config.setUpdateTime(new Date()); - config.setStatus(0); - int i = dao.create(config); - if(i < 1){ - return Result.fail(ErrorCode.unknownError); - } - return Result.success(null); - } - - public Result updateConfig(AppMonitorConfig config){ - config.setStatus(0); - config.setUpdateTime(new Date()); - int update = dao.update(config); - if(update < 1){ - return Result.fail(ErrorCode.unknownError); - } - return Result.success(null); - } - - public Result delConfig(Integer id){ - AppMonitorConfig config = dao.getById(id); - if(config == null){ - log.error("AppMonitorConfigService.delConfig error! no config data found By id : {}",id); - return Result.fail(ErrorCode.unknownError); - } - config.setStatus(1); - config.setUpdateTime(new Date()); - int update = dao.update(config); - if(update < 1){ - log.error("AppMonitorConfigService.delConfig failed! id : {}",id); - return Result.fail(ErrorCode.unknownError); - } - return Result.success(null); - } - - public Result getConfig(Integer projectId, Integer type, String configName, Integer status_, Integer page, Integer pageSize){ - - try { - return dao.getConfig(projectId,type,configName,status_,page,pageSize); - } catch (Exception e) { - log.error("AppMonitorConfigService.getConfig error : {}",e.getMessage()); - return Result.fail(ErrorCode.unknownError); - } - } - +public interface AppMonitorConfigService { + + + Result createConfig(AppMonitorConfig config); + + Result updateConfig(AppMonitorConfig config); + + Result delConfig(Integer id); + + Result getConfig(Integer projectId, Integer type, String configName, Integer status, Integer page, + Integer pageSize); + + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppMonitorService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppMonitorService.java index 863c1fab0..bc4a61174 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppMonitorService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppMonitorService.java @@ -13,758 +13,60 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.google.gson.Gson; -import com.google.gson.JsonElement; import com.xiaomi.mone.app.api.message.HeraAppInfoModifyMessage; -import com.xiaomi.mone.app.api.message.HeraAppModifyType; import com.xiaomi.mone.app.api.model.HeraAppBaseInfoModel; -import com.xiaomi.mone.app.api.service.HeraAppService; -import com.xiaomi.mone.monitor.bo.AlarmStrategyInfo; -import com.xiaomi.mone.monitor.bo.AppViewType; -import com.xiaomi.mone.monitor.bo.RuleStatusType; -import com.xiaomi.mone.monitor.dao.AppAlarmRuleDao; -import com.xiaomi.mone.monitor.dao.AppAlarmStrategyDao; -import com.xiaomi.mone.monitor.dao.AppMonitorDao; -import com.xiaomi.mone.monitor.dao.HeraAppRoleDao; -import com.xiaomi.mone.monitor.dao.model.*; -import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.dao.model.AlarmHealthQuery; +import com.xiaomi.mone.monitor.dao.model.AppMonitor; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.api.AppMonitorServiceExtension; -import com.xiaomi.mone.monitor.service.api.TeslaService; -import com.xiaomi.mone.monitor.service.extension.PlatFormTypeExtensionService; import com.xiaomi.mone.monitor.service.model.AppMonitorModel; import com.xiaomi.mone.monitor.service.model.AppMonitorRequest; import com.xiaomi.mone.monitor.service.model.PageData; import com.xiaomi.mone.monitor.service.model.ProjectInfo; -import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleData; -import com.xiaomi.mone.monitor.service.model.prometheus.MetricData; -import com.xiaomi.mone.monitor.service.model.prometheus.MetricDataSet; -import com.xiaomi.mone.monitor.service.model.prometheus.MetricResponse; -import com.xiaomi.mone.monitor.service.prometheus.AlarmService; -import com.xiaomi.mone.monitor.service.prometheus.PrometheusService; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.apache.dubbo.config.annotation.Reference; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; -import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; /** * @author gaoxihui * @date 2021/8/13 11:07 AM */ -@Slf4j -@Service -public class AppMonitorService { - - @Autowired - AppMonitorDao appMonitorDao; - - @Autowired - HeraBaseInfoService heraBaseInfoService; - - @Autowired - AppGrafanaMappingService appGrafanaMappingService; - - @Autowired - AppAlarmService appAlarmService; - - @Autowired - private TeslaService teslaService; - - @Autowired - PrometheusService prometheusService; - - @Autowired - ResourceUsageService resourceUsageService; - - @Autowired - HeraAppRoleDao heraAppRoleDao; - - @Autowired - AppAlarmStrategyDao strategyDao; - - @Autowired - AppAlarmRuleDao ruleDao; - - @Autowired - AlarmService alarmService; - - @Autowired - AppMonitorServiceExtension appMonitorServiceExtension; - - @Autowired - PlatFormTypeExtensionService platFormTypeExtensionService; - - @Autowired - AlarmStrategyService alarmStrategyService; - - private static final Gson gson = new Gson(); - - @Reference(registry = "registryConfig", check = false, interfaceClass = HeraAppService.class, group = "${dubbo.group.heraapp}",timeout = 5000) - HeraAppService hearAppService; - - public void appPlatMove(Integer OProjectId, Integer OPlat, Integer NProjectId, Integer Nplat, Integer newIamId, String NprojectName, Boolean rebuildRule) { - - - log.info("appPlatMove OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", OProjectId, OPlat, NProjectId, Nplat, NprojectName); - - if (OProjectId == null || OPlat == null || NProjectId == null || Nplat == null || StringUtils.isBlank(NprojectName)) { - log.error("appPlatMove has invalid param! OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", OProjectId, OPlat, NProjectId, Nplat, NprojectName); - return; - } - - HeraAppBaseInfoModel baseInfo = new HeraAppBaseInfoModel(); - baseInfo.setBindId(String.valueOf(OProjectId)); - baseInfo.setPlatformType(OPlat); - - List query = heraBaseInfoService.query(baseInfo, null, null); - if (CollectionUtils.isEmpty(query)) { - log.info("appPlatMove nodata found!OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", OProjectId, OPlat, NProjectId, Nplat, NprojectName); - return; - } - if (query.size() > 1) { - log.info("appPlatMove more than one data found!OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", OProjectId, OPlat, NProjectId, Nplat, NprojectName); - return; - } - - - HeraAppBaseInfoModel heraAppBaseInfo = query.get(0); - - String oldProjectName = heraAppBaseInfo.getAppName(); - - - heraAppBaseInfo.setPlatformType(Nplat); - heraAppBaseInfo.setIamTreeId(newIamId); - heraAppBaseInfo.setBindId(String.valueOf(NProjectId)); - heraAppBaseInfo.setAppName(NprojectName); - - int update = heraBaseInfoService.insertOrUpdate(heraAppBaseInfo); - if (update < 1) { - log.error("appPlatMove update heraBaseInfo fail!OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", OProjectId, OPlat, NProjectId, Nplat, NprojectName); - } - - - /** - * 关联的角色信息 - */ - HeraAppRole heraAppRole = new HeraAppRole(); - heraAppRole.setAppPlatform(OPlat); - heraAppRole.setAppId(String.valueOf(OProjectId)); - List appRoles = heraAppRoleDao.query(heraAppRole, null, null); - if (!CollectionUtils.isEmpty(appRoles)) { - appRoles.forEach(t -> { - t.setAppId(String.valueOf(NProjectId)); - t.setAppPlatform(Nplat); - heraAppRoleDao.update(t); - }); - } - - /** - * 3、关注信息 - * -参与列表 - * -关注列表 - */ - List appMonitors = appMonitorDao.getByProjectIdAndPlat(OProjectId, OPlat); - if (!CollectionUtils.isEmpty(appMonitors)) { - appMonitors.forEach(t -> { - - t.setAppSource(Nplat); - t.setProjectId(NProjectId); - t.setIamTreeId(newIamId); - t.setProjectName(NprojectName); - appMonitorDao.update(t); - }); - } - - - if (rebuildRule) { - AlarmStrategy strategy = new AlarmStrategy(); - strategy.setAppId(OProjectId); - strategy.setAppName(oldProjectName); - PageData> listPageData = strategyDao.searchByCondNoUser(strategy, 1, 1000, null, null); - List list = listPageData.getList(); - if (!CollectionUtils.isEmpty(list)) { - list.forEach(t -> { - List rules = ruleDao.selectByStrategyId(t.getId()); - for (AppAlarmRule rule : rules) { - /** - * 1、app的projectId、Name、iamId都更新好 - * 2、rule的iamId - * 3、user采用rule的creater - * 4、ruleData补数据 - * - * ruleData 标签数据来源: - * 1、strategy - * 2、rule的labels - * - * 使用到ruleData的数据: - * AlarmCallbackUrl - * ruleData.getAlarmDetailUrl() - * - * getEnvLabels: - * serverEnv(include、except) - * service(include、except) - * functionId(include、except) - * methodName:http、dubbo - * errorCode:http - * serviceName:dubbo - * - * Tesla - * rule.getTeslaGroup() - * rule.getTeslaUrls() - * rule.getExcludeTeslaUrls() - * - * - * ruleData.getExpr() 自定义表达式 - * ruleData.getAlertMembers() - * - * - * - */ - - Integer oldAlarmId = rule.getAlarmId(); - Integer oldIamId = rule.getIamId(); - - AppMonitor app = new AppMonitor(); - app.setProjectId(NProjectId); - app.setProjectName(NprojectName); - - rule.setIamId(newIamId); - rule.setProjectId(NProjectId); - - StringBuilder cname = new StringBuilder(); - cname.append(newIamId); - cname.append("-").append(rule.getAlert()); - cname.append("-").append(System.currentTimeMillis()); - rule.setCname(cname.toString()); - - AlarmRuleData ruleData = new AlarmRuleData(); - BeanUtils.copyProperties(rule, ruleData); - ruleData.setLabels(rule.getLabels()); - ruleData.convertLabels(); - - ruleData.setIncludeEnvs(t.getIncludeEnvs()); - ruleData.setExceptEnvs(t.getExceptEnvs()); - ruleData.setIncludeZones(t.getIncludeZones()); - ruleData.setExceptZones(t.getExceptZones()); - ruleData.setIncludeContainerName(t.getIncludeContainerName()); - ruleData.setExceptContainerName(t.getExceptContainerName()); - ruleData.setAlertMembers(t.getAlertMembers()); - - if (!CollectionUtils.isEmpty(t.getIncludeFunctions())) { - ruleData.setIncludeFunctions(t.getIncludeFunctions().stream().map(String::valueOf).collect(Collectors.toList())); - } - - if (!CollectionUtils.isEmpty(t.getExceptFunctions())) { - ruleData.setExceptFunctions(t.getExceptFunctions().stream().map(String::valueOf).collect(Collectors.toList())); - } - - ruleData.setIncludeModules(t.getIncludeModules()); - ruleData.setExceptModules(t.getExceptModules()); - - - Result result1 = alarmService.addRule(app, rule, rule.getCreater(), ruleData); - if (!result1.isSuccess()) { - log.error("appPlatMove add new rule fail!rule{}", rule.toString()); - return; - } - - JsonElement data = (JsonElement) result1.getData(); - Integer alarmId = data.getAsJsonObject().get("id").getAsInt(); - rule.setAlarmId(alarmId); - - if (rule.getRuleStatus().equals(RuleStatusType.pause.getCode())) { - Result result = alarmService.enabledRule(alarmId, RuleStatusType.pause.getCode(), rule.getIamId(), rule.getCreater()); - if (!result.isSuccess()) { - log.error("appPlatMove pause rule fail!rule{}", rule.toString()); - } - } - - - Result result = alarmService.deleteRule(oldAlarmId, oldIamId, rule.getCreater()); - if (!result.isSuccess()) { - log.error("appPlatMove del old rule fail!rule{}", rule.toString()); - } - - int i = ruleDao.updateByIdSelective(rule); - if (i < 1) { - log.error("appPlatMove update rule db fail! rule{}", rule.toString()); - } - - } - - AlarmStrategy strategyUp = new AlarmStrategy(); - strategyUp.setId(t.getId()); - strategyUp.setAppId(NProjectId); - strategyUp.setAppName(NprojectName); - strategyUp.setIamId(newIamId); - boolean b = strategyDao.updateById(strategyUp); - if (!b) { - log.error("appPlatMove update strategy fail! oldP:{},new:{}", t.toString(), strategyUp.toString()); - } - - }); - } - } - } - - public Result selectAppAlarmHealth(AlarmHealthQuery query) { - try { - List alarmHealthResults = appMonitorDao.selectAppHealth(query); - if (!CollectionUtils.isEmpty(alarmHealthResults)) { - - for (AlarmHealthResult alarmHealth : alarmHealthResults) { - /** - * 基础指标得分 - */ - alarmHealth.setBasicMetricScore((alarmHealth.getCpuUseRate().intValue() > 0 ? 2 : 0) + - (alarmHealth.getCpuLoad().intValue() > 0 ? 2 : 0) + - (alarmHealth.getMemUseRate().intValue() > 0 ? 2 : 0) + - (alarmHealth.getContainerNum().intValue() > 0 ? 2 : 0) + - (alarmHealth.getJvmThread().intValue() > 0 ? 1 : 0) + - (alarmHealth.getJvmGc().intValue() > 0 ? 1 : 0) - ); - - /** - * 接口指标得分 - */ - alarmHealth.setInterfaceMetricScore((alarmHealth.getHttpServerAvailability().intValue() > 0 ? 2 : 0) + - (alarmHealth.getHttpServerQps().intValue() > 0 ? 1 : 0) + - (alarmHealth.getHttpServerTimeCost().intValue() > 0 ? 1 : 0) + - (alarmHealth.getHttpClientAvailability().intValue() > 0 ? 1 : 0) + - (alarmHealth.getHttpClientQps().intValue() > 0 ? 1 : 0) + - (alarmHealth.getHttpClientTimeCost().intValue() > 0 ? 1 : 0) + - (alarmHealth.getDubboProviderAvailability().intValue() > 0 ? 2 : 0) + - (alarmHealth.getDubboProviderQps().intValue() > 0 ? 1 : 0) + - (alarmHealth.getDubboProviderTimeCost().intValue() > 0 ? 1 : 0) + - (alarmHealth.getDubboProviderSlowQuery().intValue() > 0 ? 1 : 0) + - (alarmHealth.getDubboConsumerAvailability().intValue() > 0 ? 1 : 0) + - (alarmHealth.getDubboConsumerQps().intValue() > 0 ? 1 : 0) + - (alarmHealth.getDubboConsumerTimeCost().intValue() > 0 ? 1 : 0) + - (alarmHealth.getDubboConsumerSlowQuery().intValue() > 0 ? 1 : 0) + - (alarmHealth.getDbAvailability().intValue() > 0 ? 2 : 0) + - (alarmHealth.getDbSlowQuery().intValue() > 0 ? 1 : 0) - ); - - /** - * 综合得分 - */ - alarmHealth.setComprehensiveScore(alarmHealth.getBasicMetricScore() + alarmHealth.getInterfaceMetricScore()); - } - } - return Result.success(alarmHealthResults); - } catch (Exception e) { - log.error("selectAppAlarmHealth Error!{}", e.getMessage(), e); - return Result.fail(ErrorCode.unknownError); - } - } - - public Result getResourceUsageUrlForK8s(Integer appId, String appName) { - return appMonitorServiceExtension.getResourceUsageUrlForK8s(appId, appName); - } - - public Result initAppsByUsername(String userName){ - return appMonitorServiceExtension.initAppsByUsername(userName); - } - - public List getAppsByUserName(String username){ - return appMonitorServiceExtension.getAppsByUserName(username); - } - - public Result getProjectInfos(String userName, String appName, Integer page, Integer pageSize) { - - if (page == null) { - page = 1; - } - - if (pageSize == null) { - pageSize = 10; - } - - PageData pd = new PageData(); - pd.setPage(page); - pd.setPageSize(pageSize); - - HeraAppBaseInfoModel model = new HeraAppBaseInfoModel(); - model.setAppName(appName); - - Long total = hearAppService.count(model); - pd.setTotal(total); - - List query = hearAppService.query(model, page, pageSize); - - List list = new ArrayList(); - if (CollectionUtils.isEmpty(query)) { - pd.setList(list); - return Result.success(pd); - } - - query.forEach(t -> { - ProjectInfo info = new ProjectInfo(); - info.setId(Long.valueOf(t.getBindId())); - info.setName(t.getAppName()); - info.setIamTreeId(t.getIamTreeId() != null ? Long.valueOf(t.getIamTreeId()) : t.getIamTreeId()); - list.add(info); - }); - - pd.setList(list); - return Result.success(pd); - - } - - public Result createWithBaseInfo(AppMonitorModel appMonitorModel, String user) { - - HeraAppBaseInfoModel heraAppBaseInfo = appMonitorModel.baseInfo(); - Integer baseInfoId = createBaseInfo(heraAppBaseInfo); - if (baseInfoId == null) { - log.error("createBaseInfo fail!heraAppBaseInfo:{}", heraAppBaseInfo); - return Result.fail(ErrorCode.unknownError); - } - - AppMonitor appMonitor = appMonitorModel.appMonitor(); - appMonitor.setBaseInfoId(baseInfoId); - - if (!appMonitorServiceExtension.checkCreateParam(appMonitor)) { - log.error("AppMonitorService.createWithBaseInfo 用户{}添加项目{},参数不合法", user, appMonitor); - return Result.fail(ErrorCode.invalidParamError); - } - if (StringUtils.isNotBlank(appMonitor.getOwner()) && appMonitor.getOwner().equals("yes")) { - appMonitor.setOwner(user); - } else { - appMonitor.setCareUser(user); - } - - Result result = create(appMonitor); - if (!result.isSuccess()) { - log.error("AppMonitorController.addApp fail! user:{},appMonitorModel:{}", user, appMonitorModel); - return Result.fail(ErrorCode.invalidParamError); - } - - return Result.success(null); - - } - - public Integer createBaseInfo(HeraAppBaseInfoModel heraAppBaseInfo) { - - HeraAppBaseInfoModel queryCondition = new HeraAppBaseInfoModel(); - queryCondition.setBindId(heraAppBaseInfo.getBindId()); - queryCondition.setPlatformType(heraAppBaseInfo.getPlatformType()); - - List query = heraBaseInfoService.query(queryCondition, 1, 10); - - if (!CollectionUtils.isEmpty(query)) { - log.info("createBaseInfo HeraAppBaseInfo has exist!heraAppBaseInfo:{},query Result:{}", heraAppBaseInfo, new Gson().toJson(query)); - return query.get(0).getId(); - } - - int i = heraBaseInfoService.insertOrUpdate(heraAppBaseInfo); - - if (i <= 0) { - return null; - } - - return heraAppBaseInfo.getId(); - } - - public Result create(AppMonitor appMonitor) { - - if (appMonitor == null) { - log.error("AppMonitorService.create param is null"); - return Result.fail(ErrorCode.invalidParamError); - } - if (appMonitor.getProjectId() == null || StringUtils.isEmpty(appMonitor.getProjectName())) { - log.error("AppMonitorService.create param is avalid! projectId or projectName is empty!"); - return Result.fail(ErrorCode.invalidParamError); - } - if (StringUtils.isEmpty(appMonitor.getOwner()) && StringUtils.isEmpty(appMonitor.getCareUser())) { - log.error("AppMonitorService.create param is avalid! owner and careUser can not both null at same time!"); - return Result.fail(ErrorCode.invalidParamError); - } - - /** - * owner、mycare只能存在一种,如果存在owner直接按owner处理,不再继续判断!正常的业务场景不会同时指定owner和mycare; - */ - AppViewType viewType = AppViewType.MyApp; - String userName = appMonitor.getOwner(); - if (StringUtils.isEmpty(appMonitor.getOwner())) { - viewType = AppViewType.MyCareApp; - userName = appMonitor.getCareUser(); - } - AppMonitor app = appMonitorDao.getMyApp(appMonitor.getProjectId(), appMonitor.getIamTreeId(), userName, viewType); - if (app != null) { - log.info("AppMonitorService.create update Data appMonitor : {}", appMonitor); - //如果指定的projectId,userName,viewType已经存在,则更新一下项目名称即可,无需重复创建数据! - app.setProjectName(appMonitor.getProjectName()); - app.setBaseInfoId(appMonitor.getBaseInfoId()); - int update = appMonitorDao.update(app); - if (update > 0) { - log.info("AppMonitorService.create update Data success appMonitor : {}", appMonitor); - return Result.success(null); - } else { - log.info("AppMonitorService.create database update Data failed appMonitor : {}", appMonitor); - return Result.fail(ErrorCode.unknownError); - } - } - //兼容不同类型的重复添加 - if (AppViewType.MyCareApp.equals(viewType)) { - app = appMonitorDao.getMyApp(appMonitor.getProjectId(), appMonitor.getIamTreeId(), userName, AppViewType.MyApp); - if (app != null) { - log.error("AppMonitorService.create项目已经添加过参与项目, param={}", appMonitor); - return Result.fail(ErrorCode.REPEAT_ADD_PROJECT); - } - } else { - app = appMonitorDao.getMyApp(appMonitor.getProjectId(), appMonitor.getIamTreeId(), userName, AppViewType.MyCareApp); - if (app != null) { - //清除已经关注的数据 - Result delResult = this.delete(app.getId()); - if (delResult.getCode() != ErrorCode.success.getCode()) { - return delResult; - } - } - } - try { - int i = appMonitorDao.create(appMonitor); - if (i > 0) { - createGrafana(appMonitor); - log.info("AppMonitorService.create success appMonitor : {}", appMonitor); - return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), null); - } else { - log.info("AppMonitorService.create database create data failed appMonitor : {}", appMonitor); - return Result.fail(ErrorCode.unknownError); - } - - } catch (Exception e) { - log.error("AppMonitorService.create error" + e.getMessage(), e); - return Result.fail(ErrorCode.unknownError); - } - - } - - /** - * 创建grafana图表 - * - * @param appMonitor - */ - private void createGrafana(AppMonitor appMonitor) { - if (appMonitor.getAppSource() == null) { - return; - } - StringBuilder appName = new StringBuilder(); - appName.append(appMonitor.getProjectId()).append("_").append(appMonitor.getProjectName().replace('.', '_')); - - String area = platFormTypeExtensionService.getGrafanaDirByTypeCode(appMonitor.getAppSource()); - - if (StringUtils.isBlank(area)) { - log.error("invalid grafana area!appMonitor:{}", appMonitor); - return; - } - - HeraAppBaseInfoModel heraAppBaseInfo = new HeraAppBaseInfoModel(); - heraAppBaseInfo.setBindId(appMonitor.getProjectId() + ""); - heraAppBaseInfo.setPlatformType(appMonitor.getAppSource()); - - List query = heraBaseInfoService.query(heraAppBaseInfo, null, null); - HeraAppBaseInfoModel baseInfo = CollectionUtils.isEmpty(query) ? null : query.get(0); - if (baseInfo == null) { - log.error("no base data found for app : {},stop generate grafana url", appMonitor.getProjectName()); - return; - } - - appGrafanaMappingService.createTmpByAppBaseInfo(baseInfo); - - } - - - public Result delete(Integer id) { - - if (id == null) { - log.error("AppMonitorService.delete error param id is null!"); - return Result.fail(ErrorCode.invalidParamError); - } - - AppMonitor app = appMonitorDao.getById(id); - if (app == null) { - log.error("AppMonitorService.delete error cannot find data by id : {}", id); - return Result.fail(ErrorCode.invalidParamError); - } - - app.setStatus(1); - int update = appMonitorDao.update(app); - if (update < 1) { - log.error("AppMonitorService.delete error delete database failed!"); - return Result.fail(ErrorCode.unknownError); - } - - return Result.success(null); - - } - - public Result deleteByUser(Integer projectId, Integer appSource, String userName) { - - - try { - List apps = appMonitorDao.getMyOwnerOrCareAppById(projectId, appSource, userName); - - if (CollectionUtils.isEmpty(apps)) { - log.error("AppMonitorService.deleteByUser error cannot find data,projectId : {}", projectId); - return Result.fail(ErrorCode.unknownError); - } - - for (AppMonitor app : apps) { - app.setStatus(1); - int update = appMonitorDao.update(app); - if (update < 1) { - log.error("AppMonitorService.deleteByUser error delete database failed!app:{}", app); - return Result.fail(ErrorCode.unknownError); - } - } - - return Result.success(null); - - } catch (Exception e) { - log.error("deleteByUser error!" + e.getMessage(), e); - return Result.fail(ErrorCode.unknownError); - } - } - - public Result>> listApp(String appName, String userName, Integer page, Integer pageSize_) { - - if (StringUtils.isEmpty(userName)) { - log.error("AppMonitorService.listApp param is invalid userName is empty!"); - return Result.fail(ErrorCode.invalidParamError); - } - - Integer pageNum = page; - Integer pageSize = pageSize_; - - if (pageNum == null || pageNum.intValue() < 1) { - pageNum = 1; - } - - if (pageSize == null) { - pageSize = 10; - } - - PageData pd = new PageData(); - pd.setPage(pageNum); - pd.setPageSize(pageSize); - - try { - - Long dataTotal = appMonitorDao.getDataTotalByOr(appName, userName, userName); - pd.setTotal(dataTotal); - - if (dataTotal != null && dataTotal.intValue() > 0) { - List apps = appMonitorDao.getMyOwnerOrCareApp(appName, userName, pageNum, pageSize); - pd.setList(apps); - } - - log.info("AppMonitorService.listApp success! param appName : {}, userName : {},result Count : {} ", appName, userName, dataTotal); - - return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), pd); - - } catch (Exception e) { - log.error("AppMonitorService.listApp error" + e.getMessage(), e); - return Result.fail(ErrorCode.unknownError); - } - - } - - public Result>> listAppDistinct(String userName, String appName, Integer page, Integer pageSize_) { - - if (StringUtils.isEmpty(userName)) { - log.error("AppMonitorService.listAppDistinct param is invalid userName is empty!"); - return Result.fail(ErrorCode.invalidParamError); - } - - Integer pageNum = page; - Integer pageSize = pageSize_; - - if (pageNum == null || pageNum.intValue() < 1) { - pageNum = 1; - } - - if (pageSize == null) { - pageSize = 10; - } - - PageData pd = new PageData(); - pd.setPage(pageNum); - pd.setPageSize(pageSize); - - try { - - Long dataTotal = appMonitorDao.countAllMyAppDistinct(userName, appName); - pd.setTotal(dataTotal); - - if (dataTotal != null && dataTotal.intValue() > 0) { - List apps = appMonitorDao.getAllMyAppDistinct(userName, appName, pageNum, pageSize); - pd.setList(apps); -// initAppAlarmData(apps); - } - - log.info("AppMonitorService.listAppDistinct success! param appName : {}, userName : {},result Count : {} ", appName, userName, dataTotal); - - return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), pd); - - } catch (Exception e) { - log.error("AppMonitorService.listAppDistinct error" + e.getMessage(), e); - return Result.fail(ErrorCode.unknownError); - } - - } - - public Result>> listMyApp(AppMonitor appMonitor, String userName, Integer page, Integer pageSize_) { - - if (StringUtils.isEmpty(userName)) { - log.error("AppMonitorService.listMyApp param is invalid userName is empty!"); - return Result.fail(ErrorCode.invalidParamError); - } - - Integer pageNum = page; - Integer pageSize = pageSize_; - - if (pageNum == null || pageNum.intValue() < 1) { - pageNum = 1; - } - - if (pageSize == null) { - pageSize = 10; - } - - PageData pd = new PageData(); - pd.setPage(pageNum); - pd.setPageSize(pageSize); - - try { - - Long dataTotal = appMonitorDao.getDataTotal(appMonitor, userName, null); - pd.setTotal(dataTotal); - - log.info("AppMonitorService.listMyApp success! param appName : {}, userName : {},result Count : {} ", appMonitor.getProjectName(), userName, dataTotal); - - if (dataTotal != null && dataTotal.intValue() > 0) { - List myCareApp = appMonitorDao.getMyOwnerApp(appMonitor, userName, pageNum, pageSize); - pd.setList(myCareApp); - } - - return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), pd); - - } catch (Exception e) { - log.error("AppMonitorService.listMyApp error" + e.getMessage(), e); - return Result.fail(ErrorCode.unknownError); - } - - } +public interface AppMonitorService { + + void appPlatMove(Integer OProjectId, Integer OPlat, Integer NProjectId, Integer Nplat, Integer newIamId, + String NprojectName, Boolean rebuildRule); + + Result selectAppAlarmHealth(AlarmHealthQuery query); + + Result getResourceUsageUrlForK8s(Integer appId, String appName); + + Result initAppsByUsername(String userName); + + List getAppsByUserName(String username); + + Result getProjectInfos(String userName, String appName, Integer page, Integer pageSize); + + Result createWithBaseInfo(AppMonitorModel appMonitorModel, String user); + + Integer createBaseInfo(HeraAppBaseInfoModel heraAppBaseInfo); + + Result create(AppMonitor appMonitor); + + + Result delete(Integer id); + + Result deleteByUser(Integer projectId, Integer appSource, String userName); + + Result>> listApp(String appName, String userName, Integer page, Integer pageSize_); + + Result>> listAppDistinct(String userName, String appName, Integer page, + Integer pageSize_); + + Result>> listMyApp(AppMonitor appMonitor, String userName, Integer page, + Integer pageSize_); + /** * 查询我拥有的或关注的项目列表 * @@ -772,379 +74,31 @@ public Result>> listMyApp(AppMonitor appMonitor, Strin * @param param * @return */ - public Result>> myAndCareAppList(String user, AppMonitorRequest param) { - PageData> pageData = appMonitorDao.getMyAndCareAppList(user, param.getAppName(), param.getPage(), param.getPageSize(), param.isNeedPage()); - return Result.success(pageData); - } - - public Result>> listMyCareApp(String appName, String careUser, Integer page, Integer pageSize_) { - - if (StringUtils.isEmpty(careUser)) { - log.error("AppMonitorService.listMyCareApp param is invalid careUser is empty!"); - return Result.fail(ErrorCode.invalidParamError); - } - - Integer pageNum = page; - Integer pageSize = pageSize_; - - if (pageNum == null || pageNum.intValue() < 1) { - pageNum = 1; - } - - if (pageSize == null) { - pageSize = 10; - } - - PageData pd = new PageData(); - pd.setPage(pageNum); - pd.setPageSize(pageSize); - - try { - - AppMonitor appMonitor = new AppMonitor(); - appMonitor.setProjectName(appName); - Long dataTotal = appMonitorDao.getDataTotal(appMonitor, null, careUser); - pd.setTotal(dataTotal); - - log.info("AppMonitorService.listMyCareApp success! param appName : {}, careUser : {},result Count : {} ", appName, careUser, dataTotal); - - if (dataTotal != null && dataTotal.intValue() > 0) { - List myCareApp = appMonitorDao.getMyCareApp(appName, careUser, pageNum, pageSize); - pd.setList(myCareApp); - } - - return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), pd); - - } catch (Exception e) { - log.error("AppMonitorService.listMyCareApp error" + e.getMessage(), e); - return Result.fail(ErrorCode.unknownError); - } - - } - - public AppMonitor getByIamTreeId(Integer aimTreeId) { - - try { - return appMonitorDao.getByIamTreeId(aimTreeId); - } catch (Exception e) { - log.error("AppMonitorService.getByIamTreeId error! {}", e.getMessage(), e); - return null; - } - } - - public Result getTeslaAlarmHealthByUser(String user){ - return teslaService.getTeslaAlarmHealthByUser(user); - } - - public void washBaseId() { - Integer pageSize = 100; - Integer page = 0; - - Long dataTotalL = appMonitorDao.getDataTotal(new AppMonitor(), null, null); - log.info("washBaseId totalNum:{}", dataTotalL); - Integer dataTotal = dataTotalL.intValue(); - page = dataTotal % pageSize == 0 ? (dataTotal / pageSize) : (dataTotal / pageSize + 1); - - for (int i = 1; i <= page; i++) { - - List allApps = appMonitorDao.getAllApps(i, pageSize); - for (AppMonitor app : allApps) { - - HeraAppBaseInfoModel queryCondition = new HeraAppBaseInfoModel(); - queryCondition.setBindId(String.valueOf(app.getProjectId())); - queryCondition.setPlatformType(app.getAppSource()); - List appBase = heraBaseInfoService.query(queryCondition, 1, 1); - - if (CollectionUtils.isEmpty(appBase)) { - log.info("washBaseId no HeraAppBaseInfo found for app:{}", app.toString()); - continue; - } - app.setBaseInfoId(appBase.get(0).getId()); - int update = appMonitorDao.update(app); - log.info("wash baseId for app:{},result:{}", app.toString(), update); - } - } - } - - public Result grafanaInterfaceList() { - return appMonitorServiceExtension.grafanaInterfaceList(); - } - - public Result selectByIAMId(Integer iamId, Integer iamType, String userName){ - try { - List appMonitors = appMonitorDao.selectByIAMId(iamId, iamType, userName); - return Result.success(appMonitors); - }catch (Throwable t){ - log.error("select by iamId error : ",t); - return Result.fail(ErrorCode.unknownError); - } - } - - public void heraAppInfoModify(HeraAppInfoModifyMessage baseInfoModify) { - - if(HeraAppModifyType.create.equals(baseInfoModify.getModifyType())){ - HeraAppBaseInfoModel appBaseInfoModel = baseInfoModify.baseInfoModel(); - appGrafanaMappingService.createTmpByAppBaseInfo(appBaseInfoModel); - } - - if(HeraAppModifyType.update.equals(baseInfoModify.getModifyType())){ - this.modifyAppAndAlarm(baseInfoModify); - } - - if(HeraAppModifyType.delete.equals(baseInfoModify.getModifyType())){ - this.heraAppDelete(baseInfoModify); - } - } - - private void heraAppDelete(HeraAppInfoModifyMessage message){ - - deleteByBaseInfoId(message.getId()); - - alarmStrategyService.deleteByAppIdAndIamId(message.getAppId(),message.getIamTreeId()); - } - - private void deleteByBaseInfoId(Integer baseInfoId){ - - List appMonitors = appMonitorDao.listAppsByBaseInfoId(baseInfoId); - if(CollectionUtils.isEmpty(appMonitors)){ - log.info("deleteByBaseInfoId no data found! baseInfoId:{}",baseInfoId); - return; - } - - for(AppMonitor appMonitor : appMonitors){ - appMonitorDao.delete(appMonitor.getId()); - } - } - - public void modifyAppAndAlarm(HeraAppInfoModifyMessage baseInfoModify) { - - /** - * appMonitor 信息同步变更 - */ - List appMonitors = appMonitorDao.listAppsByBaseInfoId(baseInfoModify.getId()); - if (!CollectionUtils.isEmpty(appMonitors)) { - appMonitors.forEach(t -> { - t.setAppSource(baseInfoModify.getPlatformType()); - t.setProjectId(baseInfoModify.getAppId()); - t.setIamTreeId(baseInfoModify.getIamTreeId()); - t.setIamTreeType(baseInfoModify.getIamTreeType()); - t.setProjectName(baseInfoModify.getAppName()); - appMonitorDao.update(t); - }); - } - - /** - * appName变更,报警策略和报警规则同步变更 - */ - if (baseInfoModify.getIsNameChange()) { - - AlarmStrategy strategy = new AlarmStrategy(); - strategy.setAppId(baseInfoModify.getAppId()); - strategy.setIamId(baseInfoModify.getIamTreeId()); - - if(!appMonitorServiceExtension.checkAppModifyStrategySearchCondition(baseInfoModify)){ - return; - } - - PageData> listPageData = strategyDao.searchByCondNoUser(strategy, 1, 1000, null, null); - List list = listPageData.getList(); - if (!CollectionUtils.isEmpty(list)) { - list.forEach(t -> { - List rules = ruleDao.selectByStrategyId(t.getId()); - for (AppAlarmRule rule : rules) { - - AppMonitor app = new AppMonitor(); - app.setProjectId(baseInfoModify.getAppId()); - app.setProjectName(baseInfoModify.getAppName()); - - AlarmRuleData ruleData = new AlarmRuleData(); - BeanUtils.copyProperties(rule, ruleData); - ruleData.setLabels(rule.getLabels()); - ruleData.convertLabels(); - - ruleData.setIncludeEnvs(t.getIncludeEnvs()); - ruleData.setExceptEnvs(t.getExceptEnvs()); - ruleData.setIncludeZones(t.getIncludeZones()); - ruleData.setExceptZones(t.getExceptZones()); - - ruleData.setIncludeContainerName(t.getIncludeContainerName()); - ruleData.setExceptContainerName(t.getExceptContainerName()); - ruleData.setAlertMembers(t.getAlertMembers()); - ruleData.setAtMembers(t.getAtMembers()); - - if (!CollectionUtils.isEmpty(t.getIncludeFunctions())) { - ruleData.setIncludeFunctions(t.getIncludeFunctions().stream().map(String::valueOf).collect(Collectors.toList())); - } - - if (!CollectionUtils.isEmpty(t.getExceptFunctions())) { - ruleData.setExceptFunctions(t.getExceptFunctions().stream().map(String::valueOf).collect(Collectors.toList())); - } - - ruleData.setIncludeModules(t.getIncludeModules()); - ruleData.setExceptModules(t.getExceptModules()); - - Result result = alarmService.editRule(rule, ruleData, app, rule.getCreater()); - if(!result.isSuccess()){ - log.error("heraAppInfoModify fail! rule : {} , result : {}",rule.toString(),new Gson().toJson(result)); - continue; - } - - int i = ruleDao.updateByIdSelective(rule); - if (i < 1) { - log.error("heraAppInfoModify update rule db fail! rule{}", rule.toString()); - } - - } - - AlarmStrategy strategyUp = new AlarmStrategy(); - strategyUp.setId(t.getId()); - strategyUp.setAppId(baseInfoModify.getAppId()); - strategyUp.setAppName(baseInfoModify.getAppName()); - strategyUp.setIamId(baseInfoModify.getIamTreeId()); - boolean b = strategyDao.updateById(strategyUp); - if (!b) { - log.error("heraAppInfoModify update strategy fail! old:{},new:{}", t.toString(), strategyUp.toString()); - } - - }); - } - } - } - - public void washBugData(){ - AlarmStrategy strategy = new AlarmStrategy(); - PageData> listPageData = strategyDao.searchByCondNoUser(strategy, 1, 1000, null, null); - List list = listPageData.getList(); - if (!CollectionUtils.isEmpty(list)) { - list.forEach(t -> { - List rules = ruleDao.selectByStrategyId(t.getId()); - - if(!CollectionUtils.isEmpty(rules)){ - AppAlarmRule rule = rules.get(0); - AppMonitor appMonitor = appMonitorDao.getByIamTreeIdAndAppId(rule.getIamId(), rule.getProjectId()); - - if(appMonitor != null){ - - AlarmStrategy strategyUp = new AlarmStrategy(); - strategyUp.setId(t.getId()); - strategyUp.setAppId(appMonitor.getProjectId()); - strategyUp.setAppName(appMonitor.getProjectName()); - strategyUp.setIamId(appMonitor.getIamTreeId()); - boolean b = strategyDao.updateById(strategyUp); - if (!b) { - log.error("heraAppInfoModify update strategy fail! old:{},new:{}", t.toString(), strategyUp.toString()); - } - - for (AppAlarmRule rule1 : rules) { - - AlarmRuleData ruleData = new AlarmRuleData(); - BeanUtils.copyProperties(rule1, ruleData); - ruleData.setLabels(rule1.getLabels()); - ruleData.convertLabels(); - - ruleData.setIncludeEnvs(t.getIncludeEnvs()); - ruleData.setExceptEnvs(t.getExceptEnvs()); - ruleData.setIncludeZones(t.getIncludeZones()); - ruleData.setExceptZones(t.getExceptZones()); - ruleData.setIncludeContainerName(t.getIncludeContainerName()); - ruleData.setExceptContainerName(t.getExceptContainerName()); - -// ruleData.setIncludeZones(t.getIncludeEnvs()); -// ruleData.setExceptZones(t.getExceptEnvs()); - ruleData.setAlertMembers(t.getAlertMembers()); - ruleData.setAtMembers(t.getAtMembers()); - - if (!CollectionUtils.isEmpty(t.getIncludeFunctions())) { - ruleData.setIncludeFunctions(t.getIncludeFunctions().stream().map(String::valueOf).collect(Collectors.toList())); - } - - if (!CollectionUtils.isEmpty(t.getExceptFunctions())) { - ruleData.setExceptFunctions(t.getExceptFunctions().stream().map(String::valueOf).collect(Collectors.toList())); - } - - ruleData.setIncludeModules(t.getIncludeModules()); - ruleData.setExceptModules(t.getExceptModules()); - - Result result = alarmService.editRule(rule1, ruleData, appMonitor, rule1.getCreater()); - if(!result.isSuccess()){ - log.error("washBugData fail! rule1 : {} , result : {}",rule1.toString(),new Gson().toJson(result)); - continue; - } - - } - } - - - } - - - }); - } - } - - public void washBugDataForAppMonitor(){ - List allApps = appMonitorDao.getAllApps(1, 5000); - for(AppMonitor appMonitor : allApps){ - if(appMonitor.getBaseInfoId() == null){ - log.error("update appMonitor no baseId found! appMonitor : {}" ,appMonitor.toString()); - continue; - } - HeraAppBaseInfoModel byId = heraBaseInfoService.getById(appMonitor.getBaseInfoId()); - - if(byId == null || StringUtils.isBlank(byId.getBindId())){ - log.error("update appMonitor HeraAppBaseInfo error! appMonitor : {}" ,appMonitor.toString()); - continue; - } - - try { - appMonitor.setProjectId(Integer.valueOf(byId.getBindId())); - appMonitor.setIamTreeId(byId.getIamTreeId()); - int update = appMonitorDao.update(appMonitor); - if(update < 1){ - log.error("update appMonitor fail! appMonitor : {}" ,appMonitor.toString()); - } - } catch (NumberFormatException e) { - log.error("update appMonitor error!" + e.getMessage(),e); - continue; - } - } - } - - public Result historyInstance(String application,Long startTime, Long endTime) { - String promql = "count(jvm_classes_loaded_classes{application=\""+ application +"\"}) by (serverIp)"; - log.info("historyInstance promql : {}",promql); - MetricResponse rangeMetricResponse = prometheusService.queryRangePrometheusByPromQl(promql, startTime, endTime, - null,null); - if(rangeMetricResponse == null || rangeMetricResponse.getData() == null){ - return Result.fail(ErrorCode.unknownError); - } - MetricData rangeMetricData = rangeMetricResponse.getData(); - List rangeResult = rangeMetricData.getResult(); - List rangeIps = new ArrayList<>(); - rangeResult.forEach(t -> { - rangeIps.add(t.getMetric().getServerIp()); - }); - // first time no comparison -// List momentIps = new ArrayList<>(); -// MetricResponse momentMetricResponse = prometheusService.queryRangePrometheusByPromQl(promql, startTime, endTime, null,PrometheusService.MOMENT_REQUEST_MODE); -// if(momentMetricResponse == null || momentMetricResponse.getData() == null){ -// return Result.fail(ErrorCode.unknownError); -// } -// MetricData momentMetricData = momentMetricResponse.getData(); -// List momentResult = momentMetricData.getResult(); -// momentResult.forEach(t -> { -// momentIps.add(t.getMetric().getServerIp()); -// }); -// //The difference between momentIps and rangeIps -// List ips = rangeIps.stream().filter(item -> !momentIps.contains(item)).collect(Collectors.toList()); - return Result.success(rangeIps); - } - - public Long countByBaseInfoId(List baseInfoIds,String user){ - return appMonitorDao.countByBaseInfoIds(baseInfoIds,user); - } - - public List searchByBaseInfoId(List baseInfoIds,String user,Integer page,Integer pageSize){ - return appMonitorDao.getDataByBaseInfoIds(baseInfoIds,user,page,pageSize); - } + Result>> myAndCareAppList(String user, AppMonitorRequest param); + + Result>> listMyCareApp(String appName, String careUser, Integer page, Integer pageSize_); + + AppMonitor getByIamTreeId(Integer aimTreeId); + + Result getTeslaAlarmHealthByUser(String user); + + void washBaseId(); + + Result grafanaInterfaceList(); + + Result selectByIAMId(Integer iamId, Integer iamType, String userName); + + void heraAppInfoModify(HeraAppInfoModifyMessage baseInfoModify); + + void modifyAppAndAlarm(HeraAppInfoModifyMessage baseInfoModify); + + void washBugData(); + + void washBugDataForAppMonitor(); + + Result historyInstance(String application, Long startTime, Long endTime); + + Long countByBaseInfoId(List baseInfoIds, String user); + + List searchByBaseInfoId(List baseInfoIds, String user, Integer page, Integer pageSize); } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ComputeTimerService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ComputeTimerService.java index b049eb2c5..ebad3378f 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ComputeTimerService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ComputeTimerService.java @@ -13,219 +13,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.google.common.collect.Lists; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.api.ComputeTimerServiceExtension; -import com.xiaomi.mone.monitor.service.helper.AlertHelper; import com.xiaomi.mone.monitor.service.model.AppMonitorRequest; import com.xiaomi.mone.monitor.service.model.ProjectInfo; -import com.xiaomi.mone.monitor.service.model.prometheus.MetricKind; import com.xiaomi.mone.monitor.service.model.redis.AppAlarmData; -import com.xiaomi.mone.monitor.service.prometheus.PrometheusService; -import com.xiaomi.mone.monitor.utils.CommonUtil; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; -import javax.annotation.PreDestroy; -import java.util.ArrayList; import java.util.List; -import java.util.Optional; -import java.util.concurrent.Callable; -import java.util.concurrent.Future; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; /** * @author gaoxihui * @date 2021/8/17 10:08 AM */ -@Slf4j -@Service -public class ComputeTimerService { - - private final ThreadPoolExecutor executor; - - private final ThreadPoolExecutor metricExecutor; - - @Autowired - PrometheusService prometheusService; -// @Autowired -// private RedisHelper redisHelper; - @Autowired - private AlertHelper alertHelper; - - @Autowired - private ComputeTimerServiceExtension computeTimerServiceExtension; - - public ComputeTimerService() { - executor = new ThreadPoolExecutor(10, 50, 5, TimeUnit.MINUTES, new LinkedBlockingQueue(100), - (Runnable r) -> new Thread(r, "compute-execute-thread-v2"), new ThreadPoolExecutor.CallerRunsPolicy()); - - metricExecutor = new ThreadPoolExecutor(50, 100, 5, TimeUnit.MINUTES, new LinkedBlockingQueue(1000), - (Runnable r) -> new Thread(r, "compute-metric-thread-v2"), new ThreadPoolExecutor.CallerRunsPolicy()); - } - - @PreDestroy - public void destory() { - executor.shutdownNow(); - metricExecutor.shutdownNow(); - } - - public Result> getProjectStatistics(AppMonitorRequest param) { - final Long endTime = CommonUtil.toSeconds(System.currentTimeMillis()); - final Long startTime = endTime - param.getDuration(); - final String timeDurarion = param.getDuration() + "s"; - final Long step = param.getDuration(); - int projectSize = param.getProjectList().size() > 50 ? 50 : param.getProjectList().size(); - List> callList = new ArrayList<>(); - for (int idx = 0; idx < projectSize; idx++) { - final ProjectInfo project = param.getProjectList().get(idx); - if (project.getId() == null || StringUtils.isBlank(project.getName()) || project.getIamTreeId() == null) { - continue; - } - callList.add(new Callable() { - @Override - public AppAlarmData call() throws Exception { - return getAppAlarmData(project, startTime, endTime, timeDurarion, step, param); - } - }); - } - List dataList = new ArrayList<>(); - try { - List> futures = executor.invokeAll(callList, 30, TimeUnit.SECONDS); - if (!CollectionUtils.isEmpty(futures)) { - AppAlarmData data = null; - for (Future future : futures) { - data = future.get(); - if (data == null) { - continue; - } - dataList.add(data); - } - } - } catch (Exception e) { - log.info("ComputeTimerServiceV2.executor.invokeAll异常 param={}, ", param, e); - } - return Result.success(dataList); - } - +public interface ComputeTimerService { + + + void destory(); + + Result> getProjectStatistics(AppMonitorRequest param); + /** - * * @param project * @param startTime * @param endTime * @param timeDurarion * @param param */ - public AppAlarmData getAppAlarmData(ProjectInfo project, Long startTime, Long endTime, String timeDurarion, Long step, AppMonitorRequest param) { - String appName = new StringBuilder().append(project.getId()).append("_").append(project.getName().replaceAll("-","_")).toString(); - - AppAlarmData data = null; -// AppAlarmData data = redisHelper.getAppAlarmData(appName); -// if (data != null) { -// log.info("ComputeTimerServiceV2.getAppAlarmData cache-result appName={}, data={}", appName, data); -// return data; -// } - - AppAlarmData.AppAlarmDataBuilder dataBuilder = AppAlarmData.builder(); - dataBuilder.id(project.getId()).name(project.getName()).iamTreeId(project.getIamTreeId()); - dataBuilder.startTime(startTime).endTime(endTime); - List kinds = Lists.newArrayList(MetricKind.http,MetricKind.dubbo, MetricKind.grpc, MetricKind.apus, MetricKind.thrift, MetricKind.db, MetricKind.redis); - List> callList = kinds.stream().map(kind -> { - return new Callable() { - @Override - public Void call() throws Exception { - computByMetricType(param, appName, kind, dataBuilder,startTime,endTime,timeDurarion, step); - return null; - } - }; - }).collect(Collectors.toList()); - try { - List> futureList = metricExecutor.invokeAll(callList); - if (param == null) { - dataBuilder.alertTotal(alertHelper.queryAlertEventNum(project.getId().intValue(), project.getIamTreeId().intValue(), startTime, endTime)); - } - futureList.stream().forEach(f -> { - try { - f.get(); - } catch (Throwable e) { - log.warn("metric统计查询异步get结果异常", project.getName(), e); - } - }); - } catch (Throwable e) { - log.error("metric统计异步查询异常 projectName={}", project.getName(), e); - } - dataBuilder.logExceptionNum(0); - data = dataBuilder.build(); - calExceptionTotal(data); - calSlowQueryTotal(data); - log.info("ComputeTimerServiceV2.getAppAlarmData new-result appName={}, data={}", appName, data); -// redisHelper.setAppAlarmData(appName, data); - return data; - } - - private void computByMetricType(AppMonitorRequest param, String appName,MetricKind metricKind, AppAlarmData.AppAlarmDataBuilder dataBuilder,Long startTime, Long endTime, String timeDurarion, Long step){ - computeTimerServiceExtension.computByMetricType(param, appName, metricKind, dataBuilder, startTime, endTime, timeDurarion, step); - } - - public AppAlarmData countAppMetricData(AppMonitorRequest param) { - ProjectInfo project = param.getProjectList().get(0); - Long startTime = param.getStartTimeCurrent(); - Long endTime = param.getEndTimeCurrent(); - Long step = endTime - startTime; - return getAppAlarmData(project, startTime, endTime, step + "s", step, param); - } - - - /** - * 计算异常总数 - * @return - */ - private void calExceptionTotal(AppAlarmData data) { - Integer exceptionTotal = 0; - exceptionTotal += Optional.ofNullable(data.getHttpExceptionNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getHttpClientExceptionNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getDubboExceptionNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getDubboPExceptionNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getSqlExceptionNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getOracleExceptionNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getRedisExceptionNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getGrpcServerErrorNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getGrpcClientErrorNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getApusServerErrorNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getApusClientErrorNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getThriftServerErrorNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getThriftClientErrorNum()).orElse(0); - exceptionTotal += Optional.ofNullable(data.getEsExceptionNum()).orElse(0); - data.setExceptionTotal(exceptionTotal); - } - /** - * 计算慢查询总数 - * @return - */ - private void calSlowQueryTotal(AppAlarmData data) { - Integer slowQueryTotal = 0; - slowQueryTotal += Optional.ofNullable(data.getHttpSlowNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getHttpClientSlowNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getDubboCSlowQueryNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getDubboProviderSlowQueryNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getSqlSlowQueryNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getOracleSlowNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getRedisSlowNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getGrpcClientSlowQueryNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getGrpcServerSlowQueryNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getApusClientSlowQueryNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getApusServerSlowQueryNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getThriftClientSlowQueryNum()).orElse(0); - slowQueryTotal += Optional.ofNullable(data.getEsSlowNum()).orElse(0); - data.setSlowTotal(slowQueryTotal); - } - + AppAlarmData getAppAlarmData(ProjectInfo project, Long startTime, Long endTime, String timeDurarion, Long step, + AppMonitorRequest param); + + + AppAlarmData countAppMetricData(AppMonitorRequest param); + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/GrafanaBlackListService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/GrafanaBlackListService.java index ef9b55fea..c7ccceacc 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/GrafanaBlackListService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/GrafanaBlackListService.java @@ -13,73 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.xiaomi.mone.monitor.dao.GrafanaBlackListDao; -import com.xiaomi.mone.monitor.dao.model.AppGrafanaBlackList; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.model.PageData; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.List; - -@Service -@Slf4j -public class GrafanaBlackListService { - - @Autowired - GrafanaBlackListDao grafanaBlackListDao; - - public Result createBlackList(String serverName) { - //插库 - List blackListByServerName = grafanaBlackListDao.getBlackListByServerName(serverName); - if (blackListByServerName.size() >= 1) { - return Result.fail(ErrorCode.REPEAT_ADD_PROJECT); - } - Integer blackList = grafanaBlackListDao.createBlackList(serverName); - if (blackList == null || blackList == -1 || blackList == 0) { - log.error("GrafanaBlackListService.createBlackList error"); - return Result.fail(ErrorCode.unknownError); - } - return Result.success(blackList); - } - - public Result getBlackList(String serverName) { - //查库 - AppGrafanaBlackList blackList = grafanaBlackListDao.getBlackList(serverName); - return Result.success(blackList); - } - - public Result delBlackList(String serverName) { - //删除 - Integer res = grafanaBlackListDao.delBlackListByServerName(serverName); - if (res == null || res == 0 || res == -1) { - log.error("GrafanaBlackListService.delBlackList error"); - return Result.fail(ErrorCode.unknownError); - } - return Result.success(res); - } - - public Result getBlackListList(Integer page,Integer pageSize) { - //获取列表 - PageData pd = new PageData(); - pd.setPage(page); - pd.setPageSize(pageSize); - pd.setTotal(grafanaBlackListDao.getTotalBlackList()); - pd.setList(grafanaBlackListDao.getAllBlackList(page, pageSize)); - return Result.success(pd); - } - - public boolean isInBlackList(String serverName) { - List blackListByServerName = grafanaBlackListDao.getBlackListByServerName(serverName); - if (blackListByServerName.size() >= 1) { - return true; - } - return false; - } +public interface GrafanaBlackListService { + + + Result createBlackList(String serverName); + + Result getBlackList(String serverName); + + Result delBlackList(String serverName); + + Result getBlackListList(Integer page, Integer pageSize); + + boolean isInBlackList(String serverName); + + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/GrafanaService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/GrafanaService.java index 52b9e07c4..4f7049575 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/GrafanaService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/GrafanaService.java @@ -13,74 +13,49 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.google.gson.*; +import com.google.gson.JsonArray; import com.xiaomi.mone.monitor.dao.model.GrafanaTemplate; -import com.xiaomi.mone.monitor.service.api.GrafanaServiceExtension; import com.xiaomi.mone.monitor.service.model.MutiGrafanaResponse; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import java.util.*; + +import java.util.List; +import java.util.Map; /** * @author zhangxiaowei6 */ -@Slf4j -@Service -public class GrafanaService { - @Autowired - GrafanaServiceExtension grafanaServiceExtension; - - public void setFolderData(String area) { - grafanaServiceExtension.setFolderData(area); - } - - public void setContainerAndHostUrl(String area) { - grafanaServiceExtension.setContainerAndHostUrl(area); - } - - public String requestGrafana(String serverType, String appName, String area) { - return grafanaServiceExtension.requestGrafana(serverType, appName, area); - } - - public MutiGrafanaResponse requestGrafanaTemplate(String group, String title, String area, GrafanaTemplate template, List funcList) { - return grafanaServiceExtension.requestGrafanaTemplate(group, title, area, template, funcList); - } - - public Map beforeRequestGrafana(String area, String title) { - return grafanaServiceExtension.beforeRequestGrafana(area, title); - } - - public String innerRequestGrafanaStr(String area, String title, String containerName, String group, GrafanaTemplate template, String application) { - return grafanaServiceExtension.innerRequestGrafanaStr(area, title, containerName, group, template, application); - } - +public interface GrafanaService { + + void setFolderData(String area); + + void setContainerAndHostUrl(String area); + + String requestGrafana(String serverType, String appName, String area); + + MutiGrafanaResponse requestGrafanaTemplate(String group, String title, String area, GrafanaTemplate template, + List funcList); + + Map beforeRequestGrafana(String area, String title); + + String innerRequestGrafanaStr(String area, String title, String containerName, String group, + GrafanaTemplate template, String application); + //Get grafana template variables - private Map getTemplateVariables(String folderId, String group, String title, String folderUid, String grafanaUrl, String containerName, String area, String application) { - return grafanaServiceExtension.getTemplateVariables(folderId, group, title, folderUid, grafanaUrl, containerName, area, application); - } - + Map getTemplateVariables(String folderId, String group, String title, String folderUid, + String grafanaUrl, String containerName, String area, String application); + //Replace the base panel and keep the user-defined panel - private String getFinalData(String data, String url, String apiKey, String method, String title, String panelIdList) { - return grafanaServiceExtension.getFinalData(data, url, apiKey, method, title, panelIdList, false, null); - } - - private String innerRequestGrafana(String data, String url, String apiKey, String method) { - return grafanaServiceExtension.innerRequestGrafana(data, url, apiKey, method); - } - - public void getCustomPanels(String grafanaStr, JsonArray basicPanels, int basicDiyPanelGirdPosY, String title, String panelIdList) { - grafanaServiceExtension.getCustomPanels(grafanaStr, basicPanels, basicDiyPanelGirdPosY, title, panelIdList); - } - + String getFinalData(String data, String url, String apiKey, String method, String title, String panelIdList); + + String innerRequestGrafana(String data, String url, String apiKey, String method); + + void getCustomPanels(String grafanaStr, JsonArray basicPanels, int basicDiyPanelGirdPosY, String title, + String panelIdList); + //Determine whether the request result of generating/updating the grafana graph is json in the specific format of grafana - private String isGrafanaDataJson(String jobJson) { - return grafanaServiceExtension.isGrafanaDataJson(jobJson); - } - - private String getDashboardLastVersion(String dashboardId) { - return grafanaServiceExtension.getDashboardLastVersion(dashboardId); - } + String isGrafanaDataJson(String jobJson); + + String getDashboardLastVersion(String dashboardId); } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/HeraBaseInfoService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/HeraBaseInfoService.java index 2a1368f2b..e6c4601aa 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/HeraBaseInfoService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/HeraBaseInfoService.java @@ -13,423 +13,61 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.google.common.collect.Lists; -import com.google.gson.Gson; import com.xiaomi.mone.app.api.model.HeraAppBaseInfoModel; -import com.xiaomi.mone.app.api.model.HeraAppBaseInfoParticipant; import com.xiaomi.mone.app.api.model.HeraAppRoleModel; -import com.xiaomi.mone.app.api.service.HeraAppService; -import com.xiaomi.mone.monitor.bo.AlertGroupParam; -import com.xiaomi.mone.monitor.bo.PlatForm; -import com.xiaomi.mone.monitor.bo.UserInfo; -import com.xiaomi.mone.monitor.dao.HeraAppRoleDao; -import com.xiaomi.mone.monitor.dao.model.HeraAppRole; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.extension.PlatFormTypeExtensionService; -import com.xiaomi.mone.monitor.service.model.Area; -import com.xiaomi.mone.monitor.service.model.EnvMapping; import com.xiaomi.mone.monitor.service.model.HeraAppBaseQuery; -import com.xiaomi.mone.monitor.service.model.PageData; -import com.xiaomi.mone.monitor.service.model.Region; -import com.xiaomi.mone.tpc.login.util.UserUtil; -import com.xiaomi.mone.tpc.login.vo.AuthUserVo; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.apache.dubbo.config.annotation.Reference; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; -import java.util.ArrayList; -import java.util.Date; import java.util.List; -import java.util.stream.Collectors; /** * @author gaoxihui * @date 2022/3/24 4:51 PM */ -@Slf4j -@Service -public class HeraBaseInfoService { - - @Autowired - HeraAppRoleDao heraAppRoleDao; - @Autowired - AlertGroupService alertGroupService; - - @Reference(registry = "registryConfig", check = false, interfaceClass = HeraAppService.class, group = "${dubbo.group.heraapp}",timeout = 5000) - HeraAppService hearAppService; - - @Autowired - PlatFormTypeExtensionService platFormTypeExtensionService; - - public Result addRole(HeraAppRoleModel model) { - Integer integer = hearAppService.addRole(model); - - log.info("addRole param:{},result:{}", model.toString(), integer); - - if (integer.intValue() > 0) { - return Result.success(); - } - - return Result.fail(ErrorCode.unknownError); - } - - public Result delRole(Integer id) { - - Integer i = hearAppService.delRoleById(id); - if (i.intValue() > 0) { - return Result.success(); - } - - return Result.fail(ErrorCode.unknownError); - - } - - public Result queryRole(HeraAppRoleModel model, Integer pageNo, Integer pageCount) { - - if (pageNo == null || pageNo.intValue() < 1) { - pageNo = 1; - } - if (pageCount == null || pageCount.intValue() < 1) { - pageCount = 10; - } - - Long aLong = hearAppService.countRole(model); - - PageData pd = new PageData(); - pd.setPage(pageNo); - pd.setPageSize(pageCount); - pd.setTotal(aLong); - - if (aLong == null || aLong.intValue() == 0) { - log.info("查询hera app角色没有数据,param:{}", model.toString()); - return Result.success(pd); - } - - List heraAppRoleModels = hearAppService.queryRole(model, pageNo, pageCount); - - pd.setList(heraAppRoleModels); - - return Result.success(pd); - - } - - public String getArea(String bindId, Integer plat, String regionSign) { - - HeraAppBaseInfoModel appBaseInfo = this.getAppByBindId(bindId, plat); - - log.info("getArea#appBaseInfo :{},", appBaseInfo.toString()); - - if (platFormTypeExtensionService.belongPlatForm(appBaseInfo.getPlatformType(), PlatForm.miCloud)) { - String envsMap = appBaseInfo.getEnvsMap(); - if (StringUtils.isBlank(envsMap)) { - return null; - } - EnvMapping envMapping = new Gson().fromJson(envsMap, EnvMapping.class); - log.info("getArea# appId:{},regionSign:{}, envMapping:{}", bindId, regionSign, envMapping.toString()); - if (envMapping == null || CollectionUtils.isEmpty(envMapping.getAreas())) { - return null; - } - - List areas = envMapping.getAreas(); - for (Area area : areas) { - List regions = area.getRegions(); - if (CollectionUtils.isEmpty(regions)) { - log.info("getArea,no regions found!bindId:{}", bindId); - return null; - } - //这里的regionSign对应的是region的name - for (Region region1 : regions) { - if (region1.getName().equals(regionSign)) { - return area.getName(); - } - - } - } - } - - return null; - - } - - public Result queryByParticipant(HeraAppBaseQuery query) { - - //适配到远程查询 - return queryByParticipantRemote(query); - - } - - public HeraAppBaseInfoModel getById(Integer id) { - return this.getByIdRemote(id); - } - - public void deleAppById(Integer id) { - - HeraAppBaseInfoModel baseInfoModel = this.getById(id); - - Integer integer = this.deleteByIdRemote(id); - if (integer.intValue() > 0) { - log.info("deleAppById sucess!dataId:{}", id); - } else { - log.info("deleAppById fail!dataId:{}", id); - } - - - HeraAppRole role = new HeraAppRole(); - role.setAppId(baseInfoModel.getBindId()); - List roles = heraAppRoleDao.query(role, 1, 1000); - if (!CollectionUtils.isEmpty(roles)) { - for (HeraAppRole roleTmp : roles) { - Integer integer1 = heraAppRoleDao.delById(roleTmp.getId()); - if (integer1.intValue() > 0) { - log.info("del HeraAppRole AppById sucess!dataId:{}", id); - } else { - log.info("del HeraAppRole AppById fail!dataId:{}", id); - } - } - } - } - - public HeraAppBaseInfoModel getByBindIdAndPlat(String bindId, Integer plat){ - HeraAppBaseInfoModel query = new HeraAppBaseInfoModel(); - query.setBindId(bindId); - query.setPlatformType(plat); - - List list = this.query(query, null, null); - - if (CollectionUtils.isEmpty(list)) { - log.info("getByBindIdAndPlat no data found! bindId:{},plat:{}", bindId, plat); - return null; - } - return list.get(0); - } - - public void deleAppByBindIdAndPlat(String bindId, Integer plat) { - - if (StringUtils.isBlank(bindId) || plat == null) { - log.error("invalid param,bindId:{},plat:{}", bindId, plat); - return; - } - - HeraAppBaseInfoModel query = new HeraAppBaseInfoModel(); - query.setBindId(bindId); - query.setPlatformType(plat); - - List list = this.query(query, null, null); - - if (CollectionUtils.isEmpty(list)) { - log.info("deleAppByBindIdAndPlat no data found! bindId:{},plat:{}", bindId, plat); - } - - for (HeraAppBaseInfoModel baseInfo : list) { - Integer integer = hearAppService.delById(baseInfo.getId()); - if (integer.intValue() > 0) { - log.info("deleAppByBindIdAndPlat success!baseInfo:{}", new Gson().toJson(baseInfo)); - } else { - log.error("deleAppByBindIdAndPlat success!baseInfo:{}", new Gson().toJson(baseInfo)); - } - } - - } - - public HeraAppBaseInfoModel getByBindIdAndName(String bindId, String appName) { - if (StringUtils.isBlank(bindId) || StringUtils.isBlank(appName)) { - log.error("getByBindIdAndName invalid param,bindId:{},appName:{}", bindId, appName); - return null; - } - - HeraAppBaseInfoModel query = new HeraAppBaseInfoModel(); - query.setBindId(bindId); - query.setAppName(appName); - - List list = this.query(query, null, null); - - if (CollectionUtils.isEmpty(list)) { - log.info("HeraAppBaseInfo#getByBindIdAndName no data found,bindId:{}", bindId); - return null; - } - - return list.get(0); - - } - - public HeraAppBaseInfoModel getAppByBindId(String bindId, Integer platFromType) { - - if (StringUtils.isBlank(bindId)) { - log.error("invalid param,bindId:{}", bindId); - return null; - } - - HeraAppBaseInfoModel query = new HeraAppBaseInfoModel(); - query.setBindId(bindId); - query.setPlatformType(platFromType); - - List list = this.query(query, null, null); - - if (CollectionUtils.isEmpty(list)) { - log.info("HeraAppBaseInfo#getAppByBindId no data found,bindId:{}", bindId); - return null; - } - - return list.get(0); - - } - - public Result getAppMembersByAppId(String appId, Integer platForm, String user) { - HeraAppRole role = new HeraAppRole(); - role.setAppId(appId); - role.setAppPlatform(platForm); - List roles = heraAppRoleDao.query(role, 1, 1000); - log.info("HeraBaseInfoService#getAppMembersByAppId appId:{}, platForm:{},result:{}",appId,platForm,new Gson().toJson(roles)); - - PageData pageData = new PageData<>(); - pageData.setTotal(0l); - - if (CollectionUtils.isEmpty(roles)) { - log.info("getAppMembersByAppId no data found!appId:{},platForm:{}", appId, platForm); - return Result.success(pageData); - } - - List members = roles.stream().filter(t -> StringUtils.isNotBlank(t.getUser())).map(t1 -> t1.getUser()).collect(Collectors.toList()); - - if (CollectionUtils.isEmpty(members)) { - return Result.success(pageData); - } - - List userList = Lists.newArrayList(); - - AlertGroupParam param = new AlertGroupParam(); - param.setPage(1); - param.setPageSize(50); - - for (String userName : members) { - param.setName(userName); - Result>> pageDataResult = alertGroupService.userSearch(user, param); - log.info("alertGroupService#userSearch userName:{}, result:{}",userName,new Gson().toJson(pageDataResult)); - - AuthUserVo userVoSearch = UserUtil.parseFullAccount(userName); - - String compUser = userVoSearch == null ? userName : StringUtils.isBlank(userVoSearch.getAccount()) ? userName : userVoSearch.getAccount(); - - if (pageDataResult.getData().getTotal().intValue() > 0) { - userList.addAll(pageDataResult.getData().getList().stream().filter(t -> compUser.equals(t.getName())).collect(Collectors.toList())); - } - } - - pageData.setList(userList); - pageData.setTotal(Long.valueOf(userList.size())); - - return Result.success(pageData); - } - - public Long count(HeraAppBaseInfoModel baseInfo) { - - return this.countRemote(baseInfo); - } - - public List query(HeraAppBaseInfoModel baseInfo, Integer pageCount, Integer pageNum) { - return this.queryRemote(baseInfo, pageCount, pageNum); - } - - public List queryRemote(HeraAppBaseInfoModel baseInfo, Integer pageCount, Integer pageNum) { - - List baseInfoModels = hearAppService.query(baseInfo, pageCount, pageNum); - if (CollectionUtils.isEmpty(baseInfoModels)) { - return Lists.newArrayList(); - } - - return baseInfoModels; - - } - - public Long countRemote(HeraAppBaseInfoModel baseInfo) { - return hearAppService.count(baseInfo); - - } - - public HeraAppBaseInfoModel getByIdRemote(Integer id) { - HeraAppBaseInfoModel baseInfoModel = hearAppService.getById(id); - if (baseInfoModel == null) { - return null; - } - - return baseInfoModel; - } - - public int deleteByIdRemote(Integer id) { - - return hearAppService.delById(id); - - } - - public Result queryByParticipantRemote(HeraAppBaseQuery query) { - - - com.xiaomi.mone.app.api.model.HeraAppBaseQuery queryRemote = new com.xiaomi.mone.app.api.model.HeraAppBaseQuery(); - BeanUtils.copyProperties(query, queryRemote); - - //MyParticipant只有值为yes才查询我参与的应用,传其他值均查询所有 - if (StringUtils.isBlank(query.getMyParticipant()) || !"yes".equals(query.getMyParticipant())) { - query.setMyParticipant(null); - } - - PageData pd = new PageData(); - - Long aLong = hearAppService.countByParticipant(queryRemote); - log.info("queryByParticipantRemote#countByParticipant count : {}",aLong); - pd.setTotal(aLong); - pd.setPage(query.getPage()); - pd.setPageSize(query.getPageSize()); - - if (aLong != null && aLong.intValue() > 0) { - List list = new ArrayList<>(); - List heraAppBaseInfoParticipants = hearAppService.queryByParticipant(queryRemote); - log.info("queryByParticipantRemote#queryByParticipant result : {}",new Gson().toJson(heraAppBaseInfoParticipants)); - if (!CollectionUtils.isEmpty(heraAppBaseInfoParticipants)) { - heraAppBaseInfoParticipants.forEach(t -> { - HeraAppBaseInfoParticipant heraAppBaseInfoParticipant = new HeraAppBaseInfoParticipant(); - BeanUtils.copyProperties(t, heraAppBaseInfoParticipant); - list.add(heraAppBaseInfoParticipant); - }); - } - - pd.setList(list); - } - - return Result.success(pd); - - } - - - public int insertOrUpdate(HeraAppBaseInfoModel heraAppBaseInfo) { - if (null == heraAppBaseInfo) { - log.error("[HeraBaseInfoDao.create] null heraAppBaseInfo"); - return 0; - } - - heraAppBaseInfo.setCreateTime(new Date()); - heraAppBaseInfo.setUpdateTime(new Date()); - heraAppBaseInfo.setStatus(0); - - heraAppBaseInfo.setAppSignId(heraAppBaseInfo.getBindId() + "-" + heraAppBaseInfo.getPlatformType()); - - try { - int affected = hearAppService.insertOrUpdate(heraAppBaseInfo); - if (affected < 1) { - log.warn("[HeraBaseInfoDao.create] failed to insert heraAppBaseInfo: {}", heraAppBaseInfo.toString()); - return 0; - } - } catch (Exception e) { - log.error("[HeraBaseInfoDao.create] failed to insert heraAppBaseInfo: {}, err: {}", heraAppBaseInfo.toString(), e); - return 0; - } - return 1; - } - +public interface HeraBaseInfoService { + + Result addRole(HeraAppRoleModel model); + + Result delRole(Integer id); + + Result queryRole(HeraAppRoleModel model, Integer pageNo, Integer pageCount); + + String getArea(String bindId, Integer plat, String regionSign); + + Result queryByParticipant(HeraAppBaseQuery query); + + HeraAppBaseInfoModel getById(Integer id); + + void deleAppById(Integer id); + + HeraAppBaseInfoModel getByBindIdAndPlat(String bindId, Integer plat); + + void deleAppByBindIdAndPlat(String bindId, Integer plat); + + HeraAppBaseInfoModel getByBindIdAndName(String bindId, String appName); + + HeraAppBaseInfoModel getAppByBindId(String bindId, Integer platFromType); + + Result getAppMembersByAppId(String appId, Integer platForm, String user); + + Long count(HeraAppBaseInfoModel baseInfo); + + List query(HeraAppBaseInfoModel baseInfo, Integer pageCount, Integer pageNum); + + List queryRemote(HeraAppBaseInfoModel baseInfo, Integer pageCount, Integer pageNum); + + Long countRemote(HeraAppBaseInfoModel baseInfo); + + HeraAppBaseInfoModel getByIdRemote(Integer id); + + int deleteByIdRemote(Integer id); + + Result queryByParticipantRemote(HeraAppBaseQuery query); + + + int insertOrUpdate(HeraAppBaseInfoModel heraAppBaseInfo); + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/HeraDashboardService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/HeraDashboardService.java index b0c3ebf83..f30980b35 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/HeraDashboardService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/HeraDashboardService.java @@ -13,416 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.alibaba.nacos.api.NacosFactory; -import com.alibaba.nacos.api.config.ConfigService; -import com.alibaba.nacos.api.config.annotation.NacosValue; -import com.alibaba.nacos.api.exception.NacosException; -import com.google.gson.Gson; -import com.xiaomi.data.push.client.HttpClientV5; -import com.xiaomi.mone.monitor.DashboardConstant; -import com.xiaomi.mone.monitor.bo.*; -import com.xiaomi.mone.monitor.dao.GrafanaTemplateDao; -import com.xiaomi.mone.monitor.dao.model.GrafanaTemplate; -import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.bo.DashboardDTO; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.prometheus.JobService; -import com.xiaomi.mone.monitor.utils.FreeMarkerUtil; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.stereotype.Service; - -import javax.annotation.PostConstruct; -import java.io.*; -import java.util.*; /** * @author zhangxiaowei6 * @date 2023-02-22 */ -@Service -@Slf4j -@ConditionalOnProperty(name = "service.selector.property", havingValue = "outer") -public class HeraDashboardService { - private Gson gson = new Gson(); - - @Autowired - GrafanaTemplateDao grafanaTemplateDao; - @Autowired - JobService jobService; - - @Value("${nacos.config.addrs}") - private String nacosAddress; - - @NacosValue(value = "${grafana.address}", autoRefreshed = true) - public String grafanaUrl; - - @NacosValue(value = "${grafana.domain}", autoRefreshed = true) - public String grafanaDomain; - @NacosValue(value = "${grafana.apikey.url}", autoRefreshed = true) - public String grafanaApiKeyUrl; - - @NacosValue(value = "${grafana.datasource.url}", autoRefreshed = true) - public String grafanaDatasourceUrl; - @NacosValue(value = "${grafana.folder.url}", autoRefreshed = true) - public String grafanaFolderUrl; - - @NacosValue(value = "${grafana.jaeger.query.token}", autoRefreshed = true) - public String jaegerQueryToken; - - @NacosValue(value = "${grafana.createDashboard.url}", autoRefreshed = true) - public String grafanaDashboardUrl; - - public static final String HERA_GRAFANA_TEMPLATE = "/heraGrafanaTemplate"; - public static final String HERA_SCRAPE_JOB_TEMPLATE = "/heraScrapeJobTemplate"; - - @Value("${server.type}") - private String serverType; - - @NacosValue(value = "${prometheus.url}", autoRefreshed = true) - private String prometheusUrl; - - @NacosValue(value = "${grafana.username}", autoRefreshed = true) - private String grafanaUserName; - - @NacosValue(value = "${grafana.password}", autoRefreshed = true) - private String grafanaPassword; - - @PostConstruct - public void init() { - try { - log.info("begin createDefaultGrafanaResource"); - createDefaultScrapeJob(); - createDefaultDashboardTemplate(); - DashboardDTO dataSourceDTO = new DashboardDTO(); - if (StringUtils.isBlank(dataSourceDTO.getPrometheusDatasource())) { - dataSourceDTO.setPrometheusDatasource(prometheusUrl); - } - if (StringUtils.isBlank(dataSourceDTO.getUsername())) { - dataSourceDTO.setUsername(grafanaUserName); - } - if (StringUtils.isBlank(dataSourceDTO.getPassword())) { - dataSourceDTO.setPassword(grafanaPassword); - } - if (StringUtils.isBlank(dataSourceDTO.getDashboardFolderName())) { - dataSourceDTO.setDashboardFolderName(DashboardConstant.DEFAULT_FOLDER_NAME); - } - Result dashboard = createGrafanaResources(dataSourceDTO); - } catch (Exception e) { - log.error("GrafanaInitController init error:", e); - throw new RuntimeException("GrafanaInitController init error"); - } - } - - public Result createGrafanaResources(DashboardDTO dashboardDTO) { - log.info("HeraDashboardService.createGrafanaResources param:{}", gson.toJson(dashboardDTO)); - //base64 username & password - String base64Str = dashboardDTO.getUsername() + ":" + dashboardDTO.getPassword(); - String basicAuth = Base64.getEncoder().encodeToString(base64Str.getBytes()); - //request api key - Map header = new HashMap<>(); - header.put("Content-Type", "application/json"); - header.put("Authorization", "Basic " + basicAuth); - String apiKey = createApiKey(header); - if (apiKey == null || apiKey.isEmpty()) { - return Result.fail(ErrorCode.API_KEY_CREATE_FAIL); - } - log.info("createGrafanaResources.apikey:{}", apiKey); - //by api key to create prometheus dataSource - header.put("Authorization", "Bearer " + apiKey); - String datasourceUid = createDataSource(header, dashboardDTO.getPrometheusDatasource()); - if (datasourceUid == null || datasourceUid.isEmpty()) { - return Result.fail(ErrorCode.DATASOURCE_CREATE_FAIL); - } - log.info("createGrafanaResources.datasourceUid:{}", datasourceUid); - //by api key to create hera folder - int folderId = createFolder(header, dashboardDTO.getDashboardFolderName()); - if (folderId == -1) { - return Result.fail(ErrorCode.FOLDER_CREATE_FAIL); - } - //grafana template replace template variable,request grafana generate dashboard and panel. - createDefaultGrafanaDashboard(datasourceUid, header); - - //Write the url returned by the panel back to mimonitor's nacos configuration - try { - ConfigService configService = NacosFactory.createConfigService(nacosAddress); - String nacosResult = configService.getConfig(DashboardConstant.DEFAULT_MIMONITOR_NACOS_CONFIG, - DashboardConstant.DEFAULT_MIMONITOR_NACOS_GROUP, 5000); - Properties props = new Properties(); - props.load(new StringReader(nacosResult)); - props.setProperty("grafana.api.key", apiKey); - props.setProperty("grafana.prometheus.datasource", DashboardConstant.GRAFANA_DATASOURCE_NAME); - props.setProperty("grafana.folder.uid", DashboardConstant.GRAFANA_FOLDER_UID); - props.setProperty("prometheusUid", datasourceUid); - - StringWriter writer = new StringWriter(); - props.store(writer, "after replace!"); - String finalNacosConfig = writer.getBuffer().toString(); - //request nacos cover config - log.info("createGrafanaResources.before overlays nacos config:{}", finalNacosConfig); - boolean postResult = configService.publishConfig(DashboardConstant.DEFAULT_MIMONITOR_NACOS_CONFIG, - DashboardConstant.DEFAULT_MIMONITOR_NACOS_GROUP, finalNacosConfig); - if (!postResult) { - log.error("createGrafanaResources.create nacos config failed:{}", postResult); - } else { - log.info("createGrafanaResources.create nacos config success"); - } - } catch (IOException | NacosException e) { - log.error("createGrafanaResources.request nacos error:{}", e.getMessage()); - } - return Result.success("success"); - } - +public interface HeraDashboardService { + + public void init(); + + public Result createGrafanaResources(DashboardDTO dashboardDTO); + //create biz、docker、node、serviceMarket、interfaceMarket etc... - public void createDefaultDashboardTemplate() { - //Only the first initialization is created using freeMarker. First check if the templates already exist in the database. - // If so, do not create them again - DashboardConstant.GRAFANA_SRE_TEMPLATES.forEach( - name -> { - GrafanaTemplate grafanaTemplate = grafanaTemplateDao.fetchOneByName(name); - if (grafanaTemplate == null) { - //If it has not been created, it is created from the ftl file - try { - insertDashboardTemplate(name); - } catch (IOException e) { - log.error("HeraDashboardService.createDefaultDashboardTemplate error :{}", e.getMessage()); - } - } else { - // If it already exists, delete it from db first, then fetch the latest one from the template and create a new one - int resCount = grafanaTemplateDao.deleteHard(grafanaTemplate.getId()); - if (resCount >= 1) { - try { - insertDashboardTemplate(name); - } catch (IOException e) { - log.error("HeraDashboardService.createDefaultDashboardTemplate Multiple create error :{}", e.getMessage()); - } - } - } - }); - - //create java biz template - GrafanaTemplate grafanaTemplate = grafanaTemplateDao.fetchOneByName("hera-java模板"); - if (grafanaTemplate == null) { - try { - String content = FreeMarkerUtil.getTemplateStr(HERA_GRAFANA_TEMPLATE, DashboardConstant.JAEGER_QUERY_File_NAME); - GrafanaTemplate template = new GrafanaTemplate(); - template.setName("hera-java模板"); - template.setCreateTime(new Date()); - template.setUpdateTime(new Date()); - template.setLanguage(0); - template.setPlatform(0); - template.setAppType(0); - template.setTemplate(content); - template.setDeleted(false); - template.setPanelIdList(DashboardConstant.DEFAULT_PANEL_ID_LIST); - int insertRes = grafanaTemplateDao.insert(template); - log.info("HeraDashboardService.createDefaultDashboardTemplate name:{},insertRes:{}", "hera-java模板", insertRes); - } catch (IOException e) { - log.error("HeraDashboardService.createDefaultDashboardTemplate java template error :{}", e.getMessage()); - } - } - - //create golang biz template - GrafanaTemplate grafanaGoTemplate = grafanaTemplateDao.fetchOneByName("hera-golang模板"); - if (grafanaGoTemplate == null) { - try { - String content = FreeMarkerUtil.getTemplateStr(HERA_GRAFANA_TEMPLATE, DashboardConstant.GOLANG_File_NAME); - GrafanaTemplate template = new GrafanaTemplate(); - template.setName("hera-golang模板"); - template.setCreateTime(new Date()); - template.setUpdateTime(new Date()); - template.setLanguage(1); - template.setPlatform(0); - template.setAppType(0); - template.setTemplate(content); - template.setDeleted(false); - template.setPanelIdList(DashboardConstant.DEFAULT_GOLANG_ID_LIST); - int insertRes = grafanaTemplateDao.insert(template); - log.info("HeraDashboardService.createDefaultDashboardTemplate name:{},insertRes:{}", "hera-golang模板", insertRes); - } catch (IOException e) { - log.error("HeraDashboardService.createDefaultDashboardTemplate golang template error :{}", e.getMessage()); - } - } - - } - - private void insertDashboardTemplate(String name) throws IOException { - String content = FreeMarkerUtil.getTemplateStr(HERA_GRAFANA_TEMPLATE, name + ".ftl"); - GrafanaTemplate template = new GrafanaTemplate(); - template.setName(name); - template.setCreateTime(new Date()); - template.setUpdateTime(new Date()); - template.setLanguage(0); - template.setPlatform(0); - template.setAppType(1); - template.setTemplate(content); - template.setDeleted(false); - int insertRes = grafanaTemplateDao.insert(template); - log.info("HeraDashboardService.insertDashboardTemplate name:{},insertRes:{}", name, insertRes); - } - - //request prometheus-agent create biz、docker、node、jvm ...etc prometheus job - public void createDefaultScrapeJob() { - //Get jobJson from a file - Map jaegerQueryMap = new HashMap<>(); - jaegerQueryMap.put("token", jaegerQueryToken); - try { - //create jaeger_query monitor - Result jaegerResult = jobService.searchJobByName(null, "hera", DashboardConstant.DEFAULT_JAEGER_QUERY_JOB_NAME); - log.info("jaegerResult:{}", jaegerResult); - if (jaegerResult.getData().equals("null")) { - log.info("jaeger_query job begin create"); - String jaegerQueryJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "jaegerQueryScrapeJob.ftl", jaegerQueryMap); - Result jaegerQueryJobRes = jobService.createJob(null, "Hera", jaegerQueryJobJson, "初始化创建业务监控"); - log.info("HeraDashboardService.createDefaultScrapeJob jaeger_query res: {}", jaegerQueryJobRes.getData()); - } - - //create jvm monitor - Result jvmResult = jobService.searchJobByName(null, "hera", DashboardConstant.DEFAULT_JVM_JOB_NAME); - if (jvmResult.getData().equals("null")) { - log.info("jvm job begin create"); - String jvmJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "jvmScrapeJob.ftl", new HashMap<>()); - Result jvmJobJsonRes = jobService.createJob(null, "Hera", jvmJobJson, "初始化创建jvm监控"); - log.info("HeraDashboardService.createDefaultScrapeJob jvm res: {}", jvmJobJsonRes.getData()); - } - - //create docker monitor - Result dockerResult = jobService.searchJobByName(null, "hera", DashboardConstant.DEFAULT_DOCKER_JOB_NAME); - if (dockerResult.getData().equals("null")) { - log.info("docker job begin create"); - String dockerJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "dockerScrapeJob.ftl", new HashMap<>()); - Result dockerJobJsonRes = jobService.createJob(null, "Hera", dockerJobJson, "初始化创建容器监控"); - log.info("HeraDashboardService.createDefaultScrapeJob docker res: {}", dockerJobJsonRes.getData()); - } - - //create node monitor - Result nodeResult = jobService.searchJobByName(null, "hera", DashboardConstant.DEFAULT_NODE_JOB_NAME); - if (nodeResult.getData().equals("null")) { - log.info("node job begin create"); - String nodeJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "nodeScrapeJob.ftl", new HashMap<>()); - Result nodeJobJsonRes = jobService.createJob(null, "Hera", nodeJobJson, "初始化创建物理机监控"); - log.info("HeraDashboardService.createDefaultScrapeJob node res: {}", nodeJobJsonRes.getData()); - } - //create custom monitor - Result customizeResult = jobService.searchJobByName(null, "hera", DashboardConstant.DEFAULT_CUSTOMIZE_JOB_NAME); - if (customizeResult.getData().equals("null")) { - log.info("customize job begin create"); - String customizeJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "customizeScrapeJob.ftl", new HashMap<>()); - Result customizeJobJsonRes = jobService.createJob(null, "Hera", customizeJobJson, "初始化创建自定义监控"); - log.info("HeraDashboardService.createDefaultScrapeJob customize res: {}", customizeJobJsonRes.getData()); - } - //create golang runtime monitor - Result golangRuntimeResult = jobService.searchJobByName(null, "hera", DashboardConstant.DEFAULT_GOLANG_RUNTIME_JOB_NAME); - if (golangRuntimeResult.getData().equals("null")) { - log.info("golangRuntimeResult job begin create"); - String golangRuntimeJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "golangRuntimeScrapeJob.ftl", new HashMap<>()); - Result golangRuntimeJobJsonRes = jobService.createJob(null, "Hera", golangRuntimeJobJson, "初始化创建golang Runtime监控"); - log.info("HeraDashboardService.createDefaultScrapeJob golang runtime res: {}", golangRuntimeJobJsonRes.getData()); - } - } catch (Exception e) { - log.error("HeraDashboardService.createDefaultScrapeJob error :{}", e.getMessage()); - } - - } - - private String createApiKey(Map header) { - String apiKeyName = DashboardConstant.GRAFANA_API_KEY_NAME + "-" + System.currentTimeMillis(); - GrafanaApiKeyReq req = new GrafanaApiKeyReq(apiKeyName, DashboardConstant.GRAFANA_API_KEY_ROLE); - log.info("GrafanaApiKeyReq:{}", gson.toJson(req)); - try { - String grafanaApiKeyResStr = HttpClientV5.post(grafanaUrl + grafanaApiKeyUrl, gson.toJson(req), header); - log.info("HeraDashboardService.createApiKey request apikey res:{}", grafanaApiKeyResStr); - GrafanaApiKeyRes grafanaApiKeyRes = gson.fromJson(grafanaApiKeyResStr, GrafanaApiKeyRes.class); - return grafanaApiKeyRes.getKey(); - } catch (Exception e) { - log.error("HeraDashboardService.createApiKey error :{}", e.getMessage()); - return null; - } - } - - private String createDataSource(Map header, String prometheusDatasourceUrl) { - GrafanaCreateDataSourceReq req = new GrafanaCreateDataSourceReq(); - req.setName(DashboardConstant.GRAFANA_DATASOURCE_NAME); - req.setType(DashboardConstant.GRAFANA_DATASOURCE_TYPE); - req.setAccess("proxy"); - req.setBasicAuth(false); - req.setUrl(prometheusDatasourceUrl); - log.info("GrafanaCreateDataSourceReq:{}", gson.toJson(req)); - try { - //If yes, no creation is required - String getDatasourceRes = HttpClientV5.get(grafanaUrl + grafanaDatasourceUrl + "/name/" + - DashboardConstant.GRAFANA_DATASOURCE_NAME, header); - log.info("HeraDashboardService.createDataSource getDatasourceRes:{}", getDatasourceRes); - GrafanaGetDataSourceRes grafanaGetDataSourceRes = gson.fromJson(getDatasourceRes, GrafanaGetDataSourceRes.class); - if (grafanaGetDataSourceRes.getUid() != null) { - return grafanaGetDataSourceRes.getUid(); - } - String grafanaDatasourceResStr = HttpClientV5.post(grafanaUrl + grafanaDatasourceUrl, gson.toJson(req), header); - log.info("HeraDashboardService.createDataSource request res:{}", grafanaDatasourceResStr); - GrafanaCreateDataSourceRes grafanaCreateDataSourceRes = gson.fromJson(grafanaDatasourceResStr, GrafanaCreateDataSourceRes.class); - return grafanaCreateDataSourceRes.getDatasource().getUid(); - } catch (Exception e) { - log.error("HeraDashboardService.createDataSource error :{}", e.getMessage()); - return null; - } - } - - private int createFolder(Map header, String folderTitle) { - GrafanaFolderReq req = new GrafanaFolderReq(); - req.setTitle(folderTitle); - req.setUid(DashboardConstant.GRAFANA_FOLDER_UID); - log.info("GrafanaCreateFolderReq:{}", gson.toJson(req)); - try { - String getFolderRes = HttpClientV5.get(grafanaUrl + grafanaFolderUrl + "/" + DashboardConstant.GRAFANA_FOLDER_UID, header); - log.info("HeraDashboardService.createFolder getFolderRes:{}", getFolderRes); - GrafanaGetFolderRes grafanaGetFolderRes = gson.fromJson(getFolderRes, GrafanaGetFolderRes.class); - if (grafanaGetFolderRes.getId() != null) { - return grafanaGetFolderRes.getId(); - } - String grafanaFolderResStr = HttpClientV5.post(grafanaUrl + grafanaFolderUrl, gson.toJson(req), header); - log.info("HeraDashboardService.createFolder request res:{}", grafanaFolderResStr); - GrafanaFolderRes grafanaFolderRes = gson.fromJson(grafanaFolderResStr, GrafanaFolderRes.class); - return grafanaFolderRes.getId(); - } catch (Exception e) { - log.error("HeraDashboardService.createFolder error :{}", e.getMessage()); - return -1; - } - } - - private void createDefaultGrafanaDashboard(String prometheusUid, Map header) { - Map map = new HashMap<>(); - map.put("prometheusUid", prometheusUid); - map.put("serviceMarketUrl", grafanaDomain + "/d/${__data.fields.application.text}/ye-wu-jian-kong-" + - "${__data.fields.application.text}?orgId=1&refresh=30s&theme=light"); - map.put("query0", "${query0}"); - map.put("env", serverType); - map.put("serviceName", "hera"); - - DashboardConstant.GRAFANA_SRE_TEMPLATES.forEach( - name -> { - GrafanaTemplate grafanaTemplate = grafanaTemplateDao.fetchOneByName(name); - try { - if (grafanaTemplate != null) { - String template = FreeMarkerUtil.freemarkerProcess(map, grafanaTemplate.getTemplate()); - log.info("HeraDashboardService.createDefaultGrafanaDashboard name :{} begin!", name); - //request grafana - String grafanaDashboardResStr = HttpClientV5.post(grafanaUrl + grafanaDashboardUrl, template, header); - log.info("HeraDashboardService.createDefaultGrafanaDashboard request " + name + " template res:{}", grafanaDashboardResStr); - GrafanaCreateDashboardRes grafanaCreateDashboardRes = gson.fromJson(grafanaDashboardResStr, GrafanaCreateDashboardRes.class); - if (!grafanaCreateDashboardRes.getStatus().equals("success")) { - log.error("HeraDashboardService.createDefaultGrafanaDashboard name:{},status:{},message:{}", - name, grafanaCreateDashboardRes.getStatus(), grafanaCreateDashboardRes.getMessage()); - } - } else { - log.error("HeraDashboardService.createDefaultGrafanaDashboard " + name + " template fetch error!"); - } - } catch (Exception e) { - log.error("HeraDashboardService.createDefaultGrafanaDashboard error:{}", e); - } - }); - } + public void createDefaultDashboardTemplate(); + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/QualityMarketService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/QualityMarketService.java index 79bb80598..0288d80cc 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/QualityMarketService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/QualityMarketService.java @@ -13,106 +13,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.xiaomi.mone.monitor.dao.AppQualityMarketDao; -import com.xiaomi.mone.monitor.dao.model.AppQualityMarket; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.model.PageData; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Slf4j -@Service -public class QualityMarketService { - @Autowired - AppQualityMarketDao appQualityMarketDao; - public Result createMarket(String user, String marketName, String serviceList, String remark) { - try { - //按;切分serviceList - String[] services = serviceList.split(";"); - //入库 - AppQualityMarket appQualityMarket = new AppQualityMarket(); - appQualityMarket.setMarketName(marketName.trim()); - appQualityMarket.setServiceList(serviceList.trim()); - appQualityMarket.setCreator(user); - appQualityMarket.setLastUpdater(user); - appQualityMarket.setRemark(remark.trim()); - int dbResult = appQualityMarketDao.insertServiceMarket(appQualityMarket); - log.info("QualityMarketService.createMarket dbResult: {}", dbResult); - return Result.success("success"); - } - catch (Exception e) { - log.error("QualityMarketService.createMarket error : {}",e.toString()); - return Result.fail(ErrorCode.unknownError); - } - } - public Result searchMarket(String user,int id) { - try { - AppQualityMarket appQualityMarket = appQualityMarketDao.SearchAppQualityMarket(id); - return Result.success(appQualityMarket); - }catch (Exception e) { - log.error("QualityMarketService.searchMarket error : {}",e.toString()); - return Result.fail(ErrorCode.unknownError); - } - } +public interface QualityMarketService { + + Result createMarket(String user, String marketName, String serviceList, String remark); + + Result searchMarket(String user, int id); + //改 - public Result updateMarket(String user,int id,String serviceList,String marketName,String remark) { - try{ - //查库是否有该记录 - AppQualityMarket appQualityMarket = appQualityMarketDao.SearchAppQualityMarket(id); - if (appQualityMarket == null) { - return Result.fail(ErrorCode.nonExistentServiceMarketId); - } - appQualityMarket.setMarketName(marketName.trim()); - appQualityMarket.setServiceList(serviceList.trim()); - appQualityMarket.setLastUpdater(user); - appQualityMarket.setRemark(remark.trim()); - int dbResult = appQualityMarketDao.updateQualityMarket(appQualityMarket); - log.info("QualityMarketService.updateMarket dbResult: {}", dbResult); - return Result.success("success"); - }catch (Exception e) { - log.error("QualityMarketService.updateMarket error : {}",e.toString()); - return Result.fail(ErrorCode.unknownError); - } - } - + Result updateMarket(String user, int id, String serviceList, String marketName, String remark); + //删 - public Result deleteMarket(String user,Integer id) { - try{ - //查库是否有该记录 - AppQualityMarket appQualityMarket = appQualityMarketDao.SearchAppQualityMarket(id); - if (appQualityMarket == null) { - return Result.fail(ErrorCode.nonExistentServiceMarketId); - } - //删除 - int result = appQualityMarketDao.deleteQualityMarket(id); - log.info("QualityMarketService.deleteMarket dbResult:{}",result); - return Result.success("success"); - }catch (Exception e) { - log.error("QualityMarketService.deleteMarket error : {}",e.toString()); - return Result.fail(ErrorCode.unknownError); - } - } + Result deleteMarket(String user, Integer id); + + + //查列表 + Result searchMarketList(String user, int pageSize, int pageNo, String creator, String marketName, + String serviceName); + +} - //查列表 - public Result searchMarketList(String user,int pageSize,int pageNo,String creator,String marketName,String serviceName) { - AppQualityMarket appQualityMarket = new AppQualityMarket(); - if (StringUtils.isNotEmpty(creator)) { - appQualityMarket.setCreator(creator); - } - PageData pd = new PageData(); - pd.setPage(pageNo); - pd.setPageSize(pageSize); - pd.setTotal(appQualityMarketDao.getTotal(creator,marketName,serviceName)); - pd.setList(appQualityMarketDao.SearchAppQualityMarketList(pageNo,pageSize,creator,marketName,serviceName)); - return Result.success(pd); - } -} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ResourceUsageService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ResourceUsageService.java index 2db1cf6c5..73a5b7bb8 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ResourceUsageService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ResourceUsageService.java @@ -37,56 +37,10 @@ * @author gaoxihui * @date 2022/5/12 5:18 PM */ -@Service -public class ResourceUsageService { +public interface ResourceUsageService { - @Autowired - AlarmExprService alarmExprService; - @Autowired - PrometheusService prometheusService; + public List getCpuUsageData(); - @Autowired - HeraAppRoleDao heraAppRoleDao; - - @NacosValue(value = "${resource.use.rate.alarm.config:1}",autoRefreshed = true) - private String resourceAlarm; - - public List getCpuUsageData(){ - String mimonitor = alarmExprService.getContainerCpuResourceAlarmExpr(null, "mimonitor", "<", Integer.valueOf(resourceAlarm),false,null); - Result pageDataResult = prometheusService.queryByMetric(mimonitor); - PageData data = pageDataResult.getData(); - List listMsg = new ArrayList<>(); - if(data != null){ - List list = (List)data.getList(); - if(CollectionUtils.isNotEmpty(list)){ - listMsg = list.stream().map(t -> { - List query = heraAppRoleDao.queryByPlatTypes(t.getContainer_label_PROJECT_ID(), Lists.newArrayList(0,2),0, 5); - List members = CollectionUtils.isEmpty(query) ? Lists.newArrayList() : query.stream().map( r -> r.getUser()).collect(Collectors.toList()); - return new ResourceUsageMessage(t.getIp(), t.getContainer_label_PROJECT_ID(), t.getName().substring(0, t.getName().lastIndexOf("-")), String.valueOf(t.getValue()), null,members,resourceAlarm + "%"); - }).collect(Collectors.toList()); - } - } - - return listMsg; - } - - public List getMemUsageData(){ - String mimonitor = alarmExprService.getContainerMemReourceAlarmExpr(null, "mimonitor", "<", Integer.valueOf(resourceAlarm),false,null); - Result pageDataResult = prometheusService.queryByMetric(mimonitor); - PageData data = pageDataResult.getData(); - List listMsg = new ArrayList<>(); - if(data != null){ - List list = (List)data.getList(); - if(CollectionUtils.isNotEmpty(list)){ - listMsg = list.stream().map(t -> { - List query = heraAppRoleDao.queryByPlatTypes(t.getContainer_label_PROJECT_ID(), Lists.newArrayList(0,2),0, 5); - List members = CollectionUtils.isEmpty(query) ? Lists.newArrayList() : query.stream().map( r -> r.getUser()).collect(Collectors.toList()); - return new ResourceUsageMessage(t.getIp(), t.getContainer_label_PROJECT_ID(), t.getName().substring(0, t.getName().lastIndexOf("-")), String.valueOf(t.getValue()), null,members,resourceAlarm + "%"); - }).collect(Collectors.toList()); - } - } - - return listMsg; - } + public List getMemUsageData(); } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/RulePromQLTemplateService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/RulePromQLTemplateService.java index d8160c940..db2c0930b 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/RulePromQLTemplateService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/RulePromQLTemplateService.java @@ -13,108 +13,47 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.alibaba.nacos.api.config.annotation.NacosValue; import com.xiaomi.mone.monitor.bo.RulePromQLTemplateInfo; import com.xiaomi.mone.monitor.bo.RulePromQLTemplateParam; -import com.xiaomi.mone.monitor.dao.RulePromQLTemplateDao; -import com.xiaomi.mone.monitor.dao.model.RulePromQLTemplate; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; import com.xiaomi.mone.monitor.service.model.PageData; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; import java.util.List; /** - * * @author zhanggaofeng1 */ -@Slf4j -@Service -public class RulePromQLTemplateService { - - @Autowired - private RulePromQLTemplateDao rulePromQLTemplateDao; - - @NacosValue(value = "${prometheus.url}",autoRefreshed = true) - private String prometheusUrl; - @NacosValue(value = "${prometheus.check.url}",autoRefreshed = true) - private String prometheusCheckUrl; +public interface RulePromQLTemplateService { + + /** * 添加模板 + * * @param user * @param param * @return */ - public Result add(String user, RulePromQLTemplateParam param) { - List templates = rulePromQLTemplateDao.getByName(user, param.getName()); - if (!CollectionUtils.isEmpty(templates)) { - Result result = Result.fail(ErrorCode.invalidParamError); - result.setMessage("名称不允许重复"); - return result; - } - RulePromQLTemplate template = new RulePromQLTemplate(); - BeanUtils.copyProperties(param, template); - template.setCreater(user); - if (!rulePromQLTemplateDao.insert(template)) { - return Result.fail(ErrorCode.unknownError); - } - log.info("add PromQL template 成功:user={}, template={}",user, template); - return Result.success(null); - } - + Result add(String user, RulePromQLTemplateParam param); + /** * 编辑模板 + * * @param user * @param param * @return */ - public Result edit(String user, RulePromQLTemplateParam param) { - List templates = rulePromQLTemplateDao.getByName(user, param.getName()); - if (templates != null && templates.size() > 1) { - Result result = Result.fail(ErrorCode.invalidParamError); - result.setMessage("名称不允许重复"); - return result; - } - RulePromQLTemplate template = new RulePromQLTemplate(); - BeanUtils.copyProperties(param, template); - template.setCreater(null); - if (!rulePromQLTemplateDao.updateById(template)) { - return Result.fail(ErrorCode.unknownError); - } - log.info("updateById PromQL template 成功:user={}, template={}",user, template); - return Result.success(null); - } - - - public Result deleteById(String user, Integer templateId) { - if (!rulePromQLTemplateDao.deleteById(templateId)) { - return Result.fail(ErrorCode.unknownError); - } - log.info("deleteById PromQL template 成功:user={}, templateId={}",user, templateId); - return Result.success(null); - } - - public Result>> search(String user, RulePromQLTemplateParam param) { - RulePromQLTemplate template = new RulePromQLTemplate(); - PageData> pageData = rulePromQLTemplateDao.searchByCond(user, param); - log.info("query promQL template user={}, param={}, pageData={}", user, param, pageData); - return Result.success(pageData); - } - - public Result testPromQL(String user, RulePromQLTemplateParam param) throws UnsupportedEncodingException { - StringBuilder url = new StringBuilder(); - url.append(prometheusCheckUrl).append("graph?g0.expr=").append(URLEncoder.encode(param.getPromql(), "UTF-8")).append("&g0.tab=1&g0.stacked=0&g0.range_input=1h"); - return Result.success(url.toString()); - } - + Result edit(String user, RulePromQLTemplateParam param); + + + Result deleteById(String user, Integer templateId); + + Result>> search(String user, RulePromQLTemplateParam param); + + Result testPromQL(String user, RulePromQLTemplateParam param) throws UnsupportedEncodingException; + } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ServiceMarketService.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ServiceMarketService.java index 17caa744f..9e027a193 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ServiceMarketService.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ServiceMarketService.java @@ -13,140 +13,35 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service; -import com.alibaba.nacos.api.config.annotation.NacosValue; -import com.xiaomi.mone.monitor.dao.AppServiceMarketDao; -import com.xiaomi.mone.monitor.dao.model.AppServiceMarket; -import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; -import com.xiaomi.mone.monitor.service.api.ServiceMarketExtension; -import com.xiaomi.mone.monitor.service.extension.PlatFormTypeExtensionService; -import com.xiaomi.mone.monitor.service.model.PageData; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; /** * @author zhangxiaowei6 */ -@Slf4j -@Service -public class ServiceMarketService { - @Autowired - AppServiceMarketDao appServiceMarketDao; - - @Value("${server.type}") - private String env; - - @NacosValue(value = "${grafana.domain}", autoRefreshed = true) - private String grafanaDomain; - - @Autowired - ServiceMarketExtension serviceMarketExtension; - - @Autowired - PlatFormTypeExtensionService platFormTypeExtensionService; - - //线上mione 服务大盘url - public static final String MIONE_ONLINE_SERVICE_MARKET_GRAFANA_URL = "/d/hera-serviceMarket/hera-fu-wu-da-pan?orgId=1"; - +public interface ServiceMarketService { + //增 - public Result createMarket(String user, String marketName, String belongTeam, String serviceList, String remark, Integer serviceType) { - serviceType = platFormTypeExtensionService.getMarketType(serviceType); - try { - //按;切分serviceList - String[] services = serviceList.split(";"); - //入库 - AppServiceMarket appServiceMarket = new AppServiceMarket(); - appServiceMarket.setMarketName(marketName.trim()); - appServiceMarket.setServiceList(serviceList.trim()); - appServiceMarket.setCreator(user); - appServiceMarket.setLastUpdater(user); - appServiceMarket.setRemark(remark.trim()); - appServiceMarket.setBelongTeam(belongTeam.trim()); - appServiceMarket.setServiceType(serviceType); - int dbResult = appServiceMarketDao.insertServiceMarket(appServiceMarket); - log.info("ServiceMarketService.createMarket dbResult: {}", dbResult); - return Result.success("success"); - } catch (Exception e) { - log.error("ServiceMarketService.createMarket error : {}", e.toString()); - return Result.fail(ErrorCode.unknownError); - } - } - + Result createMarket(String user, String marketName, String belongTeam, String serviceList, String remark, + Integer serviceType); + //查 - public Result searchMarket(String user, int id) { - try { - AppServiceMarket appServiceMarket = appServiceMarketDao.SearchAppServiceMarket(id); - return Result.success(appServiceMarket); - } catch (Exception e) { - log.error("ServiceMarketService.searchMarket error : {}", e.toString()); - return Result.fail(ErrorCode.unknownError); - } - } - + Result searchMarket(String user, int id); + //获取大盘grafanaUrl - public String getServiceMarketGrafana(Integer serviceType) { - return serviceMarketExtension.getServiceMarketGrafana(serviceType); - } - + String getServiceMarketGrafana(Integer serviceType); + //查列表 - public Result searchMarketList(String user, int pageSize, int pageNo, String creator, String marketName, String serviceName) { - AppServiceMarket appServiceMarket = new AppServiceMarket(); - if (StringUtils.isNotEmpty(creator)) { - appServiceMarket.setCreator(creator); - } - PageData pd = new PageData(); - pd.setPage(pageNo); - pd.setPageSize(pageSize); - pd.setTotal(appServiceMarketDao.getTotal(creator, marketName, serviceName)); - pd.setList(appServiceMarketDao.SearchAppServiceMarketList(pageNo, pageSize, creator, marketName, serviceName)); - return Result.success(pd); - } - + Result searchMarketList(String user, int pageSize, int pageNo, String creator, String marketName, + String serviceName); + //改 - public Result updateMarket(String user, int id, String serviceList, String marketName, String remark, String belongTeam, int serviceType) { - serviceType = platFormTypeExtensionService.getMarketType(serviceType); - try { - //查库是否有该记录 - AppServiceMarket appServiceMarket = appServiceMarketDao.SearchAppServiceMarket(id); - if (appServiceMarket == null) { - return Result.fail(ErrorCode.nonExistentServiceMarketId); - } - appServiceMarket.setMarketName(marketName.trim()); - appServiceMarket.setServiceList(serviceList.trim()); - appServiceMarket.setLastUpdater(user); - appServiceMarket.setRemark(remark.trim()); - appServiceMarket.setBelongTeam(belongTeam.trim()); - appServiceMarket.setServiceType(serviceType); - int dbResult = appServiceMarketDao.updateServiceMarket(appServiceMarket); - log.info("ServiceMarketService.updateMarket dbResult: {}", dbResult); - return Result.success("success"); - } catch (Exception e) { - log.error("ServiceMarketService.updateMarket error : {}", e.toString()); - return Result.fail(ErrorCode.unknownError); - } - } - + Result updateMarket(String user, int id, String serviceList, String marketName, String remark, String belongTeam, + int serviceType); + //删 - public Result deleteMarket(String user, Integer id) { - try { - //查库是否有该记录 - AppServiceMarket appServiceMarket = appServiceMarketDao.SearchAppServiceMarket(id); - if (appServiceMarket == null) { - return Result.fail(ErrorCode.nonExistentServiceMarketId); - } - //删除 - int result = appServiceMarketDao.deleteServiceMarket(id); - log.info("ServiceMarketService.deleteMarket dbResult:{}", result); - return Result.success("success"); - } catch (Exception e) { - log.error("ServiceMarketService.deleteMarket error : {}", e.toString()); - return Result.fail(ErrorCode.unknownError); - } - } + Result deleteMarket(String user, Integer id); } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlarmPresetMetricsServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AlarmPresetMetricsServiceImpl.java similarity index 98% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlarmPresetMetricsServiceImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AlarmPresetMetricsServiceImpl.java index 408f57a82..50f48f5f7 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AlarmPresetMetricsServiceImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AlarmPresetMetricsServiceImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service; +package com.xiaomi.mone.monitor.service.api.impl; import com.xiaomi.mone.monitor.bo.AlarmPresetMetrics; import com.xiaomi.mone.monitor.bo.BasicUrlType; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppAlarmServiceExtensionImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppAlarmServiceExtensionImpl.java similarity index 96% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppAlarmServiceExtensionImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppAlarmServiceExtensionImpl.java index 689405552..0842e618d 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppAlarmServiceExtensionImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppAlarmServiceExtensionImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service; +package com.xiaomi.mone.monitor.service.api.impl; import com.xiaomi.mone.monitor.result.Result; import com.xiaomi.mone.monitor.service.api.AppAlarmServiceExtension; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppGrafanaMappingServiceExtensionImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppGrafanaMappingServiceExtensionImpl.java similarity index 96% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppGrafanaMappingServiceExtensionImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppGrafanaMappingServiceExtensionImpl.java index d8c6050c6..ac6286fd0 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/AppGrafanaMappingServiceExtensionImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppGrafanaMappingServiceExtensionImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service; +package com.xiaomi.mone.monitor.service.api.impl; import com.xiaomi.mone.monitor.dao.model.GrafanaTemplate; import com.xiaomi.mone.monitor.service.api.AppGrafanaMappingServiceExtension; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppMonitorServiceExtensionImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppMonitorServiceExtensionImpl.java new file mode 100644 index 000000000..f93570373 --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/AppMonitorServiceExtensionImpl.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.xiaomi.mone.monitor.service.api.impl; + +import com.alibaba.nacos.api.config.annotation.NacosValue; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.xiaomi.mone.app.api.message.HeraAppInfoModifyMessage; +import com.xiaomi.mone.monitor.bo.GrafanaInterfaceRes; +import com.xiaomi.mone.monitor.dao.model.AppMonitor; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.api.AppMonitorServiceExtension; +import com.xiaomi.mone.monitor.service.model.ProjectInfo; +import com.xiaomi.mone.monitor.utils.FreeMarkerUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author zhangxiaowei6 + */ +@Slf4j +@Service +@ConditionalOnProperty(name = "service.selector.property", havingValue = "outer") +public class AppMonitorServiceExtensionImpl implements AppMonitorServiceExtension { + + @NacosValue(value = "${grafana.domain}", autoRefreshed = true) + private String grafanaDomain; + + + private String resourceUrl = "/d/hera-resource-utilization/hera-k8szi-yuan-shi-yong-lu-da-pan?orgId=1&var-application="; + + private String dubboProviderOverview = "/d/hera-dubboprovider-overview/hera-dubboproviderzong-lan?orgId=1&kiosk&theme=light"; + private String dubboConsumerOverview = "/d/hera-dubboconsumer-overview/hera-dubboconsumerzong-lan?orgId=1&kiosk&theme=light"; + private String dubboProviderMarket = "/d/Hera-DubboProviderMarket/hera-dubboproviderda-pan?orgId=1&kiosk&theme=light"; + private String dubboConsumerMarket = "/d/Hera-DubboConsumerMarket/hera-dubboconsumerda-pan?orgId=1&kiosk&theme=light"; + private String httpOverview = "/d/Hera-HTTPServer-overview/hera-httpserver-zong-lan?orgId=1&kiosk&theme=light"; + private String httpMarket = "/d/Hera-HTTPServerMarket/hera-httpserverda-pan?orgId=1&kiosk&theme=light"; + + private String grpcProviderOverview = "/d/hera-grpcprovider-overview/hera-grpcproviderzong-lan?orgId=1&kiosk&theme=light"; + private String grpcProviderMarket = "/d/hera-grpcproviderMarket/hera-grpcproviderda-pan?orgId=1&kiosk&theme=light"; + private String grpcConsumerOverview = "/d/hera-grpcconsumer-overview/hera-grpcconsumerzong-lan?orgId=1&kiosk&theme=light"; + private String grpcConsumerMarket = "/d/hera-grpcconsumerMarket/hera-grpcconsumerda-pan?orgId=1&kiosk&theme=light"; + + private static final Gson gson = new Gson(); + + public Result getResourceUsageUrlForK8s(Integer appId, String appName) { + //A link back to the grafana resource utilization graph + String application = String.valueOf(appId) + "_" + StringUtils.replace(appName, "-", "_"); + String url = grafanaDomain + resourceUrl + application; + log.info("getResourceUsageUrlForK8s url:{}", url); + return Result.success(url); + } + + @Override + public Result grafanaInterfaceList() { + Map map = new HashMap<>(); + map.put("dubboProviderOverview", grafanaDomain + dubboProviderOverview); + map.put("dubboConsumerOverview", grafanaDomain + dubboConsumerOverview); + map.put("dubboProviderMarket", grafanaDomain + dubboProviderMarket); + map.put("dubboConsumerMarket", grafanaDomain + dubboConsumerMarket); + map.put("httpOverview", grafanaDomain + httpOverview); + map.put("httpMarket", grafanaDomain + httpMarket); + map.put("grpcProviderOverview", grafanaDomain + grpcProviderOverview); + map.put("grpcProviderMarket", grafanaDomain + grpcProviderMarket); + map.put("grpcConsumerOverview", grafanaDomain + grpcConsumerOverview); + map.put("grpcConsumerMarket", grafanaDomain + grpcConsumerMarket); + + try { + log.info("grafanaInterfaceList map:{}", map); + String data = FreeMarkerUtil.getContentExceptJson("/heraGrafanaTemplate", "grafanaInterfaceList.ftl", map); + JsonArray jsonElements = gson.fromJson(data, JsonArray.class); + log.info(jsonElements.toString()); + List resList = new ArrayList<>(); + jsonElements.forEach(it -> { + GrafanaInterfaceRes grafanaInterfaceRes = gson.fromJson(it, GrafanaInterfaceRes.class); + resList.add(grafanaInterfaceRes); + }); + log.info("grafanaInterfaceList success! data:{}", resList); + return Result.success(resList); + } catch (Exception e) { + log.error("grafanaInterfaceList error! {}", e); + return Result.fail(ErrorCode.unknownError); + } + } + + @Override + public Result initAppsByUsername(String userName) { + return null; + } + + @Override + public List getAppsByUserName(String username) { + return null; + } + + @Override + public Boolean checkCreateParam(AppMonitor appMonitor) { + + if (appMonitor.getProjectId() == null || StringUtils.isBlank(appMonitor.getProjectName())) { + return false; + } + return true; + } + + @Override + public Boolean checkAppModifyStrategySearchCondition(HeraAppInfoModifyMessage message) { + if (message.getAppId() == null) { + log.error("checkAppModifyStrategySearchCondition appId is null message : {}", message); + return false; + } + return true; + } + + @Override + public void changeAlarmServiceToZone(Integer pageSize, String appName) { + + } + + + @Override + public Result getResourceUsageUrl(Integer appId, String appName) { + return null; + } + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ComputeTimerServiceExtensionImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ComputeTimerServiceExtensionImpl.java similarity index 99% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ComputeTimerServiceExtensionImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ComputeTimerServiceExtensionImpl.java index 96f5588fa..e5afbc39b 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ComputeTimerServiceExtensionImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ComputeTimerServiceExtensionImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service; +package com.xiaomi.mone.monitor.service.api.impl; import com.xiaomi.mone.monitor.bo.ReqErrorMetrics; import com.xiaomi.mone.monitor.bo.ReqSlowMetrics; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaServiceExtensionImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/GrafanaServiceExtensionImpl.java similarity index 99% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaServiceExtensionImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/GrafanaServiceExtensionImpl.java index 760ed3d25..28262493a 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaServiceExtensionImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/GrafanaServiceExtensionImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service.impl; +package com.xiaomi.mone.monitor.service.api.impl; import com.alibaba.nacos.api.config.annotation.NacosValue; import com.google.gson.Gson; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/IAMServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/IAMServiceImpl.java similarity index 96% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/IAMServiceImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/IAMServiceImpl.java index dfe575497..60130f4e0 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/IAMServiceImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/IAMServiceImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service; +package com.xiaomi.mone.monitor.service.api.impl; import com.xiaomi.mone.monitor.service.api.IAMService; import com.xiaomi.mone.tpc.common.param.NodeQryParam; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/MetricsLabelKindServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/MetricsLabelKindServiceImpl.java similarity index 97% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/MetricsLabelKindServiceImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/MetricsLabelKindServiceImpl.java index 73ad44282..8e14d9697 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/MetricsLabelKindServiceImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/MetricsLabelKindServiceImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service; +package com.xiaomi.mone.monitor.service.api.impl; import com.xiaomi.mone.monitor.bo.MetricLabelKind; import com.xiaomi.mone.monitor.service.api.MetricsLabelKindService; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/PrometheusServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/PrometheusServiceImpl.java similarity index 99% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/PrometheusServiceImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/PrometheusServiceImpl.java index 809c09721..417b40f1d 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/PrometheusServiceImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/PrometheusServiceImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service.impl; +package com.xiaomi.mone.monitor.service.api.impl; import com.alibaba.nacos.api.config.annotation.NacosValue; import com.google.gson.Gson; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ReqErrorMetricsServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ReqErrorMetricsServiceImpl.java similarity index 97% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ReqErrorMetricsServiceImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ReqErrorMetricsServiceImpl.java index 8a927d86b..b17d60c5d 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ReqErrorMetricsServiceImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ReqErrorMetricsServiceImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service; +package com.xiaomi.mone.monitor.service.api.impl; import com.xiaomi.mone.monitor.bo.AlarmPresetMetrics; import com.xiaomi.mone.monitor.bo.ReqErrorMetrics; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ReqSlowMetricsServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ReqSlowMetricsServiceImpl.java similarity index 97% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ReqSlowMetricsServiceImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ReqSlowMetricsServiceImpl.java index e5bab1789..46ffa418a 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/ReqSlowMetricsServiceImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ReqSlowMetricsServiceImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service; +package com.xiaomi.mone.monitor.service.api.impl; import com.xiaomi.mone.monitor.bo.ReqSlowMetrics; import com.xiaomi.mone.monitor.pojo.ReqSlowMetricsPOJO; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ServiceMarketImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ServiceMarketImpl.java similarity index 97% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ServiceMarketImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ServiceMarketImpl.java index bb4914647..3a91399fa 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ServiceMarketImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/ServiceMarketImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service.impl; +package com.xiaomi.mone.monitor.service.api.impl; import com.alibaba.nacos.api.config.annotation.NacosValue; import com.xiaomi.mone.monitor.service.api.ServiceMarketExtension; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/TeslaServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/TeslaServiceImpl.java similarity index 96% rename from ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/TeslaServiceImpl.java rename to ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/TeslaServiceImpl.java index 07db8a4e6..951d5544e 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/TeslaServiceImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/api/impl/TeslaServiceImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xiaomi.mone.monitor.service; +package com.xiaomi.mone.monitor.service.api.impl; import com.xiaomi.mone.monitor.result.Result; import com.xiaomi.mone.monitor.service.api.TeslaService; diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlarmStrategyServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlarmStrategyServiceImpl.java new file mode 100644 index 000000000..45e83b05f --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlarmStrategyServiceImpl.java @@ -0,0 +1,496 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.xiaomi.mone.monitor.bo.AlarmStrategyInfo; +import com.xiaomi.mone.monitor.bo.AlarmStrategyParam; +import com.xiaomi.mone.monitor.bo.AlarmStrategyType; +import com.xiaomi.mone.monitor.bo.AppViewType; +import com.xiaomi.mone.monitor.dao.AppAlarmRuleDao; +import com.xiaomi.mone.monitor.dao.AppAlarmStrategyDao; +import com.xiaomi.mone.monitor.dao.AppMonitorDao; +import com.xiaomi.mone.monitor.dao.model.AlarmStrategy; +import com.xiaomi.mone.monitor.dao.model.AppAlarmRule; +import com.xiaomi.mone.monitor.dao.model.AppMonitor; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.AlarmStrategyService; +import com.xiaomi.mone.monitor.service.AppAlarmService; +import com.xiaomi.mone.monitor.service.model.PageData; +import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleRequest; +import com.xiaomi.mone.monitor.service.prometheus.PrometheusService; +import com.xiaomi.mone.monitor.service.user.LocalUser; +import com.xiaomi.mone.monitor.service.user.UseDetailInfo; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @author zhanggaofeng1 + */ +@Slf4j +@Service +public class AlarmStrategyServiceImpl implements AlarmStrategyService { + + @Autowired + private AppAlarmStrategyDao appAlarmStrategyDao; + + @Autowired + private AppAlarmRuleDao appAlarmRuleDao; + + @Autowired + private AppAlarmService appAlarmService; + + @Autowired + private AppMonitorDao appMonitorDao; + + @Autowired + private PrometheusService prometheusService; + + + private static final List orderSorts = Lists.newArrayList("asc", "ASC", "desc", "DESC"); + + private static final List orderColumn = Lists.newArrayList("update_time", "strategy_name"); + + @Override + public AlarmStrategy getById(Integer id) { + return appAlarmStrategyDao.getById(id); + } + + @Override + public boolean updateById(AlarmStrategy strategy) { + return appAlarmStrategyDao.updateById(strategy); + } + + /** + * 创建策略 + * + * @param param + * @param app + * @return + */ + @Override + public AlarmStrategy create(AlarmRuleRequest param, AppMonitor app) { + if (StringUtils.isBlank(param.getStrategyName()) || param.getStrategyType() == null) { + throw new IllegalArgumentException("strategy name or type is null"); + } + AlarmStrategy strategy = new AlarmStrategy(); + strategy.setCreater(param.getUser()); + strategy.setStrategyName(param.getStrategyName()); + strategy.setStrategyType(param.getStrategyType()); + strategy.setAppId(app.getProjectId()); + strategy.setIamId(app.getIamTreeId()); + strategy.setAppName(param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode()) ? param.getAppAlias() + : app.getProjectName()); + strategy.setDesc(param.getStrategyDesc()); + strategy.setStatus(0); + if (StringUtils.isNotBlank(param.getAlertTeam())) { + strategy.setAlertTeam(param.getAlertTeam()); + } else if (!CollectionUtils.isEmpty(param.getAlarmRules())) { + strategy.setAlertTeam(param.getAlarmRules().get(0).getAlertTeam()); + } + Map deptMap = LocalUser.getDepts(); + if (deptMap.containsKey(3)) { + strategy.setGroup3(deptMap.get(3).getDeptName()); + } + if (deptMap.containsKey(4)) { + strategy.setGroup4(deptMap.get(4).getDeptName()); + } + if (deptMap.containsKey(5)) { + strategy.setGroup5(deptMap.get(5).getDeptName()); + } + + JsonObject envs = new JsonObject(); + if (!CollectionUtils.isEmpty(param.getIncludeEnvs())) { + envs.addProperty("includeEnvs", String.join(",", param.getIncludeEnvs())); + } + if (!CollectionUtils.isEmpty(param.getExceptEnvs())) { + envs.addProperty("exceptEnvs", String.join(",", param.getExceptEnvs())); + } + + if (!CollectionUtils.isEmpty(param.getIncludeZones())) { + envs.addProperty("includeZones", String.join(",", param.getIncludeZones())); + } + if (!CollectionUtils.isEmpty(param.getExceptZones())) { + envs.addProperty("exceptZones", String.join(",", param.getExceptZones())); + } + + if (!CollectionUtils.isEmpty(param.getIncludeContainerName())) { + envs.addProperty("includeContainerName", String.join(",", param.getIncludeContainerName())); + } + if (!CollectionUtils.isEmpty(param.getExceptContainerName())) { + envs.addProperty("exceptContainerName", String.join(",", param.getExceptContainerName())); + } + + if (!CollectionUtils.isEmpty(param.getIncludeModules())) { + envs.addProperty("includeModules", String.join(",", param.getIncludeModules())); + } + if (!CollectionUtils.isEmpty(param.getExceptModules())) { + envs.addProperty("exceptModules", String.join(",", param.getExceptModules())); + } + + if (!CollectionUtils.isEmpty(param.getIncludeFunctions())) { + envs.addProperty("includeFunctions", String.join(",", param.getIncludeFunctions())); + } + if (!CollectionUtils.isEmpty(param.getExceptFunctions())) { + envs.addProperty("exceptFunctions", String.join(",", param.getExceptFunctions())); + } + strategy.setEnvs(envs.toString()); + + if (!CollectionUtils.isEmpty(param.getAlertMembers())) { + strategy.setAlertMembers(String.join(",", param.getAlertMembers())); + } + + if (!CollectionUtils.isEmpty(param.getAtMembers())) { + strategy.setAtMembers(String.join(",", param.getAtMembers())); + } + + if (!appAlarmStrategyDao.insert(strategy)) { + return null; + } + + log.info("插入规则策略成功:strategy={}", strategy); + + return strategy; + } + + @Override + public Result updateByParam(AlarmRuleRequest param) { + + AlarmStrategy strategy = new AlarmStrategy(); + + strategy.setId(param.getStrategyId()); + strategy.setStrategyName(param.getStrategyName()); + strategy.setDesc(param.getStrategyDesc()); + + if (StringUtils.isNotBlank(param.getAlertTeam())) { + strategy.setAlertTeam(param.getAlertTeam()); + } + + JsonObject envs = new JsonObject(); + if (!CollectionUtils.isEmpty(param.getIncludeEnvs())) { + envs.addProperty("includeEnvs", String.join(",", param.getIncludeEnvs())); + } + if (!CollectionUtils.isEmpty(param.getExceptEnvs())) { + envs.addProperty("exceptEnvs", String.join(",", param.getExceptEnvs())); + } + + if (!CollectionUtils.isEmpty(param.getIncludeZones())) { + envs.addProperty("includeZones", String.join(",", param.getIncludeZones())); + } + if (!CollectionUtils.isEmpty(param.getExceptZones())) { + envs.addProperty("exceptZones", String.join(",", param.getExceptZones())); + } + + if (!CollectionUtils.isEmpty(param.getIncludeContainerName())) { + envs.addProperty("includeContainerName", String.join(",", param.getIncludeContainerName())); + } + if (!CollectionUtils.isEmpty(param.getExceptContainerName())) { + envs.addProperty("exceptContainerName", String.join(",", param.getExceptContainerName())); + } + + if (!CollectionUtils.isEmpty(param.getIncludeModules())) { + envs.addProperty("includeModules", String.join(",", param.getIncludeModules())); + } + if (!CollectionUtils.isEmpty(param.getExceptModules())) { + envs.addProperty("exceptModules", String.join(",", param.getExceptModules())); + } + + if (!CollectionUtils.isEmpty(param.getIncludeFunctions())) { + envs.addProperty("includeFunctions", String.join(",", param.getIncludeFunctions())); + } + if (!CollectionUtils.isEmpty(param.getExceptFunctions())) { + envs.addProperty("exceptFunctions", String.join(",", param.getExceptFunctions())); + } + + strategy.setEnvs(envs.toString()); + + strategy.setAlertMembers( + String.join(",", param.getAlertMembers() == null ? new ArrayList<>() : param.getAlertMembers())); + strategy.setAtMembers( + String.join(",", param.getAtMembers() == null ? new ArrayList<>() : param.getAtMembers())); + + if (!appAlarmStrategyDao.updateById(strategy)) { + return Result.fail(ErrorCode.unknownError); + } + + log.info("更新规则策略成功:strategy={}", strategy); + return Result.success(strategy); + } + + @Override + public Result enabled(String user, AlarmStrategyParam param) { + AlarmStrategy strategy = appAlarmStrategyDao.getById(param.getId()); + if (strategy == null) { + return Result.fail(ErrorCode.nonExistentStrategy); + } + AppMonitor app = + AlarmStrategyType.TESLA.getCode().equals(strategy.getStrategyType()) ? appMonitorDao.getByIamTreeId( + strategy.getIamId()) + : appMonitorDao.getMyApp(strategy.getAppId(), strategy.getIamId(), user, AppViewType.MyApp); + if (app == null) { + return Result.fail(ErrorCode.NoOperPermission); + } + Integer ruleStat = param.getStatus() == 0 ? 1 : 0; + Result result = appAlarmService.enabledRules(strategy.getIamId(), strategy.getId(), ruleStat, user); + if (result.getCode() != ErrorCode.success.getCode()) { + return result; + } + strategy = new AlarmStrategy(); + strategy.setId(param.getId()); + strategy.setStatus(param.getStatus()); + if (!appAlarmStrategyDao.updateById(strategy)) { + return Result.fail(ErrorCode.unknownError); + } + log.info("enabled规则策略成功:strategy={}", strategy); + return Result.success(null); + } + + @Override + public Result batchDeleteStrategy(String user, List strategyIds) { + + if (CollectionUtils.isEmpty(strategyIds)) { + log.error("batchDeleteStrategy param error!user:{} strategyIds:{}", user, strategyIds); + return Result.success(); + } + + for (Integer id : strategyIds) { + try { + Result result = deleteById(user, id); + if (!result.isSuccess()) { + log.error("deleteById fail! user : {}, id : {} ,result : {}", user, id, new Gson().toJson(result)); + } + } catch (Exception e) { + log.error("deleteById error!user:" + user + ",strategyId:" + id + ",exception:" + e.getMessage(), e); + } + } + + return Result.success(); + + } + + @Override + public Result deleteById(String user, Integer strategyId) { + if (strategyId == null) { + return Result.fail(ErrorCode.invalidParamError); + } + AlarmStrategy strategy = appAlarmStrategyDao.getById(strategyId); + if (strategy == null) { + return Result.fail(ErrorCode.nonExistentStrategy); + } + log.info("AlarmStrategyService.deleteById strategy : {}", new Gson().toJson(strategy)); + AppMonitor app = + AlarmStrategyType.TESLA.getCode().equals(strategy.getStrategyType()) ? appMonitorDao.getByIamTreeId( + strategy.getIamId()) + : appMonitorDao.getMyApp(strategy.getAppId(), strategy.getIamId(), user, AppViewType.MyApp); + if (app == null) { + return Result.fail(ErrorCode.NoOperPermission); + } + Result result = appAlarmService.deleteRulesByIamId(strategy.getIamId(), strategyId, user); + if (result.getCode() != ErrorCode.success.getCode()) { + return result; + } + if (!appAlarmStrategyDao.deleteById(strategyId)) { + return Result.fail(ErrorCode.unknownError); + } + log.info("删除规则策略成功:strategy={}", strategy); + return Result.success(null); + } + + @Override + public Result deleteByStrategyId(String user, Integer strategyId) { + if (strategyId == null) { + return Result.fail(ErrorCode.invalidParamError); + } + AlarmStrategy strategy = appAlarmStrategyDao.getById(strategyId); + if (strategy == null) { + return Result.fail(ErrorCode.nonExistentStrategy); + } + log.info("AlarmStrategyService.deleteById strategy : {}", new Gson().toJson(strategy)); + + Result result = appAlarmService.deleteRulesByIamId(strategy.getIamId(), strategyId, user); + if (result.getCode() != ErrorCode.success.getCode()) { + return result; + } + if (!appAlarmStrategyDao.deleteById(strategyId)) { + return Result.fail(ErrorCode.unknownError); + } + log.info("删除规则策略成功:strategy={}", strategy); + return Result.success(null); + } + + @Override + public Result detailed(String user, AlarmStrategyParam param) { + AlarmStrategyInfo info = appAlarmStrategyDao.getInfoById(param.getId()); + if (info != null) { + setRuleListData(info); + } + return Result.success(info); + } + + private void setRuleListData(AlarmStrategyInfo info) { + if (info == null) { + return; + } + List ruleList = appAlarmRuleDao.selectByStrategyId(info.getId()); + info.setAlarmRules(ruleList); + if (StringUtils.isBlank(info.getAlertTeam()) && !CollectionUtils.isEmpty(ruleList)) { + info.setAlertTeam(ruleList.get(0).getAlertTeam()); + } + } + + @Override + public Result>> search(String user, AlarmStrategyParam param) { + + if (!paramCheck(param)) { + return Result.fail(ErrorCode.invalidParamError); + } + AlarmStrategy strategy = new AlarmStrategy(); + strategy.setStrategyName(param.getStrategyName()); + strategy.setStrategyType(param.getStrategyType()); + strategy.setAppId(param.getAppId()); + strategy.setAppName(param.getAppName()); + strategy.setStatus(param.getStatus()); + Map deptMap = LocalUser.getDepts(); + if (deptMap.containsKey(3)) { + strategy.setGroup3(deptMap.get(3).getDeptName()); + } + if (deptMap.containsKey(4)) { + strategy.setGroup4(deptMap.get(4).getDeptName()); + } + if (deptMap.containsKey(5)) { + strategy.setGroup5(deptMap.get(5).getDeptName()); + } + PageData> pageData = appAlarmStrategyDao.searchByCond(user, param.isOwner(), strategy, + param.getPage(), param.getPageSize(), param.getSortBy(), param.getSortOrder()); + ruleDataHandler(pageData.getList()); + return Result.success(pageData); + } + + private boolean paramCheck(AlarmStrategyParam param) { + if (!orderColumn.contains(param.getSortBy())) { + log.info("param sortBy is error!"); + log.error("param sortBy is error!"); + return false; + } + if (!orderSorts.contains(param.getSortOrder())) { + log.info("param sortOrder is error!"); + log.error("param sortOrder is error!"); + return false; + } + return true; + } + + private List listStrategyByAppIdAndIamId(Integer appId, Integer iamId) { + + AlarmStrategy strategy = new AlarmStrategy(); + strategy.setStatus(0); + strategy.setAppId(appId); + strategy.setIamId(iamId); + PageData> listPageData = appAlarmStrategyDao.searchByCondNoUser(strategy, 1, 5000, null, + null); + if (listPageData == null) { + return null; + } + return listPageData.getList(); + } + + @Override + public void deleteByAppIdAndIamId(Integer appId, Integer iamId) { + List alarmStrategyInfos = listStrategyByAppIdAndIamId(appId, iamId); + if (CollectionUtils.isEmpty(alarmStrategyInfos)) { + log.info("AlarmStrategyService#deleteByAppIdAndIamId no data found!appId:{},iamId:{}", appId, iamId); + return; + } + + for (AlarmStrategyInfo strategy : alarmStrategyInfos) { + + Result result = appAlarmService.deleteRulesByIamId(strategy.getIamId(), strategy.getId(), null); + if (result.getCode() != ErrorCode.success.getCode()) { + log.error("deleteByAppIdAndIamId delete alarmRules fail! strategy : {}", strategy.toString()); + continue; + } + if (!appAlarmStrategyDao.deleteById(strategy.getId())) { + log.error("deleteByAppIdAndIamId delete strategy fail! strategy : {}", strategy.toString()); + continue; + } + } + } + + @Override + public Result dubboSearch(String user, AlarmStrategyParam param) { + StringBuilder metricStr = new StringBuilder(); + metricStr.append("sum(sum_over_time(staging_").append(param.getAppId()).append("_").append(param.getAppName()) + .append("_jaeger_dubboBisTotalCount_total{}[24h])) by (serviceName)"); + return prometheusService.queryByMetric(metricStr.toString()); + } + + private void ruleDataHandler(List infoList) { + if (infoList == null || infoList.isEmpty()) { + return; + } + Map infoMap = listToMap(infoList); + List ruleList = appAlarmRuleDao.selectByStrategyIdList( + infoMap.keySet().stream().collect(Collectors.toList())); + if (ruleList == null) { + return; + } + AlarmStrategyInfo info = null; + for (AppAlarmRule rule : ruleList) { + if (rule.getStrategyId() == null) { + continue; + } + info = infoMap.get(rule.getStrategyId()); + if (info == null) { + continue; + } + if (info.getAlarmRules() == null) { + info.setAlarmRules(new ArrayList<>()); + } + info.getAlarmRules().add(rule); + if (StringUtils.isBlank(info.getAlertTeam())) { + info.setAlertTeam(rule.getAlertTeam()); + } + } + } + + private Map listToMap(List infoList) { + if (CollectionUtils.isEmpty(infoList)) { + return null; + } + Map infoMap = new HashMap<>(); + for (AlarmStrategyInfo info : infoList) { + if (infoMap.containsKey(info.getId()) && !info.isOwner()) { + continue; + } + infoMap.put(info.getId(), info); + } + return infoMap; + } + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlertGroupServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlertGroupServiceImpl.java new file mode 100644 index 000000000..7bf6dea16 --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlertGroupServiceImpl.java @@ -0,0 +1,321 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.xiaomi.mone.monitor.bo.AlertGroupInfo; +import com.xiaomi.mone.monitor.bo.AlertGroupParam; +import com.xiaomi.mone.monitor.bo.UserInfo; +import com.xiaomi.mone.monitor.dao.AlertGroupDao; +import com.xiaomi.mone.monitor.dao.model.AlertGroup; +import com.xiaomi.mone.monitor.dao.model.AlertGroupMember; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.ExceptionCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.AlertGroupService; +import com.xiaomi.mone.monitor.service.alertmanager.AlertServiceAdapt; +import com.xiaomi.mone.monitor.service.helper.AlertHelper; +import com.xiaomi.mone.monitor.service.model.PageData; +import com.xiaomi.mone.monitor.service.user.UserConfigService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 报警组service + */ +@Slf4j +@Service +public class AlertGroupServiceImpl implements AlertGroupService { + + @Autowired + private AlertServiceAdapt alertServiceAdapt; + + @Autowired + private AlertHelper alertHelper; + + @Autowired + private AlertGroupDao alertGroupDao; + + @Autowired + UserConfigService userConfigService; + + /** + * 数据清洗专用 + * + * @return + */ + @Override + public Result initAlertGroupData() { + Result pageDataResult = alertServiceAdapt.getAlertGroupPageData(null, null, 1, 1000); + if (pageDataResult == null || pageDataResult.getData() == null || pageDataResult.getData().getList() == null) { + return Result.success(null); + } + JsonElement ele = (JsonElement) pageDataResult.getData().getList(); + JsonArray arr = ele.getAsJsonArray(); + arr.forEach(subEle -> { + AlertGroup ag = alertHelper.buildAlertGroup(subEle.getAsJsonObject()); + if (alertGroupDao.getByRelId("alert", ag.getRelId()) != null) { + return; + } + alertGroupDao.insert(ag); + }); + return Result.success(null); + } + + /** + * 用户搜索 + * + * @param user + * @param param + * @return + */ + @Override + public Result>> userSearch(String user, AlertGroupParam param) { + return alertServiceAdapt.searchUser(user, param.getName(), param.getPage(), param.getPageSize()); + } + + /** + * 告警组搜索 + * + * @param user + * @param param + * @return + */ + @Override + public Result>> alertGroupSearch(String user, AlertGroupParam param) { + PageData> pageData = new PageData<>(); + pageData.setPage(param.getPage()); + pageData.setPageSize(param.getPageSize()); + pageData.setTotal(0L); + boolean admin = userConfigService.isAdmin(user); + PageData> pageAgList = alertGroupDao.searchByCond(admin, user, param.getName(), + param.getType(), param.getPage(), param.getPageSize()); + if (pageAgList != null && pageAgList.getList() != null) { + pageData.setTotal(pageAgList.getTotal()); + pageData.setList(alertHelper.buildAlertGroupInfoList(admin, user, pageAgList.getList())); + } + return Result.success(pageData); + } + + /** + * 告警组查询通过id列表 + * + * @param user + * @param ids + * @return + */ + @Override + public Result> queryByIds(String user, List ids) { + List agList = alertGroupDao.getByIds(ids, true); + boolean admin = userConfigService.isAdmin(user); + return Result.success(alertHelper.buildAlertGroupInfoList(admin, user, agList)); + } + + /** + * 告警组同步 + * + * @param user + * @param type + * @return + */ + @Override + public Result sync(String user, String type) { + Result pageDataResult = alertServiceAdapt.getAlertGroupPageData(user, null, 1, 100); + if (pageDataResult == null || !pageDataResult.isSuccess()) { + Result.fail(ErrorCode.unknownError); + } + if (pageDataResult.getData() == null || pageDataResult.getData().getList() == null) { + return Result.success(null); + } + JsonArray list = (JsonArray) pageDataResult.getData().getList(); + if (list.isEmpty()) { + return Result.success(null); + } + Map relIdMap = new HashMap<>(); + for (int idx = 0; idx < list.size(); idx++) { + AlertGroup ag = alertHelper.buildAlertGroup((JsonObject) list.get(idx)); + ag.setType(type); + relIdMap.put(ag.getRelId(), ag); + } + List dbAgList = alertGroupDao.getByRelIds(type, new ArrayList<>(relIdMap.keySet())); + if (!CollectionUtils.isEmpty(dbAgList)) { + dbAgList.stream().filter(g -> g.getRelId() != null).forEach(g -> relIdMap.remove(g.getRelId())); + } + relIdMap.values().forEach(g -> alertGroupDao.insert(g)); + return Result.success(null); + } + + /** + * 告警组搜索 + * + * @param user + * @param param + * @return + */ + @Override + public Result> alertGroupSearchByIds(String user, AlertGroupParam param) { + Map mapData = new HashMap<>(); + AlertGroupInfo groupInfo = null; + AlertGroup am = null; + boolean admin = userConfigService.isAdmin(user); + for (Integer relId : param.getRelIds()) { + am = alertGroupDao.getByRelId(param.getType(), relId); + groupInfo = alertHelper.buildAlertGroupInfo(admin, user, am); + if (groupInfo == null) { + continue; + } + mapData.put(relId.longValue(), groupInfo); + } + return Result.success(mapData); + } + + /** + * 告警组详情 + * + * @param user + * @param param + * @return + */ + @Override + public Result alertGroupDetailed(String user, AlertGroupParam param) { + AlertGroup ag = alertGroupDao.getById(param.getId()); + boolean isAdmin = userConfigService.isAdmin(user); + return Result.success(alertHelper.buildAlertGroupInfo(isAdmin, user, ag)); + } + + /** + * 告警组创建 + * + * @param user + * @param param + * @return + */ + @Override + public Result alertGroupCreate(String user, AlertGroupParam param) { + Result result = alertServiceAdapt.createAlertGroup(user, param.getName(), param.getNote(), + param.getChatId(), param.getMemberIds(), param.getDutyInfo()); + if (result == null) { + return Result.fail(ErrorCode.unknownError); + } + if (result.getCode() != ErrorCode.success.getCode()) { + return Result.fail(new ExceptionCode(result.getCode(), result.getMessage())); + } + param.setId(result.getData().get("id").getAsLong()); + Result resultData = alertServiceAdapt.getAlertGroup(user, param.getId()); + if (resultData == null || resultData.getData() == null) { + return Result.fail(ErrorCode.unknownError); + } + AlertGroup ag = alertHelper.buildAlertGroup(resultData.getData()); + ag.setType(param.getType()); + if (!alertGroupDao.insert(ag)) { + return Result.fail(ErrorCode.OperFailed); + } + boolean isAdmin = userConfigService.isAdmin(user); + return Result.success(alertHelper.buildAlertGroupInfo(isAdmin, user, ag)); + } + + /** + * 告警组编辑 + * + * @param user + * @param param + * @return + */ + @Override + public Result alertGroupEdit(String user, AlertGroupParam param) { + AlertGroup ag = alertGroupDao.getById(param.getId()); + if (ag == null) { + return Result.fail(ErrorCode.NoOperPermission); + } + Result result = alertServiceAdapt.editAlertGroup(user, ag.getRelId(), param.getName(), param.getNote(), + param.getChatId(), param.getMemberIds(), param.getDutyInfo()); + if (result == null) { + return Result.fail(ErrorCode.unknownError); + } + if (result.getCode() != ErrorCode.success.getCode()) { + return result; + } + Result resultData = alertServiceAdapt.getAlertGroup(user, ag.getRelId()); + if (resultData == null || resultData.getData() == null) { + return Result.fail(ErrorCode.unknownError); + } + AlertGroup newAg = alertHelper.buildAlertGroup(resultData.getData()); + newAg.setId(ag.getId()); + List addMembers = alertHelper.getDiffAgMember(ag.getMembers(), newAg.getMembers()); + List delMembers = alertHelper.getDiffAgMember(newAg.getMembers(), ag.getMembers()); + if (!alertGroupDao.updateById(newAg, addMembers, delMembers)) { + return Result.fail(ErrorCode.OperFailed); + } + return Result.success(null); + } + + /** + * 告警组删除 + * + * @param user + * @param param + * @return + */ + @Override + public Result alertGroupDelete(String user, AlertGroupParam param) { + AlertGroup ag = alertGroupDao.getById(param.getId()); + if (ag == null) { + return Result.fail(ErrorCode.NoOperPermission); + } + Result resultData = alertServiceAdapt.getAlertGroup(user, ag.getRelId()); + if (resultData == null || resultData.getData() == null) { + return Result.fail(ErrorCode.unknownError); + } + if (resultData.getData().has("used") && resultData.getData().get("used").getAsInt() != 0) { + return Result.fail(ErrorCode.ALERT_GROUP_USED_FAIL); + } + Result result = alertServiceAdapt.deleteAlertGroup(user, ag.getRelId()); + if (result == null) { + return Result.fail(ErrorCode.unknownError); + } + if (result.getCode() != ErrorCode.success.getCode()) { + return result; + } + alertGroupDao.delete(ag); + return Result.success(null); + } + + @Override + public Result dutyInfoList(String user, AlertGroupParam param) { + Result result = alertServiceAdapt.dutyInfoList(user, param.getId(), param.getStart(), + param.getEnd()); + if (result.isSuccess() && result.getData() != null) { + JsonArray asJsonArray = result.getData().getAsJsonArray(); + List list = JSONObject.parseArray(new Gson().toJson(asJsonArray), Map.class); + return Result.success(list); + } + log.info("dutyInfoList param:{}, result:{}", param.toString(), new Gson().toJson(result)); + return result; + } + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlertHistoryServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlertHistoryServiceImpl.java new file mode 100644 index 000000000..7499ab86d --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AlertHistoryServiceImpl.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.xiaomi.mone.monitor.bo.AlertHistory; +import com.xiaomi.mone.monitor.bo.AlertHistoryDetailed; +import com.xiaomi.mone.monitor.bo.AlertHistoryParam; +import com.xiaomi.mone.monitor.dao.AppMonitorDao; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.AlertHistoryService; +import com.xiaomi.mone.monitor.service.helper.AlertHelper; +import com.xiaomi.mone.monitor.service.model.PageData; +import com.xiaomi.mone.monitor.service.prometheus.AlarmService; +import com.xiaomi.mone.monitor.utils.CommonUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @author zhanggaofeng1 + */ +@Slf4j +@Service +public class AlertHistoryServiceImpl implements AlertHistoryService { + + @Autowired + private AlarmService alarmService; + + @Autowired + private AlertHelper alertHelper; + + @Autowired + private AppMonitorDao appMonitorDao; + + @Override + public Result>> metricList(String user, AlertHistoryParam param) { + PageData> pageData = new PageData(); + pageData.setPage(param.getPage()); + pageData.setPageSize(param.getPageSize()); + pageData.setTotal(0L); + Set treeIdSet = null; + if (param.getIamTreeId() != null) { + treeIdSet = new HashSet<>(); + treeIdSet.add(param.getIamTreeId()); + } else { + treeIdSet = appMonitorDao.selectTreeIdByOwnerOrCareUser(user); + } + if (CollectionUtils.isEmpty(treeIdSet)) { + return Result.success(pageData); + } + JsonObject labels = alertHelper.buildLabels(param); + Long startTime = CommonUtil.toSeconds(param.getStartTime()); + Long endTime = CommonUtil.toSeconds(param.getEndTime()); + Result pageResult = alarmService.queryLatestEvents(treeIdSet, param.getAlertStat(), + param.getAlertLevel(), startTime, endTime, param.getPage(), param.getPageSize(), labels); + if (pageResult.getData() != null) { + pageData.setTotal(pageResult.getData().getTotal()); + pageData.setList(alertHelper.buildAlertHistoryList((JsonElement) pageResult.getData().getList())); + } + return Result.success(pageData); + } + + @Override + public Result metricDetailed(String user, AlertHistoryParam param) { + Result dataResult = alarmService.getEventById(user, param.getIamTreeId(), param.getId()); + if (dataResult == null || dataResult.getData() == null) { + return Result.fail(ErrorCode.ALERT_NOT_FOUND); + } + return Result.success(alertHelper.buildAlertHistoryDetailed(dataResult.getData())); + } + + @Override + public Result metricResolved(String user, AlertHistoryParam param) { + Long startTime = CommonUtil.toSeconds(param.getStartTime()); + Long endTime = CommonUtil.toSeconds(param.getEndTime()); + Result dataResult = alarmService.resolvedEvent(user, param.getIamTreeId(), param.getAlertName(), + param.getComment(), startTime, endTime); + if (dataResult == null || dataResult.getCode() != ErrorCode.success.getCode()) { + return Result.fail(ErrorCode.unknownError); + } + return Result.success(null); + } + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppAlarmServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppAlarmServiceImpl.java new file mode 100644 index 000000000..544d31a79 --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppAlarmServiceImpl.java @@ -0,0 +1,1108 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.alibaba.nacos.api.config.annotation.NacosValue; +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.xiaomi.mone.monitor.bo.AlarmCheckDataCount; +import com.xiaomi.mone.monitor.bo.AlarmRuleMetricType; +import com.xiaomi.mone.monitor.bo.AlarmRuleTemplateType; +import com.xiaomi.mone.monitor.bo.AlarmRuleType; +import com.xiaomi.mone.monitor.bo.AlarmSendInterval; +import com.xiaomi.mone.monitor.bo.AlarmStrategyType; +import com.xiaomi.mone.monitor.bo.AppViewType; +import com.xiaomi.mone.monitor.bo.RuleStatusType; +import com.xiaomi.mone.monitor.dao.AppAlarmRuleDao; +import com.xiaomi.mone.monitor.dao.AppAlarmRuleTemplateDao; +import com.xiaomi.mone.monitor.dao.AppMonitorDao; +import com.xiaomi.mone.monitor.dao.model.AlarmStrategy; +import com.xiaomi.mone.monitor.dao.model.AppAlarmRule; +import com.xiaomi.mone.monitor.dao.model.AppAlarmRuleTemplate; +import com.xiaomi.mone.monitor.dao.model.AppMonitor; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.AlarmStrategyService; +import com.xiaomi.mone.monitor.service.AppAlarmService; +import com.xiaomi.mone.monitor.service.AppMonitorService; +import com.xiaomi.mone.monitor.service.aop.context.HeraRequestMappingContext; +import com.xiaomi.mone.monitor.service.api.AlarmPresetMetricsService; +import com.xiaomi.mone.monitor.service.api.AppAlarmServiceExtension; +import com.xiaomi.mone.monitor.service.model.PageData; +import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleData; +import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleRequest; +import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleTemplateRequest; +import com.xiaomi.mone.monitor.service.model.prometheus.AlarmTemplateResponse; +import com.xiaomi.mone.monitor.service.model.prometheus.AppAlarmRuleTemplateQuery; +import com.xiaomi.mone.monitor.service.model.prometheus.AppWithAlarmRules; +import com.xiaomi.mone.monitor.service.model.prometheus.ProjectAlarmInfo; +import com.xiaomi.mone.monitor.service.prometheus.AlarmService; +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.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * @author gaoxihui + */ +@Slf4j +@Service +public class AppAlarmServiceImpl implements AppAlarmService { + + @Autowired + AppAlarmRuleDao appAlarmRuleDao; + + @Autowired + AppAlarmRuleTemplateDao appAlarmRuleTemplateDao; + + @Autowired + AppMonitorService appMonitorService; + + @Autowired + AlarmService alarmService; + + @Autowired + private AppMonitorDao appMonitorDao; + + @Autowired + private AlarmStrategyService alarmStrategyService; + + @Autowired + private AlarmPresetMetricsService alarmPresetMetricsService; + + @Autowired + private AppAlarmServiceExtension appAlarmServiceExtension; + + + @NacosValue("${rule.evaluation.interval:20}") + private Integer evaluationInterval; + + @NacosValue("${rule.evaluation.unit:s}") + private String evaluationUnit; + + @Value("${prometheus.alarm.env:staging}") + private String prometheusAlarmEnv; + + @Value("${server.type}") + private String env; + + + @Value("${alert.manager.env:staging}") + private String alertManagerEnv; + + + @Override + @Deprecated + public void alarmRuleSwitchPlat(AppAlarmRule oldRule, Integer newProjectId, Integer newIamId, String oldProjectName, + String newProjectName) { + + Result alarmRuleRemote = alarmService.getAlarmRuleRemote(oldRule.getAlarmId(), + oldRule.getProjectId(), oldRule.getCreater()); + if (!alarmRuleRemote.isSuccess()) { + log.error( + "appPlatMove update get remote rule fail!oldRule:{},newProjectId:{},newIamId:{},newProjectName:{}", + oldRule, newProjectId, newIamId, newProjectName); + return; + } + JsonElement remoteRule = alarmRuleRemote.getData(); + + if (remoteRule == null) { + log.error( + "appPlatMove update no remote rule found!oldRule:{},newProjectId:{},newIamId:{},newProjectName:{}", + oldRule, newProjectId, newIamId, newProjectName); + return; + } + + JsonObject asJsonObject = remoteRule.getAsJsonObject(); + + /** + * 表达式中的projectId、projectName替换 + */ + String expr = asJsonObject.get("expr").getAsString(); + + String oldApplication = oldRule.getProjectId() + "_" + oldProjectName.replaceAll("-", "_"); + String newApplication = newProjectId + "_" + newProjectName.replaceAll("-", "_"); + String newExpr = expr.replace(oldApplication, newApplication); + asJsonObject.remove("expr"); + asJsonObject.addProperty("expr", newExpr); + + /** + * 替换iamId + */ + asJsonObject.remove("tree_id"); + asJsonObject.addProperty("tree_id", newIamId); + + /** + * labels内容替换 + */ + + JsonObject labels = asJsonObject.getAsJsonObject("labels"); + labels.remove("project_id"); + labels.addProperty("project_id", newProjectId); + labels.remove("project_name"); + labels.addProperty("project_name", newProjectName); + labels.remove("app_iam_id"); + labels.addProperty("app_iam_id", newIamId); + + Result result = alarmService.updateAlarm(oldRule.getAlarmId(), oldRule.getIamId(), oldRule.getCreater(), + asJsonObject.toString()); + + log.info("alarmRuleSwitchPlat updateAlarm request body:{},response{}", asJsonObject.toString(), + new Gson().toJson(result)); + if (result.isSuccess()) { + + } + + } + + @Override + public Result queryFunctionList(Integer projectId) { + return appAlarmServiceExtension.queryFunctionList(projectId); + } + + @Override + public Result queryRulesByAppName(String appName, String userName, Integer page, Integer pageSize) { + + if (page == null) { + page = 1; + } + if (pageSize == null) { + pageSize = 10; + } + PageData pd = new PageData(); + pd.setPage(page); + pd.setPageSize(pageSize); + + Long aLong = appAlarmRuleDao.countAlarmRuleByAppName(userName, appName); + pd.setTotal(aLong); + + List resultList = appAlarmRuleDao.queryRulesByAppName(userName, appName, page, pageSize); + if (!CollectionUtils.isEmpty(resultList)) { + for (AppWithAlarmRules appWithAlarmRule : resultList) { + List alarmRules = appWithAlarmRule.getAlarmRules(); + + appWithAlarmRule.setMetricMap(alarmPresetMetricsService.getEnumMap()); + appWithAlarmRule.setCheckDataMap(AlarmCheckDataCount.getEnumMap()); + appWithAlarmRule.setSendIntervalMap(AlarmSendInterval.getEnumMap()); + + AppAlarmRule rule = new AppAlarmRule(); + rule.setIamId(appWithAlarmRule.getIamId()); + rule.setStatus(0); + List rules = appAlarmRuleDao.query(rule, 0, Integer.MAX_VALUE); + appWithAlarmRule.setAlarmRules(rules); + if (!CollectionUtils.isEmpty(rules)) { + AppAlarmRule rule1 = rules.get(0); + appWithAlarmRule.setCreater(rule1.getCreater()); + appWithAlarmRule.setLastUpdateTime(rule1.getUpdateTime()); + appWithAlarmRule.setRuleStatus(rule1.getRuleStatus()); + } + } + + } + + // Result>> appsResult = appMonitorService.listMyApp(appName, userName, page, pageSize); + // if(appsResult.getData() == null || CollectionUtils.isEmpty(appsResult.getData().getList())){ + // pd.setTotal(0l); + // return Result.success(pd); + // } + // + // pd.setTotal(appsResult.getData().getTotal()); + // + // List list = appsResult.getData().getList(); + // + // List resultList = new ArrayList<>(); + // + // for(AppMonitor app : list){ + // + // log.info("queryRulesByAppName : app id :{},appname:{},iamTreeId:{}",app.getId(),app.getProjectName(),app.getIamTreeId()); + // AppWithAlarmRules appAlarmRuleList = new AppWithAlarmRules(); + // appAlarmRuleList.setAppName(app.getProjectName()); + // appAlarmRuleList.setIamId(app.getIamTreeId()); + // appAlarmRuleList.setProjectId(app.getProjectId()); + // + // AppAlarmRule rule = new AppAlarmRule(); + // rule.setIamId(app.getIamTreeId()); + // rule.setStatus(0); + // List rules = appAlarmRuleDao.query(rule, 0, Integer.MAX_VALUE); + // + // appAlarmRuleList.setAlarmRules(rules); + // if(!CollectionUtils.isEmpty(rules)){ + // AppAlarmRule rule1 = rules.get(0); + // appAlarmRuleList.setCreater(rule1.getCreater()); + // appAlarmRuleList.setLastUpdateTime(rule1.getUpdateTime()); + // appAlarmRuleList.setRuleStatus(rule1.getRuleStatus()); + // appAlarmRuleList.setMetricMap(AlarmPresetMetrics.getEnumMap()); + // appAlarmRuleList.setCheckDataMap(AlarmCheckDataCount.getEnumMap()); + // appAlarmRuleList.setSendIntervalMap(AlarmSendInterval.getEnumMap()); + // + // } + // resultList.add(appAlarmRuleList); + // } + + pd.setList(resultList); + + return Result.success(pd); + + } + + @Override + public Result queryNoRulesConfig(String appName, String userName, Integer page, Integer pageSize) { + + if (page == null) { + page = 1; + } + if (pageSize == null) { + pageSize = 10; + } + PageData pd = new PageData(); + pd.setPage(page); + pd.setPageSize(pageSize); + + Long aLong = appAlarmRuleDao.countAppNoAlarmRulesConfig(userName, appName); + pd.setTotal(aLong); + + List resultList = appAlarmRuleDao.queryAppNoAlarmRulesConfig(userName, appName, page, + pageSize); + + pd.setList(resultList); + + return Result.success(pd); + + } + + @Override + public Result queryRulesByIamId(Integer iamId, String userName) { + return appAlarmServiceExtension.queryRulesByIamId(iamId, userName); + } + + @Override + public Integer getAlarmConfigNumByTeslaGroup(String group) { + AppAlarmRule rule = new AppAlarmRule(); + rule.setLabels(group); + rule.setStatus(0); + Long aLong = appAlarmRuleDao.countByExample(rule); + return aLong == null ? 0 : aLong.intValue(); + } + + + @Override + public Result addRulesWithStrategy(AlarmRuleRequest param) { + + /** + * 校验当前操作人是否具有权限 + */ + AppMonitor app = null; + if (param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())) { + app = appMonitorDao.getByIamTreeId(param.getIamId()); + } else { + app = appMonitorDao.getMyApp(param.getProjectId(), param.getIamId(), param.getUser(), AppViewType.MyApp); + } + + if (app == null) { + log.error("不存在projectId={}的项目", param.getProjectId()); + return Result.fail(ErrorCode.NoOperPermission); + } + + /** + * 创建策略 + */ + AlarmStrategy strategy = alarmStrategyService.create(param, app); + if (strategy == null) { + log.error("规则策略创建失败; strategyResult={}", strategy); + return Result.fail(ErrorCode.unknownError); + } + + Integer strategyId = strategy.getId(); + HeraRequestMappingContext.set("strategyId", strategyId); + + param.setStrategyId(strategyId); + + return addRules(param, app); + + } + + @Override + public Result batchAddRulesWithStrategy(AlarmRuleRequest param) { + + List projectsAlarmInfo = param.getProjectsAlarmInfo(); + if (projectsAlarmInfo == null) { + projectsAlarmInfo = Lists.newArrayList(); + } + if (param.getIamId() != null && param.getProjectId() != null) { + ProjectAlarmInfo projectAlarmInfo = new ProjectAlarmInfo(); + projectAlarmInfo.setIamId(param.getIamId()); + projectAlarmInfo.setIamType(param.getIamType()); + projectAlarmInfo.setProjectId(param.getProjectId()); + projectsAlarmInfo.add(projectAlarmInfo); + } + + if (CollectionUtils.isEmpty(projectsAlarmInfo)) { + log.info("batchAddRulesWithStrategy no projectsAlarmInfo found!"); + return Result.fail(ErrorCode.invalidParamError); + } + + for (ProjectAlarmInfo t : projectsAlarmInfo) { + + param.setProjectId(t.getProjectId()); + param.setIamType(t.getIamType()); + param.setIamId(t.getIamId()); + + /** + * check permission for current user. + */ + AppMonitor app = null; + if (param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())) { + app = appMonitorDao.getByIamTreeId(param.getIamId()); + } else { + app = appMonitorDao.getMyApp(param.getProjectId(), param.getIamId(), param.getUser(), + AppViewType.MyApp); + } + + if (app == null) { + log.error("batchAddRulesWithStrategy# current user has not permission for projectId={} param:{}", + param.getProjectId(), param.toString()); + continue; + } + + /** + * create strategy + */ + AlarmStrategy strategy = alarmStrategyService.create(param, app); + if (strategy == null) { + log.error("batchAddRulesWithStrategy remote fail; param:{},strategyResult={}", param.toString(), + strategy.toString()); + continue; + } + + Integer strategyId = strategy.getId(); + HeraRequestMappingContext.set("strategyId", strategyId); + + param.setStrategyId(strategyId); + + Result result = addRules(param, app); + if (!result.isSuccess()) { + log.error("batchAddRulesWithStrategy#local create strategy fail! param:{},result:{}", param.toString(), + new Gson().toJson(result)); + } + + } + + return Result.success(); + } + + @Override + public Result addRules(AlarmRuleRequest param, AppMonitor app) { + + for (AlarmRuleData ruleData : param.getAlarmRules()) { + + ruleData.setIncludeEnvs(param.getIncludeEnvs()); + ruleData.setExceptEnvs(param.getExceptEnvs()); + ruleData.setIncludeZones(param.getIncludeZones()); + ruleData.setExceptZones(param.getExceptZones()); + ruleData.setIncludeContainerName(param.getIncludeContainerName()); + ruleData.setExceptContainerName(param.getExceptContainerName()); + ruleData.setAlertMembers(param.getAlertMembers()); + ruleData.setAtMembers(param.getAtMembers()); + + ruleData.setIncludeFunctions(param.getIncludeFunctions()); + ruleData.setExceptFunctions(param.getExceptFunctions()); + ruleData.setIncludeModules(param.getIncludeModules()); + ruleData.setExceptModules(param.getExceptModules()); + + ruleData.convertLabels(); + AppAlarmRule rule = new AppAlarmRule(); + BeanUtils.copyProperties(ruleData, rule); + StringBuilder cname = new StringBuilder(); + cname.append(param.getIamId() != null ? param.getIamId() : param.getProjectId()); + if (param.getStrategyType().intValue() == AlarmStrategyType.PAOMQL.getCode()) { + if (rule.getMetricType() == null) { + rule.setMetricType(AlarmRuleMetricType.customer_promql.getCode()); + } + + if (StringUtils.isBlank(ruleData.getAlert())) { + cname.append("-").append(AlarmRuleMetricType.customer_promql.getDesc()); + } else { + cname.append("-").append(ruleData.getAlert()); + } + + cname.append("-").append(System.currentTimeMillis()); + rule.setCname(cname.toString()); + rule.setAlert(StringUtils.isBlank(rule.getAlert()) ? cname.toString() : rule.getAlert()); + } else { + if (rule.getMetricType() == null) { + rule.setMetricType(AlarmRuleMetricType.preset.getCode()); + } + cname.append("-").append(ruleData.getAlert()); + cname.append("-").append(System.currentTimeMillis()); + rule.setCname(cname.toString()); + } + + rule.setRuleType(AlarmRuleType.app_config.getCode()); + rule.setProjectId(param.getProjectId()); + + rule.setIamId(param.getIamId()); + int alarmForTime = evaluationInterval * ruleData.getDataCount(); + String alarmForTimeS = alarmForTime + evaluationUnit; + rule.setForTime(alarmForTimeS); + rule.setRuleGroup("group" + param.getIamId()); + rule.setEnv(prometheusAlarmEnv); + rule.setStatus(0); + rule.setCreater(param.getUser()); + rule.setRuleStatus(RuleStatusType.active.getCode()); + + String remark = null; + if (rule.getMetricType() == AlarmRuleMetricType.customer_promql.getCode()) { + remark = StringUtils.isBlank(rule.getRemark()) ? param.getStrategyDesc() : rule.getRemark(); + } else { + remark = param.getStrategyDesc(); + } + rule.setRemark(remark); + + rule.setCreateTime(new Date()); + rule.setUpdateTime(new Date()); + rule.setStrategyId(param.getStrategyId()); + + Result result = alarmService.addRule(app, rule, param.getUser(), ruleData); + if (result.getCode() != 0) { + log.error("AppAlarmService.addRules error! remote add ruleData fail!ruleData:{}", rule.toString()); + return Result.fail(ErrorCode.unknownError); + } + + Integer alarmId = appAlarmServiceExtension.getAlarmIdByResult(result); + rule.setAlarmId(alarmId); + + int i = appAlarmRuleDao.create(rule); + if (i < 0) { + log.error("AppAlarmService.addRules error! add ruleData data fail!ruleData:{}", rule.toString()); + return Result.fail(ErrorCode.unknownError); + } + + } + return Result.success(null); + + } + + + @Override + @Deprecated + public Result editRules(List rules, AlarmRuleRequest param, String user, String userName) { + + Result result = alarmStrategyService.deleteById(user, param.getStrategyId()); + if (result.getCode() != ErrorCode.success.getCode()) { + return result; + } + + /** + * 校验当前操作人是否具有权限 + */ + AppMonitor app = null; + if (param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())) { + app = appMonitorDao.getByIamTreeId(param.getIamId()); + } else { + app = appMonitorDao.getMyApp(param.getProjectId(), param.getIamId(), user, AppViewType.MyApp); + } + + if (app == null) { + log.error("不存在projectId={}的项目", param.getProjectId()); + return Result.fail(ErrorCode.NoOperPermission); + } + + /** + * 创建策略 + */ + AlarmStrategy strategy = alarmStrategyService.create(param, app); + if (strategy == null) { + log.error("规则策略创建失败; strategyResult={}", strategy); + return Result.fail(ErrorCode.unknownError); + } + + Integer strategyId = strategy.getId(); + HeraRequestMappingContext.set("strategyId", strategyId); + + param.setStrategyId(strategyId); + + return addRules(param, app); + + } + + @Override + public Result delAlarmRules(List ids, String user) { + + if (CollectionUtils.isEmpty(ids)) { + return Result.fail(ErrorCode.invalidParamError); + } + + for (Integer id : ids) { + + AppAlarmRule rule = appAlarmRuleDao.getById(id); + if (rule == null) { + return Result.fail(ErrorCode.nonExistentAlarmRule); + } + + Result result = alarmService.deleteRule(rule.getAlarmId(), rule.getIamId(), user); + if (!result.isSuccess()) { + return Result.fail(ErrorCode.DeleteJobFail); + } + + int i = appAlarmRuleDao.delById(id); + if (i < 1) { + log.error("fail to delete rule in db,id:{}", id); + return Result.fail(ErrorCode.FAIL_TO_DELETE_RULE_IN_DB); + } + } + + return Result.success(); + + } + + @Override + public Result editAlarmRuleSingle(AlarmRuleData ruleData, String user) { + + if (ruleData == null) { + log.error("editAlarmRuleSingle invalid ruleData : {} ", ruleData); + return Result.fail(ErrorCode.invalidParamError); + } + + /** + * 检查策略是否存在 + */ + AlarmStrategy alarmStrategy = alarmStrategyService.getById(ruleData.getStrategyId()); + if (alarmStrategy == null) { + log.error("editAlarmRuleSingle strategy is not exist!ruleData:{}", ruleData); + return Result.fail(ErrorCode.nonExistentStrategy); + } + + /** + * 校验当前操作人是否具有权限 + */ + AppMonitor app = null; + if (alarmStrategy.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())) { + app = appMonitorDao.getByIamTreeId(ruleData.getIamId()); + } else { + app = appMonitorDao.getMyApp(ruleData.getProjectId(), ruleData.getIamId(), user, AppViewType.MyApp); + } + + if (app == null) { + log.error("不存在 owner 为 user : {}, projectId={}的项目", user, ruleData.getProjectId()); + return Result.fail(ErrorCode.NoOperPermission); + } + + ruleData.setAlertMembers(StringUtils.isBlank(alarmStrategy.getAlertMembers()) ? null + : Arrays.asList(alarmStrategy.getAlertMembers().split(","))); + ruleData.setAtMembers(StringUtils.isBlank(alarmStrategy.getAtMembers()) ? null + : Arrays.asList(alarmStrategy.getAtMembers().split(","))); + + return editAlarmRule(ruleData, alarmStrategy, app, user); + + } + + @Override + public Result editRulesByStrategy(AlarmRuleRequest param) { + + /** + * 参数校验 + */ + List alarmRuleDatas = param.getAlarmRules(); + if (CollectionUtils.isEmpty(alarmRuleDatas)) { + log.error("editRules no rule data found!param:{}", param); + return Result.fail(ErrorCode.invalidParamError); + } + + /** + * 校验当前操作人是否具有权限 + */ + AppMonitor app = null; + if (param.getStrategyType().equals(AlarmStrategyType.TESLA.getCode())) { + app = appMonitorDao.getByIamTreeId(param.getIamId()); + } else { + app = appMonitorDao.getMyApp(param.getProjectId(), param.getIamId(), param.getUser(), AppViewType.MyApp); + } + + if (app == null) { + log.error("不存在projectId={}的项目", param.getProjectId()); + return Result.fail(ErrorCode.NoOperPermission); + } + + /** + * 检查策略是否存在 + */ + AlarmStrategy alarmStrategy = alarmStrategyService.getById(param.getStrategyId()); + if (alarmStrategy == null) { + log.error("the strategy is not exist!param:{}", param.toString()); + return Result.fail(ErrorCode.nonExistentStrategy); + } + + /** + * 过滤参数中新增的规则(rule的id为null),进行创建 + */ + List addRules = alarmRuleDatas.stream().filter(t -> t.getId() == null) + .collect(Collectors.toList()); + + if (!CollectionUtils.isEmpty(addRules)) { + + //指定要添加的报警规则列表 + param.setAlarmRules(addRules); + Result result1 = addRules(param, app); + if (result1.getCode() != 0) { + return result1; + } + } + + /** + * 检查报警组、通知人列表、选择的环境是否有更新,有更新则级联更新历史报警规则 + */ + if (!nullToEmpty(alarmStrategy.getAlertTeam()).equals(nullToEmpty(param.getAlertTeam())) || !nullToEmpty( + alarmStrategy.getAlertMembers()).equals(nullToEmpty(String.join(",", param.getAlertMembers()))) + || !nullToEmpty(alarmStrategy.getAtMembers()).equals( + nullToEmpty(String.join(",", param.getAtMembers() == null ? new ArrayList<>() : param.getAtMembers()))) + || !nullToEmpty(alarmStrategy.getEnvs()).equals(nullToEmpty(param.convertEnvs())) || !nullToEmpty( + alarmStrategy.getDesc()).equals(nullToEmpty(param.getStrategyDesc()))) { + + alarmStrategy.setDesc(param.getStrategyDesc()); + + List updateRules = alarmRuleDatas.stream().filter(t -> t.getId() != null) + .collect(Collectors.toList()); + + if (!CollectionUtils.isEmpty(updateRules)) { + + AppMonitor appMonitor = app; + + Optional failResult = Optional.empty(); + + updateRules.forEach(ruleData -> { + + ruleData.setIncludeEnvs(param.getIncludeEnvs()); + ruleData.setExceptEnvs(param.getExceptEnvs()); + ruleData.setIncludeZones(param.getIncludeZones()); + ruleData.setExceptZones(param.getExceptZones()); + ruleData.setIncludeContainerName(param.getIncludeContainerName()); + ruleData.setExceptContainerName(param.getExceptContainerName()); + ruleData.setIncludeModules(param.getIncludeModules()); + ruleData.setExceptModules(param.getExceptModules()); + ruleData.setIncludeFunctions(param.getIncludeFunctions()); + ruleData.setExceptFunctions(param.getExceptFunctions()); + + ruleData.setAlertMembers(param.getAlertMembers()); + ruleData.setAtMembers(param.getAtMembers()); + + Result result = editAlarmRule(ruleData, alarmStrategy, appMonitor, param.getUser()); + if (!result.isSuccess()) { + failResult.orElse(result); + return; + } + + }); + + if (failResult.isPresent()) { + return failResult.get(); + } + + } + + } + + /** + * 更新策略信息 + */ + Result strategyUpdateResult = alarmStrategyService.updateByParam(param); + if (strategyUpdateResult.getCode() != ErrorCode.success.getCode()) { + log.error("update strategy in db fail!param:{}", param.toString()); + return Result.fail(ErrorCode.ALARM_STRATEGY_INFO_UPDATE_FAIL); + } + + return Result.success(null); + } + + @Override + public Result editAlarmRule(AlarmRuleData ruleData, AlarmStrategy alarmStrategy, AppMonitor app, String user) { + + AppAlarmRule rule = appAlarmRuleDao.getById(ruleData.getId()); + if (rule == null) { + log.info("edit alarm rule,no data found in db!ruleData:{}", ruleData); + return Result.fail(ErrorCode.nonExistentAlarmRule); + } + + ruleData.convertLabels(); + + rule.setOp(ruleData.getOp()); + rule.setValue(ruleData.getValue()); + + int alarmForTime = evaluationInterval * ruleData.getDataCount(); + String alarmForTimeS = alarmForTime + evaluationUnit; + rule.setForTime(alarmForTimeS); + rule.setDataCount(ruleData.getDataCount()); + + rule.setPriority(ruleData.getPriority()); + rule.setSendInterval(ruleData.getSendInterval()); + + rule.setAlertTeam(ruleData.getAlertTeam()); + + rule.setLabels(ruleData.getLabels()); + + /** + * 兼容自定义报警单条填写描述信息 + */ + String remark = null; + if (rule.getMetricType() == AlarmRuleMetricType.customer_promql.getCode()) { + remark = StringUtils.isNotBlank(ruleData.getRemark()) ? ruleData.getRemark() : alarmStrategy.getDesc(); + } else { + remark = alarmStrategy.getDesc(); + } + rule.setRemark(remark); + + rule.setUpdateTime(new Date()); + + Result result = alarmService.editRule(rule, ruleData, app, user); + + if (result.getCode() != 0) { + log.error("AppAlarmService.editRules error! remote add ruleData fail!ruleData:{}", rule.toString()); + return Result.fail(ErrorCode.UpdateJobFail); + } + + log.info("appAlarmRuleDao.updateByIdSelective rule:{}", new Gson().toJson(rule)); + int i = appAlarmRuleDao.updateByIdSelective(rule); + if (i < 1) { + log.error("AppAlarmService.editRules save db fail!ruleData:{}", rule.toString()); + return Result.fail(ErrorCode.ALARM_RULE_INFO_UPDATE_FAIL); + } + + return Result.success(""); + } + + private String nullToEmpty(String s) { + if (s == null) { + return ""; + } + return s; + } + + @Override + public Result deleteRulesByIamId(Integer iamId, Integer strategyId, String user) { + AppAlarmRule rulequery = new AppAlarmRule(); + rulequery.setStatus(0); + if (iamId != null) { + rulequery.setIamId(iamId); + } + rulequery.setStrategyId(strategyId); + List delRules = appAlarmRuleDao.query(rulequery, 0, Integer.MAX_VALUE); + if (CollectionUtils.isEmpty(delRules)) { + log.info("AppAlarmService.deleteRulesByIamId no data found! iamId : {},user:{}", iamId, user); + return Result.success(null); + } + + /** + * 同时删除远程接口的数据,和本地数据 + */ + for (AppAlarmRule rule : delRules) { + Result result = alarmService.deleteRule(rule.getAlarmId(), iamId, user); + if (result.getCode() == 0) { + int delete = appAlarmRuleDao.delete(rule); + if (delete < 1) { + log.error("AppAlarmService.deleteRulesByIamId delete local database fail!iamId : {},user:{}", iamId, + user); + return Result.fail(ErrorCode.unknownError); + } + } else { + + if (result.getCode() == 404) { + log.error("AppAlarmService.deleteRulesByIamId delete remote data,no data found!iamId : {},user:{}", + iamId, user); + + int delete = appAlarmRuleDao.delete(rule); + if (delete < 1) { + log.error("AppAlarmService.deleteRulesByIamId delete local database fail!iamId : {},user:{}", + iamId, user); + return Result.fail(ErrorCode.unknownError); + } + Result.success(""); + } + log.error("AppAlarmService.deleteRulesByIamId delete remote data fail!iamId : {},user:{}", iamId, user); + Result.fail(ErrorCode.unknownError); + } + } + + return Result.success(""); + } + + @Override + public Result enabledRules(Integer iamId, Integer strategyId, Integer pauseStatus, String user) { + + AppAlarmRule rulequery = new AppAlarmRule(); + rulequery.setStatus(0); + if (iamId != null) { + rulequery.setIamId(iamId); + } + rulequery.setStrategyId(strategyId); + List delRules = appAlarmRuleDao.query(rulequery, 0, Integer.MAX_VALUE); + if (CollectionUtils.isEmpty(delRules)) { + log.info("AppAlarmService.enabledRules no data found! iamId : {},user:{}", iamId, user); + return Result.success(null); + } + + /** + * 同时暂停远程接口的数据,和本地数据 + */ + for (AppAlarmRule rule : delRules) { + Result result = alarmService.enabledRule(rule.getAlarmId(), pauseStatus, iamId, user); + if (result.getCode() == 0) { + AppAlarmRule condition = new AppAlarmRule(); + condition.setIamId(rule.getIamId()); + condition.setStatus(0); + AppAlarmRule value = new AppAlarmRule(); + value.setRuleStatus(pauseStatus); + try { + int update = appAlarmRuleDao.update(condition, value); + if (update < 1) { + log.info("AppAlarmService.enabledRules update data failed!"); + return Result.fail(ErrorCode.unknownError); + } + } catch (Exception e) { + log.error("AppAlarmService.enabledRules error!{}", e.getMessage(), e); + return Result.fail(ErrorCode.unknownError); + } + } else { + log.error("AppAlarmService.enabledRules update remote data fail!iamId : {},user:{}", iamId, user); + Result.fail(ErrorCode.unknownError); + } + } + + return Result.success(""); + } + + @Override + public Result queryTemplate(AppAlarmRuleTemplateQuery query) { + if (StringUtils.isNotBlank(query.getCreater())) { + query.setCreater(query.getCreater()); + } + + Integer page = query.getPage(); + if (page == null || page.intValue() == 0) { + page = 1; + } + + Integer pageSize = query.getPageSize(); + if (pageSize == null) { + pageSize = 10; + } + + Integer start = (page - 1) * pageSize; + Long dataTotal = appAlarmRuleTemplateDao.getDataTotal(query); + + PageData pd = new PageData(); + pd.setPage(page); + pd.setPageSize(pageSize); + pd.setTotal(dataTotal); + + List resultList = new ArrayList<>(); + + /** + * 模版查询 + */ + List list = appAlarmRuleTemplateDao.query(query, start, pageSize); + if (CollectionUtils.isEmpty(list)) { + pd.setList(resultList); + } + + /** + * 遍历附加规则 + */ + AppAlarmRule rule = new AppAlarmRule(); + rule.setRuleType(AlarmRuleType.template.getCode()); + rule.setStatus(0); + for (AppAlarmRuleTemplate template : list) { + rule.setTemplateId(template.getId()); + List rules = appAlarmRuleDao.query(rule, 0, Integer.MAX_VALUE); + + AlarmTemplateResponse response = new AlarmTemplateResponse(); + response.setTemplate(template); + response.setAlarmRules(rules); + resultList.add(response); + } + + pd.setList(resultList); + + return Result.success(pd); + } + + @Override + public Result getTemplateById(Integer id) { + + /** + * 模版查询 + */ + AppAlarmRuleTemplate template = appAlarmRuleTemplateDao.getById(id); + if (template == null) { + log.info("getTemplateById no data found id : {}", id); + return Result.success(null); + } + + /** + * 遍历附加规则 + */ + AppAlarmRule rule = new AppAlarmRule(); + rule.setRuleType(AlarmRuleType.template.getCode()); + rule.setStatus(0); + rule.setTemplateId(template.getId()); + List rules = appAlarmRuleDao.query(rule, 0, Integer.MAX_VALUE); + + AlarmTemplateResponse response = new AlarmTemplateResponse(); + response.setTemplate(template); + response.setAlarmRules(rules); + return Result.success(response); + } + + @Override + public Result getTemplateByCreater(String user) { + + AppAlarmRuleTemplateQuery query = new AppAlarmRuleTemplateQuery(); + query.setCreater(user); + List list = appAlarmRuleTemplateDao.query(query, 0, Integer.MAX_VALUE); + return Result.success(list); + } + + @Override + public Result addTemplate(AlarmRuleTemplateRequest request, String user) { + + AppAlarmRuleTemplate alarmRuleTemplate = request.getTemplate(); + alarmRuleTemplate.setCreater(user); + alarmRuleTemplate.setType(AlarmRuleTemplateType.user.getCode()); + int i = appAlarmRuleTemplateDao.create(alarmRuleTemplate); + if (i < 1) { + log.error("AppAlarmService.addTemplate,insert template data failed!"); + return Result.fail(ErrorCode.unknownError); + } + + List alarmRules = request.getAlarmRules(); + + boolean b = saveTemplateRules(alarmRules, alarmRuleTemplate.getId(), user); + if (!b) { + return Result.fail(ErrorCode.unknownError); + } + + return Result.success(""); + + } + + @Override + public Result editTemplate(AlarmRuleTemplateRequest request, String user) { + + AppAlarmRuleTemplate alarmRuleTemplate = request.getTemplate(); + + AppAlarmRuleTemplate update = new AppAlarmRuleTemplate(); + update.setId(alarmRuleTemplate.getId()); + update.setRemark(alarmRuleTemplate.getRemark()); + update.setName(alarmRuleTemplate.getName()); + update.setStrategyType(alarmRuleTemplate.getStrategyType()); + int i = appAlarmRuleTemplateDao.update(update); + if (i < 1) { + log.error("AppAlarmService.editTemplate,update template data failed!"); + return Result.fail(ErrorCode.unknownError); + } + + AppAlarmRule rule = new AppAlarmRule(); + rule.setTemplateId(alarmRuleTemplate.getId()); + rule.setRuleType(0); + appAlarmRuleDao.delete(rule); + + List alarmRules = request.getAlarmRules(); + + boolean b = saveTemplateRules(alarmRules, alarmRuleTemplate.getId(), user); + if (!b) { + return Result.fail(ErrorCode.unknownError); + } + + return Result.success(""); + + } + + @Override + public Result deleteTemplate(Integer templateId) { + + int i = appAlarmRuleTemplateDao.deleteById(templateId); + if (i < 1) { + log.error("AppAlarmService.deleteTemplate,delete template data failed!"); + return Result.fail(ErrorCode.unknownError); + } + + AppAlarmRule rule = new AppAlarmRule(); + rule.setTemplateId(templateId); + rule.setRuleType(0); + int delete = appAlarmRuleDao.delete(rule); + if (delete < 1) { + log.info("AppAlarmService.deleteTemplate,delete template rules no data found!"); + } + + return Result.success(""); + + } + + /** + * 保存模版规则 + * + * @param alarmRules + * @param templateId + * @return + */ + private boolean saveTemplateRules(List alarmRules, Integer templateId, String user) { + + /** + * 创建新的关联规则 + */ + if (CollectionUtils.isEmpty(alarmRules)) { + log.info("AppAlarmService.saveTemplateRules,alarmRules is empty!templateId : {}", templateId); + return true; + } + Iterator iterator = alarmRules.iterator(); + while (iterator.hasNext()) { + AppAlarmRule next = iterator.next(); + next.setCreater(user); + + next.setTemplateId(templateId); + //没有和任何应用关联配置,单纯隶属于一个模版的模版规则 + next.setRuleType(AlarmRuleType.template.getCode()); + //因为是模版,模版的数据不关联任何应用,默认不生效 + next.setRuleStatus(0); + next.setForTime(next.getDataCount() * 30 + "s"); + next.setStatus(0); + next.setCreateTime(new Date()); + next.setUpdateTime(new Date()); + } + + int i1 = appAlarmRuleDao.batchInsert(alarmRules); + if (i1 < 1) { + log.error("AppAlarmService.saveTemplateRules,insert rules data failed! templateId:{},alarmRules:{}", + templateId, alarmRules); + return false; + } + return true; + } + + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppGrafanaMappingServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppGrafanaMappingServiceImpl.java new file mode 100644 index 000000000..c5a47b986 --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppGrafanaMappingServiceImpl.java @@ -0,0 +1,427 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import com.xiaomi.mone.app.api.model.HeraAppBaseInfoModel; +import com.xiaomi.mone.monitor.bo.AppLanguage; +import com.xiaomi.mone.monitor.bo.AppType; +import com.xiaomi.mone.monitor.bo.PlatFormType; +import com.xiaomi.mone.monitor.dao.AppGrafanaMappingDao; +import com.xiaomi.mone.monitor.dao.AppMonitorDao; +import com.xiaomi.mone.monitor.dao.GrafanaTemplateDao; +import com.xiaomi.mone.monitor.dao.model.AppGrafanaMapping; +import com.xiaomi.mone.monitor.dao.model.AppMonitor; +import com.xiaomi.mone.monitor.dao.model.GrafanaTemplate; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.AppGrafanaMappingService; +import com.xiaomi.mone.monitor.service.GrafanaService; +import com.xiaomi.mone.monitor.service.HeraBaseInfoService; +import com.xiaomi.mone.monitor.service.api.AppGrafanaMappingServiceExtension; +import com.xiaomi.mone.monitor.service.extension.PlatFormTypeExtensionService; +import com.xiaomi.mone.monitor.service.model.GrafanaResponse; +import com.xiaomi.mone.monitor.service.model.MutiGrafanaResponse; +import com.xiaomi.mone.monitor.service.serverless.ServerLessService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + * @author gaoxihui + * @date 2021/7/8 11:05 PM + */ +@Slf4j +@Service +public class AppGrafanaMappingServiceImpl implements AppGrafanaMappingService { + + @Autowired + AppGrafanaMappingDao appGrafanaMappingDao; + + @Autowired + GrafanaService grafanaService; + + @Autowired + AppMonitorDao appMonitorDao; + + @Autowired + GrafanaTemplateDao grafanaTemplateDao; + + @Autowired + HeraBaseInfoService heraBaseInfoService; + + @Autowired + PlatFormTypeExtensionService platFormTypeExtensionService; + + @Autowired + private ServerLessService serverLessService; + + @Autowired + private AppGrafanaMappingServiceExtension appGrafanaMappingServiceExtension; + + + @Value("${server.type}") + private String serverType; + + @Value("${grafana.domain}") + private String grafanaDomain; + + ExecutorService executor = null; + + + public static final String OLD_ST_GRAFANA_DOMAIN = "http://grafana.mione"; + + public static final String OLD_ONLINE_GRAFANA_DOMAIN = "http://grafana.be"; + + public AppGrafanaMappingServiceImpl() { + executor = Executors.newSingleThreadExecutor(); + } + + @Override + public void exeReloadTemplateBase(Integer pSize) { + + log.info("exeReloadTemplateBase will be running!! pSize:" + pSize); + Future result = executor.submit(() -> { + + log.info("exeReloadTemplateBase start running!! pSize:" + pSize); + + try { + HeraAppBaseInfoModel baseInfoCondition = new HeraAppBaseInfoModel(); + Long total = heraBaseInfoService.count(baseInfoCondition); + log.info("AppGrafanaMappingService.exeReloadTemplateBase data totalNum ====== {}", total); + if (total == null || total.intValue() == 0) { + log.info("AppGrafanaMappingService.exeReloadTemplateBase no data found!!!"); + return "No data found!!"; + } + + int pageCount = total.intValue() / pSize + (total.intValue() % pSize > 0 ? 1 : 0); + + for (int i = 0; i < pageCount; i++) { + int offset = i + 1; + List list = heraBaseInfoService.query(baseInfoCondition, offset, pSize); + for (HeraAppBaseInfoModel heraAppBaseInfo : list) { + + try { + this.createTmpByAppBaseInfo(heraAppBaseInfo); + } catch (Exception e1) { + log.error("grafanaMappingService.exeReloadTemplateBase error appName:{} error : {}", + heraAppBaseInfo.getAppName(), e1.getMessage(), e1); + continue; + } + + } + } + } catch (Exception e) { + log.error("AppGrafanaMappingService.exeReloadTemplateBase error : {}", e.getMessage(), e); + return ErrorCode.unknownError.getMessage(); + } + + return ErrorCode.success.getMessage(); + }); + } + + @Override + public void reloadTmpByAppId(Integer id) { + HeraAppBaseInfoModel baseInfo = heraBaseInfoService.getById(id); + if (baseInfo == null) { + log.error("reloadTmpByAppId no data found id:{}", id); + return; + } + createTmpByAppBaseInfo(baseInfo); + } + + @Override + public void createTmpByAppBaseInfo(HeraAppBaseInfoModel baseInfo) { + GrafanaTemplate template = new GrafanaTemplate(); + Integer appType = baseInfo.getAppType(); + /** + * 默认使用业务类型; + * mesh类型应用与业务类型公用模版 + */ + if (appType == null || AppType.mesh.getCode().equals(appType)) { + appType = AppType.businessType.getCode(); + } + template.setAppType(appType); + template.setPlatform(baseInfo.getPlatformType()); + + try { + Integer langUageCode = AppLanguage.getCodeByMessage(baseInfo.getAppLanguage()); + //无语言配置,默认使用java语言模版 + if (langUageCode == null) { + langUageCode = AppLanguage.java.getCode(); + log.error("the app base info no language set! set default language java. baseInfo:{}", + new Gson().toJson(baseInfo)); + } + template.setLanguage(langUageCode); + + appGrafanaMappingServiceExtension.setPlatFormByLanguage(template, baseInfo.getAppLanguage()); + + List search = grafanaTemplateDao.search(template); + if (CollectionUtils.isEmpty(search)) { + log.error("createTmpByAppBaseInfo,no template config found! baseInfo:{}", new Gson().toJson(baseInfo)); + return; + } + + Optional platformType = Optional.ofNullable(baseInfo.getPlatformType()); + //找不到平台类型,默认放在open下 + String grafanaDirByCode = + platformType.isPresent() ? platFormTypeExtensionService.getGrafanaDirByTypeCode(platformType.get()) + : PlatFormType.open.getGrafanaDir(); + + List funcList = new ArrayList<>(); + + if (AppType.serverless.getCode().equals(baseInfo.getAppType())) { + funcList = serverLessService.getFaasFunctionList(Integer.valueOf(baseInfo.getBindId())); + } + + MutiGrafanaResponse mutiGrafanaResponse = grafanaService.requestGrafanaTemplate(serverType, + baseInfo.getBindId() + "_" + baseInfo.getAppName(), grafanaDirByCode, search.get(0), funcList); + + log.info("createTmpByAppBaseInfo response info : {}", mutiGrafanaResponse); + + appGrafanaMappingServiceExtension.dealRequestGrafanaTemplateCode(mutiGrafanaResponse.getCode(), + baseInfo.getBindId(), baseInfo.getAppName()); + + if (mutiGrafanaResponse.getCode() != 0 || mutiGrafanaResponse.getData() == null) { + log.info("create grafana fail! baseInfo:{},response:{}", baseInfo.toString(), + new Gson().toJson(mutiGrafanaResponse)); + return; + } + log.info( + "grafanaMappingService.createTmpByAppBaseInfo success appName : {}, version:{},area : {}, returnUrl :{}", + baseInfo.getAppName(), mutiGrafanaResponse.getData().get(0).getMimonitor_version(), + grafanaDirByCode, mutiGrafanaResponse); + + String url = new StringBuffer().append(grafanaDomain).append(mutiGrafanaResponse.getUrl()).toString(); + + AppGrafanaMapping grafanaMapping = new AppGrafanaMapping(); + grafanaMapping.setGrafanaUrl(url); + grafanaMapping.setAppName(baseInfo.getBindId() + "_" + baseInfo.getAppName()); + grafanaMapping.setMioneEnv(grafanaDirByCode); + int i = this.saveOrUpdate(grafanaMapping); + if (i > 0) { + log.info("createTmpByAppBaseInfo sucess!grafanaMapping:{}", grafanaMapping.toString()); + } else { + log.info("createTmpByAppBaseInfo fail!grafanaMapping:{}", grafanaMapping.toString()); + } + } catch (JsonSyntaxException e) { + log.error("createTmpByAppBaseInfo error!{}", e.getMessage(), e); + } + + } + + @Override + @Deprecated + public String createGrafanaUrlByAppName(String appName, String area) { + if (StringUtils.isEmpty(appName) || StringUtils.isEmpty(area)) { + log.error("GrafanaMappingController.createGrafanaUrlByAppName error! param appName or area is empty!"); + return ErrorCode.invalidParamError.getMessage(); + } + + try { + + String grafanaReqResult = grafanaService.requestGrafana(serverType, appName, area); + log.info( + "GrafanaMappingController.createGrafanaUrlByAppName requestGrafana serverType:{} ,appName : {},area : {}, result : {}", + serverType, appName, area, grafanaReqResult); + + GrafanaResponse grafanaResponse = new Gson().fromJson(grafanaReqResult, GrafanaResponse.class); + String url = new StringBuffer().append(grafanaDomain).append(grafanaResponse.getUrl()).toString(); + + AppGrafanaMapping appGrafanaMapping = new AppGrafanaMapping(); + appGrafanaMapping.setMioneEnv(area); + appGrafanaMapping.setAppName(appName); + appGrafanaMapping.setGrafanaUrl(url); + Integer ret = this.saveOrUpdate(appGrafanaMapping); + + if (ret > 0) { + log.info( + "GrafanaMappingController.createGrafanaUrlByAppName save data success name : {}, area : {}, url : {} ", + appName, area, url); + return ErrorCode.success.getMessage(); + } else { + log.info( + "GrafanaMappingController.createGrafanaUrlByAppName save data failed name : {}, area : {}, url : {} ", + appName, area, url); + return "data save failed!!"; + } + + } catch (Exception e) { + log.error("GrafanaMappingController.createGrafanaUrlByAppName error:{}", e.getMessage(), e); + return ErrorCode.unknownError.getMessage(); + } + } + + @Override + public Result getGrafanaUrlByAppName(String appName) { + + if (StringUtils.isEmpty(appName)) { + log.error("AppGrafanaMappingService#getGrafanaUrlByAppName param is empty"); + return Result.fail(ErrorCode.invalidParamError); + } + + int i = appName.indexOf("_"); + String bindId = appName.substring(0, i); + String appNameBase = appName.substring(i + 1); + + String appType = null; + + try { + AppGrafanaMapping mapping = appGrafanaMappingDao.getByAppName(appName); + if (mapping == null) { + log.info("AppGrafanaMappingService#getGrafanaUrlByAppName can not find data for appName : {}", appName); + return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), null); + } + log.info("AppGrafanaMappingService#getGrafanaUrlByAppName by appName : {} ,return : {}", appName, + mapping.getGrafanaUrl()); + + HeraAppBaseInfoModel heraAppBaseInfo = new HeraAppBaseInfoModel(); + heraAppBaseInfo.setBindId(bindId); + heraAppBaseInfo.setAppName(appNameBase); + + List query = heraBaseInfoService.query(heraAppBaseInfo, null, null); + if (CollectionUtils.isEmpty(query) || query.get(0).getAppType() == null) { + log.error("no appType found for appName:{},has set default type by businessType type", appName); + appType = AppType.businessType.getMessage(); + } else { + AppType anEnum = AppType.getEnum(query.get(0).getAppType()); + if (anEnum == null) { + log.error("error AppType appName:{},has set default type by businessType type", appName); + appType = AppType.businessType.getMessage(); + } else { + appType = anEnum.getMessage(); + } + + // mesh类型和业务类型应用使用相同的模版 + if (appType.equals(AppType.mesh.getMessage())) { + appType = AppType.businessType.getMessage(); + } + + } + Map map = new HashMap(); + map.put("appType", appType); + map.put("url", transferGrafanaUrl(mapping.getGrafanaUrl())); + + return Result.success(map); + } catch (Exception e) { + log.error("AppGrafanaMappingService#getGrafanaUrlByAppName error" + e.getMessage(), e); + return Result.fail(ErrorCode.unknownError); + } + + } + + @Override + @Deprecated + public Result getGrafanaUrlByAppId(Integer appId) { + + if (appId == null) { + log.error("AppGrafanaMappingService#getGrafanaUrlByAppId param is empty"); + return Result.fail(ErrorCode.invalidParamError); + } + + try { + AppMonitor appMonitor = appMonitorDao.getByAppId(appId); + if (appMonitor == null) { + return Result.success(null); + } + + String grafanaName = new StringBuilder().append(appMonitor.getProjectId()).append("_") + .append(appMonitor.getProjectName()).toString(); + AppGrafanaMapping mapping = appGrafanaMappingDao.getByAppName(grafanaName); + if (mapping == null) { + log.info("AppGrafanaMappingService#getGrafanaUrlByAppId can not find data for appName : {}", + grafanaName); + return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), null); + } + log.info("AppGrafanaMappingService#getGrafanaUrlByAppId by appName : {} ,return : {}", grafanaName, + mapping.getGrafanaUrl()); + return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), mapping.getGrafanaUrl()); + } catch (Exception e) { + log.error("AppGrafanaMappingService#getGrafanaUrlByAppId error" + e.getMessage(), e); + return Result.fail(ErrorCode.unknownError); + } + + } + + @Override + public Integer save(AppGrafanaMapping appGrafanaMapping) { + try { + return appGrafanaMappingDao.generateGrafanaMapping(appGrafanaMapping); + } catch (Exception e) { + log.error("AppGrafanaMappingService#save error" + e.getMessage(), e); + } + return 0; + } + + @Override + public Integer saveOrUpdate(AppGrafanaMapping appGrafanaMapping) { + try { + AppGrafanaMapping byAppName = appGrafanaMappingDao.getByAppName(appGrafanaMapping.getAppName()); + if (byAppName == null) { + return appGrafanaMappingDao.generateGrafanaMapping(appGrafanaMapping); + } + + appGrafanaMapping.setId(byAppName.getId()); + appGrafanaMapping.setCreateTime(byAppName.getCreateTime()); + return appGrafanaMappingDao.updateByPrimaryKey(appGrafanaMapping); + + } catch (Exception e) { + log.error("AppGrafanaMappingService#saveOrUpdate error" + e.getMessage(), e); + } + return 0; + } + + private String transferGrafanaUrl(String grafanaUrl) { + if (!serverType.equals("online")) { + //测试环境由于grafana升级https 域名发生变换 + if (grafanaUrl.startsWith(OLD_ST_GRAFANA_DOMAIN)) { + String[] grafanaUrlArr = grafanaUrl.split(OLD_ST_GRAFANA_DOMAIN); + if (grafanaUrlArr.length < 2) { + return grafanaUrl; + } else { + return grafanaDomain + grafanaUrlArr[1]; + } + } + return grafanaUrl; + } else { + //线上环境更换域名,替换名称 + if (grafanaUrl.startsWith(OLD_ONLINE_GRAFANA_DOMAIN)) { + String[] grafanaUrlArr = grafanaUrl.split(OLD_ONLINE_GRAFANA_DOMAIN); + if (grafanaUrlArr.length < 2) { + return grafanaUrl; + } else { + return grafanaDomain + grafanaUrlArr[1]; + } + } + return grafanaUrl; + } + } + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppMonitorConfigServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppMonitorConfigServiceImpl.java new file mode 100644 index 000000000..08a5a74fb --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppMonitorConfigServiceImpl.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.xiaomi.mone.monitor.dao.AppMonitorConfigDao; +import com.xiaomi.mone.monitor.dao.model.AppMonitorConfig; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.AppMonitorConfigService; +import com.xiaomi.mone.monitor.service.model.PageData; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; + +/** + * @author gaoxihui + * @date 2021/8/19 4:07 PM + */ +@Slf4j +@Service +public class AppMonitorConfigServiceImpl implements AppMonitorConfigService { + + @Autowired + AppMonitorConfigDao dao; + + @Override + public Result createConfig(AppMonitorConfig config) { + config.setCreateTime(new Date()); + config.setUpdateTime(new Date()); + config.setStatus(0); + int i = dao.create(config); + if (i < 1) { + return Result.fail(ErrorCode.unknownError); + } + return Result.success(null); + } + + @Override + public Result updateConfig(AppMonitorConfig config) { + config.setStatus(0); + config.setUpdateTime(new Date()); + int update = dao.update(config); + if (update < 1) { + return Result.fail(ErrorCode.unknownError); + } + return Result.success(null); + } + + @Override + public Result delConfig(Integer id) { + AppMonitorConfig config = dao.getById(id); + if (config == null) { + log.error("AppMonitorConfigService.delConfig error! no config data found By id : {}", id); + return Result.fail(ErrorCode.unknownError); + } + config.setStatus(1); + config.setUpdateTime(new Date()); + int update = dao.update(config); + if (update < 1) { + log.error("AppMonitorConfigService.delConfig failed! id : {}", id); + return Result.fail(ErrorCode.unknownError); + } + return Result.success(null); + } + + @Override + public Result getConfig(Integer projectId, Integer type, String configName, Integer status, Integer page, + Integer pageSize) { + + try { + return dao.getConfig(projectId, type, configName, status, page, pageSize); + } catch (Exception e) { + log.error("AppMonitorConfigService.getConfig error : {}", e.getMessage()); + return Result.fail(ErrorCode.unknownError); + } + } + + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppMonitorServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppMonitorServiceImpl.java index 1f7f778d1..b75c67f46 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppMonitorServiceImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/AppMonitorServiceImpl.java @@ -13,134 +13,1205 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service.impl; -import com.alibaba.nacos.api.config.annotation.NacosValue; import com.google.gson.Gson; -import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.xiaomi.mone.app.api.message.HeraAppInfoModifyMessage; -import com.xiaomi.mone.monitor.bo.GrafanaInterfaceRes; +import com.xiaomi.mone.app.api.message.HeraAppModifyType; +import com.xiaomi.mone.app.api.model.HeraAppBaseInfoModel; +import com.xiaomi.mone.app.api.service.HeraAppService; +import com.xiaomi.mone.monitor.bo.AlarmStrategyInfo; +import com.xiaomi.mone.monitor.bo.AppViewType; +import com.xiaomi.mone.monitor.bo.RuleStatusType; +import com.xiaomi.mone.monitor.dao.AppAlarmRuleDao; +import com.xiaomi.mone.monitor.dao.AppAlarmStrategyDao; +import com.xiaomi.mone.monitor.dao.AppMonitorDao; +import com.xiaomi.mone.monitor.dao.HeraAppRoleDao; +import com.xiaomi.mone.monitor.dao.model.AlarmHealthQuery; +import com.xiaomi.mone.monitor.dao.model.AlarmHealthResult; +import com.xiaomi.mone.monitor.dao.model.AlarmStrategy; +import com.xiaomi.mone.monitor.dao.model.AppAlarmRule; import com.xiaomi.mone.monitor.dao.model.AppMonitor; +import com.xiaomi.mone.monitor.dao.model.HeraAppRole; import com.xiaomi.mone.monitor.result.ErrorCode; import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.AlarmStrategyService; +import com.xiaomi.mone.monitor.service.AppAlarmService; +import com.xiaomi.mone.monitor.service.AppGrafanaMappingService; +import com.xiaomi.mone.monitor.service.AppMonitorService; +import com.xiaomi.mone.monitor.service.HeraBaseInfoService; +import com.xiaomi.mone.monitor.service.ResourceUsageService; import com.xiaomi.mone.monitor.service.api.AppMonitorServiceExtension; +import com.xiaomi.mone.monitor.service.api.TeslaService; +import com.xiaomi.mone.monitor.service.extension.PlatFormTypeExtensionService; +import com.xiaomi.mone.monitor.service.model.AppMonitorModel; +import com.xiaomi.mone.monitor.service.model.AppMonitorRequest; +import com.xiaomi.mone.monitor.service.model.PageData; import com.xiaomi.mone.monitor.service.model.ProjectInfo; -import com.xiaomi.mone.monitor.utils.FreeMarkerUtil; +import com.xiaomi.mone.monitor.service.model.prometheus.AlarmRuleData; +import com.xiaomi.mone.monitor.service.model.prometheus.MetricData; +import com.xiaomi.mone.monitor.service.model.prometheus.MetricDataSet; +import com.xiaomi.mone.monitor.service.model.prometheus.MetricResponse; +import com.xiaomi.mone.monitor.service.prometheus.AlarmService; +import com.xiaomi.mone.monitor.service.prometheus.PrometheusService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.apache.dubbo.config.annotation.Reference; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; +import java.util.stream.Collectors; /** - * @author zhangxiaowei6 + * @author gaoxihui + * @date 2021/8/13 11:07 AM */ @Slf4j @Service -@ConditionalOnProperty(name = "service.selector.property", havingValue = "outer") -public class AppMonitorServiceImpl implements AppMonitorServiceExtension { - - @NacosValue(value = "${grafana.domain}", autoRefreshed = true) - private String grafanaDomain; - - - private String resourceUrl = "/d/hera-resource-utilization/hera-k8szi-yuan-shi-yong-lu-da-pan?orgId=1&var-application="; - - private String dubboProviderOverview = "/d/hera-dubboprovider-overview/hera-dubboproviderzong-lan?orgId=1&kiosk&theme=light"; - private String dubboConsumerOverview = "/d/hera-dubboconsumer-overview/hera-dubboconsumerzong-lan?orgId=1&kiosk&theme=light"; - private String dubboProviderMarket = "/d/Hera-DubboProviderMarket/hera-dubboproviderda-pan?orgId=1&kiosk&theme=light"; - private String dubboConsumerMarket = "/d/Hera-DubboConsumerMarket/hera-dubboconsumerda-pan?orgId=1&kiosk&theme=light"; - private String httpOverview = "/d/Hera-HTTPServer-overview/hera-httpserver-zong-lan?orgId=1&kiosk&theme=light"; - private String httpMarket = "/d/Hera-HTTPServerMarket/hera-httpserverda-pan?orgId=1&kiosk&theme=light"; - - private String grpcProviderOverview = "/d/hera-grpcprovider-overview/hera-grpcproviderzong-lan?orgId=1&kiosk&theme=light"; - private String grpcProviderMarket = "/d/hera-grpcproviderMarket/hera-grpcproviderda-pan?orgId=1&kiosk&theme=light"; - private String grpcConsumerOverview = "/d/hera-grpcconsumer-overview/hera-grpcconsumerzong-lan?orgId=1&kiosk&theme=light"; - private String grpcConsumerMarket = "/d/hera-grpcconsumerMarket/hera-grpcconsumerda-pan?orgId=1&kiosk&theme=light"; - - private static final Gson gson = new Gson(); - - public Result getResourceUsageUrlForK8s(Integer appId, String appName) { - //A link back to the grafana resource utilization graph - String application = String.valueOf(appId) + "_" + StringUtils.replace(appName, "-", "_"); - String url = grafanaDomain + resourceUrl + application; - log.info("getResourceUsageUrlForK8s url:{}", url); - return Result.success(url); +public class AppMonitorServiceImpl implements AppMonitorService { + + @Autowired + AppMonitorDao appMonitorDao; + + @Autowired + HeraBaseInfoService heraBaseInfoService; + + @Autowired + AppGrafanaMappingService appGrafanaMappingService; + + @Autowired + AppAlarmService appAlarmService; + + @Autowired + private TeslaService teslaService; + + @Autowired + PrometheusService prometheusService; + + @Autowired + ResourceUsageService resourceUsageService; + + @Autowired + HeraAppRoleDao heraAppRoleDao; + + @Autowired + AppAlarmStrategyDao strategyDao; + + @Autowired + AppAlarmRuleDao ruleDao; + + @Autowired + AlarmService alarmService; + + @Autowired + AppMonitorServiceExtension appMonitorServiceExtension; + + @Autowired + PlatFormTypeExtensionService platFormTypeExtensionService; + + @Autowired + AlarmStrategyService alarmStrategyService; + + + @Reference(registry = "registryConfig", check = false, interfaceClass = HeraAppService.class, group = "${dubbo.group.heraapp}", timeout = 5000) + HeraAppService hearAppService; + + @Override + public void appPlatMove(Integer OProjectId, Integer OPlat, Integer NProjectId, Integer Nplat, Integer newIamId, + String NprojectName, Boolean rebuildRule) { + + log.info("appPlatMove OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", OProjectId, OPlat, + NProjectId, Nplat, NprojectName); + + if (OProjectId == null || OPlat == null || NProjectId == null || Nplat == null || StringUtils.isBlank( + NprojectName)) { + log.error("appPlatMove has invalid param! OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", + OProjectId, OPlat, NProjectId, Nplat, NprojectName); + return; + } + + HeraAppBaseInfoModel baseInfo = new HeraAppBaseInfoModel(); + baseInfo.setBindId(String.valueOf(OProjectId)); + baseInfo.setPlatformType(OPlat); + + List query = heraBaseInfoService.query(baseInfo, null, null); + if (CollectionUtils.isEmpty(query)) { + log.info("appPlatMove nodata found!OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", + OProjectId, OPlat, NProjectId, Nplat, NprojectName); + return; + } + if (query.size() > 1) { + log.info( + "appPlatMove more than one data found!OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", + OProjectId, OPlat, NProjectId, Nplat, NprojectName); + return; + } + + HeraAppBaseInfoModel heraAppBaseInfo = query.get(0); + + String oldProjectName = heraAppBaseInfo.getAppName(); + + heraAppBaseInfo.setPlatformType(Nplat); + heraAppBaseInfo.setIamTreeId(newIamId); + heraAppBaseInfo.setBindId(String.valueOf(NProjectId)); + heraAppBaseInfo.setAppName(NprojectName); + + int update = heraBaseInfoService.insertOrUpdate(heraAppBaseInfo); + if (update < 1) { + log.error( + "appPlatMove update heraBaseInfo fail!OProjectId:{},OPlat:{},NProjectId:{},Nplat:{},NprojectName:{}", + OProjectId, OPlat, NProjectId, Nplat, NprojectName); + } + + /** + * 关联的角色信息 + */ + HeraAppRole heraAppRole = new HeraAppRole(); + heraAppRole.setAppPlatform(OPlat); + heraAppRole.setAppId(String.valueOf(OProjectId)); + List appRoles = heraAppRoleDao.query(heraAppRole, null, null); + if (!CollectionUtils.isEmpty(appRoles)) { + appRoles.forEach(t -> { + t.setAppId(String.valueOf(NProjectId)); + t.setAppPlatform(Nplat); + heraAppRoleDao.update(t); + }); + } + + /** + * 3、关注信息 + * -参与列表 + * -关注列表 + */ + List appMonitors = appMonitorDao.getByProjectIdAndPlat(OProjectId, OPlat); + if (!CollectionUtils.isEmpty(appMonitors)) { + appMonitors.forEach(t -> { + + t.setAppSource(Nplat); + t.setProjectId(NProjectId); + t.setIamTreeId(newIamId); + t.setProjectName(NprojectName); + appMonitorDao.update(t); + }); + } + + if (rebuildRule) { + AlarmStrategy strategy = new AlarmStrategy(); + strategy.setAppId(OProjectId); + strategy.setAppName(oldProjectName); + PageData> listPageData = strategyDao.searchByCondNoUser(strategy, 1, 1000, null, + null); + List list = listPageData.getList(); + if (!CollectionUtils.isEmpty(list)) { + list.forEach(t -> { + List rules = ruleDao.selectByStrategyId(t.getId()); + for (AppAlarmRule rule : rules) { + /** + * 1、app的projectId、Name、iamId都更新好 + * 2、rule的iamId + * 3、user采用rule的creater + * 4、ruleData补数据 + * + * ruleData 标签数据来源: + * 1、strategy + * 2、rule的labels + * + * 使用到ruleData的数据: + * AlarmCallbackUrl + * ruleData.getAlarmDetailUrl() + * + * getEnvLabels: + * serverEnv(include、except) + * service(include、except) + * functionId(include、except) + * methodName:http、dubbo + * errorCode:http + * serviceName:dubbo + * + * Tesla + * rule.getTeslaGroup() + * rule.getTeslaUrls() + * rule.getExcludeTeslaUrls() + * + * + * ruleData.getExpr() 自定义表达式 + * ruleData.getAlertMembers() + * + * + * + */ + + Integer oldAlarmId = rule.getAlarmId(); + Integer oldIamId = rule.getIamId(); + + AppMonitor app = new AppMonitor(); + app.setProjectId(NProjectId); + app.setProjectName(NprojectName); + + rule.setIamId(newIamId); + rule.setProjectId(NProjectId); + + StringBuilder cname = new StringBuilder(); + cname.append(newIamId); + cname.append("-").append(rule.getAlert()); + cname.append("-").append(System.currentTimeMillis()); + rule.setCname(cname.toString()); + + AlarmRuleData ruleData = new AlarmRuleData(); + BeanUtils.copyProperties(rule, ruleData); + ruleData.setLabels(rule.getLabels()); + ruleData.convertLabels(); + + ruleData.setIncludeEnvs(t.getIncludeEnvs()); + ruleData.setExceptEnvs(t.getExceptEnvs()); + ruleData.setIncludeZones(t.getIncludeZones()); + ruleData.setExceptZones(t.getExceptZones()); + ruleData.setIncludeContainerName(t.getIncludeContainerName()); + ruleData.setExceptContainerName(t.getExceptContainerName()); + ruleData.setAlertMembers(t.getAlertMembers()); + + if (!CollectionUtils.isEmpty(t.getIncludeFunctions())) { + ruleData.setIncludeFunctions( + t.getIncludeFunctions().stream().map(String::valueOf).collect(Collectors.toList())); + } + + if (!CollectionUtils.isEmpty(t.getExceptFunctions())) { + ruleData.setExceptFunctions( + t.getExceptFunctions().stream().map(String::valueOf).collect(Collectors.toList())); + } + + ruleData.setIncludeModules(t.getIncludeModules()); + ruleData.setExceptModules(t.getExceptModules()); + + Result result1 = alarmService.addRule(app, rule, rule.getCreater(), ruleData); + if (!result1.isSuccess()) { + log.error("appPlatMove add new rule fail!rule{}", rule.toString()); + return; + } + + JsonElement data = (JsonElement) result1.getData(); + Integer alarmId = data.getAsJsonObject().get("id").getAsInt(); + rule.setAlarmId(alarmId); + + if (rule.getRuleStatus().equals(RuleStatusType.pause.getCode())) { + Result result = alarmService.enabledRule(alarmId, RuleStatusType.pause.getCode(), + rule.getIamId(), rule.getCreater()); + if (!result.isSuccess()) { + log.error("appPlatMove pause rule fail!rule{}", rule.toString()); + } + } + + Result result = alarmService.deleteRule(oldAlarmId, oldIamId, rule.getCreater()); + if (!result.isSuccess()) { + log.error("appPlatMove del old rule fail!rule{}", rule.toString()); + } + + int i = ruleDao.updateByIdSelective(rule); + if (i < 1) { + log.error("appPlatMove update rule db fail! rule{}", rule.toString()); + } + + } + + AlarmStrategy strategyUp = new AlarmStrategy(); + strategyUp.setId(t.getId()); + strategyUp.setAppId(NProjectId); + strategyUp.setAppName(NprojectName); + strategyUp.setIamId(newIamId); + boolean b = strategyDao.updateById(strategyUp); + if (!b) { + log.error("appPlatMove update strategy fail! oldP:{},new:{}", t.toString(), + strategyUp.toString()); + } + + }); + } + } } - + @Override - public Result grafanaInterfaceList() { - Map map = new HashMap<>(); - map.put("dubboProviderOverview", grafanaDomain + dubboProviderOverview); - map.put("dubboConsumerOverview", grafanaDomain + dubboConsumerOverview); - map.put("dubboProviderMarket", grafanaDomain + dubboProviderMarket); - map.put("dubboConsumerMarket", grafanaDomain + dubboConsumerMarket); - map.put("httpOverview", grafanaDomain + httpOverview); - map.put("httpMarket", grafanaDomain + httpMarket); - map.put("grpcProviderOverview", grafanaDomain + grpcProviderOverview); - map.put("grpcProviderMarket", grafanaDomain + grpcProviderMarket); - map.put("grpcConsumerOverview", grafanaDomain + grpcConsumerOverview); - map.put("grpcConsumerMarket", grafanaDomain + grpcConsumerMarket); - + public Result selectAppAlarmHealth(AlarmHealthQuery query) { try { - log.info("grafanaInterfaceList map:{}", map); - String data = FreeMarkerUtil.getContentExceptJson("/heraGrafanaTemplate", "grafanaInterfaceList.ftl", map); - JsonArray jsonElements = gson.fromJson(data, JsonArray.class); - log.info(jsonElements.toString()); - List resList = new ArrayList<>(); - jsonElements.forEach(it -> { - GrafanaInterfaceRes grafanaInterfaceRes = gson.fromJson(it, GrafanaInterfaceRes.class); - resList.add(grafanaInterfaceRes); - }); - log.info("grafanaInterfaceList success! data:{}", resList); - return Result.success(resList); + List alarmHealthResults = appMonitorDao.selectAppHealth(query); + if (!CollectionUtils.isEmpty(alarmHealthResults)) { + + for (AlarmHealthResult alarmHealth : alarmHealthResults) { + /** + * 基础指标得分 + */ + alarmHealth.setBasicMetricScore((alarmHealth.getCpuUseRate().intValue() > 0 ? 2 : 0) + ( + alarmHealth.getCpuLoad().intValue() > 0 ? 2 : 0) + ( + alarmHealth.getMemUseRate().intValue() > 0 ? 2 : 0) + ( + alarmHealth.getContainerNum().intValue() > 0 ? 2 : 0) + ( + alarmHealth.getJvmThread().intValue() > 0 ? 1 : 0) + (alarmHealth.getJvmGc().intValue() > 0 + ? 1 : 0)); + + /** + * 接口指标得分 + */ + alarmHealth.setInterfaceMetricScore( + (alarmHealth.getHttpServerAvailability().intValue() > 0 ? 2 : 0) + ( + alarmHealth.getHttpServerQps().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getHttpServerTimeCost().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getHttpClientAvailability().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getHttpClientQps().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getHttpClientTimeCost().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getDubboProviderAvailability().intValue() > 0 ? 2 : 0) + ( + alarmHealth.getDubboProviderQps().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getDubboProviderTimeCost().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getDubboProviderSlowQuery().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getDubboConsumerAvailability().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getDubboConsumerQps().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getDubboConsumerTimeCost().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getDubboConsumerSlowQuery().intValue() > 0 ? 1 : 0) + ( + alarmHealth.getDbAvailability().intValue() > 0 ? 2 : 0) + ( + alarmHealth.getDbSlowQuery().intValue() > 0 ? 1 : 0)); + + /** + * 综合得分 + */ + alarmHealth.setComprehensiveScore( + alarmHealth.getBasicMetricScore() + alarmHealth.getInterfaceMetricScore()); + } + } + return Result.success(alarmHealthResults); } catch (Exception e) { - log.error("grafanaInterfaceList error! {}", e); + log.error("selectAppAlarmHealth Error!{}", e.getMessage(), e); return Result.fail(ErrorCode.unknownError); } } - + + @Override + public Result getResourceUsageUrlForK8s(Integer appId, String appName) { + return appMonitorServiceExtension.getResourceUsageUrlForK8s(appId, appName); + } + @Override public Result initAppsByUsername(String userName) { - return null; + return appMonitorServiceExtension.initAppsByUsername(userName); } - + @Override public List getAppsByUserName(String username) { - return null; + return appMonitorServiceExtension.getAppsByUserName(username); } - + @Override - public Boolean checkCreateParam(AppMonitor appMonitor) { - - if (appMonitor.getProjectId() == null || StringUtils.isBlank(appMonitor.getProjectName())) { - return false; + public Result getProjectInfos(String userName, String appName, Integer page, Integer pageSize) { + + if (page == null) { + page = 1; } - return true; + + if (pageSize == null) { + pageSize = 10; + } + + PageData pd = new PageData(); + pd.setPage(page); + pd.setPageSize(pageSize); + + HeraAppBaseInfoModel model = new HeraAppBaseInfoModel(); + model.setAppName(appName); + + Long total = hearAppService.count(model); + pd.setTotal(total); + + List query = hearAppService.query(model, page, pageSize); + + List list = new ArrayList(); + if (CollectionUtils.isEmpty(query)) { + pd.setList(list); + return Result.success(pd); + } + + query.forEach(t -> { + ProjectInfo info = new ProjectInfo(); + info.setId(Long.valueOf(t.getBindId())); + info.setName(t.getAppName()); + info.setIamTreeId(t.getIamTreeId() != null ? Long.valueOf(t.getIamTreeId()) : t.getIamTreeId()); + list.add(info); + }); + + pd.setList(list); + return Result.success(pd); + } - + @Override - public Boolean checkAppModifyStrategySearchCondition(HeraAppInfoModifyMessage message) { - if (message.getAppId() == null) { - log.error("checkAppModifyStrategySearchCondition appId is null message : {}", message); - return false; + public Result createWithBaseInfo(AppMonitorModel appMonitorModel, String user) { + + HeraAppBaseInfoModel heraAppBaseInfo = appMonitorModel.baseInfo(); + Integer baseInfoId = createBaseInfo(heraAppBaseInfo); + if (baseInfoId == null) { + log.error("createBaseInfo fail!heraAppBaseInfo:{}", heraAppBaseInfo); + return Result.fail(ErrorCode.unknownError); + } + + AppMonitor appMonitor = appMonitorModel.appMonitor(); + appMonitor.setBaseInfoId(baseInfoId); + + if (!appMonitorServiceExtension.checkCreateParam(appMonitor)) { + log.error("AppMonitorService.createWithBaseInfo 用户{}添加项目{},参数不合法", user, appMonitor); + return Result.fail(ErrorCode.invalidParamError); } - return true; + if (StringUtils.isNotBlank(appMonitor.getOwner()) && appMonitor.getOwner().equals("yes")) { + appMonitor.setOwner(user); + } else { + appMonitor.setCareUser(user); + } + + Result result = create(appMonitor); + if (!result.isSuccess()) { + log.error("AppMonitorController.addApp fail! user:{},appMonitorModel:{}", user, appMonitorModel); + return Result.fail(ErrorCode.invalidParamError); + } + + return Result.success(null); + } - + @Override - public void changeAlarmServiceToZone(Integer pageSize, String appName) { - + public Integer createBaseInfo(HeraAppBaseInfoModel heraAppBaseInfo) { + + HeraAppBaseInfoModel queryCondition = new HeraAppBaseInfoModel(); + queryCondition.setBindId(heraAppBaseInfo.getBindId()); + queryCondition.setPlatformType(heraAppBaseInfo.getPlatformType()); + + List query = heraBaseInfoService.query(queryCondition, 1, 10); + + if (!CollectionUtils.isEmpty(query)) { + log.info("createBaseInfo HeraAppBaseInfo has exist!heraAppBaseInfo:{},query Result:{}", heraAppBaseInfo, + new Gson().toJson(query)); + return query.get(0).getId(); + } + + int i = heraBaseInfoService.insertOrUpdate(heraAppBaseInfo); + + if (i <= 0) { + return null; + } + + return heraAppBaseInfo.getId(); } - - + @Override - public Result getResourceUsageUrl(Integer appId, String appName) { - return null; + public Result create(AppMonitor appMonitor) { + + if (appMonitor == null) { + log.error("AppMonitorService.create param is null"); + return Result.fail(ErrorCode.invalidParamError); + } + if (appMonitor.getProjectId() == null || StringUtils.isEmpty(appMonitor.getProjectName())) { + log.error("AppMonitorService.create param is avalid! projectId or projectName is empty!"); + return Result.fail(ErrorCode.invalidParamError); + } + if (StringUtils.isEmpty(appMonitor.getOwner()) && StringUtils.isEmpty(appMonitor.getCareUser())) { + log.error("AppMonitorService.create param is avalid! owner and careUser can not both null at same time!"); + return Result.fail(ErrorCode.invalidParamError); + } + + /** + * owner、mycare只能存在一种,如果存在owner直接按owner处理,不再继续判断!正常的业务场景不会同时指定owner和mycare; + */ + AppViewType viewType = AppViewType.MyApp; + String userName = appMonitor.getOwner(); + if (StringUtils.isEmpty(appMonitor.getOwner())) { + viewType = AppViewType.MyCareApp; + userName = appMonitor.getCareUser(); + } + AppMonitor app = appMonitorDao.getMyApp(appMonitor.getProjectId(), appMonitor.getIamTreeId(), userName, + viewType); + if (app != null) { + log.info("AppMonitorService.create update Data appMonitor : {}", appMonitor); + //如果指定的projectId,userName,viewType已经存在,则更新一下项目名称即可,无需重复创建数据! + app.setProjectName(appMonitor.getProjectName()); + app.setBaseInfoId(appMonitor.getBaseInfoId()); + int update = appMonitorDao.update(app); + if (update > 0) { + log.info("AppMonitorService.create update Data success appMonitor : {}", appMonitor); + return Result.success(null); + } else { + log.info("AppMonitorService.create database update Data failed appMonitor : {}", appMonitor); + return Result.fail(ErrorCode.unknownError); + } + } + //兼容不同类型的重复添加 + if (AppViewType.MyCareApp.equals(viewType)) { + app = appMonitorDao.getMyApp(appMonitor.getProjectId(), appMonitor.getIamTreeId(), userName, + AppViewType.MyApp); + if (app != null) { + log.error("AppMonitorService.create项目已经添加过参与项目, param={}", appMonitor); + return Result.fail(ErrorCode.REPEAT_ADD_PROJECT); + } + } else { + app = appMonitorDao.getMyApp(appMonitor.getProjectId(), appMonitor.getIamTreeId(), userName, + AppViewType.MyCareApp); + if (app != null) { + //清除已经关注的数据 + Result delResult = this.delete(app.getId()); + if (delResult.getCode() != ErrorCode.success.getCode()) { + return delResult; + } + } + } + try { + int i = appMonitorDao.create(appMonitor); + if (i > 0) { + createGrafana(appMonitor); + log.info("AppMonitorService.create success appMonitor : {}", appMonitor); + return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), null); + } else { + log.info("AppMonitorService.create database create data failed appMonitor : {}", appMonitor); + return Result.fail(ErrorCode.unknownError); + } + + } catch (Exception e) { + log.error("AppMonitorService.create error" + e.getMessage(), e); + return Result.fail(ErrorCode.unknownError); + } + + } + + /** + * 创建grafana图表 + * + * @param appMonitor + */ + + private void createGrafana(AppMonitor appMonitor) { + if (appMonitor.getAppSource() == null) { + return; + } + StringBuilder appName = new StringBuilder(); + appName.append(appMonitor.getProjectId()).append("_").append(appMonitor.getProjectName().replace('.', '_')); + + String area = platFormTypeExtensionService.getGrafanaDirByTypeCode(appMonitor.getAppSource()); + + if (StringUtils.isBlank(area)) { + log.error("invalid grafana area!appMonitor:{}", appMonitor); + return; + } + + HeraAppBaseInfoModel heraAppBaseInfo = new HeraAppBaseInfoModel(); + heraAppBaseInfo.setBindId(appMonitor.getProjectId() + ""); + heraAppBaseInfo.setPlatformType(appMonitor.getAppSource()); + + List query = heraBaseInfoService.query(heraAppBaseInfo, null, null); + HeraAppBaseInfoModel baseInfo = CollectionUtils.isEmpty(query) ? null : query.get(0); + if (baseInfo == null) { + log.error("no base data found for app : {},stop generate grafana url", appMonitor.getProjectName()); + return; + } + + appGrafanaMappingService.createTmpByAppBaseInfo(baseInfo); + + } + + @Override + public Result delete(Integer id) { + + if (id == null) { + log.error("AppMonitorService.delete error param id is null!"); + return Result.fail(ErrorCode.invalidParamError); + } + + AppMonitor app = appMonitorDao.getById(id); + if (app == null) { + log.error("AppMonitorService.delete error cannot find data by id : {}", id); + return Result.fail(ErrorCode.invalidParamError); + } + + app.setStatus(1); + int update = appMonitorDao.update(app); + if (update < 1) { + log.error("AppMonitorService.delete error delete database failed!"); + return Result.fail(ErrorCode.unknownError); + } + + return Result.success(null); + + } + + @Override + public Result deleteByUser(Integer projectId, Integer appSource, String userName) { + + try { + List apps = appMonitorDao.getMyOwnerOrCareAppById(projectId, appSource, userName); + + if (CollectionUtils.isEmpty(apps)) { + log.error("AppMonitorService.deleteByUser error cannot find data,projectId : {}", projectId); + return Result.fail(ErrorCode.unknownError); + } + + for (AppMonitor app : apps) { + app.setStatus(1); + int update = appMonitorDao.update(app); + if (update < 1) { + log.error("AppMonitorService.deleteByUser error delete database failed!app:{}", app); + return Result.fail(ErrorCode.unknownError); + } + } + + return Result.success(null); + + } catch (Exception e) { + log.error("deleteByUser error!" + e.getMessage(), e); + return Result.fail(ErrorCode.unknownError); + } + } + + @Override + public Result>> listApp(String appName, String userName, Integer page, + Integer pageSize_) { + + if (StringUtils.isEmpty(userName)) { + log.error("AppMonitorService.listApp param is invalid userName is empty!"); + return Result.fail(ErrorCode.invalidParamError); + } + + Integer pageNum = page; + Integer pageSize = pageSize_; + + if (pageNum == null || pageNum.intValue() < 1) { + pageNum = 1; + } + + if (pageSize == null) { + pageSize = 10; + } + + PageData pd = new PageData(); + pd.setPage(pageNum); + pd.setPageSize(pageSize); + + try { + + Long dataTotal = appMonitorDao.getDataTotalByOr(appName, userName, userName); + pd.setTotal(dataTotal); + + if (dataTotal != null && dataTotal.intValue() > 0) { + List apps = appMonitorDao.getMyOwnerOrCareApp(appName, userName, pageNum, pageSize); + pd.setList(apps); + } + + log.info("AppMonitorService.listApp success! param appName : {}, userName : {},result Count : {} ", + appName, userName, dataTotal); + + return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), pd); + + } catch (Exception e) { + log.error("AppMonitorService.listApp error" + e.getMessage(), e); + return Result.fail(ErrorCode.unknownError); + } + + } + + @Override + public Result>> listAppDistinct(String userName, String appName, Integer page, + Integer pageSize_) { + + if (StringUtils.isEmpty(userName)) { + log.error("AppMonitorService.listAppDistinct param is invalid userName is empty!"); + return Result.fail(ErrorCode.invalidParamError); + } + + Integer pageNum = page; + Integer pageSize = pageSize_; + + if (pageNum == null || pageNum.intValue() < 1) { + pageNum = 1; + } + + if (pageSize == null) { + pageSize = 10; + } + + PageData pd = new PageData(); + pd.setPage(pageNum); + pd.setPageSize(pageSize); + + try { + + Long dataTotal = appMonitorDao.countAllMyAppDistinct(userName, appName); + pd.setTotal(dataTotal); + + if (dataTotal != null && dataTotal.intValue() > 0) { + List apps = appMonitorDao.getAllMyAppDistinct(userName, appName, pageNum, pageSize); + pd.setList(apps); + // initAppAlarmData(apps); + } + + log.info("AppMonitorService.listAppDistinct success! param appName : {}, userName : {},result Count : {} ", + appName, userName, dataTotal); + + return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), pd); + + } catch (Exception e) { + log.error("AppMonitorService.listAppDistinct error" + e.getMessage(), e); + return Result.fail(ErrorCode.unknownError); + } + + } + + @Override + public Result>> listMyApp(AppMonitor appMonitor, String userName, Integer page, + Integer pageSize_) { + + if (StringUtils.isEmpty(userName)) { + log.error("AppMonitorService.listMyApp param is invalid userName is empty!"); + return Result.fail(ErrorCode.invalidParamError); + } + + Integer pageNum = page; + Integer pageSize = pageSize_; + + if (pageNum == null || pageNum.intValue() < 1) { + pageNum = 1; + } + + if (pageSize == null) { + pageSize = 10; + } + + PageData pd = new PageData(); + pd.setPage(pageNum); + pd.setPageSize(pageSize); + + try { + + Long dataTotal = appMonitorDao.getDataTotal(appMonitor, userName, null); + pd.setTotal(dataTotal); + + log.info("AppMonitorService.listMyApp success! param appName : {}, userName : {},result Count : {} ", + appMonitor.getProjectName(), userName, dataTotal); + + if (dataTotal != null && dataTotal.intValue() > 0) { + List myCareApp = appMonitorDao.getMyOwnerApp(appMonitor, userName, pageNum, pageSize); + pd.setList(myCareApp); + } + + return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), pd); + + } catch (Exception e) { + log.error("AppMonitorService.listMyApp error" + e.getMessage(), e); + return Result.fail(ErrorCode.unknownError); + } + + } + + /** + * 查询我拥有的或关注的项目列表 + * + * @param user + * @param param + * @return + */ + @Override + public Result>> myAndCareAppList(String user, AppMonitorRequest param) { + PageData> pageData = appMonitorDao.getMyAndCareAppList(user, param.getAppName(), + param.getPage(), param.getPageSize(), param.isNeedPage()); + return Result.success(pageData); + } + + @Override + public Result>> listMyCareApp(String appName, String careUser, Integer page, + Integer pageSize_) { + + if (StringUtils.isEmpty(careUser)) { + log.error("AppMonitorService.listMyCareApp param is invalid careUser is empty!"); + return Result.fail(ErrorCode.invalidParamError); + } + + Integer pageNum = page; + Integer pageSize = pageSize_; + + if (pageNum == null || pageNum.intValue() < 1) { + pageNum = 1; + } + + if (pageSize == null) { + pageSize = 10; + } + + PageData pd = new PageData(); + pd.setPage(pageNum); + pd.setPageSize(pageSize); + + try { + + AppMonitor appMonitor = new AppMonitor(); + appMonitor.setProjectName(appName); + Long dataTotal = appMonitorDao.getDataTotal(appMonitor, null, careUser); + pd.setTotal(dataTotal); + + log.info("AppMonitorService.listMyCareApp success! param appName : {}, careUser : {},result Count : {} ", + appName, careUser, dataTotal); + + if (dataTotal != null && dataTotal.intValue() > 0) { + List myCareApp = appMonitorDao.getMyCareApp(appName, careUser, pageNum, pageSize); + pd.setList(myCareApp); + } + + return new Result<>(ErrorCode.success.getCode(), ErrorCode.success.getMessage(), pd); + + } catch (Exception e) { + log.error("AppMonitorService.listMyCareApp error" + e.getMessage(), e); + return Result.fail(ErrorCode.unknownError); + } + + } + + @Override + public AppMonitor getByIamTreeId(Integer aimTreeId) { + + try { + return appMonitorDao.getByIamTreeId(aimTreeId); + } catch (Exception e) { + log.error("AppMonitorService.getByIamTreeId error! {}", e.getMessage(), e); + return null; + } + } + + @Override + public Result getTeslaAlarmHealthByUser(String user) { + return teslaService.getTeslaAlarmHealthByUser(user); + } + + @Override + public void washBaseId() { + Integer pageSize = 100; + Integer page = 0; + + Long dataTotalL = appMonitorDao.getDataTotal(new AppMonitor(), null, null); + log.info("washBaseId totalNum:{}", dataTotalL); + Integer dataTotal = dataTotalL.intValue(); + page = dataTotal % pageSize == 0 ? (dataTotal / pageSize) : (dataTotal / pageSize + 1); + + for (int i = 1; i <= page; i++) { + + List allApps = appMonitorDao.getAllApps(i, pageSize); + for (AppMonitor app : allApps) { + + HeraAppBaseInfoModel queryCondition = new HeraAppBaseInfoModel(); + queryCondition.setBindId(String.valueOf(app.getProjectId())); + queryCondition.setPlatformType(app.getAppSource()); + List appBase = heraBaseInfoService.query(queryCondition, 1, 1); + + if (CollectionUtils.isEmpty(appBase)) { + log.info("washBaseId no HeraAppBaseInfo found for app:{}", app.toString()); + continue; + } + app.setBaseInfoId(appBase.get(0).getId()); + int update = appMonitorDao.update(app); + log.info("wash baseId for app:{},result:{}", app.toString(), update); + } + } + } + + @Override + public Result grafanaInterfaceList() { + return appMonitorServiceExtension.grafanaInterfaceList(); + } + + @Override + public Result selectByIAMId(Integer iamId, Integer iamType, String userName) { + try { + List appMonitors = appMonitorDao.selectByIAMId(iamId, iamType, userName); + return Result.success(appMonitors); + } catch (Throwable t) { + log.error("select by iamId error : ", t); + return Result.fail(ErrorCode.unknownError); + } + } + + @Override + public void heraAppInfoModify(HeraAppInfoModifyMessage baseInfoModify) { + + if (HeraAppModifyType.create.equals(baseInfoModify.getModifyType())) { + HeraAppBaseInfoModel appBaseInfoModel = baseInfoModify.baseInfoModel(); + appGrafanaMappingService.createTmpByAppBaseInfo(appBaseInfoModel); + } + + if (HeraAppModifyType.update.equals(baseInfoModify.getModifyType())) { + this.modifyAppAndAlarm(baseInfoModify); + } + + if (HeraAppModifyType.delete.equals(baseInfoModify.getModifyType())) { + this.heraAppDelete(baseInfoModify); + } + } + + private void heraAppDelete(HeraAppInfoModifyMessage message) { + + deleteByBaseInfoId(message.getId()); + + alarmStrategyService.deleteByAppIdAndIamId(message.getAppId(), message.getIamTreeId()); + } + + private void deleteByBaseInfoId(Integer baseInfoId) { + + List appMonitors = appMonitorDao.listAppsByBaseInfoId(baseInfoId); + if (CollectionUtils.isEmpty(appMonitors)) { + log.info("deleteByBaseInfoId no data found! baseInfoId:{}", baseInfoId); + return; + } + + for (AppMonitor appMonitor : appMonitors) { + appMonitorDao.delete(appMonitor.getId()); + } + } + + @Override + public void modifyAppAndAlarm(HeraAppInfoModifyMessage baseInfoModify) { + + /** + * appMonitor 信息同步变更 + */ + List appMonitors = appMonitorDao.listAppsByBaseInfoId(baseInfoModify.getId()); + if (!CollectionUtils.isEmpty(appMonitors)) { + appMonitors.forEach(t -> { + t.setAppSource(baseInfoModify.getPlatformType()); + t.setProjectId(baseInfoModify.getAppId()); + t.setIamTreeId(baseInfoModify.getIamTreeId()); + t.setIamTreeType(baseInfoModify.getIamTreeType()); + t.setProjectName(baseInfoModify.getAppName()); + appMonitorDao.update(t); + }); + } + + /** + * appName变更,报警策略和报警规则同步变更 + */ + if (baseInfoModify.getIsNameChange()) { + + AlarmStrategy strategy = new AlarmStrategy(); + strategy.setAppId(baseInfoModify.getAppId()); + strategy.setIamId(baseInfoModify.getIamTreeId()); + + if (!appMonitorServiceExtension.checkAppModifyStrategySearchCondition(baseInfoModify)) { + return; + } + + PageData> listPageData = strategyDao.searchByCondNoUser(strategy, 1, 1000, null, + null); + List list = listPageData.getList(); + if (!CollectionUtils.isEmpty(list)) { + list.forEach(t -> { + List rules = ruleDao.selectByStrategyId(t.getId()); + for (AppAlarmRule rule : rules) { + + AppMonitor app = new AppMonitor(); + app.setProjectId(baseInfoModify.getAppId()); + app.setProjectName(baseInfoModify.getAppName()); + + AlarmRuleData ruleData = new AlarmRuleData(); + BeanUtils.copyProperties(rule, ruleData); + ruleData.setLabels(rule.getLabels()); + ruleData.convertLabels(); + + ruleData.setIncludeEnvs(t.getIncludeEnvs()); + ruleData.setExceptEnvs(t.getExceptEnvs()); + ruleData.setIncludeZones(t.getIncludeZones()); + ruleData.setExceptZones(t.getExceptZones()); + + ruleData.setIncludeContainerName(t.getIncludeContainerName()); + ruleData.setExceptContainerName(t.getExceptContainerName()); + ruleData.setAlertMembers(t.getAlertMembers()); + ruleData.setAtMembers(t.getAtMembers()); + + if (!CollectionUtils.isEmpty(t.getIncludeFunctions())) { + ruleData.setIncludeFunctions( + t.getIncludeFunctions().stream().map(String::valueOf).collect(Collectors.toList())); + } + + if (!CollectionUtils.isEmpty(t.getExceptFunctions())) { + ruleData.setExceptFunctions( + t.getExceptFunctions().stream().map(String::valueOf).collect(Collectors.toList())); + } + + ruleData.setIncludeModules(t.getIncludeModules()); + ruleData.setExceptModules(t.getExceptModules()); + + Result result = alarmService.editRule(rule, ruleData, app, rule.getCreater()); + if (!result.isSuccess()) { + log.error("heraAppInfoModify fail! rule : {} , result : {}", rule.toString(), + new Gson().toJson(result)); + continue; + } + + int i = ruleDao.updateByIdSelective(rule); + if (i < 1) { + log.error("heraAppInfoModify update rule db fail! rule{}", rule.toString()); + } + + } + + AlarmStrategy strategyUp = new AlarmStrategy(); + strategyUp.setId(t.getId()); + strategyUp.setAppId(baseInfoModify.getAppId()); + strategyUp.setAppName(baseInfoModify.getAppName()); + strategyUp.setIamId(baseInfoModify.getIamTreeId()); + boolean b = strategyDao.updateById(strategyUp); + if (!b) { + log.error("heraAppInfoModify update strategy fail! old:{},new:{}", t.toString(), + strategyUp.toString()); + } + + }); + } + } + } + + @Override + public void washBugData() { + AlarmStrategy strategy = new AlarmStrategy(); + PageData> listPageData = strategyDao.searchByCondNoUser(strategy, 1, 1000, null, null); + List list = listPageData.getList(); + if (!CollectionUtils.isEmpty(list)) { + list.forEach(t -> { + List rules = ruleDao.selectByStrategyId(t.getId()); + + if (!CollectionUtils.isEmpty(rules)) { + AppAlarmRule rule = rules.get(0); + AppMonitor appMonitor = appMonitorDao.getByIamTreeIdAndAppId(rule.getIamId(), rule.getProjectId()); + + if (appMonitor != null) { + + AlarmStrategy strategyUp = new AlarmStrategy(); + strategyUp.setId(t.getId()); + strategyUp.setAppId(appMonitor.getProjectId()); + strategyUp.setAppName(appMonitor.getProjectName()); + strategyUp.setIamId(appMonitor.getIamTreeId()); + boolean b = strategyDao.updateById(strategyUp); + if (!b) { + log.error("heraAppInfoModify update strategy fail! old:{},new:{}", t.toString(), + strategyUp.toString()); + } + + for (AppAlarmRule rule1 : rules) { + + AlarmRuleData ruleData = new AlarmRuleData(); + BeanUtils.copyProperties(rule1, ruleData); + ruleData.setLabels(rule1.getLabels()); + ruleData.convertLabels(); + + ruleData.setIncludeEnvs(t.getIncludeEnvs()); + ruleData.setExceptEnvs(t.getExceptEnvs()); + ruleData.setIncludeZones(t.getIncludeZones()); + ruleData.setExceptZones(t.getExceptZones()); + ruleData.setIncludeContainerName(t.getIncludeContainerName()); + ruleData.setExceptContainerName(t.getExceptContainerName()); + + // ruleData.setIncludeZones(t.getIncludeEnvs()); + // ruleData.setExceptZones(t.getExceptEnvs()); + ruleData.setAlertMembers(t.getAlertMembers()); + ruleData.setAtMembers(t.getAtMembers()); + + if (!CollectionUtils.isEmpty(t.getIncludeFunctions())) { + ruleData.setIncludeFunctions(t.getIncludeFunctions().stream().map(String::valueOf) + .collect(Collectors.toList())); + } + + if (!CollectionUtils.isEmpty(t.getExceptFunctions())) { + ruleData.setExceptFunctions(t.getExceptFunctions().stream().map(String::valueOf) + .collect(Collectors.toList())); + } + + ruleData.setIncludeModules(t.getIncludeModules()); + ruleData.setExceptModules(t.getExceptModules()); + + Result result = alarmService.editRule(rule1, ruleData, appMonitor, rule1.getCreater()); + if (!result.isSuccess()) { + log.error("washBugData fail! rule1 : {} , result : {}", rule1.toString(), + new Gson().toJson(result)); + continue; + } + + } + } + + + } + + + }); + } + } + + @Override + public void washBugDataForAppMonitor() { + List allApps = appMonitorDao.getAllApps(1, 5000); + for (AppMonitor appMonitor : allApps) { + if (appMonitor.getBaseInfoId() == null) { + log.error("update appMonitor no baseId found! appMonitor : {}", appMonitor.toString()); + continue; + } + HeraAppBaseInfoModel byId = heraBaseInfoService.getById(appMonitor.getBaseInfoId()); + + if (byId == null || StringUtils.isBlank(byId.getBindId())) { + log.error("update appMonitor HeraAppBaseInfo error! appMonitor : {}", appMonitor.toString()); + continue; + } + + try { + appMonitor.setProjectId(Integer.valueOf(byId.getBindId())); + appMonitor.setIamTreeId(byId.getIamTreeId()); + int update = appMonitorDao.update(appMonitor); + if (update < 1) { + log.error("update appMonitor fail! appMonitor : {}", appMonitor.toString()); + } + } catch (NumberFormatException e) { + log.error("update appMonitor error!" + e.getMessage(), e); + continue; + } + } + } + + @Override + public Result historyInstance(String application, Long startTime, Long endTime) { + String promql = "count(jvm_classes_loaded_classes{application=\"" + application + "\"}) by (serverIp)"; + log.info("historyInstance promql : {}", promql); + MetricResponse rangeMetricResponse = prometheusService.queryRangePrometheusByPromQl(promql, startTime, endTime, + null, null); + if (rangeMetricResponse == null || rangeMetricResponse.getData() == null) { + return Result.fail(ErrorCode.unknownError); + } + MetricData rangeMetricData = rangeMetricResponse.getData(); + List rangeResult = rangeMetricData.getResult(); + List rangeIps = new ArrayList<>(); + rangeResult.forEach(t -> { + rangeIps.add(t.getMetric().getServerIp()); + }); + // first time no comparison + // List momentIps = new ArrayList<>(); + // MetricResponse momentMetricResponse = prometheusService.queryRangePrometheusByPromQl(promql, startTime, endTime, null,PrometheusService.MOMENT_REQUEST_MODE); + // if(momentMetricResponse == null || momentMetricResponse.getData() == null){ + // return Result.fail(ErrorCode.unknownError); + // } + // MetricData momentMetricData = momentMetricResponse.getData(); + // List momentResult = momentMetricData.getResult(); + // momentResult.forEach(t -> { + // momentIps.add(t.getMetric().getServerIp()); + // }); + // //The difference between momentIps and rangeIps + // List ips = rangeIps.stream().filter(item -> !momentIps.contains(item)).collect(Collectors.toList()); + return Result.success(rangeIps); + } + + @Override + public Long countByBaseInfoId(List baseInfoIds, String user) { + return appMonitorDao.countByBaseInfoIds(baseInfoIds, user); + } + + @Override + public List searchByBaseInfoId(List baseInfoIds, String user, Integer page, Integer pageSize) { + return appMonitorDao.getDataByBaseInfoIds(baseInfoIds, user, page, pageSize); } - } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ComputeTimerServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ComputeTimerServiceImpl.java new file mode 100644 index 000000000..6b95230ff --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ComputeTimerServiceImpl.java @@ -0,0 +1,246 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.google.common.collect.Lists; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.ComputeTimerService; +import com.xiaomi.mone.monitor.service.api.ComputeTimerServiceExtension; +import com.xiaomi.mone.monitor.service.helper.AlertHelper; +import com.xiaomi.mone.monitor.service.model.AppMonitorRequest; +import com.xiaomi.mone.monitor.service.model.ProjectInfo; +import com.xiaomi.mone.monitor.service.model.prometheus.MetricKind; +import com.xiaomi.mone.monitor.service.model.redis.AppAlarmData; +import com.xiaomi.mone.monitor.service.prometheus.PrometheusService; +import com.xiaomi.mone.monitor.utils.CommonUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import javax.annotation.PreDestroy; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * @author gaoxihui + * @date 2021/8/17 10:08 AM + */ +@Slf4j +@Service +public class ComputeTimerServiceImpl implements ComputeTimerService { + + private final ThreadPoolExecutor executor; + + private final ThreadPoolExecutor metricExecutor; + + @Autowired + PrometheusService prometheusService; + + // @Autowired + // private RedisHelper redisHelper; + @Autowired + private AlertHelper alertHelper; + + @Autowired + private ComputeTimerServiceExtension computeTimerServiceExtension; + + public ComputeTimerServiceImpl() { + executor = new ThreadPoolExecutor(10, 50, 5, TimeUnit.MINUTES, new LinkedBlockingQueue(100), + (Runnable r) -> new Thread(r, "compute-execute-thread-v2"), new ThreadPoolExecutor.CallerRunsPolicy()); + + metricExecutor = new ThreadPoolExecutor(50, 100, 5, TimeUnit.MINUTES, new LinkedBlockingQueue(1000), + (Runnable r) -> new Thread(r, "compute-metric-thread-v2"), new ThreadPoolExecutor.CallerRunsPolicy()); + } + + @Override + @PreDestroy + public void destory() { + executor.shutdownNow(); + metricExecutor.shutdownNow(); + } + @Override + public Result> getProjectStatistics(AppMonitorRequest param) { + final Long endTime = CommonUtil.toSeconds(System.currentTimeMillis()); + final Long startTime = endTime - param.getDuration(); + final String timeDurarion = param.getDuration() + "s"; + final Long step = param.getDuration(); + int projectSize = param.getProjectList().size() > 50 ? 50 : param.getProjectList().size(); + List> callList = new ArrayList<>(); + for (int idx = 0; idx < projectSize; idx++) { + final ProjectInfo project = param.getProjectList().get(idx); + if (project.getId() == null || StringUtils.isBlank(project.getName()) || project.getIamTreeId() == null) { + continue; + } + callList.add(new Callable() { + @Override + public AppAlarmData call() throws Exception { + return getAppAlarmData(project, startTime, endTime, timeDurarion, step, param); + } + }); + } + List dataList = new ArrayList<>(); + try { + List> futures = executor.invokeAll(callList, 30, TimeUnit.SECONDS); + if (!CollectionUtils.isEmpty(futures)) { + AppAlarmData data = null; + for (Future future : futures) { + data = future.get(); + if (data == null) { + continue; + } + dataList.add(data); + } + } + } catch (Exception e) { + log.info("ComputeTimerServiceV2.executor.invokeAll异常 param={}, ", param, e); + } + return Result.success(dataList); + } + + /** + * @param project + * @param startTime + * @param endTime + * @param timeDurarion + * @param param + */ + @Override + public AppAlarmData getAppAlarmData(ProjectInfo project, Long startTime, Long endTime, String timeDurarion, + Long step, AppMonitorRequest param) { + String appName = new StringBuilder().append(project.getId()).append("_") + .append(project.getName().replaceAll("-", "_")).toString(); + + AppAlarmData data = null; + // AppAlarmData data = redisHelper.getAppAlarmData(appName); + // if (data != null) { + // log.info("ComputeTimerServiceV2.getAppAlarmData cache-result appName={}, data={}", appName, data); + // return data; + // } + + AppAlarmData.AppAlarmDataBuilder dataBuilder = AppAlarmData.builder(); + dataBuilder.id(project.getId()).name(project.getName()).iamTreeId(project.getIamTreeId()); + dataBuilder.startTime(startTime).endTime(endTime); + List kinds = Lists.newArrayList(MetricKind.http, MetricKind.dubbo, MetricKind.grpc, MetricKind.apus, + MetricKind.thrift, MetricKind.db, MetricKind.redis); + List> callList = kinds.stream().map(kind -> { + return new Callable() { + @Override + public Void call() throws Exception { + computByMetricType(param, appName, kind, dataBuilder, startTime, endTime, timeDurarion, step); + return null; + } + }; + }).collect(Collectors.toList()); + try { + List> futureList = metricExecutor.invokeAll(callList); + if (param == null) { + dataBuilder.alertTotal( + alertHelper.queryAlertEventNum(project.getId().intValue(), project.getIamTreeId().intValue(), + startTime, endTime)); + } + futureList.stream().forEach(f -> { + try { + f.get(); + } catch (Throwable e) { + log.warn("metric统计查询异步get结果异常", project.getName(), e); + } + }); + } catch (Throwable e) { + log.error("metric统计异步查询异常 projectName={}", project.getName(), e); + } + dataBuilder.logExceptionNum(0); + data = dataBuilder.build(); + calExceptionTotal(data); + calSlowQueryTotal(data); + log.info("ComputeTimerServiceV2.getAppAlarmData new-result appName={}, data={}", appName, data); + // redisHelper.setAppAlarmData(appName, data); + return data; + } + + private void computByMetricType(AppMonitorRequest param, String appName, MetricKind metricKind, + AppAlarmData.AppAlarmDataBuilder dataBuilder, Long startTime, Long endTime, String timeDurarion, + Long step) { + computeTimerServiceExtension.computByMetricType(param, appName, metricKind, dataBuilder, startTime, endTime, + timeDurarion, step); + } + @Override + public AppAlarmData countAppMetricData(AppMonitorRequest param) { + ProjectInfo project = param.getProjectList().get(0); + Long startTime = param.getStartTimeCurrent(); + Long endTime = param.getEndTimeCurrent(); + Long step = endTime - startTime; + return getAppAlarmData(project, startTime, endTime, step + "s", step, param); + } + + + /** + * 计算异常总数 + * + * @return + */ + private void calExceptionTotal(AppAlarmData data) { + Integer exceptionTotal = 0; + exceptionTotal += Optional.ofNullable(data.getHttpExceptionNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getHttpClientExceptionNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getDubboExceptionNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getDubboPExceptionNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getSqlExceptionNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getOracleExceptionNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getRedisExceptionNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getGrpcServerErrorNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getGrpcClientErrorNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getApusServerErrorNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getApusClientErrorNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getThriftServerErrorNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getThriftClientErrorNum()).orElse(0); + exceptionTotal += Optional.ofNullable(data.getEsExceptionNum()).orElse(0); + data.setExceptionTotal(exceptionTotal); + } + + /** + * 计算慢查询总数 + * + * @return + */ + private void calSlowQueryTotal(AppAlarmData data) { + Integer slowQueryTotal = 0; + slowQueryTotal += Optional.ofNullable(data.getHttpSlowNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getHttpClientSlowNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getDubboCSlowQueryNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getDubboProviderSlowQueryNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getSqlSlowQueryNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getOracleSlowNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getRedisSlowNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getGrpcClientSlowQueryNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getGrpcServerSlowQueryNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getApusClientSlowQueryNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getApusServerSlowQueryNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getThriftClientSlowQueryNum()).orElse(0); + slowQueryTotal += Optional.ofNullable(data.getEsSlowNum()).orElse(0); + data.setSlowTotal(slowQueryTotal); + } + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaApiServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaApiServiceImpl.java new file mode 100644 index 000000000..47600f88d --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaApiServiceImpl.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.xiaomi.mone.monitor.service.impl; + +import com.google.gson.Gson; +import com.xiaomi.mone.monitor.service.AppGrafanaMappingService; +import com.xiaomi.mone.monitor.service.GrafanaApiService; +import lombok.extern.slf4j.Slf4j; +import org.apache.dubbo.config.annotation.Service; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * @author gaoxihui + * @date 2021/7/10 5:23 PM + */ +@Slf4j +@Service(registry = "registryConfig",interfaceClass = GrafanaApiService.class, retries = 0,group = "${dubbo.group}") +public class GrafanaApiServiceImpl implements GrafanaApiService { + + @Autowired + AppGrafanaMappingService appGrafanaMappingService; + + @Override + public String getUrlByAppName(String appName) { + log.info("Dubbo.GrafanaServiceImpl.getUrlByAppName param appName : {}" ,appName); + + String result = new Gson().toJson(appGrafanaMappingService.getGrafanaUrlByAppName(appName)); + log.info("Dubbo.GrafanaServiceImpl.getUrlByAppName param appName : {} ,return result : {}" ,appName,result); + return result; + } + + @Override + public String createGrafanaUrlByAppName(String appName,String area) { + log.info("Dubbo.GrafanaServiceImpl.createGrafanaUrlByAppName param appName : {}" ,appName); + + String result = appGrafanaMappingService.createGrafanaUrlByAppName(appName,area); + log.info("Dubbo.GrafanaServiceImpl.createGrafanaUrlByAppName param appName : {} ,return result : {}" ,appName,result); + return result; + } +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaBlackListServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaBlackListServiceImpl.java new file mode 100644 index 000000000..8729aab4d --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaBlackListServiceImpl.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.xiaomi.mone.monitor.dao.GrafanaBlackListDao; +import com.xiaomi.mone.monitor.dao.model.AppGrafanaBlackList; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.GrafanaBlackListService; +import com.xiaomi.mone.monitor.service.model.PageData; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@Slf4j +public class GrafanaBlackListServiceImpl implements GrafanaBlackListService { + + @Autowired + GrafanaBlackListDao grafanaBlackListDao; + + @Override + public Result createBlackList(String serverName) { + //插库 + List blackListByServerName = grafanaBlackListDao.getBlackListByServerName(serverName); + if (blackListByServerName.size() >= 1) { + return Result.fail(ErrorCode.REPEAT_ADD_PROJECT); + } + Integer blackList = grafanaBlackListDao.createBlackList(serverName); + if (blackList == null || blackList == -1 || blackList == 0) { + log.error("GrafanaBlackListService.createBlackList error"); + return Result.fail(ErrorCode.unknownError); + } + return Result.success(blackList); + } + + @Override + public Result getBlackList(String serverName) { + //查库 + AppGrafanaBlackList blackList = grafanaBlackListDao.getBlackList(serverName); + return Result.success(blackList); + } + + @Override + public Result delBlackList(String serverName) { + //删除 + Integer res = grafanaBlackListDao.delBlackListByServerName(serverName); + if (res == null || res == 0 || res == -1) { + log.error("GrafanaBlackListService.delBlackList error"); + return Result.fail(ErrorCode.unknownError); + } + return Result.success(res); + } + + @Override + public Result getBlackListList(Integer page, Integer pageSize) { + //获取列表 + PageData pd = new PageData(); + pd.setPage(page); + pd.setPageSize(pageSize); + pd.setTotal(grafanaBlackListDao.getTotalBlackList()); + pd.setList(grafanaBlackListDao.getAllBlackList(page, pageSize)); + return Result.success(pd); + } + + @Override + public boolean isInBlackList(String serverName) { + List blackListByServerName = grafanaBlackListDao.getBlackListByServerName(serverName); + if (blackListByServerName.size() >= 1) { + return true; + } + return false; + } + + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaServiceImpl.java index 1bf0e0307..ade047456 100644 --- a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaServiceImpl.java +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/GrafanaServiceImpl.java @@ -13,41 +13,97 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.xiaomi.mone.monitor.service.impl; -import com.google.gson.Gson; -import com.xiaomi.mone.monitor.service.AppGrafanaMappingService; -import com.xiaomi.mone.monitor.service.GrafanaApiService; +import com.google.gson.JsonArray; +import com.xiaomi.mone.monitor.dao.model.GrafanaTemplate; +import com.xiaomi.mone.monitor.service.GrafanaService; +import com.xiaomi.mone.monitor.service.api.GrafanaServiceExtension; +import com.xiaomi.mone.monitor.service.model.MutiGrafanaResponse; import lombok.extern.slf4j.Slf4j; -import org.apache.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; /** - * @author gaoxihui - * @date 2021/7/10 5:23 PM + * @author zhangxiaowei6 */ @Slf4j -@Service(registry = "registryConfig",interfaceClass = GrafanaApiService.class, retries = 0,group = "${dubbo.group}") -public class GrafanaServiceImpl implements GrafanaApiService { - +@Service +public class GrafanaServiceImpl implements GrafanaService { + @Autowired - AppGrafanaMappingService appGrafanaMappingService; - + GrafanaServiceExtension grafanaServiceExtension; + @Override - public String getUrlByAppName(String appName) { - log.info("Dubbo.GrafanaServiceImpl.getUrlByAppName param appName : {}" ,appName); - - String result = new Gson().toJson(appGrafanaMappingService.getGrafanaUrlByAppName(appName)); - log.info("Dubbo.GrafanaServiceImpl.getUrlByAppName param appName : {} ,return result : {}" ,appName,result); - return result; + public void setFolderData(String area) { + grafanaServiceExtension.setFolderData(area); } - + @Override - public String createGrafanaUrlByAppName(String appName,String area) { - log.info("Dubbo.GrafanaServiceImpl.createGrafanaUrlByAppName param appName : {}" ,appName); - - String result = appGrafanaMappingService.createGrafanaUrlByAppName(appName,area); - log.info("Dubbo.GrafanaServiceImpl.createGrafanaUrlByAppName param appName : {} ,return result : {}" ,appName,result); - return result; + public void setContainerAndHostUrl(String area) { + grafanaServiceExtension.setContainerAndHostUrl(area); + } + + @Override + public String requestGrafana(String serverType, String appName, String area) { + return grafanaServiceExtension.requestGrafana(serverType, appName, area); + } + + @Override + public MutiGrafanaResponse requestGrafanaTemplate(String group, String title, String area, GrafanaTemplate template, + List funcList) { + return grafanaServiceExtension.requestGrafanaTemplate(group, title, area, template, funcList); + } + + @Override + public Map beforeRequestGrafana(String area, String title) { + return grafanaServiceExtension.beforeRequestGrafana(area, title); + } + + @Override + public String innerRequestGrafanaStr(String area, String title, String containerName, String group, + GrafanaTemplate template, String application) { + return grafanaServiceExtension.innerRequestGrafanaStr(area, title, containerName, group, template, application); + } + + //Get grafana template variables + @Override + public Map getTemplateVariables(String folderId, String group, String title, String folderUid, + String grafanaUrl, String containerName, String area, String application) { + return grafanaServiceExtension.getTemplateVariables(folderId, group, title, folderUid, grafanaUrl, + containerName, area, application); + } + + //Replace the base panel and keep the user-defined panel + @Override + public String getFinalData(String data, String url, String apiKey, String method, String title, + String panelIdList) { + return grafanaServiceExtension.getFinalData(data, url, apiKey, method, title, panelIdList, false, null); + } + + @Override + public String innerRequestGrafana(String data, String url, String apiKey, String method) { + return grafanaServiceExtension.innerRequestGrafana(data, url, apiKey, method); + } + + @Override + public void getCustomPanels(String grafanaStr, JsonArray basicPanels, int basicDiyPanelGirdPosY, String title, + String panelIdList) { + grafanaServiceExtension.getCustomPanels(grafanaStr, basicPanels, basicDiyPanelGirdPosY, title, panelIdList); + } + + //Determine whether the request result of generating/updating the grafana graph is json in the specific format of grafana + @Override + public String isGrafanaDataJson(String jobJson) { + return grafanaServiceExtension.isGrafanaDataJson(jobJson); + } + + @Override + public String getDashboardLastVersion(String dashboardId) { + return grafanaServiceExtension.getDashboardLastVersion(dashboardId); } } diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/HeraBaseInfoServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/HeraBaseInfoServiceImpl.java new file mode 100644 index 000000000..8b6d512cb --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/HeraBaseInfoServiceImpl.java @@ -0,0 +1,464 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.xiaomi.mone.app.api.model.HeraAppBaseInfoModel; +import com.xiaomi.mone.app.api.model.HeraAppBaseInfoParticipant; +import com.xiaomi.mone.app.api.model.HeraAppRoleModel; +import com.xiaomi.mone.app.api.service.HeraAppService; +import com.xiaomi.mone.monitor.bo.AlertGroupParam; +import com.xiaomi.mone.monitor.bo.PlatForm; +import com.xiaomi.mone.monitor.bo.UserInfo; +import com.xiaomi.mone.monitor.dao.HeraAppRoleDao; +import com.xiaomi.mone.monitor.dao.model.HeraAppRole; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.AlertGroupService; +import com.xiaomi.mone.monitor.service.HeraBaseInfoService; +import com.xiaomi.mone.monitor.service.extension.PlatFormTypeExtensionService; +import com.xiaomi.mone.monitor.service.model.Area; +import com.xiaomi.mone.monitor.service.model.EnvMapping; +import com.xiaomi.mone.monitor.service.model.HeraAppBaseQuery; +import com.xiaomi.mone.monitor.service.model.PageData; +import com.xiaomi.mone.monitor.service.model.Region; +import com.xiaomi.mone.tpc.login.util.UserUtil; +import com.xiaomi.mone.tpc.login.vo.AuthUserVo; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.dubbo.config.annotation.Reference; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author gaoxihui + * @date 2022/3/24 4:51 PM + */ +@Slf4j +@Service +public class HeraBaseInfoServiceImpl implements HeraBaseInfoService { + + @Autowired + HeraAppRoleDao heraAppRoleDao; + + @Autowired + AlertGroupService alertGroupService; + + @Reference(registry = "registryConfig", check = false, interfaceClass = HeraAppService.class, group = "${dubbo.group.heraapp}", timeout = 5000) + HeraAppService hearAppService; + + @Autowired + PlatFormTypeExtensionService platFormTypeExtensionService; + + @Override + public Result addRole(HeraAppRoleModel model) { + Integer integer = hearAppService.addRole(model); + + log.info("addRole param:{},result:{}", model.toString(), integer); + + if (integer.intValue() > 0) { + return Result.success(); + } + + return Result.fail(ErrorCode.unknownError); + } + + @Override + public Result delRole(Integer id) { + + Integer i = hearAppService.delRoleById(id); + if (i.intValue() > 0) { + return Result.success(); + } + + return Result.fail(ErrorCode.unknownError); + + } + + @Override + public Result queryRole(HeraAppRoleModel model, Integer pageNo, Integer pageCount) { + + if (pageNo == null || pageNo.intValue() < 1) { + pageNo = 1; + } + if (pageCount == null || pageCount.intValue() < 1) { + pageCount = 10; + } + + Long aLong = hearAppService.countRole(model); + + PageData pd = new PageData(); + pd.setPage(pageNo); + pd.setPageSize(pageCount); + pd.setTotal(aLong); + + if (aLong == null || aLong.intValue() == 0) { + log.info("查询hera app角色没有数据,param:{}", model.toString()); + return Result.success(pd); + } + + List heraAppRoleModels = hearAppService.queryRole(model, pageNo, pageCount); + + pd.setList(heraAppRoleModels); + + return Result.success(pd); + + } + + @Override + public String getArea(String bindId, Integer plat, String regionSign) { + + HeraAppBaseInfoModel appBaseInfo = this.getAppByBindId(bindId, plat); + + log.info("getArea#appBaseInfo :{},", appBaseInfo.toString()); + + if (platFormTypeExtensionService.belongPlatForm(appBaseInfo.getPlatformType(), PlatForm.miCloud)) { + String envsMap = appBaseInfo.getEnvsMap(); + if (StringUtils.isBlank(envsMap)) { + return null; + } + EnvMapping envMapping = new Gson().fromJson(envsMap, EnvMapping.class); + log.info("getArea# appId:{},regionSign:{}, envMapping:{}", bindId, regionSign, envMapping.toString()); + if (envMapping == null || CollectionUtils.isEmpty(envMapping.getAreas())) { + return null; + } + + List areas = envMapping.getAreas(); + for (Area area : areas) { + List regions = area.getRegions(); + if (CollectionUtils.isEmpty(regions)) { + log.info("getArea,no regions found!bindId:{}", bindId); + return null; + } + //这里的regionSign对应的是region的name + for (Region region1 : regions) { + if (region1.getName().equals(regionSign)) { + return area.getName(); + } + + } + } + } + + return null; + + } + + @Override + public Result queryByParticipant(HeraAppBaseQuery query) { + + //适配到远程查询 + return queryByParticipantRemote(query); + + } + + @Override + public HeraAppBaseInfoModel getById(Integer id) { + return this.getByIdRemote(id); + } + + @Override + public void deleAppById(Integer id) { + + HeraAppBaseInfoModel baseInfoModel = this.getById(id); + + Integer integer = this.deleteByIdRemote(id); + if (integer.intValue() > 0) { + log.info("deleAppById sucess!dataId:{}", id); + } else { + log.info("deleAppById fail!dataId:{}", id); + } + + HeraAppRole role = new HeraAppRole(); + role.setAppId(baseInfoModel.getBindId()); + List roles = heraAppRoleDao.query(role, 1, 1000); + if (!CollectionUtils.isEmpty(roles)) { + for (HeraAppRole roleTmp : roles) { + Integer integer1 = heraAppRoleDao.delById(roleTmp.getId()); + if (integer1.intValue() > 0) { + log.info("del HeraAppRole AppById sucess!dataId:{}", id); + } else { + log.info("del HeraAppRole AppById fail!dataId:{}", id); + } + } + } + } + + @Override + public HeraAppBaseInfoModel getByBindIdAndPlat(String bindId, Integer plat) { + HeraAppBaseInfoModel query = new HeraAppBaseInfoModel(); + query.setBindId(bindId); + query.setPlatformType(plat); + + List list = this.query(query, null, null); + + if (CollectionUtils.isEmpty(list)) { + log.info("getByBindIdAndPlat no data found! bindId:{},plat:{}", bindId, plat); + return null; + } + return list.get(0); + } + + @Override + public void deleAppByBindIdAndPlat(String bindId, Integer plat) { + + if (StringUtils.isBlank(bindId) || plat == null) { + log.error("invalid param,bindId:{},plat:{}", bindId, plat); + return; + } + + HeraAppBaseInfoModel query = new HeraAppBaseInfoModel(); + query.setBindId(bindId); + query.setPlatformType(plat); + + List list = this.query(query, null, null); + + if (CollectionUtils.isEmpty(list)) { + log.info("deleAppByBindIdAndPlat no data found! bindId:{},plat:{}", bindId, plat); + } + + for (HeraAppBaseInfoModel baseInfo : list) { + Integer integer = hearAppService.delById(baseInfo.getId()); + if (integer.intValue() > 0) { + log.info("deleAppByBindIdAndPlat success!baseInfo:{}", new Gson().toJson(baseInfo)); + } else { + log.error("deleAppByBindIdAndPlat success!baseInfo:{}", new Gson().toJson(baseInfo)); + } + } + + } + + @Override + public HeraAppBaseInfoModel getByBindIdAndName(String bindId, String appName) { + if (StringUtils.isBlank(bindId) || StringUtils.isBlank(appName)) { + log.error("getByBindIdAndName invalid param,bindId:{},appName:{}", bindId, appName); + return null; + } + + HeraAppBaseInfoModel query = new HeraAppBaseInfoModel(); + query.setBindId(bindId); + query.setAppName(appName); + + List list = this.query(query, null, null); + + if (CollectionUtils.isEmpty(list)) { + log.info("HeraAppBaseInfo#getByBindIdAndName no data found,bindId:{}", bindId); + return null; + } + + return list.get(0); + + } + + @Override + public HeraAppBaseInfoModel getAppByBindId(String bindId, Integer platFromType) { + + if (StringUtils.isBlank(bindId)) { + log.error("invalid param,bindId:{}", bindId); + return null; + } + + HeraAppBaseInfoModel query = new HeraAppBaseInfoModel(); + query.setBindId(bindId); + query.setPlatformType(platFromType); + + List list = this.query(query, null, null); + + if (CollectionUtils.isEmpty(list)) { + log.info("HeraAppBaseInfo#getAppByBindId no data found,bindId:{}", bindId); + return null; + } + + return list.get(0); + + } + + @Override + public Result getAppMembersByAppId(String appId, Integer platForm, String user) { + HeraAppRole role = new HeraAppRole(); + role.setAppId(appId); + role.setAppPlatform(platForm); + List roles = heraAppRoleDao.query(role, 1, 1000); + log.info("HeraBaseInfoService#getAppMembersByAppId appId:{}, platForm:{},result:{}", appId, platForm, + new Gson().toJson(roles)); + + PageData pageData = new PageData<>(); + pageData.setTotal(0l); + + if (CollectionUtils.isEmpty(roles)) { + log.info("getAppMembersByAppId no data found!appId:{},platForm:{}", appId, platForm); + return Result.success(pageData); + } + + List members = roles.stream().filter(t -> StringUtils.isNotBlank(t.getUser())).map(t1 -> t1.getUser()) + .collect(Collectors.toList()); + + if (CollectionUtils.isEmpty(members)) { + return Result.success(pageData); + } + + List userList = Lists.newArrayList(); + + AlertGroupParam param = new AlertGroupParam(); + param.setPage(1); + param.setPageSize(50); + + for (String userName : members) { + param.setName(userName); + Result>> pageDataResult = alertGroupService.userSearch(user, param); + log.info("alertGroupService#userSearch userName:{}, result:{}", userName, + new Gson().toJson(pageDataResult)); + + AuthUserVo userVoSearch = UserUtil.parseFullAccount(userName); + + String compUser = userVoSearch == null ? userName + : StringUtils.isBlank(userVoSearch.getAccount()) ? userName : userVoSearch.getAccount(); + + if (pageDataResult.getData().getTotal().intValue() > 0) { + userList.addAll(pageDataResult.getData().getList().stream().filter(t -> compUser.equals(t.getName())) + .collect(Collectors.toList())); + } + } + + pageData.setList(userList); + pageData.setTotal(Long.valueOf(userList.size())); + + return Result.success(pageData); + } + + @Override + public Long count(HeraAppBaseInfoModel baseInfo) { + + return this.countRemote(baseInfo); + } + + @Override + public List query(HeraAppBaseInfoModel baseInfo, Integer pageCount, Integer pageNum) { + return this.queryRemote(baseInfo, pageCount, pageNum); + } + + @Override + public List queryRemote(HeraAppBaseInfoModel baseInfo, Integer pageCount, Integer pageNum) { + + List baseInfoModels = hearAppService.query(baseInfo, pageCount, pageNum); + if (CollectionUtils.isEmpty(baseInfoModels)) { + return Lists.newArrayList(); + } + + return baseInfoModels; + + } + + @Override + public Long countRemote(HeraAppBaseInfoModel baseInfo) { + return hearAppService.count(baseInfo); + + } + + @Override + public HeraAppBaseInfoModel getByIdRemote(Integer id) { + HeraAppBaseInfoModel baseInfoModel = hearAppService.getById(id); + if (baseInfoModel == null) { + return null; + } + + return baseInfoModel; + } + + @Override + public int deleteByIdRemote(Integer id) { + + return hearAppService.delById(id); + + } + + @Override + public Result queryByParticipantRemote(HeraAppBaseQuery query) { + + com.xiaomi.mone.app.api.model.HeraAppBaseQuery queryRemote = new com.xiaomi.mone.app.api.model.HeraAppBaseQuery(); + BeanUtils.copyProperties(query, queryRemote); + + //MyParticipant只有值为yes才查询我参与的应用,传其他值均查询所有 + if (StringUtils.isBlank(query.getMyParticipant()) || !"yes".equals(query.getMyParticipant())) { + query.setMyParticipant(null); + } + + PageData pd = new PageData(); + + Long aLong = hearAppService.countByParticipant(queryRemote); + log.info("queryByParticipantRemote#countByParticipant count : {}", aLong); + pd.setTotal(aLong); + pd.setPage(query.getPage()); + pd.setPageSize(query.getPageSize()); + + if (aLong != null && aLong.intValue() > 0) { + List list = new ArrayList<>(); + List heraAppBaseInfoParticipants = hearAppService.queryByParticipant( + queryRemote); + log.info("queryByParticipantRemote#queryByParticipant result : {}", + new Gson().toJson(heraAppBaseInfoParticipants)); + if (!CollectionUtils.isEmpty(heraAppBaseInfoParticipants)) { + heraAppBaseInfoParticipants.forEach(t -> { + HeraAppBaseInfoParticipant heraAppBaseInfoParticipant = new HeraAppBaseInfoParticipant(); + BeanUtils.copyProperties(t, heraAppBaseInfoParticipant); + list.add(heraAppBaseInfoParticipant); + }); + } + + pd.setList(list); + } + + return Result.success(pd); + + } + + @Override + public int insertOrUpdate(HeraAppBaseInfoModel heraAppBaseInfo) { + if (null == heraAppBaseInfo) { + log.error("[HeraBaseInfoDao.create] null heraAppBaseInfo"); + return 0; + } + + heraAppBaseInfo.setCreateTime(new Date()); + heraAppBaseInfo.setUpdateTime(new Date()); + heraAppBaseInfo.setStatus(0); + + heraAppBaseInfo.setAppSignId(heraAppBaseInfo.getBindId() + "-" + heraAppBaseInfo.getPlatformType()); + + try { + int affected = hearAppService.insertOrUpdate(heraAppBaseInfo); + if (affected < 1) { + log.warn("[HeraBaseInfoDao.create] failed to insert heraAppBaseInfo: {}", heraAppBaseInfo.toString()); + return 0; + } + } catch (Exception e) { + log.error("[HeraBaseInfoDao.create] failed to insert heraAppBaseInfo: {}, err: {}", + heraAppBaseInfo.toString(), e); + return 0; + } + return 1; + } + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/HeraDashboardServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/HeraDashboardServiceImpl.java new file mode 100644 index 000000000..abef1a216 --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/HeraDashboardServiceImpl.java @@ -0,0 +1,479 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.config.annotation.NacosValue; +import com.alibaba.nacos.api.exception.NacosException; +import com.google.gson.Gson; +import com.xiaomi.data.push.client.HttpClientV5; +import com.xiaomi.mone.monitor.DashboardConstant; +import com.xiaomi.mone.monitor.bo.DashboardDTO; +import com.xiaomi.mone.monitor.bo.GrafanaApiKeyReq; +import com.xiaomi.mone.monitor.bo.GrafanaApiKeyRes; +import com.xiaomi.mone.monitor.bo.GrafanaCreateDashboardRes; +import com.xiaomi.mone.monitor.bo.GrafanaCreateDataSourceReq; +import com.xiaomi.mone.monitor.bo.GrafanaCreateDataSourceRes; +import com.xiaomi.mone.monitor.bo.GrafanaFolderReq; +import com.xiaomi.mone.monitor.bo.GrafanaFolderRes; +import com.xiaomi.mone.monitor.bo.GrafanaGetDataSourceRes; +import com.xiaomi.mone.monitor.bo.GrafanaGetFolderRes; +import com.xiaomi.mone.monitor.dao.GrafanaTemplateDao; +import com.xiaomi.mone.monitor.dao.model.GrafanaTemplate; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.HeraDashboardService; +import com.xiaomi.mone.monitor.service.prometheus.JobService; +import com.xiaomi.mone.monitor.utils.FreeMarkerUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.Base64; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * @author zhangxiaowei6 + * @date 2023-02-22 + */ +@Service +@Slf4j +@ConditionalOnProperty(name = "service.selector.property", havingValue = "outer") +public class HeraDashboardServiceImpl implements HeraDashboardService { + + private Gson gson = new Gson(); + + @Autowired + GrafanaTemplateDao grafanaTemplateDao; + + @Autowired + JobService jobService; + + @Value("${nacos.config.addrs}") + private String nacosAddress; + + @NacosValue(value = "${grafana.address}", autoRefreshed = true) + public String grafanaUrl; + + @NacosValue(value = "${grafana.domain}", autoRefreshed = true) + public String grafanaDomain; + + @NacosValue(value = "${grafana.apikey.url}", autoRefreshed = true) + public String grafanaApiKeyUrl; + + @NacosValue(value = "${grafana.datasource.url}", autoRefreshed = true) + public String grafanaDatasourceUrl; + + @NacosValue(value = "${grafana.folder.url}", autoRefreshed = true) + public String grafanaFolderUrl; + + @NacosValue(value = "${grafana.jaeger.query.token}", autoRefreshed = true) + public String jaegerQueryToken; + + @NacosValue(value = "${grafana.createDashboard.url}", autoRefreshed = true) + public String grafanaDashboardUrl; + + public static final String HERA_GRAFANA_TEMPLATE = "/heraGrafanaTemplate"; + + public static final String HERA_SCRAPE_JOB_TEMPLATE = "/heraScrapeJobTemplate"; + + @Value("${server.type}") + private String serverType; + + @NacosValue(value = "${prometheus.url}", autoRefreshed = true) + private String prometheusUrl; + + @NacosValue(value = "${grafana.username}", autoRefreshed = true) + private String grafanaUserName; + + @NacosValue(value = "${grafana.password}", autoRefreshed = true) + private String grafanaPassword; + + @PostConstruct + @Override + public void init() { + try { + log.info("begin createDefaultGrafanaResource"); + createDefaultScrapeJob(); + createDefaultDashboardTemplate(); + DashboardDTO dataSourceDTO = new DashboardDTO(); + if (StringUtils.isBlank(dataSourceDTO.getPrometheusDatasource())) { + dataSourceDTO.setPrometheusDatasource(prometheusUrl); + } + if (StringUtils.isBlank(dataSourceDTO.getUsername())) { + dataSourceDTO.setUsername(grafanaUserName); + } + if (StringUtils.isBlank(dataSourceDTO.getPassword())) { + dataSourceDTO.setPassword(grafanaPassword); + } + if (StringUtils.isBlank(dataSourceDTO.getDashboardFolderName())) { + dataSourceDTO.setDashboardFolderName(DashboardConstant.DEFAULT_FOLDER_NAME); + } + Result dashboard = createGrafanaResources(dataSourceDTO); + } catch (Exception e) { + log.error("GrafanaInitController init error:", e); + throw new RuntimeException("GrafanaInitController init error"); + } + } + + @Override + public Result createGrafanaResources(DashboardDTO dashboardDTO) { + log.info("HeraDashboardService.createGrafanaResources param:{}", gson.toJson(dashboardDTO)); + //base64 username & password + String base64Str = dashboardDTO.getUsername() + ":" + dashboardDTO.getPassword(); + String basicAuth = Base64.getEncoder().encodeToString(base64Str.getBytes()); + //request api key + Map header = new HashMap<>(); + header.put("Content-Type", "application/json"); + header.put("Authorization", "Basic " + basicAuth); + String apiKey = createApiKey(header); + if (apiKey == null || apiKey.isEmpty()) { + return Result.fail(ErrorCode.API_KEY_CREATE_FAIL); + } + log.info("createGrafanaResources.apikey:{}", apiKey); + //by api key to create prometheus dataSource + header.put("Authorization", "Bearer " + apiKey); + String datasourceUid = createDataSource(header, dashboardDTO.getPrometheusDatasource()); + if (datasourceUid == null || datasourceUid.isEmpty()) { + return Result.fail(ErrorCode.DATASOURCE_CREATE_FAIL); + } + log.info("createGrafanaResources.datasourceUid:{}", datasourceUid); + //by api key to create hera folder + int folderId = createFolder(header, dashboardDTO.getDashboardFolderName()); + if (folderId == -1) { + return Result.fail(ErrorCode.FOLDER_CREATE_FAIL); + } + //grafana template replace template variable,request grafana generate dashboard and panel. + createDefaultGrafanaDashboard(datasourceUid, header); + + //Write the url returned by the panel back to mimonitor's nacos configuration + try { + ConfigService configService = NacosFactory.createConfigService(nacosAddress); + String nacosResult = configService.getConfig(DashboardConstant.DEFAULT_MIMONITOR_NACOS_CONFIG, + DashboardConstant.DEFAULT_MIMONITOR_NACOS_GROUP, 5000); + Properties props = new Properties(); + props.load(new StringReader(nacosResult)); + props.setProperty("grafana.api.key", apiKey); + props.setProperty("grafana.prometheus.datasource", DashboardConstant.GRAFANA_DATASOURCE_NAME); + props.setProperty("grafana.folder.uid", DashboardConstant.GRAFANA_FOLDER_UID); + props.setProperty("prometheusUid", datasourceUid); + + StringWriter writer = new StringWriter(); + props.store(writer, "after replace!"); + String finalNacosConfig = writer.getBuffer().toString(); + //request nacos cover config + log.info("createGrafanaResources.before overlays nacos config:{}", finalNacosConfig); + boolean postResult = configService.publishConfig(DashboardConstant.DEFAULT_MIMONITOR_NACOS_CONFIG, + DashboardConstant.DEFAULT_MIMONITOR_NACOS_GROUP, finalNacosConfig); + if (!postResult) { + log.error("createGrafanaResources.create nacos config failed:{}", postResult); + } else { + log.info("createGrafanaResources.create nacos config success"); + } + } catch (IOException | NacosException e) { + log.error("createGrafanaResources.request nacos error:{}", e.getMessage()); + } + return Result.success("success"); + } + + //create biz、docker、node、serviceMarket、interfaceMarket etc... + @Override + public void createDefaultDashboardTemplate() { + //Only the first initialization is created using freeMarker. First check if the templates already exist in the database. + // If so, do not create them again + DashboardConstant.GRAFANA_SRE_TEMPLATES.forEach(name -> { + GrafanaTemplate grafanaTemplate = grafanaTemplateDao.fetchOneByName(name); + if (grafanaTemplate == null) { + //If it has not been created, it is created from the ftl file + try { + insertDashboardTemplate(name); + } catch (IOException e) { + log.error("HeraDashboardService.createDefaultDashboardTemplate error :{}", e.getMessage()); + } + } else { + // If it already exists, delete it from db first, then fetch the latest one from the template and create a new one + int resCount = grafanaTemplateDao.deleteHard(grafanaTemplate.getId()); + if (resCount >= 1) { + try { + insertDashboardTemplate(name); + } catch (IOException e) { + log.error("HeraDashboardService.createDefaultDashboardTemplate Multiple create error :{}", + e.getMessage()); + } + } + } + }); + + //create java biz template + GrafanaTemplate grafanaTemplate = grafanaTemplateDao.fetchOneByName("hera-java模板"); + if (grafanaTemplate == null) { + try { + String content = FreeMarkerUtil.getTemplateStr(HERA_GRAFANA_TEMPLATE, + DashboardConstant.JAEGER_QUERY_File_NAME); + GrafanaTemplate template = new GrafanaTemplate(); + template.setName("hera-java模板"); + template.setCreateTime(new Date()); + template.setUpdateTime(new Date()); + template.setLanguage(0); + template.setPlatform(0); + template.setAppType(0); + template.setTemplate(content); + template.setDeleted(false); + template.setPanelIdList(DashboardConstant.DEFAULT_PANEL_ID_LIST); + int insertRes = grafanaTemplateDao.insert(template); + log.info("HeraDashboardService.createDefaultDashboardTemplate name:{},insertRes:{}", "hera-java模板", + insertRes); + } catch (IOException e) { + log.error("HeraDashboardService.createDefaultDashboardTemplate java template error :{}", + e.getMessage()); + } + } + + //create golang biz template + GrafanaTemplate grafanaGoTemplate = grafanaTemplateDao.fetchOneByName("hera-golang模板"); + if (grafanaGoTemplate == null) { + try { + String content = FreeMarkerUtil.getTemplateStr(HERA_GRAFANA_TEMPLATE, + DashboardConstant.GOLANG_File_NAME); + GrafanaTemplate template = new GrafanaTemplate(); + template.setName("hera-golang模板"); + template.setCreateTime(new Date()); + template.setUpdateTime(new Date()); + template.setLanguage(1); + template.setPlatform(0); + template.setAppType(0); + template.setTemplate(content); + template.setDeleted(false); + template.setPanelIdList(DashboardConstant.DEFAULT_GOLANG_ID_LIST); + int insertRes = grafanaTemplateDao.insert(template); + log.info("HeraDashboardService.createDefaultDashboardTemplate name:{},insertRes:{}", "hera-golang模板", + insertRes); + } catch (IOException e) { + log.error("HeraDashboardService.createDefaultDashboardTemplate golang template error :{}", + e.getMessage()); + } + } + + } + + private void insertDashboardTemplate(String name) throws IOException { + String content = FreeMarkerUtil.getTemplateStr(HERA_GRAFANA_TEMPLATE, name + ".ftl"); + GrafanaTemplate template = new GrafanaTemplate(); + template.setName(name); + template.setCreateTime(new Date()); + template.setUpdateTime(new Date()); + template.setLanguage(0); + template.setPlatform(0); + template.setAppType(1); + template.setTemplate(content); + template.setDeleted(false); + int insertRes = grafanaTemplateDao.insert(template); + log.info("HeraDashboardService.insertDashboardTemplate name:{},insertRes:{}", name, insertRes); + } + + //request prometheus-agent create biz、docker、node、jvm ...etc prometheus job + public void createDefaultScrapeJob() { + //Get jobJson from a file + Map jaegerQueryMap = new HashMap<>(); + jaegerQueryMap.put("token", jaegerQueryToken); + try { + //create jaeger_query monitor + Result jaegerResult = jobService.searchJobByName(null, "hera", + DashboardConstant.DEFAULT_JAEGER_QUERY_JOB_NAME); + log.info("jaegerResult:{}", jaegerResult); + if (jaegerResult.getData().equals("null")) { + log.info("jaeger_query job begin create"); + String jaegerQueryJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, + "jaegerQueryScrapeJob.ftl", jaegerQueryMap); + Result jaegerQueryJobRes = jobService.createJob(null, "Hera", jaegerQueryJobJson, "初始化创建业务监控"); + log.info("HeraDashboardService.createDefaultScrapeJob jaeger_query res: {}", + jaegerQueryJobRes.getData()); + } + + //create jvm monitor + Result jvmResult = jobService.searchJobByName(null, "hera", DashboardConstant.DEFAULT_JVM_JOB_NAME); + if (jvmResult.getData().equals("null")) { + log.info("jvm job begin create"); + String jvmJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "jvmScrapeJob.ftl", + new HashMap<>()); + Result jvmJobJsonRes = jobService.createJob(null, "Hera", jvmJobJson, "初始化创建jvm监控"); + log.info("HeraDashboardService.createDefaultScrapeJob jvm res: {}", jvmJobJsonRes.getData()); + } + + //create docker monitor + Result dockerResult = jobService.searchJobByName(null, "hera", DashboardConstant.DEFAULT_DOCKER_JOB_NAME); + if (dockerResult.getData().equals("null")) { + log.info("docker job begin create"); + String dockerJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "dockerScrapeJob.ftl", + new HashMap<>()); + Result dockerJobJsonRes = jobService.createJob(null, "Hera", dockerJobJson, "初始化创建容器监控"); + log.info("HeraDashboardService.createDefaultScrapeJob docker res: {}", dockerJobJsonRes.getData()); + } + + //create node monitor + Result nodeResult = jobService.searchJobByName(null, "hera", DashboardConstant.DEFAULT_NODE_JOB_NAME); + if (nodeResult.getData().equals("null")) { + log.info("node job begin create"); + String nodeJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "nodeScrapeJob.ftl", + new HashMap<>()); + Result nodeJobJsonRes = jobService.createJob(null, "Hera", nodeJobJson, "初始化创建物理机监控"); + log.info("HeraDashboardService.createDefaultScrapeJob node res: {}", nodeJobJsonRes.getData()); + } + //create custom monitor + Result customizeResult = jobService.searchJobByName(null, "hera", + DashboardConstant.DEFAULT_CUSTOMIZE_JOB_NAME); + if (customizeResult.getData().equals("null")) { + log.info("customize job begin create"); + String customizeJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, "customizeScrapeJob.ftl", + new HashMap<>()); + Result customizeJobJsonRes = jobService.createJob(null, "Hera", customizeJobJson, + "初始化创建自定义监控"); + log.info("HeraDashboardService.createDefaultScrapeJob customize res: {}", + customizeJobJsonRes.getData()); + } + //create golang runtime monitor + Result golangRuntimeResult = jobService.searchJobByName(null, "hera", + DashboardConstant.DEFAULT_GOLANG_RUNTIME_JOB_NAME); + if (golangRuntimeResult.getData().equals("null")) { + log.info("golangRuntimeResult job begin create"); + String golangRuntimeJobJson = FreeMarkerUtil.getContent(HERA_SCRAPE_JOB_TEMPLATE, + "golangRuntimeScrapeJob.ftl", new HashMap<>()); + Result golangRuntimeJobJsonRes = jobService.createJob(null, "Hera", golangRuntimeJobJson, + "初始化创建golang Runtime监控"); + log.info("HeraDashboardService.createDefaultScrapeJob golang runtime res: {}", + golangRuntimeJobJsonRes.getData()); + } + } catch (Exception e) { + log.error("HeraDashboardService.createDefaultScrapeJob error :{}", e.getMessage()); + } + + } + + private String createApiKey(Map header) { + String apiKeyName = DashboardConstant.GRAFANA_API_KEY_NAME + "-" + System.currentTimeMillis(); + GrafanaApiKeyReq req = new GrafanaApiKeyReq(apiKeyName, DashboardConstant.GRAFANA_API_KEY_ROLE); + log.info("GrafanaApiKeyReq:{}", gson.toJson(req)); + try { + String grafanaApiKeyResStr = HttpClientV5.post(grafanaUrl + grafanaApiKeyUrl, gson.toJson(req), header); + log.info("HeraDashboardService.createApiKey request apikey res:{}", grafanaApiKeyResStr); + GrafanaApiKeyRes grafanaApiKeyRes = gson.fromJson(grafanaApiKeyResStr, GrafanaApiKeyRes.class); + return grafanaApiKeyRes.getKey(); + } catch (Exception e) { + log.error("HeraDashboardService.createApiKey error :{}", e.getMessage()); + return null; + } + } + + private String createDataSource(Map header, String prometheusDatasourceUrl) { + GrafanaCreateDataSourceReq req = new GrafanaCreateDataSourceReq(); + req.setName(DashboardConstant.GRAFANA_DATASOURCE_NAME); + req.setType(DashboardConstant.GRAFANA_DATASOURCE_TYPE); + req.setAccess("proxy"); + req.setBasicAuth(false); + req.setUrl(prometheusDatasourceUrl); + log.info("GrafanaCreateDataSourceReq:{}", gson.toJson(req)); + try { + //If yes, no creation is required + String getDatasourceRes = HttpClientV5.get( + grafanaUrl + grafanaDatasourceUrl + "/name/" + DashboardConstant.GRAFANA_DATASOURCE_NAME, header); + log.info("HeraDashboardService.createDataSource getDatasourceRes:{}", getDatasourceRes); + GrafanaGetDataSourceRes grafanaGetDataSourceRes = gson.fromJson(getDatasourceRes, + GrafanaGetDataSourceRes.class); + if (grafanaGetDataSourceRes.getUid() != null) { + return grafanaGetDataSourceRes.getUid(); + } + String grafanaDatasourceResStr = HttpClientV5.post(grafanaUrl + grafanaDatasourceUrl, gson.toJson(req), + header); + log.info("HeraDashboardService.createDataSource request res:{}", grafanaDatasourceResStr); + GrafanaCreateDataSourceRes grafanaCreateDataSourceRes = gson.fromJson(grafanaDatasourceResStr, + GrafanaCreateDataSourceRes.class); + return grafanaCreateDataSourceRes.getDatasource().getUid(); + } catch (Exception e) { + log.error("HeraDashboardService.createDataSource error :{}", e.getMessage()); + return null; + } + } + + private int createFolder(Map header, String folderTitle) { + GrafanaFolderReq req = new GrafanaFolderReq(); + req.setTitle(folderTitle); + req.setUid(DashboardConstant.GRAFANA_FOLDER_UID); + log.info("GrafanaCreateFolderReq:{}", gson.toJson(req)); + try { + String getFolderRes = HttpClientV5.get( + grafanaUrl + grafanaFolderUrl + "/" + DashboardConstant.GRAFANA_FOLDER_UID, header); + log.info("HeraDashboardService.createFolder getFolderRes:{}", getFolderRes); + GrafanaGetFolderRes grafanaGetFolderRes = gson.fromJson(getFolderRes, GrafanaGetFolderRes.class); + if (grafanaGetFolderRes.getId() != null) { + return grafanaGetFolderRes.getId(); + } + String grafanaFolderResStr = HttpClientV5.post(grafanaUrl + grafanaFolderUrl, gson.toJson(req), header); + log.info("HeraDashboardService.createFolder request res:{}", grafanaFolderResStr); + GrafanaFolderRes grafanaFolderRes = gson.fromJson(grafanaFolderResStr, GrafanaFolderRes.class); + return grafanaFolderRes.getId(); + } catch (Exception e) { + log.error("HeraDashboardService.createFolder error :{}", e.getMessage()); + return -1; + } + } + + private void createDefaultGrafanaDashboard(String prometheusUid, Map header) { + Map map = new HashMap<>(); + map.put("prometheusUid", prometheusUid); + map.put("serviceMarketUrl", grafanaDomain + "/d/${__data.fields.application.text}/ye-wu-jian-kong-" + + "${__data.fields.application.text}?orgId=1&refresh=30s&theme=light"); + map.put("query0", "${query0}"); + map.put("env", serverType); + map.put("serviceName", "hera"); + + DashboardConstant.GRAFANA_SRE_TEMPLATES.forEach(name -> { + GrafanaTemplate grafanaTemplate = grafanaTemplateDao.fetchOneByName(name); + try { + if (grafanaTemplate != null) { + String template = FreeMarkerUtil.freemarkerProcess(map, grafanaTemplate.getTemplate()); + log.info("HeraDashboardService.createDefaultGrafanaDashboard name :{} begin!", name); + //request grafana + String grafanaDashboardResStr = HttpClientV5.post(grafanaUrl + grafanaDashboardUrl, template, + header); + log.info("HeraDashboardService.createDefaultGrafanaDashboard request " + name + " template res:{}", + grafanaDashboardResStr); + GrafanaCreateDashboardRes grafanaCreateDashboardRes = gson.fromJson(grafanaDashboardResStr, + GrafanaCreateDashboardRes.class); + if (!grafanaCreateDashboardRes.getStatus().equals("success")) { + log.error("HeraDashboardService.createDefaultGrafanaDashboard name:{},status:{},message:{}", + name, grafanaCreateDashboardRes.getStatus(), grafanaCreateDashboardRes.getMessage()); + } + } else { + log.error("HeraDashboardService.createDefaultGrafanaDashboard " + name + " template fetch error!"); + } + } catch (Exception e) { + log.error("HeraDashboardService.createDefaultGrafanaDashboard error:{}", e); + } + }); + } +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/QualityMarketServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/QualityMarketServiceImpl.java new file mode 100644 index 000000000..55fd20613 --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/QualityMarketServiceImpl.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.xiaomi.mone.monitor.dao.AppQualityMarketDao; +import com.xiaomi.mone.monitor.dao.model.AppQualityMarket; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.QualityMarketService; +import com.xiaomi.mone.monitor.service.model.PageData; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +public class QualityMarketServiceImpl implements QualityMarketService { + + @Autowired + AppQualityMarketDao appQualityMarketDao; + + @Override + public Result createMarket(String user, String marketName, String serviceList, String remark) { + try { + //按;切分serviceList + String[] services = serviceList.split(";"); + //入库 + AppQualityMarket appQualityMarket = new AppQualityMarket(); + appQualityMarket.setMarketName(marketName.trim()); + appQualityMarket.setServiceList(serviceList.trim()); + appQualityMarket.setCreator(user); + appQualityMarket.setLastUpdater(user); + appQualityMarket.setRemark(remark.trim()); + int dbResult = appQualityMarketDao.insertServiceMarket(appQualityMarket); + log.info("QualityMarketService.createMarket dbResult: {}", dbResult); + return Result.success("success"); + } catch (Exception e) { + log.error("QualityMarketService.createMarket error : {}", e.toString()); + return Result.fail(ErrorCode.unknownError); + } + } + @Override + public Result searchMarket(String user, int id) { + try { + AppQualityMarket appQualityMarket = appQualityMarketDao.SearchAppQualityMarket(id); + return Result.success(appQualityMarket); + } catch (Exception e) { + log.error("QualityMarketService.searchMarket error : {}", e.toString()); + return Result.fail(ErrorCode.unknownError); + } + } + + //改 + @Override + public Result updateMarket(String user, int id, String serviceList, String marketName, String remark) { + try { + //查库是否有该记录 + AppQualityMarket appQualityMarket = appQualityMarketDao.SearchAppQualityMarket(id); + if (appQualityMarket == null) { + return Result.fail(ErrorCode.nonExistentServiceMarketId); + } + appQualityMarket.setMarketName(marketName.trim()); + appQualityMarket.setServiceList(serviceList.trim()); + appQualityMarket.setLastUpdater(user); + appQualityMarket.setRemark(remark.trim()); + int dbResult = appQualityMarketDao.updateQualityMarket(appQualityMarket); + log.info("QualityMarketService.updateMarket dbResult: {}", dbResult); + return Result.success("success"); + } catch (Exception e) { + log.error("QualityMarketService.updateMarket error : {}", e.toString()); + return Result.fail(ErrorCode.unknownError); + } + } + + //删 + @Override + public Result deleteMarket(String user, Integer id) { + try { + //查库是否有该记录 + AppQualityMarket appQualityMarket = appQualityMarketDao.SearchAppQualityMarket(id); + if (appQualityMarket == null) { + return Result.fail(ErrorCode.nonExistentServiceMarketId); + } + //删除 + int result = appQualityMarketDao.deleteQualityMarket(id); + log.info("QualityMarketService.deleteMarket dbResult:{}", result); + return Result.success("success"); + } catch (Exception e) { + log.error("QualityMarketService.deleteMarket error : {}", e.toString()); + return Result.fail(ErrorCode.unknownError); + } + } + + + //查列表 + @Override + public Result searchMarketList(String user, int pageSize, int pageNo, String creator, String marketName, + String serviceName) { + AppQualityMarket appQualityMarket = new AppQualityMarket(); + if (StringUtils.isNotEmpty(creator)) { + appQualityMarket.setCreator(creator); + } + PageData pd = new PageData(); + pd.setPage(pageNo); + pd.setPageSize(pageSize); + pd.setTotal(appQualityMarketDao.getTotal(creator, marketName, serviceName)); + pd.setList(appQualityMarketDao.SearchAppQualityMarketList(pageNo, pageSize, creator, marketName, serviceName)); + return Result.success(pd); + } + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ResourceUsageServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ResourceUsageServiceImpl.java new file mode 100644 index 000000000..d21381362 --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ResourceUsageServiceImpl.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.alibaba.nacos.api.config.annotation.NacosValue; +import com.google.common.collect.Lists; +import com.xiaomi.mone.monitor.dao.HeraAppRoleDao; +import com.xiaomi.mone.monitor.dao.model.HeraAppRole; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.ResourceUsageService; +import com.xiaomi.mone.monitor.service.alertmanager.AlarmExprService; +import com.xiaomi.mone.monitor.service.model.PageData; +import com.xiaomi.mone.monitor.service.model.ResourceUsageMessage; +import com.xiaomi.mone.monitor.service.model.prometheus.Metric; +import com.xiaomi.mone.monitor.service.prometheus.PrometheusService; +import org.apache.commons.collections.CollectionUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author gaoxihui + * @date 2022/5/12 5:18 PM + */ +@Service +public class ResourceUsageServiceImpl implements ResourceUsageService { + + @Autowired + AlarmExprService alarmExprService; + + @Autowired + PrometheusService prometheusService; + + @Autowired + HeraAppRoleDao heraAppRoleDao; + + @NacosValue(value = "${resource.use.rate.alarm.config:1}", autoRefreshed = true) + private String resourceAlarm; + + @Override + public List getCpuUsageData() { + String mimonitor = alarmExprService.getContainerCpuResourceAlarmExpr(null, "mimonitor", "<", + Integer.valueOf(resourceAlarm), false, null); + Result pageDataResult = prometheusService.queryByMetric(mimonitor); + PageData data = pageDataResult.getData(); + List listMsg = new ArrayList<>(); + if (data != null) { + List list = (List) data.getList(); + if (CollectionUtils.isNotEmpty(list)) { + listMsg = list.stream().map(t -> { + List query = heraAppRoleDao.queryByPlatTypes(t.getContainer_label_PROJECT_ID(), + Lists.newArrayList(0, 2), 0, 5); + List members = CollectionUtils.isEmpty(query) ? Lists.newArrayList() + : query.stream().map(r -> r.getUser()).collect(Collectors.toList()); + return new ResourceUsageMessage(t.getIp(), t.getContainer_label_PROJECT_ID(), + t.getName().substring(0, t.getName().lastIndexOf("-")), String.valueOf(t.getValue()), null, + members, resourceAlarm + "%"); + }).collect(Collectors.toList()); + } + } + + return listMsg; + } + + @Override + public List getMemUsageData() { + String mimonitor = alarmExprService.getContainerMemReourceAlarmExpr(null, "mimonitor", "<", + Integer.valueOf(resourceAlarm), false, null); + Result pageDataResult = prometheusService.queryByMetric(mimonitor); + PageData data = pageDataResult.getData(); + List listMsg = new ArrayList<>(); + if (data != null) { + List list = (List) data.getList(); + if (CollectionUtils.isNotEmpty(list)) { + listMsg = list.stream().map(t -> { + List query = heraAppRoleDao.queryByPlatTypes(t.getContainer_label_PROJECT_ID(), + Lists.newArrayList(0, 2), 0, 5); + List members = CollectionUtils.isEmpty(query) ? Lists.newArrayList() + : query.stream().map(r -> r.getUser()).collect(Collectors.toList()); + return new ResourceUsageMessage(t.getIp(), t.getContainer_label_PROJECT_ID(), + t.getName().substring(0, t.getName().lastIndexOf("-")), String.valueOf(t.getValue()), null, + members, resourceAlarm + "%"); + }).collect(Collectors.toList()); + } + } + + return listMsg; + } +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/RulePromQLTemplateServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/RulePromQLTemplateServiceImpl.java new file mode 100644 index 000000000..8519e0727 --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/RulePromQLTemplateServiceImpl.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.alibaba.nacos.api.config.annotation.NacosValue; +import com.xiaomi.mone.monitor.bo.RulePromQLTemplateInfo; +import com.xiaomi.mone.monitor.bo.RulePromQLTemplateParam; +import com.xiaomi.mone.monitor.dao.RulePromQLTemplateDao; +import com.xiaomi.mone.monitor.dao.model.RulePromQLTemplate; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.RulePromQLTemplateService; +import com.xiaomi.mone.monitor.service.model.PageData; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.List; + +/** + * @author zhanggaofeng1 + */ + +@Slf4j +@Service +public class RulePromQLTemplateServiceImpl implements RulePromQLTemplateService { + + @Autowired + private RulePromQLTemplateDao rulePromQLTemplateDao; + + @NacosValue(value = "${prometheus.url}", autoRefreshed = true) + private String prometheusUrl; + + @NacosValue(value = "${prometheus.check.url}", autoRefreshed = true) + private String prometheusCheckUrl; + + /** + * 添加模板 + * + * @param user + * @param param + * @return + */ + @Override + public Result add(String user, RulePromQLTemplateParam param) { + List templates = rulePromQLTemplateDao.getByName(user, param.getName()); + if (!CollectionUtils.isEmpty(templates)) { + Result result = Result.fail(ErrorCode.invalidParamError); + result.setMessage("名称不允许重复"); + return result; + } + RulePromQLTemplate template = new RulePromQLTemplate(); + BeanUtils.copyProperties(param, template); + template.setCreater(user); + if (!rulePromQLTemplateDao.insert(template)) { + return Result.fail(ErrorCode.unknownError); + } + log.info("add PromQL template 成功:user={}, template={}", user, template); + return Result.success(null); + } + + /** + * 编辑模板 + * + * @param user + * @param param + * @return + */ + @Override + public Result edit(String user, RulePromQLTemplateParam param) { + List templates = rulePromQLTemplateDao.getByName(user, param.getName()); + if (templates != null && templates.size() > 1) { + Result result = Result.fail(ErrorCode.invalidParamError); + result.setMessage("名称不允许重复"); + return result; + } + RulePromQLTemplate template = new RulePromQLTemplate(); + BeanUtils.copyProperties(param, template); + template.setCreater(null); + if (!rulePromQLTemplateDao.updateById(template)) { + return Result.fail(ErrorCode.unknownError); + } + log.info("updateById PromQL template 成功:user={}, template={}", user, template); + return Result.success(null); + } + + @Override + public Result deleteById(String user, Integer templateId) { + if (!rulePromQLTemplateDao.deleteById(templateId)) { + return Result.fail(ErrorCode.unknownError); + } + log.info("deleteById PromQL template 成功:user={}, templateId={}", user, templateId); + return Result.success(null); + } + + @Override + public Result>> search(String user, RulePromQLTemplateParam param) { + RulePromQLTemplate template = new RulePromQLTemplate(); + PageData> pageData = rulePromQLTemplateDao.searchByCond(user, param); + log.info("query promQL template user={}, param={}, pageData={}", user, param, pageData); + return Result.success(pageData); + } + + @Override + public Result testPromQL(String user, RulePromQLTemplateParam param) throws UnsupportedEncodingException { + StringBuilder url = new StringBuilder(); + url.append(prometheusCheckUrl).append("graph?g0.expr=").append(URLEncoder.encode(param.getPromql(), "UTF-8")) + .append("&g0.tab=1&g0.stacked=0&g0.range_input=1h"); + return Result.success(url.toString()); + } + +} diff --git a/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ServiceMarketServiceImpl.java b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ServiceMarketServiceImpl.java new file mode 100644 index 000000000..556b21bcb --- /dev/null +++ b/ozhera-monitor/ozhera-monitor-service/src/main/java/com/xiaomi/mone/monitor/service/impl/ServiceMarketServiceImpl.java @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2020 Xiaomi Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xiaomi.mone.monitor.service.impl; + +import com.alibaba.nacos.api.config.annotation.NacosValue; +import com.xiaomi.mone.monitor.dao.AppServiceMarketDao; +import com.xiaomi.mone.monitor.dao.model.AppServiceMarket; +import com.xiaomi.mone.monitor.result.ErrorCode; +import com.xiaomi.mone.monitor.result.Result; +import com.xiaomi.mone.monitor.service.ServiceMarketService; +import com.xiaomi.mone.monitor.service.api.ServiceMarketExtension; +import com.xiaomi.mone.monitor.service.extension.PlatFormTypeExtensionService; +import com.xiaomi.mone.monitor.service.model.PageData; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +/** + * @author zhangxiaowei6 + */ + +@Slf4j +@Service +public class ServiceMarketServiceImpl implements ServiceMarketService { + + @Autowired + AppServiceMarketDao appServiceMarketDao; + + @Value("${server.type}") + private String env; + + @NacosValue(value = "${grafana.domain}", autoRefreshed = true) + private String grafanaDomain; + + @Autowired + ServiceMarketExtension serviceMarketExtension; + + @Autowired + PlatFormTypeExtensionService platFormTypeExtensionService; + + @Override + //增 + public Result createMarket(String user, String marketName, String belongTeam, String serviceList, String remark, + Integer serviceType) { + serviceType = platFormTypeExtensionService.getMarketType(serviceType); + try { + //按;切分serviceList + String[] services = serviceList.split(";"); + //入库 + AppServiceMarket appServiceMarket = new AppServiceMarket(); + appServiceMarket.setMarketName(marketName.trim()); + appServiceMarket.setServiceList(serviceList.trim()); + appServiceMarket.setCreator(user); + appServiceMarket.setLastUpdater(user); + appServiceMarket.setRemark(remark.trim()); + appServiceMarket.setBelongTeam(belongTeam.trim()); + appServiceMarket.setServiceType(serviceType); + int dbResult = appServiceMarketDao.insertServiceMarket(appServiceMarket); + log.info("ServiceMarketService.createMarket dbResult: {}", dbResult); + return Result.success("success"); + } catch (Exception e) { + log.error("ServiceMarketService.createMarket error : {}", e.toString()); + return Result.fail(ErrorCode.unknownError); + } + } + + //查 + @Override + public Result searchMarket(String user, int id) { + try { + AppServiceMarket appServiceMarket = appServiceMarketDao.SearchAppServiceMarket(id); + return Result.success(appServiceMarket); + } catch (Exception e) { + log.error("ServiceMarketService.searchMarket error : {}", e.toString()); + return Result.fail(ErrorCode.unknownError); + } + } + + //获取大盘grafanaUrl + @Override + public String getServiceMarketGrafana(Integer serviceType) { + return serviceMarketExtension.getServiceMarketGrafana(serviceType); + } + + //查列表 + @Override + public Result searchMarketList(String user, int pageSize, int pageNo, String creator, String marketName, + String serviceName) { + AppServiceMarket appServiceMarket = new AppServiceMarket(); + if (StringUtils.isNotEmpty(creator)) { + appServiceMarket.setCreator(creator); + } + PageData pd = new PageData(); + pd.setPage(pageNo); + pd.setPageSize(pageSize); + pd.setTotal(appServiceMarketDao.getTotal(creator, marketName, serviceName)); + pd.setList(appServiceMarketDao.SearchAppServiceMarketList(pageNo, pageSize, creator, marketName, serviceName)); + return Result.success(pd); + } + + //改 + @Override + public Result updateMarket(String user, int id, String serviceList, String marketName, String remark, + String belongTeam, int serviceType) { + serviceType = platFormTypeExtensionService.getMarketType(serviceType); + try { + //查库是否有该记录 + AppServiceMarket appServiceMarket = appServiceMarketDao.SearchAppServiceMarket(id); + if (appServiceMarket == null) { + return Result.fail(ErrorCode.nonExistentServiceMarketId); + } + appServiceMarket.setMarketName(marketName.trim()); + appServiceMarket.setServiceList(serviceList.trim()); + appServiceMarket.setLastUpdater(user); + appServiceMarket.setRemark(remark.trim()); + appServiceMarket.setBelongTeam(belongTeam.trim()); + appServiceMarket.setServiceType(serviceType); + int dbResult = appServiceMarketDao.updateServiceMarket(appServiceMarket); + log.info("ServiceMarketService.updateMarket dbResult: {}", dbResult); + return Result.success("success"); + } catch (Exception e) { + log.error("ServiceMarketService.updateMarket error : {}", e.toString()); + return Result.fail(ErrorCode.unknownError); + } + } + + //删 + @Override + public Result deleteMarket(String user, Integer id) { + try { + //查库是否有该记录 + AppServiceMarket appServiceMarket = appServiceMarketDao.SearchAppServiceMarket(id); + if (appServiceMarket == null) { + return Result.fail(ErrorCode.nonExistentServiceMarketId); + } + //删除 + int result = appServiceMarketDao.deleteServiceMarket(id); + log.info("ServiceMarketService.deleteMarket dbResult:{}", result); + return Result.success("success"); + } catch (Exception e) { + log.error("ServiceMarketService.deleteMarket error : {}", e.toString()); + return Result.fail(ErrorCode.unknownError); + } + } +}