Search in sources :

Example 1 with Message

use of com.usthe.common.entity.dto.Message in project hertzbeat by dromara.

the class AlertDefineController method getAlertDefine.

@GetMapping(path = "/{id}")
@ApiOperation(value = "查询告警定义", notes = "根据告警定义ID获取告警定义信息")
public ResponseEntity<Message<AlertDefine>> getAlertDefine(@ApiParam(value = "告警定义ID", example = "6565463543") @PathVariable("id") long id) {
    // 获取监控信息
    AlertDefine alertDefine = alertDefineService.getAlertDefine(id);
    Message.MessageBuilder<AlertDefine> messageBuilder = Message.builder();
    if (alertDefine == null) {
        messageBuilder.code(MONITOR_NOT_EXIST_CODE).msg("AlertDefine not exist.");
    } else {
        messageBuilder.data(alertDefine);
    }
    return ResponseEntity.ok(messageBuilder.build());
}
Also used : Message(com.usthe.common.entity.dto.Message) AlertDefine(com.usthe.common.entity.alerter.AlertDefine) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with Message

use of com.usthe.common.entity.dto.Message in project hertzbeat by dromara.

the class AlertDefinesController method getAlertDefines.

@GetMapping
@ApiOperation(value = "查询告警定义列表", notes = "根据查询过滤项获取告警定义信息列表")
public ResponseEntity<Message<Page<AlertDefine>>> getAlertDefines(@ApiParam(value = "告警定义ID", example = "6565463543") @RequestParam(required = false) List<Long> ids, @ApiParam(value = "告警定义级别", example = "6565463543") @RequestParam(required = false) Byte priority, @ApiParam(value = "排序字段,默认id", example = "id") @RequestParam(defaultValue = "id") String sort, @ApiParam(value = "排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") String order, @ApiParam(value = "列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex, @ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
    Specification<AlertDefine> specification = (root, query, criteriaBuilder) -> {
        List<Predicate> andList = new ArrayList<>();
        if (ids != null && !ids.isEmpty()) {
            CriteriaBuilder.In<Long> inPredicate = criteriaBuilder.in(root.get("id"));
            for (long id : ids) {
                inPredicate.value(id);
            }
            andList.add(inPredicate);
        }
        if (priority != null) {
            Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
            andList.add(predicate);
        }
        Predicate[] predicates = new Predicate[andList.size()];
        return criteriaBuilder.and(andList.toArray(predicates));
    };
    // 分页是必须的
    Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
    PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
    Page<AlertDefine> alertDefinePage = alertDefineService.getAlertDefines(specification, pageRequest);
    Message<Page<AlertDefine>> message = new Message<>(alertDefinePage);
    return ResponseEntity.ok(message);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) PageRequest(org.springframework.data.domain.PageRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Page(org.springframework.data.domain.Page) APPLICATION_JSON_VALUE(org.springframework.http.MediaType.APPLICATION_JSON_VALUE) RestController(org.springframework.web.bind.annotation.RestController) ArrayList(java.util.ArrayList) 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) AlertDefineService(com.usthe.alert.service.AlertDefineService) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) GetMapping(org.springframework.web.bind.annotation.GetMapping) Sort(org.springframework.data.domain.Sort) ResponseEntity(org.springframework.http.ResponseEntity) Api(io.swagger.annotations.Api) AlertDefine(com.usthe.common.entity.alerter.AlertDefine) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Message(com.usthe.common.entity.dto.Message) Page(org.springframework.data.domain.Page) Predicate(javax.persistence.criteria.Predicate) PageRequest(org.springframework.data.domain.PageRequest) AlertDefine(com.usthe.common.entity.alerter.AlertDefine) Sort(org.springframework.data.domain.Sort) ArrayList(java.util.ArrayList) List(java.util.List) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Example 3 with Message

use of com.usthe.common.entity.dto.Message in project hertzbeat by dromara.

the class AlertsController method getAlerts.

@GetMapping
@ApiOperation(value = "Get a list of alarm information based on query filter items", notes = "根据查询过滤项获取告警信息列表")
public ResponseEntity<Message<Page<Alert>>> getAlerts(@ApiParam(value = "Alarm ID List | 告警IDS", example = "6565466456") @RequestParam(required = false) List<Long> ids, @ApiParam(value = "Alarm monitor object ID | 告警监控对象ID", example = "6565463543") @RequestParam(required = false) Long monitorId, @ApiParam(value = "Alarm level | 告警级别", example = "6565463543") @RequestParam(required = false) Byte priority, @ApiParam(value = "Alarm Status | 告警状态", example = "6565463543") @RequestParam(required = false) Byte status, @ApiParam(value = "Alarm content fuzzy query | 告警内容模糊查询", example = "linux") @RequestParam(required = false) String content, @ApiParam(value = "Sort field, default id | 排序字段,默认id", example = "name") @RequestParam(defaultValue = "id") String sort, @ApiParam(value = "Sort Type | 排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") String order, @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) {
    Specification<Alert> specification = (root, query, criteriaBuilder) -> {
        List<Predicate> andList = new ArrayList<>();
        if (ids != null && !ids.isEmpty()) {
            CriteriaBuilder.In<Long> inPredicate = criteriaBuilder.in(root.get("id"));
            for (long id : ids) {
                inPredicate.value(id);
            }
            andList.add(inPredicate);
        }
        if (monitorId != null) {
            Predicate predicate = criteriaBuilder.equal(root.get("monitorId"), monitorId);
            andList.add(predicate);
        }
        if (priority != null) {
            Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
            andList.add(predicate);
        }
        if (status != null) {
            Predicate predicate = criteriaBuilder.equal(root.get("status"), status);
            andList.add(predicate);
        }
        if (content != null && !"".equals(content)) {
            Predicate predicateContent = criteriaBuilder.like(root.get("content"), "%" + content + "%");
            andList.add(predicateContent);
        }
        Predicate[] predicates = new Predicate[andList.size()];
        return criteriaBuilder.and(andList.toArray(predicates));
    };
    Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
    PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
    Page<Alert> alertPage = alertService.getAlerts(specification, pageRequest);
    Message<Page<Alert>> message = new Message<>(alertPage);
    return ResponseEntity.ok(message);
}
Also used : AlertService(com.usthe.alert.service.AlertService) 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) AlertReport(com.usthe.common.entity.dto.AlertReport) ArrayList(java.util.ArrayList) Valid(javax.validation.Valid) HashSet(java.util.HashSet) ApiOperation(io.swagger.annotations.ApiOperation) Alert(com.usthe.common.entity.alerter.Alert) List(java.util.List) Message(com.usthe.common.entity.dto.Message) Specification(org.springframework.data.jpa.domain.Specification) Predicate(javax.persistence.criteria.Predicate) AlertSummary(com.usthe.alert.dto.AlertSummary) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Sort(org.springframework.data.domain.Sort) ResponseEntity(org.springframework.http.ResponseEntity) Api(io.swagger.annotations.Api) Message(com.usthe.common.entity.dto.Message) Page(org.springframework.data.domain.Page) Predicate(javax.persistence.criteria.Predicate) PageRequest(org.springframework.data.domain.PageRequest) Sort(org.springframework.data.domain.Sort) Alert(com.usthe.common.entity.alerter.Alert) ArrayList(java.util.ArrayList) List(java.util.List) ApiOperation(io.swagger.annotations.ApiOperation)

Example 4 with Message

use of com.usthe.common.entity.dto.Message in project hertzbeat by dromara.

the class AccountController method refreshToken.

@GetMapping("/refresh/{refreshToken}")
@ApiOperation(value = "Use refresh TOKEN to re-acquire TOKEN", notes = "使用刷新TOKEN重新获取TOKEN")
public ResponseEntity<Message<Map<String, String>>> refreshToken(@ApiParam(value = "Refresh TOKEN | 刷新TOKEN", example = "xxx") @PathVariable("refreshToken") @NotNull final String refreshToken) {
    String userId;
    boolean isRefresh;
    try {
        Claims claims = JsonWebTokenUtil.parseJwt(refreshToken);
        userId = String.valueOf(claims.getSubject());
        isRefresh = claims.get("refresh", Boolean.class);
    } catch (Exception e) {
        log.info(e.getMessage());
        Message<Map<String, String>> message = Message.<Map<String, String>>builder().msg("刷新TOKEN过期或错误").code(MONITOR_LOGIN_FAILED_CODE).build();
        return ResponseEntity.ok(message);
    }
    if (userId == null || !isRefresh) {
        Message<Map<String, String>> message = Message.<Map<String, String>>builder().msg("非法的刷新TOKEN").code(MONITOR_LOGIN_FAILED_CODE).build();
        return ResponseEntity.ok(message);
    }
    SurenessAccount account = accountProvider.loadAccount(userId);
    if (account == null) {
        Message<Map<String, String>> message = Message.<Map<String, String>>builder().msg("TOKEN对应的账户不存在").code(MONITOR_LOGIN_FAILED_CODE).build();
        return ResponseEntity.ok(message);
    }
    List<String> roles = account.getOwnRoles();
    // Issue TOKEN      签发TOKEN
    String issueToken = JsonWebTokenUtil.issueJwt(userId, PERIOD_TIME, roles);
    Map<String, Object> customClaimMap = new HashMap<>(1);
    customClaimMap.put("refresh", true);
    String issueRefresh = JsonWebTokenUtil.issueJwt(userId, PERIOD_TIME << 5, customClaimMap);
    Map<String, String> resp = new HashMap<>(2);
    resp.put("token", issueToken);
    resp.put("refreshToken", issueRefresh);
    return ResponseEntity.ok(new Message<>(resp));
}
Also used : Claims(io.jsonwebtoken.Claims) Message(com.usthe.common.entity.dto.Message) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) SurenessAccount(com.usthe.sureness.provider.SurenessAccount) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with Message

use of com.usthe.common.entity.dto.Message in project hertzbeat by dromara.

the class MonitorsController method getMonitors.

@GetMapping
@ApiOperation(value = "Obtain a list of monitoring information based on query filter items", notes = "根据查询过滤项获取监控信息列表")
public ResponseEntity<Message<Page<Monitor>>> getMonitors(@ApiParam(value = "en: Monitor ID,zh: 监控ID", example = "6565463543") @RequestParam(required = false) final List<Long> ids, @ApiParam(value = "en: Monitor Type,zh: 监控类型", example = "linux") @RequestParam(required = false) final String app, @ApiParam(value = "en: Monitor Name,zh: 监控名称,模糊查询", example = "linux-127.0.0.1") @RequestParam(required = false) final String name, @ApiParam(value = "en: Monitor Host,zh: 监控Host,模糊查询", example = "127.0.0.1") @RequestParam(required = false) final String host, @ApiParam(value = "en: Monitor Status,zh: 监控状态 0:未监控,1:可用,2:不可用,3:不可达,4:挂起,9:全部状态", example = "1") @RequestParam(required = false) final Byte status, @ApiParam(value = "en: Sort Field,default id,zh: 排序字段,默认id", example = "name") @RequestParam(defaultValue = "id") final String sort, @ApiParam(value = "en: Sort by,zh: 排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") final String order, @ApiParam(value = "en: List current page,zh: 列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex, @ApiParam(value = "en: Number of list pagination,zh: 列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
    Specification<Monitor> specification = (root, query, criteriaBuilder) -> {
        List<Predicate> andList = new ArrayList<>();
        if (ids != null && !ids.isEmpty()) {
            CriteriaBuilder.In<Long> inPredicate = criteriaBuilder.in(root.get("id"));
            for (long id : ids) {
                inPredicate.value(id);
            }
            andList.add(inPredicate);
        }
        if (app != null && !"".equals(app)) {
            Predicate predicateApp = criteriaBuilder.equal(root.get("app"), app);
            andList.add(predicateApp);
        }
        if (status != null && status >= 0 && status < ALL_MONITOR_STATUS) {
            Predicate predicateStatus = criteriaBuilder.equal(root.get("status"), status);
            andList.add(predicateStatus);
        }
        Predicate[] andPredicates = new Predicate[andList.size()];
        Predicate andPredicate = criteriaBuilder.and(andList.toArray(andPredicates));
        List<Predicate> orList = new ArrayList<>();
        if (host != null && !"".equals(host)) {
            Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%");
            orList.add(predicateHost);
        }
        if (name != null && !"".equals(name)) {
            Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
            orList.add(predicateName);
        }
        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();
        }
    };
    // Pagination is a must         分页是必须的
    Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
    PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
    Page<Monitor> monitorPage = monitorService.getMonitors(specification, pageRequest);
    Message<Page<Monitor>> message = new Message<>(monitorPage);
    return ResponseEntity.ok(message);
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) Monitor(com.usthe.common.entity.manager.Monitor) ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) PageRequest(org.springframework.data.domain.PageRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Page(org.springframework.data.domain.Page) APPLICATION_JSON_VALUE(org.springframework.http.MediaType.APPLICATION_JSON_VALUE) RestController(org.springframework.web.bind.annotation.RestController) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ApiOperation(io.swagger.annotations.ApiOperation) List(java.util.List) MonitorService(com.usthe.manager.service.MonitorService) Message(com.usthe.common.entity.dto.Message) Specification(org.springframework.data.jpa.domain.Specification) Predicate(javax.persistence.criteria.Predicate) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) GetMapping(org.springframework.web.bind.annotation.GetMapping) Sort(org.springframework.data.domain.Sort) ResponseEntity(org.springframework.http.ResponseEntity) Api(io.swagger.annotations.Api) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Message(com.usthe.common.entity.dto.Message) Page(org.springframework.data.domain.Page) Predicate(javax.persistence.criteria.Predicate) PageRequest(org.springframework.data.domain.PageRequest) Monitor(com.usthe.common.entity.manager.Monitor) Sort(org.springframework.data.domain.Sort) ArrayList(java.util.ArrayList) List(java.util.List) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

Message (com.usthe.common.entity.dto.Message)10 ApiOperation (io.swagger.annotations.ApiOperation)9 GetMapping (org.springframework.web.bind.annotation.GetMapping)6 ResponseEntity (org.springframework.http.ResponseEntity)5 Api (io.swagger.annotations.Api)4 ApiParam (io.swagger.annotations.ApiParam)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Predicate (javax.persistence.criteria.Predicate)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Page (org.springframework.data.domain.Page)4 PageRequest (org.springframework.data.domain.PageRequest)4 Specification (org.springframework.data.jpa.domain.Specification)4 APPLICATION_JSON_VALUE (org.springframework.http.MediaType.APPLICATION_JSON_VALUE)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 Sort (org.springframework.data.domain.Sort)3 AlertSummary (com.usthe.alert.dto.AlertSummary)2 AlertDefine (com.usthe.common.entity.alerter.AlertDefine)2 Valid (javax.validation.Valid)2