Search in sources :

Example 1 with Tag

use of com.usthe.common.entity.manager.Tag 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());
    }
}
Also used : java.util(java.util) CommonConstants(com.usthe.common.util.CommonConstants) MonitorDao(com.usthe.manager.dao.MonitorDao) ParamDefine(com.usthe.common.entity.manager.ParamDefine) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Configmap(com.usthe.common.entity.job.Configmap) Autowired(org.springframework.beans.factory.annotation.Autowired) Param(com.usthe.common.entity.manager.Param) IntervalExpressionUtil(com.usthe.common.util.IntervalExpressionUtil) MonitorService(com.usthe.manager.service.MonitorService) Job(com.usthe.common.entity.job.Job) AppCount(com.usthe.manager.pojo.dto.AppCount) Tag(com.usthe.common.entity.manager.Tag) Service(org.springframework.stereotype.Service) MonitorDto(com.usthe.manager.pojo.dto.MonitorDto) CollectRep(com.usthe.common.entity.message.CollectRep) ParamDao(com.usthe.manager.dao.ParamDao) Monitor(com.usthe.common.entity.manager.Monitor) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) IpDomainUtil(com.usthe.common.util.IpDomainUtil) Slf4j(lombok.extern.slf4j.Slf4j) AlertDefineBindDao(com.usthe.alert.dao.AlertDefineBindDao) SnowFlakeIdGenerator(com.usthe.common.util.SnowFlakeIdGenerator) Specification(org.springframework.data.jpa.domain.Specification) AppService(com.usthe.manager.service.AppService) AesUtil(com.usthe.common.util.AesUtil) CollectJobService(com.usthe.collector.dispatch.entrance.internal.CollectJobService) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Metrics(com.usthe.common.entity.job.Metrics) Transactional(org.springframework.transaction.annotation.Transactional) Configmap(com.usthe.common.entity.job.Configmap) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Monitor(com.usthe.common.entity.manager.Monitor) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Tag(com.usthe.common.entity.manager.Tag) Job(com.usthe.common.entity.job.Job) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Tag

use of com.usthe.common.entity.manager.Tag 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());
    }
}
Also used : java.util(java.util) CommonConstants(com.usthe.common.util.CommonConstants) MonitorDao(com.usthe.manager.dao.MonitorDao) ParamDefine(com.usthe.common.entity.manager.ParamDefine) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Configmap(com.usthe.common.entity.job.Configmap) Autowired(org.springframework.beans.factory.annotation.Autowired) Param(com.usthe.common.entity.manager.Param) IntervalExpressionUtil(com.usthe.common.util.IntervalExpressionUtil) MonitorService(com.usthe.manager.service.MonitorService) Job(com.usthe.common.entity.job.Job) AppCount(com.usthe.manager.pojo.dto.AppCount) Tag(com.usthe.common.entity.manager.Tag) Service(org.springframework.stereotype.Service) MonitorDto(com.usthe.manager.pojo.dto.MonitorDto) CollectRep(com.usthe.common.entity.message.CollectRep) ParamDao(com.usthe.manager.dao.ParamDao) Monitor(com.usthe.common.entity.manager.Monitor) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) IpDomainUtil(com.usthe.common.util.IpDomainUtil) Slf4j(lombok.extern.slf4j.Slf4j) AlertDefineBindDao(com.usthe.alert.dao.AlertDefineBindDao) SnowFlakeIdGenerator(com.usthe.common.util.SnowFlakeIdGenerator) Specification(org.springframework.data.jpa.domain.Specification) AppService(com.usthe.manager.service.AppService) AesUtil(com.usthe.common.util.AesUtil) CollectJobService(com.usthe.collector.dispatch.entrance.internal.CollectJobService) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Metrics(com.usthe.common.entity.job.Metrics) Transactional(org.springframework.transaction.annotation.Transactional) Configmap(com.usthe.common.entity.job.Configmap) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Tag(com.usthe.common.entity.manager.Tag) Job(com.usthe.common.entity.job.Job) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Tag

use of com.usthe.common.entity.manager.Tag in project hertzbeat by dromara.

the class TagController method getTags.

@GetMapping()
@ApiOperation(value = "Get tags information", notes = "根据条件获取标签信息")
public ResponseEntity<Message<Page<Tag>>> getTags(@ApiParam(value = "Tag content search | 标签内容模糊查询", example = "status") @RequestParam(required = false) String search, @ApiParam(value = "Tag type | 标签类型", example = "0") @RequestParam(required = false) Byte type, @ApiParam(value = "List current page | 列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex, @ApiParam(value = "Number of list pagination | 列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
    // Get tag information
    Specification<Tag> specification = (root, query, criteriaBuilder) -> {
        List<Predicate> andList = new ArrayList<>();
        if (type != null) {
            Predicate predicateApp = criteriaBuilder.equal(root.get("type"), type);
            andList.add(predicateApp);
        }
        Predicate[] andPredicates = new Predicate[andList.size()];
        Predicate andPredicate = criteriaBuilder.and(andList.toArray(andPredicates));
        List<Predicate> orList = new ArrayList<>();
        if (search != null && !"".equals(search)) {
            Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + search + "%");
            orList.add(predicateName);
            Predicate predicateValue = criteriaBuilder.like(root.get("value"), "%" + search + "%");
            orList.add(predicateValue);
        }
        Predicate[] orPredicates = new Predicate[orList.size()];
        Predicate orPredicate = criteriaBuilder.or(orList.toArray(orPredicates));
        if (andPredicate.getExpressions().isEmpty() && orPredicate.getExpressions().isEmpty()) {
            return query.where().getRestriction();
        } else if (andPredicate.getExpressions().isEmpty()) {
            return query.where(orPredicate).getRestriction();
        } else if (orPredicate.getExpressions().isEmpty()) {
            return query.where(andPredicate).getRestriction();
        } else {
            return query.where(andPredicate, orPredicate).getRestriction();
        }
    };
    PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
    Page<Tag> alertPage = tagService.getTags(specification, pageRequest);
    Message<Page<Tag>> message = new Message<>(alertPage);
    return ResponseEntity.ok(message);
}
Also used : ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) APPLICATION_JSON_VALUE(org.springframework.http.MediaType.APPLICATION_JSON_VALUE) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Valid(javax.validation.Valid) HashSet(java.util.HashSet) ApiOperation(io.swagger.annotations.ApiOperation) List(java.util.List) Message(com.usthe.common.entity.dto.Message) Specification(org.springframework.data.jpa.domain.Specification) Predicate(javax.persistence.criteria.Predicate) TagService(com.usthe.manager.service.TagService) Tag(com.usthe.common.entity.manager.Tag) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ResponseEntity(org.springframework.http.ResponseEntity) Api(io.swagger.annotations.Api) PageRequest(org.springframework.data.domain.PageRequest) Message(com.usthe.common.entity.dto.Message) ArrayList(java.util.ArrayList) List(java.util.List) Page(org.springframework.data.domain.Page) Tag(com.usthe.common.entity.manager.Tag) Predicate(javax.persistence.criteria.Predicate) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

Tag (com.usthe.common.entity.manager.Tag)3 Collectors (java.util.stream.Collectors)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Page (org.springframework.data.domain.Page)3 PageRequest (org.springframework.data.domain.PageRequest)3 Specification (org.springframework.data.jpa.domain.Specification)3 AlertDefineBindDao (com.usthe.alert.dao.AlertDefineBindDao)2 CollectJobService (com.usthe.collector.dispatch.entrance.internal.CollectJobService)2 Configmap (com.usthe.common.entity.job.Configmap)2 Job (com.usthe.common.entity.job.Job)2 Metrics (com.usthe.common.entity.job.Metrics)2 Monitor (com.usthe.common.entity.manager.Monitor)2 Param (com.usthe.common.entity.manager.Param)2 ParamDefine (com.usthe.common.entity.manager.ParamDefine)2 CollectRep (com.usthe.common.entity.message.CollectRep)2 AesUtil (com.usthe.common.util.AesUtil)2 CommonConstants (com.usthe.common.util.CommonConstants)2 IntervalExpressionUtil (com.usthe.common.util.IntervalExpressionUtil)2 IpDomainUtil (com.usthe.common.util.IpDomainUtil)2 SnowFlakeIdGenerator (com.usthe.common.util.SnowFlakeIdGenerator)2