This is an automated email from the ASF dual-hosted git repository.

zhaoqingran pushed a commit to branch bulletin
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/bulletin by this push:
     new 370a17cb3 [feat(manager)] Implement Bulletin entity and service with 
JPA annotations.
370a17cb3 is described below

commit 370a17cb37eb299148d76d1ca55efef0526360cd
Author: zqr10159 <[email protected]>
AuthorDate: Thu Jul 4 17:32:33 2024 +0800

    [feat(manager)] Implement Bulletin entity and service with JPA annotations.
    
    - Create Bulletin.java with JPA annotations for database mapping.
    - Add BulletinDao interface extending JpaRepository and 
JpaSpecificationExecutor.
    - Implement BulletinServiceImpl with methods for bulletin operations.
    - Update BulletinController with new endpoints for bulletin management.
    - Refactor AlertDefinesController to remove unnecessary search criteria.- 
Add and modify tests for BulletinService and BulletinDao.
    
    BREAKING CHANGE: This commit introduces changes to the database schema and
    requires migration for existing databases to incorporate the Bulletin 
entity.
---
 .../alert/controller/AlertDefinesController.java   |  12 --
 .../hertzbeat/common/entity/manager/Monitor.java   |   4 +-
 .../manager/controller/BulletinController.java     | 126 +++++++++++++++++++++
 .../apache/hertzbeat/manager/dao/BulletinDao.java  |  30 +++++
 .../hertzbeat/manager/pojo/dto/Bulletin.java       |  66 ++++++++++-
 .../hertzbeat/manager/pojo/dto/BulletinDto.java    |  39 +++++++
 .../hertzbeat/manager/service/BulletinService.java |  29 +++++
 .../manager/service/impl/BulletinServiceImpl.java  |  88 ++++++++++++++
 manager/src/main/resources/sureness.yml            |   1 +
 9 files changed, 376 insertions(+), 19 deletions(-)

diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefinesController.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefinesController.java
index 2150f0680..c139620d9 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefinesController.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefinesController.java
@@ -86,18 +86,6 @@ public class AlertDefinesController {
                         criteriaBuilder.like(
                                 criteriaBuilder.lower(root.get("metric")),
                                 "%" + search.toLowerCase() + "%"
-                        ),
-                        criteriaBuilder.like(
-                                criteriaBuilder.lower(root.get("field")),
-                                "%" + search.toLowerCase() + "%"
-                        ),
-                        criteriaBuilder.like(
-                                criteriaBuilder.lower(root.get("expr")),
-                                "%" + search.toLowerCase() + "%"
-                        ),
-                        criteriaBuilder.like(
-                                criteriaBuilder.lower(root.get("template")),
-                                "%" + search.toLowerCase() + "%"
                         )
                 );
                 andList.add(predicate);
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Monitor.java 
b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Monitor.java
index dd59207da..2d3665bf1 100644
--- 
a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Monitor.java
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Monitor.java
@@ -139,14 +139,14 @@ public class Monitor {
     /**
      * Record create time
      */
-    @Schema(title = "Record create time", example = "1612198922000", 
accessMode = READ_ONLY)
+    @Schema(title = "Record create time", example = 
"2024-07-02T20:09:34.903217", accessMode = READ_ONLY)
     @CreatedDate
     private LocalDateTime gmtCreate;
 
     /**
      * Record the latest modification time (timestamp in milliseconds)
      */
-    @Schema(title = "Record modify time", example = "1612198444000", 
accessMode = READ_ONLY)
+    @Schema(title = "Record modify time", example = 
"2024-07-02T20:09:34.903217", accessMode = READ_ONLY)
     @LastModifiedDate
     private LocalDateTime gmtUpdate;
 
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/controller/BulletinController.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/controller/BulletinController.java
new file mode 100644
index 000000000..ab5069330
--- /dev/null
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/controller/BulletinController.java
@@ -0,0 +1,126 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  * contributor license agreements.  See the NOTICE file distributed with
+ *  * this work for additional information regarding copyright ownership.
+ *  * The ASF licenses this file to You 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 org.apache.hertzbeat.manager.controller;
+
+import io.swagger.v3.oas.annotations.Parameter;
+import jakarta.persistence.criteria.CriteriaBuilder;
+import jakarta.persistence.criteria.Predicate;
+import jakarta.validation.Valid;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hertzbeat.common.entity.alerter.AlertDefine;
+import org.apache.hertzbeat.common.entity.dto.Message;
+import org.apache.hertzbeat.manager.pojo.dto.Bulletin;
+import org.apache.hertzbeat.manager.pojo.dto.BulletinDto;
+import org.apache.hertzbeat.manager.service.BulletinService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+/**
+ * Bulletin Controller
+ */
+@Controller
+@RequestMapping("/api/bulletin")
+public class BulletinController {
+
+    @Autowired
+    private BulletinService bulletinService;
+
+    /**
+     * add a new bulletin
+     */
+    @PostMapping
+    public ResponseEntity<Message<Void>> addNewBulletin(@Valid @RequestBody 
BulletinDto bulletinDto) {
+
+//        bulletinService.validate(bulletin, false);
+//        bulletinService.saveBulletin(bulletin);
+        return ResponseEntity.ok(Message.success("Add success"));
+    }
+
+
+    /**
+     * page query bulletin
+     */
+    @GetMapping
+    public ResponseEntity<Message<Page<Bulletin>>> pageQueryBulletin(
+            @Parameter(description = "Bulletin Definition ID", example = 
"6565463543") @RequestParam(required = false) List<Long> ids,
+            @Parameter(description = "Search-Target Expr Template", example = 
"x") @RequestParam(required = false) String search,
+            @Parameter(description = "Sort field, default id", example = "id") 
@RequestParam(defaultValue = "id") String sort,
+            @Parameter(description = "Sort mode: asc: ascending, desc: 
descending", example = "desc") @RequestParam(defaultValue = "desc") String 
order,
+            @Parameter(description = "List current page", example = "0") 
@RequestParam(defaultValue = "0") int pageIndex,
+            @Parameter(description = "Number of list pages", example = "8") 
@RequestParam(defaultValue = "8") int pageSize)
+     {
+         Specification<Bulletin> 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 (StringUtils.hasText(search)) {
+                 Predicate predicate = criteriaBuilder.or(
+                         criteriaBuilder.like(
+                                 criteriaBuilder.lower(root.get("app")),
+                                 "%" + search.toLowerCase() + "%"
+                         ),
+                         criteriaBuilder.like(
+                                 criteriaBuilder.lower(root.get("metric")),
+                                 "%" + search.toLowerCase() + "%"
+                         ),
+                         criteriaBuilder.like(
+                                 criteriaBuilder.lower(root.get("field")),
+                                 "%" + search.toLowerCase() + "%"
+                         ),
+                         criteriaBuilder.like(
+                                 criteriaBuilder.lower(root.get("expr")),
+                                 "%" + search.toLowerCase() + "%"
+                         ),
+                         criteriaBuilder.like(
+                                 criteriaBuilder.lower(root.get("template")),
+                                 "%" + search.toLowerCase() + "%"
+                         )
+                 );
+                 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<Bulletin> bulletinsPage = 
bulletinService.getBulletins(specification, pageRequest);
+         return ResponseEntity.ok(Message.success(bulletinsPage));
+    }
+
+}
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/dao/BulletinDao.java 
b/manager/src/main/java/org/apache/hertzbeat/manager/dao/BulletinDao.java
new file mode 100644
index 000000000..0297c2eb7
--- /dev/null
+++ b/manager/src/main/java/org/apache/hertzbeat/manager/dao/BulletinDao.java
@@ -0,0 +1,30 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  * contributor license agreements.  See the NOTICE file distributed with
+ *  * this work for additional information regarding copyright ownership.
+ *  * The ASF licenses this file to You 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 org.apache.hertzbeat.manager.dao;
+
+
+import org.apache.hertzbeat.manager.pojo.dto.Bulletin;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+public interface BulletinDao extends JpaRepository<Bulletin, Long>, 
JpaSpecificationExecutor<Bulletin> {
+
+}
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/Bulletin.java 
b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/Bulletin.java
index f3f9135cd..1761c1e91 100644
--- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/Bulletin.java
+++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/Bulletin.java
@@ -1,24 +1,80 @@
 package org.apache.hertzbeat.manager.pojo.dto;
 
+import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY;
+import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE;
 import io.swagger.v3.oas.annotations.media.Schema;
+import jakarta.persistence.Column;
+import jakarta.persistence.Convert;
+import jakarta.persistence.Entity;
+import jakarta.persistence.FetchType;
+import jakarta.persistence.GeneratedValue;
+import jakarta.persistence.GenerationType;
+import jakarta.persistence.Id;
+import jakarta.persistence.Index;
+import jakarta.persistence.JoinColumn;
+import jakarta.persistence.OneToOne;
+import jakarta.persistence.Table;
+import jakarta.persistence.Transient;
+import java.time.LocalDateTime;
 import java.util.List;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
 import lombok.Data;
+import lombok.NoArgsConstructor;
+import 
org.apache.hertzbeat.common.entity.manager.JsonStringListAttributeConverter;
+import 
org.apache.hertzbeat.common.entity.manager.JsonTagListAttributeConverter;
 import org.apache.hertzbeat.common.entity.manager.Monitor;
+import org.apache.hertzbeat.common.entity.manager.Tag;
+import org.apache.hertzbeat.common.entity.manager.TagItem;
+import org.springframework.data.annotation.CreatedBy;
+import org.springframework.data.annotation.CreatedDate;
+import org.springframework.data.annotation.LastModifiedBy;
+import org.springframework.data.annotation.LastModifiedDate;
 
 /**
  * Bulletin
  */
+@Entity
 @Data
 @Schema(description = "Bulletin")
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+@Table(name = "hzb_bulletin")
 public class Bulletin {
 
-    @Schema(description = "Monitor Content")
-    private Monitor monitor;
+    @Id
+    @Schema(description = "Bulletin ID", example = "1")
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
 
-    @Schema(description = "Monitor Status")
-    private int Status;
+    @Schema(description = "Monitor ID", example = "1")
+    @Column(name = "monitor_id")
+    private Long monitorId;
 
-    @Schema(description = "Monitor Metrics")
+    @Schema(description = "Monitor Metrics", example = "[\"cpu\", \"memory\"]")
+    @Convert(converter = JsonStringListAttributeConverter.class)
     private List<String> metrics;
 
+    @Schema(description = "Tags(status:success,env:prod)", example = "{name: 
key1, value: value1}",
+            accessMode = READ_WRITE)
+    @Convert(converter = JsonTagListAttributeConverter.class)
+    @Column(length = 2048)
+    private List<TagItem> tags;
+
+    @Schema(title = "The creator of this record", example = "tom", accessMode 
= READ_ONLY)
+    @CreatedBy
+    private String creator;
+
+    @Schema(title = "The modifier of this record", example = "tom", accessMode 
= READ_ONLY)
+    @LastModifiedBy
+    private String modifier;
+
+    @Schema(title = "Record create time", example = 
"2024-07-02T20:09:34.903217", accessMode = READ_ONLY)
+    @CreatedDate
+    private LocalDateTime gmtCreate;
+
+    @Schema(title = "Record modify time", example = 
"2024-07-02T20:09:34.903217", accessMode = READ_ONLY)
+    @LastModifiedDate
+    private LocalDateTime gmtUpdate;
 }
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/BulletinDto.java 
b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/BulletinDto.java
new file mode 100644
index 000000000..dab070f1e
--- /dev/null
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/BulletinDto.java
@@ -0,0 +1,39 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  * contributor license agreements.  See the NOTICE file distributed with
+ *  * this work for additional information regarding copyright ownership.
+ *  * The ASF licenses this file to You 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 org.apache.hertzbeat.manager.pojo.dto;
+
+import java.util.List;
+import lombok.Data;
+import org.apache.hertzbeat.common.entity.manager.Monitor;
+import org.apache.hertzbeat.common.entity.manager.TagItem;
+
+@Data
+public class BulletinDto {
+
+    private Long id;
+
+    private Monitor monitor;
+
+    private List<String> metrics;
+
+    private List<TagItem> tags;
+
+}
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/BulletinService.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/BulletinService.java
index 30748cb9e..1edf57056 100644
--- 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/BulletinService.java
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/BulletinService.java
@@ -1,9 +1,38 @@
 package org.apache.hertzbeat.manager.service;
 
+import java.util.List;
+import org.apache.hertzbeat.common.entity.alerter.AlertDefine;
+import org.apache.hertzbeat.manager.pojo.dto.Bulletin;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.jpa.domain.Specification;
+
 /**
  * Bulletin Service
  */
 public interface BulletinService {
 
+    /**
+     * validate Bulletin
+     */
+    void validate(Bulletin bulletin, Boolean isModify) throws 
IllegalArgumentException;
+
+    /**
+     * List Bulletin
+     */
+    List<Bulletin> listBulletin();
+
+    /**
+     * Save Bulletin
+     */
+    void saveBulletin(Bulletin bulletin);
+
+    /**
+     * Dynamic conditional query
+     * @param specification Query conditions
+     * @param pageRequest Paging parameters
+     * @return The query results
+     */
+    Page<Bulletin> getBulletins(Specification<Bulletin> specification, 
PageRequest pageRequest);
 
 }
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java
new file mode 100644
index 000000000..b7ae543c0
--- /dev/null
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java
@@ -0,0 +1,88 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  * contributor license agreements.  See the NOTICE file distributed with
+ *  * this work for additional information regarding copyright ownership.
+ *  * The ASF licenses this file to You 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 org.apache.hertzbeat.manager.service.impl;
+
+import java.util.List;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.hertzbeat.manager.dao.BulletinDao;
+import org.apache.hertzbeat.manager.pojo.dto.Bulletin;
+import org.apache.hertzbeat.manager.service.BulletinService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.stereotype.Service;
+
+/**
+ * Bulletin Service Implementation
+ */
+
+@Service
+@Slf4j
+public class BulletinServiceImpl implements BulletinService {
+
+    @Autowired
+    private BulletinDao bulletinDao;
+
+    /**
+     * validate Bulletin
+     *
+     * @param bulletin
+     * @param isModify
+     */
+    @Override
+    public void validate(Bulletin bulletin, Boolean isModify) throws 
IllegalArgumentException {
+        if (bulletin == null) {
+            throw new IllegalArgumentException("Bulletin cannot be null");
+        }
+        if (isModify && bulletin.getId() == null) {
+            throw new IllegalArgumentException("Bulletin id cannot be null");
+        }
+    }
+
+    /**
+     * List Bulletin
+     */
+    @Override
+    public List<Bulletin> listBulletin() {
+        return bulletinDao.findAll();
+    }
+
+    /**
+     * Save Bulletin
+     */
+    @Override
+    public void saveBulletin(Bulletin bulletin) {
+        bulletinDao.save(bulletin);
+    }
+
+    /**
+     * Dynamic conditional query
+     *
+     * @param specification Query conditions
+     * @param pageRequest   Paging parameters
+     * @return The query results
+     */
+    @Override
+    public Page<Bulletin> getBulletins(Specification<Bulletin> specification, 
PageRequest pageRequest) {
+       return bulletinDao.findAll(specification, pageRequest);
+    }
+}
diff --git a/manager/src/main/resources/sureness.yml 
b/manager/src/main/resources/sureness.yml
index c7253ff88..cc8c866c0 100644
--- a/manager/src/main/resources/sureness.yml
+++ b/manager/src/main/resources/sureness.yml
@@ -69,6 +69,7 @@ excludedResource:
   - /api/apps/hierarchy===get
   - /api/push/**===*
   - /api/status/page/public/**===*
+  - /api/bulletin/**===*
   # web ui resource
   - /===get
   - /dashboard/**===get


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to