use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class WheelTimerTask method initJobMetrics.
/**
* Initialize job fill information
* 初始化job填充信息
*
* @param job job
*/
private void initJobMetrics(Job job) {
// 将监控实际参数值对采集字段进行替换
List<Configmap> config = job.getConfigmap();
Map<String, Configmap> configmap = config.stream().peek(item -> {
// 对加密串进行解密
if (item.getType() == CommonConstants.PARAM_TYPE_PASSWORD && item.getValue() != null) {
String decodeValue = AesUtil.aesDecode(String.valueOf(item.getValue()));
if (decodeValue == null) {
log.error("Aes Decode value {} error.", item.getValue());
}
item.setValue(decodeValue);
} else if (item.getValue() != null && item.getValue() instanceof String) {
item.setValue(((String) item.getValue()).trim());
}
}).collect(Collectors.toMap(Configmap::getKey, item -> item));
List<Metrics> metrics = job.getMetrics();
List<Metrics> metricsTmp = new ArrayList<>(metrics.size());
for (Metrics metric : metrics) {
JsonElement jsonElement = GSON.toJsonTree(metric);
jsonElement = replaceSpecialValue(jsonElement, configmap);
metric = GSON.fromJson(jsonElement, Metrics.class);
metricsTmp.add(metric);
}
job.setMetrics(metricsTmp);
}
use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class AppServiceImpl method getI18nResources.
@Override
public Map<String, String> getI18nResources(String lang) {
Map<String, String> i18nMap = new HashMap<>(32);
for (Job job : appDefines.values()) {
// todo Todo temporarily only internationalizes the monitoring type name, after which it needs to support the indicator name
// 暂时只国际化监控类型名称 后面需要支持指标名称
Map<String, String> name = job.getName();
if (name != null && !name.isEmpty()) {
String i18nName = name.get(lang);
if (i18nName == null) {
i18nName = name.values().stream().findFirst().get();
}
i18nMap.put("monitor.app." + job.getApp(), i18nName);
}
}
return i18nMap;
}
use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class MonitorServiceImpl method modifyMonitor.
@Override
@Transactional(rollbackFor = Exception.class)
public void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
long monitorId = monitor.getId();
// Check to determine whether the monitor corresponding to the monitor id exists
// 查判断monitorId对应的此监控是否存在
Optional<Monitor> queryOption = monitorDao.findById(monitorId);
if (!queryOption.isPresent()) {
throw new IllegalArgumentException("The Monitor " + monitorId + " not exists");
}
Monitor preMonitor = queryOption.get();
if (!preMonitor.getApp().equals(monitor.getApp())) {
// 监控的类型不能修改
throw new IllegalArgumentException("Can not modify monitor's app type");
}
// Auto Update Default Tags: monitorName
List<Tag> tags = monitor.getTags();
if (tags == null) {
tags = new LinkedList<>();
monitor.setTags(tags);
}
for (Tag tag : tags) {
if (CommonConstants.TAG_MONITOR_NAME.equals(tag.getName())) {
tag.setValue(monitor.getName());
}
}
// Construct the collection task Job entity
// 构造采集任务Job实体
Job appDefine = appService.getAppDefine(monitor.getApp());
appDefine.setId(preMonitor.getJobId());
appDefine.setMonitorId(monitorId);
appDefine.setInterval(monitor.getIntervals());
appDefine.setCyclic(true);
appDefine.setTimestamp(System.currentTimeMillis());
List<Configmap> configmaps = params.stream().map(param -> new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
appDefine.setConfigmap(configmaps);
// 下发更新成功后刷库
try {
monitor.setJobId(preMonitor.getJobId());
monitor.setStatus(preMonitor.getStatus());
monitorDao.save(monitor);
paramDao.saveAll(params);
// Update the collection task after the storage is completed
// 入库完成后更新采集任务
collectJobService.updateAsyncCollectJob(appDefine);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new MonitorDatabaseException(e.getMessage());
}
}
use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class MonitorServiceImpl method getAllAppMonitorsCount.
@Override
public List<AppCount> getAllAppMonitorsCount() {
List<AppCount> appCounts = monitorDao.findAppsStatusCount();
if (appCounts == null) {
return null;
}
// Statistical category information, calculate the number of corresponding states for each monitor
// 统计类别信息,计算每个监控分别对应状态的数量
Map<String, AppCount> appCountMap = new HashMap<>(appCounts.size());
for (AppCount item : appCounts) {
AppCount appCount = appCountMap.getOrDefault(item.getApp(), new AppCount());
appCount.setApp(item.getApp());
switch(item.getStatus()) {
case CommonConstants.AVAILABLE_CODE:
appCount.setAvailableSize(appCount.getAvailableSize() + item.getSize());
break;
case CommonConstants.UN_AVAILABLE_CODE:
appCount.setUnAvailableSize(appCount.getUnAvailableSize() + item.getSize());
break;
case CommonConstants.UN_MANAGE_CODE:
appCount.setUnManageSize(appCount.getUnManageSize() + item.getSize());
break;
case CommonConstants.UN_REACHABLE_CODE:
appCount.setUnReachableSize(appCount.getUnReachableSize() + item.getSize());
break;
default:
break;
}
appCountMap.put(item.getApp(), appCount);
}
// 遍历统计得到的map,转换成List<App Count>结果集
return appCountMap.values().stream().peek(item -> {
item.setSize(item.getAvailableSize() + item.getUnManageSize() + item.getUnReachableSize() + item.getUnAvailableSize());
Job job = appService.getAppDefine(item.getApp());
if (job != null) {
item.setCategory(job.getCategory());
}
}).collect(Collectors.toList());
}
use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class MonitorServiceImpl method addMonitor.
@Override
@Transactional(rollbackFor = Exception.class)
public void addMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
// Apply for monitor id 申请 monitor id
long monitorId = SnowFlakeIdGenerator.generateId();
// Init Set Default Tags: monitorId monitorName app
List<Tag> tags = monitor.getTags();
if (tags == null) {
tags = new LinkedList<>();
monitor.setTags(tags);
}
tags.add(Tag.builder().name(CommonConstants.TAG_MONITOR_ID).value(String.valueOf(monitorId)).type((byte) 0).build());
tags.add(Tag.builder().name(CommonConstants.TAG_MONITOR_NAME).value(String.valueOf(monitor.getName())).type((byte) 0).build());
// Construct the collection task Job entity 构造采集任务Job实体
Job appDefine = appService.getAppDefine(monitor.getApp());
appDefine.setMonitorId(monitorId);
appDefine.setInterval(monitor.getIntervals());
appDefine.setCyclic(true);
appDefine.setTimestamp(System.currentTimeMillis());
List<Configmap> configmaps = params.stream().map(param -> {
param.setMonitorId(monitorId);
return new Configmap(param.getField(), param.getValue(), param.getType());
}).collect(Collectors.toList());
appDefine.setConfigmap(configmaps);
// Send the collection task to get the job ID
// 下发采集任务得到jobId
long jobId = collectJobService.addAsyncCollectJob(appDefine);
// 下发成功后刷库
try {
monitor.setId(monitorId);
monitor.setJobId(jobId);
monitor.setStatus(CommonConstants.AVAILABLE_CODE);
monitorDao.save(monitor);
paramDao.saveAll(params);
} catch (Exception e) {
log.error(e.getMessage(), e);
// Repository brushing abnormally cancels the previously delivered task
// 刷库异常取消之前的下发任务
collectJobService.cancelAsyncCollectJob(jobId);
throw new MonitorDatabaseException(e.getMessage());
}
}
Aggregations