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

guoyp pushed a commit to branch griffin-2.0.0-dev
in repository https://gitbox.apache.org/repos/asf/griffin.git


The following commit(s) were added to refs/heads/griffin-2.0.0-dev by this push:
     new 47af255a add service interface for metricD/metricV/metricTag tables. 
(#667)
47af255a is described below

commit 47af255a14e282e546d098a6c9a5a7de8158894d
Author: Jin <[email protected]>
AuthorDate: Thu Oct 24 19:52:29 2024 +0800

    add service interface for metricD/metricV/metricTag tables. (#667)
    
    * add service interface for metricD table.
    
    * add the process of exception.
---
 griffin-metric/pom.xml                             |   2 +-
 .../org/apache/griffin/metric/dao/MetricDDao.java  |  22 +++-
 .../griffin/metric/dao/TagAttachmentDao.java       |  40 ++++++++
 .../griffin/metric/dao/mapper/TagsMapper.java      |   9 ++
 .../apache/griffin/metric/entity/BaseEntity.java   |   5 +
 .../org/apache/griffin/metric/entity/MetricD.java  |  11 +-
 .../apache/griffin/metric/entity/MetricTagD.java   |   2 +-
 .../org/apache/griffin/metric/entity/MetricV.java  |   5 -
 .../entity/{Tags.java => TagAttachment.java}       |  12 +--
 .../griffin/metric/exception/GriffinErr.java       |  33 ++++++
 .../metric/exception/GriffinErrorEntity.java       |  22 ++++
 .../griffin/metric/exception/GriffinException.java |  28 ++++++
 .../griffin/metric/service/MetricDService.java     |  52 +++++++---
 .../griffin/metric/service/MetricTagDService.java  |  26 -----
 .../griffin/metric/service/MetricTagService.java   | 111 +++++++++++++++++++++
 .../griffin/metric/service/MetricVService.java     |  49 ++++++++-
 griffin-metric/src/main/resources/application.yaml |  31 ++++--
 .../src/main/resources/sql/create_h2.sql           |  18 +++-
 .../apache/griffin/metric/entity/MetricTest.java   |  16 +--
 19 files changed, 410 insertions(+), 84 deletions(-)

diff --git a/griffin-metric/pom.xml b/griffin-metric/pom.xml
index 5317e6b0..76cabdb1 100644
--- a/griffin-metric/pom.xml
+++ b/griffin-metric/pom.xml
@@ -109,7 +109,7 @@ under the License.
         <dependency>
             <groupId>com.h2database</groupId>
             <artifactId>h2</artifactId>
-            <scope>test</scope>
+<!--            <scope>test</scope>-->
         </dependency>
 
         <dependency>
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricDDao.java 
b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricDDao.java
index 72a7e003..258dcd32 100644
--- a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricDDao.java
+++ b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricDDao.java
@@ -20,23 +20,37 @@ package org.apache.griffin.metric.dao;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.griffin.metric.dao.mapper.MetricDMapper;
 import org.apache.griffin.metric.entity.MetricD;
+import org.apache.griffin.metric.exception.GriffinErr;
+import org.apache.griffin.metric.exception.GriffinException;
 import org.springframework.stereotype.Repository;
 
 @Repository
 @Slf4j
 public class MetricDDao extends BaseDao<MetricD, MetricDMapper> {
 
+    public static final String NOT_BE_NULL = "The metricD argument is 
illegal." +
+            "Either metricD entity or metric name attribute must not be null.";
+
     public MetricDDao(MetricDMapper metricDMapper) {
         super(metricDMapper);
     }
 
     public int addMetricD(MetricD metricD) {
-        if (null == metricD || null == metricD.getMetricName()) {
-            log.warn("metricD is invalid");
-            return 0;
+        validateEntity(metricD);
+        int count = 0;
+        try {
+            count = mybatisMapper.insert(metricD);
+        } catch (Exception e) {
+            throw new GriffinException(e);
         }
-        int count = mybatisMapper.insert(metricD);
         log.info("add metricD: {}, count: {}", metricD, count);
         return count;
     }
+
+    private void validateEntity(MetricD metricD) {
+        if (null == metricD || null == metricD.getMetricName()) {
+            log.error(NOT_BE_NULL);
+            throw new GriffinException(NOT_BE_NULL, 
GriffinErr.validationError);
+        }
+    }
 }
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/TagAttachmentDao.java
 
b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/TagAttachmentDao.java
new file mode 100644
index 00000000..6ceb6dd1
--- /dev/null
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/TagAttachmentDao.java
@@ -0,0 +1,40 @@
+package org.apache.griffin.metric.dao;
+
+import lombok.NonNull;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.griffin.metric.dao.mapper.TagsMapper;
+import org.apache.griffin.metric.entity.TagAttachment;
+import org.springframework.stereotype.Repository;
+
+import java.util.Objects;
+
+@Repository
+@Slf4j
+public class TagAttachmentDao extends BaseDao<TagAttachment, TagsMapper> {
+
+    public TagAttachmentDao(@NonNull TagsMapper mybatisMapper) {
+        super(mybatisMapper);
+    }
+
+    public int addMetricTags(TagAttachment tagAttachment) {
+        if (Objects.isNull(tagAttachment)) {
+            log.warn("tags is invalid");
+            return 0;
+        }
+
+        if (Objects.isNull(tagAttachment.getMetricId())) {
+            log.warn("metric id is invalid");
+            return 0;
+        }
+
+        TagAttachment existence = 
mybatisMapper.selectById(tagAttachment.getMetricId());
+        if (Objects.nonNull(existence)) {
+            log.warn("tagAttachment has existed");
+            return 0;
+        }
+
+        int count = mybatisMapper.insert(tagAttachment);
+        log.info("add tags: {}, count: {}", tagAttachment, count);
+        return count;
+    }
+}
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/mapper/TagsMapper.java
 
b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/mapper/TagsMapper.java
new file mode 100644
index 00000000..b76feba1
--- /dev/null
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/mapper/TagsMapper.java
@@ -0,0 +1,9 @@
+package org.apache.griffin.metric.dao.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.griffin.metric.entity.TagAttachment;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface TagsMapper extends BaseMapper<TagAttachment> {
+}
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/BaseEntity.java 
b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/BaseEntity.java
index 67fb658b..7712e2da 100644
--- 
a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/BaseEntity.java
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/BaseEntity.java
@@ -20,6 +20,8 @@ under the License.
 package org.apache.griffin.metric.entity;
 
 import com.baomidou.mybatisplus.annotation.TableField;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
 import lombok.Data;
 
 import java.util.Date;
@@ -28,6 +30,7 @@ import java.util.Date;
  * A base class in metric function in griffin, which contains timestamp 
properties of entity creation/update.
  */
 @Data
+@JsonInclude(JsonInclude.Include.NON_NULL)
 public abstract class BaseEntity implements java.io.Serializable {
 
     private static final long serialVersionUID = 2110740953277261851L;
@@ -35,12 +38,14 @@ public abstract class BaseEntity implements 
java.io.Serializable {
     /**
      * creation time
      */
+    @JsonProperty(value = "creation_time")
     @TableField(value = "ctime")
     protected Date ctime;
 
     /**
      * update time
      */
+    @JsonProperty(value = "update_time")
     @TableField(value = "mtime")
     protected Date mtime;
 
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricD.java 
b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricD.java
index 3c9ea5c9..d17facf3 100644
--- a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricD.java
+++ b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricD.java
@@ -19,17 +19,17 @@ under the License.
 
 package org.apache.griffin.metric.entity;
 
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonInclude;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NoArgsConstructor;
 
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.annotation.TableField;
-import com.baomidou.mybatisplus.annotation.TableId;
-import com.baomidou.mybatisplus.annotation.TableName;
-
 /**
  * A metric definition entity represents fundamental metadata.
  */
@@ -38,6 +38,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
 @NoArgsConstructor
 @AllArgsConstructor
 @EqualsAndHashCode(callSuper = false)
+@JsonInclude(JsonInclude.Include.NON_NULL)
 @TableName("t_metric_d")
 public class MetricD extends BaseEntity {
 
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTagD.java 
b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTagD.java
index 65a46f12..9cd1c889 100644
--- 
a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTagD.java
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTagD.java
@@ -43,7 +43,7 @@ public class MetricTagD extends BaseEntity {
     /**
      * An unique identity for a metric tag.
      */
-    @TableId(value="mid", type = IdType.ASSIGN_ID)
+    @TableId(value="tid", type = IdType.AUTO)
     private Long id;
 
     /**
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricV.java 
b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricV.java
index 600f3739..350f297f 100644
--- a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricV.java
+++ b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricV.java
@@ -54,9 +54,4 @@ public class MetricV extends BaseEntity {
      */
     @TableField(value = "val")
     private double value;
-
-    /**
-     * The tag property assigned.
-     */
-    private Tags tags;
 }
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/Tags.java 
b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/TagAttachment.java
similarity index 86%
rename from 
griffin-metric/src/main/java/org/apache/griffin/metric/entity/Tags.java
rename to 
griffin-metric/src/main/java/org/apache/griffin/metric/entity/TagAttachment.java
index 45f20167..8dfd5d05 100644
--- a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/Tags.java
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/TagAttachment.java
@@ -18,6 +18,7 @@
 package org.apache.griffin.metric.entity;
 
 import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.AllArgsConstructor;
@@ -26,8 +27,6 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NoArgsConstructor;
 
-import java.util.List;
-
 /**
  * A common tag entity represents the relationships among metric entities and 
metric tag entities.
  */
@@ -37,16 +36,17 @@ import java.util.List;
 @AllArgsConstructor
 @EqualsAndHashCode(callSuper = false)
 @TableName("t_metric_tag")
-public class Tags extends BaseEntity {
+public class TagAttachment extends BaseEntity {
 
     /**
      * Metric entity's identity.
      */
-    @TableId(value="mid", type = IdType.AUTO)
+    @TableId(value="mid", type = IdType.INPUT)
     private Long metricId;
 
     /**
-     * All tag properties assigning to a metric entity.
+     * Metric tag's identity.
      */
-    private List<MetricTagD> metricTags;
+    @TableField(value="tid")
+    private Long tagId;
 }
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErr.java
 
b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErr.java
new file mode 100644
index 00000000..8b0f6bc5
--- /dev/null
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErr.java
@@ -0,0 +1,33 @@
+package org.apache.griffin.metric.exception;
+
+import lombok.Getter;
+
+public enum GriffinErr {
+    commonError(101, "Hit an error without details."),
+    validationError(102, "Data validation fails due to [%s]"),
+    // db operation errors
+    dbInsertionError(301, "Fail to insert a record."),
+    dbUpdateError(302, "Fail to update a record."),
+    dbDeletionError(303, "Fail to delete a record."),
+    ;
+
+    @Getter
+    private int code;
+
+    @Getter
+    private String message;
+
+    GriffinErr(int code, String message) {
+        this.code = code;
+        this.message = message;
+    }
+
+    public GriffinErrorEntity buildErrorEntity() {
+        return new GriffinErrorEntity(this);
+    }
+
+    public GriffinErrorEntity buildErrorEntity(String message) {
+        return new GriffinErrorEntity(this, message);
+    }
+
+}
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErrorEntity.java
 
b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErrorEntity.java
new file mode 100644
index 00000000..ad084e7e
--- /dev/null
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErrorEntity.java
@@ -0,0 +1,22 @@
+package org.apache.griffin.metric.exception;
+
+import lombok.Data;
+import org.apache.griffin.metric.entity.BaseEntity;
+
+@Data
+public class GriffinErrorEntity extends BaseEntity {
+
+    private Integer code;
+
+    private String message;
+
+    public GriffinErrorEntity(GriffinErr err) {
+        this.code = err.getCode();
+        this.message = err.getMessage();
+    }
+
+    public GriffinErrorEntity(GriffinErr err, String details) {
+        this.code = err.getCode();
+        this.message = String.format(err.getMessage(), details);
+    }
+}
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinException.java
 
b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinException.java
new file mode 100644
index 00000000..1712ddb0
--- /dev/null
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinException.java
@@ -0,0 +1,28 @@
+package org.apache.griffin.metric.exception;
+
+import lombok.Getter;
+
+public class GriffinException extends RuntimeException {
+    @Getter
+    private final GriffinErr error;
+
+    public GriffinException(String message) {
+        super(message);
+        this.error = GriffinErr.commonError;
+    }
+
+    public GriffinException(Throwable cause) {
+        super(cause);
+        this.error = GriffinErr.commonError;
+    }
+
+    public GriffinException(String message, Throwable cause) {
+        super(message, cause);
+        this.error = GriffinErr.commonError;
+    }
+
+    public GriffinException(String message, GriffinErr error) {
+        super(message);
+        this.error = error;
+    }
+}
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricDService.java
 
b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricDService.java
index f1aedd6e..5c35258c 100644
--- 
a/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricDService.java
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricDService.java
@@ -2,8 +2,13 @@ package org.apache.griffin.metric.service;
 
 import lombok.extern.slf4j.Slf4j;
 import org.apache.griffin.metric.dao.MetricDDao;
+import org.apache.griffin.metric.entity.BaseEntity;
 import org.apache.griffin.metric.entity.MetricD;
+import org.apache.griffin.metric.exception.GriffinErr;
+import org.apache.griffin.metric.exception.GriffinException;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.lang.NonNull;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -11,6 +16,7 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.util.List;
@@ -19,6 +25,8 @@ import java.util.List;
 @Slf4j
 public class MetricDService {
 
+    public static final String METRICD_URI = "/metricD";
+
     private final MetricDDao metricDDao;
 
     public MetricDService(MetricDDao metricDDao) {
@@ -26,31 +34,49 @@ public class MetricDService {
     }
 
     @GetMapping(value = "/ping", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String ping(){
+    public String ping() {
         return "hello";
     }
 
-    @GetMapping(value = "/allMetricDs", produces = 
MediaType.APPLICATION_JSON_VALUE)
-    public List<MetricD> allMetricDs(){
+    @GetMapping(value = "/allMetricD", produces = 
MediaType.APPLICATION_JSON_VALUE)
+    @ResponseStatus(HttpStatus.OK)
+    public List<MetricD> allMetricDs() {
         return metricDDao.queryAll();
     }
 
-    @PutMapping(value = "/metricD", consumes = 
MediaType.APPLICATION_JSON_VALUE,
+    @PutMapping(value = METRICD_URI, consumes = 
MediaType.APPLICATION_JSON_VALUE,
             produces = MediaType.APPLICATION_JSON_VALUE)
-    public MetricD createMetricD(@RequestBody MetricD metricD){
-        int id = metricDDao.addMetricD(metricD);
-        return metricD;
+    @ResponseStatus(HttpStatus.CREATED)
+    public ResponseEntity<BaseEntity> createMetricD(@RequestBody MetricD 
metricD) {
+        try {
+            int id = metricDDao.addMetricD(metricD);
+            if (id != 1) {
+                return new 
ResponseEntity<>(GriffinErr.dbInsertionError.buildErrorEntity(),
+                        HttpStatus.INTERNAL_SERVER_ERROR);
+            }
+            return new ResponseEntity<>(metricD, HttpStatus.CREATED);
+        } catch (GriffinException e) {
+            return new 
ResponseEntity<>(e.getError().buildErrorEntity(e.getMessage()),
+                    HttpStatus.INTERNAL_SERVER_ERROR);
+        }
     }
 
-    @PostMapping(value = "/metricD", consumes = 
MediaType.APPLICATION_JSON_VALUE,
+    @PostMapping(value = METRICD_URI, consumes = 
MediaType.APPLICATION_JSON_VALUE,
             produces = MediaType.APPLICATION_JSON_VALUE)
-    public MetricD updateMetricD(MetricD metricD){
+    @ResponseStatus(HttpStatus.ACCEPTED)
+    public ResponseEntity<BaseEntity> updateMetricD(MetricD metricD) {
         boolean ret = metricDDao.updateById(metricD);
-        return ret ? metricD : null;
+        return ret ? new ResponseEntity<>(metricD, HttpStatus.ACCEPTED) : new 
ResponseEntity<>(
+                GriffinErr.dbUpdateError.buildErrorEntity(),
+                HttpStatus.INTERNAL_SERVER_ERROR);
     }
 
-    @DeleteMapping(value = "/metricD/{id}")
-    public boolean deleteMetricD(@PathVariable @NonNull String id){
-        return metricDDao.deleteById(id);
+    @DeleteMapping(value = METRICD_URI + "/{id}")
+    @ResponseStatus(HttpStatus.OK)
+    public ResponseEntity<BaseEntity> deleteMetricD(@PathVariable @NonNull 
String id) {
+        boolean ret = metricDDao.deleteById(id);
+        return ret ? new ResponseEntity<>(HttpStatus.OK) : new 
ResponseEntity<>(
+                GriffinErr.dbDeletionError.buildErrorEntity(),
+                HttpStatus.INTERNAL_SERVER_ERROR);
     }
 }
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricTagDService.java
 
b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricTagDService.java
deleted file mode 100644
index 94270fe0..00000000
--- 
a/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricTagDService.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package org.apache.griffin.metric.service;
-
-import lombok.extern.slf4j.Slf4j;
-import org.apache.griffin.metric.dao.MetricTagDDao;
-import org.apache.griffin.metric.entity.MetricTagD;
-import org.springframework.http.MediaType;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RestController;
-
-@RestController
-@Slf4j
-public class MetricTagDService {
-    private final MetricTagDDao metricTagDDao;
-
-    public MetricTagDService(MetricTagDDao metricTagDDao) {
-        this.metricTagDDao = metricTagDDao;
-    }
-
-    @PutMapping(value = "/metricTagD",consumes = 
MediaType.APPLICATION_JSON_VALUE,
-            produces = MediaType.APPLICATION_JSON_VALUE)
-    public MetricTagD createMetricTagD(@RequestBody MetricTagD metricTagD) {
-        int i = metricTagDDao.addMetricTagD(metricTagD);
-        return metricTagD;
-    }
-}
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricTagService.java
 
b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricTagService.java
new file mode 100644
index 00000000..badb51c7
--- /dev/null
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricTagService.java
@@ -0,0 +1,111 @@
+package org.apache.griffin.metric.service;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.griffin.metric.dao.MetricTagDDao;
+import org.apache.griffin.metric.dao.TagAttachmentDao;
+import org.apache.griffin.metric.entity.BaseEntity;
+import org.apache.griffin.metric.entity.MetricTagD;
+import org.apache.griffin.metric.entity.TagAttachment;
+import org.apache.griffin.metric.exception.GriffinErr;
+import org.apache.griffin.metric.exception.GriffinException;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.lang.NonNull;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@Slf4j
+public class MetricTagService {
+
+    public static final String METRIC_TAG_D = "/metricTagD";
+
+    public static final String TAGS = "/tags";
+
+    private final MetricTagDDao metricTagDDao;
+
+    private final TagAttachmentDao tagAttachmentDao;
+
+    public MetricTagService(MetricTagDDao metricTagDDao, TagAttachmentDao 
tagAttachmentDao) {
+        this.metricTagDDao = metricTagDDao;
+        this.tagAttachmentDao = tagAttachmentDao;
+    }
+
+    @PutMapping(value = METRIC_TAG_D,consumes = 
MediaType.APPLICATION_JSON_VALUE,
+            produces = MediaType.APPLICATION_JSON_VALUE)
+    @ResponseStatus(HttpStatus.CREATED)
+    public ResponseEntity<BaseEntity> createMetricTagD(@RequestBody MetricTagD 
metricTagD) {
+        try {
+            int id = metricTagDDao.addMetricTagD(metricTagD);
+            if (id != 1) {
+                return new 
ResponseEntity<>(GriffinErr.dbInsertionError.buildErrorEntity(),
+                        HttpStatus.INTERNAL_SERVER_ERROR);
+            }
+            return new ResponseEntity<>(metricTagD, HttpStatus.CREATED);
+        } catch (GriffinException e) {
+            return new 
ResponseEntity<>(e.getError().buildErrorEntity(e.getMessage()),
+                    HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    @PostMapping(value = METRIC_TAG_D, consumes = 
MediaType.APPLICATION_JSON_VALUE,
+            produces = MediaType.APPLICATION_JSON_VALUE)
+    @ResponseStatus(HttpStatus.ACCEPTED)
+    public ResponseEntity<BaseEntity> updateMetricTagD(@RequestBody MetricTagD 
metricTagD) {
+        boolean ret = metricTagDDao.updateById(metricTagD);
+        return ret ? new ResponseEntity<>(metricTagD, HttpStatus.ACCEPTED) : 
new ResponseEntity<>(
+                GriffinErr.dbUpdateError.buildErrorEntity(),
+                HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+
+    @DeleteMapping(value = METRIC_TAG_D + "/{id}")
+    @ResponseStatus(HttpStatus.OK)
+    public ResponseEntity<BaseEntity> deleteMetricTagD(@PathVariable @NonNull 
String id) {
+        boolean ret = metricTagDDao.deleteById(id);
+        return ret ? new ResponseEntity<>(HttpStatus.OK) : new 
ResponseEntity<>(
+                GriffinErr.dbDeletionError.buildErrorEntity(),
+                HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+
+    @PutMapping(value = TAGS,consumes = MediaType.APPLICATION_JSON_VALUE,
+            produces = MediaType.APPLICATION_JSON_VALUE)
+    @ResponseStatus(HttpStatus.CREATED)
+    public ResponseEntity<BaseEntity> attachTags(@RequestBody TagAttachment 
tagAttachment) {
+        try {
+            int id = tagAttachmentDao.addMetricTags(tagAttachment);
+            if (id != 1) {
+                return new 
ResponseEntity<>(GriffinErr.dbInsertionError.buildErrorEntity(),
+                        HttpStatus.INTERNAL_SERVER_ERROR);
+            }
+            return new ResponseEntity<>(tagAttachment, HttpStatus.CREATED);
+        } catch (GriffinException e) {
+            return new 
ResponseEntity<>(e.getError().buildErrorEntity(e.getMessage()),
+                    HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    @PostMapping(value = TAGS, consumes = MediaType.APPLICATION_JSON_VALUE,
+            produces = MediaType.APPLICATION_JSON_VALUE)
+    @ResponseStatus(HttpStatus.ACCEPTED)
+    public ResponseEntity<BaseEntity> updateMetricD(@RequestBody TagAttachment 
tagAttachment) {
+        boolean ret = tagAttachmentDao.updateById(tagAttachment);
+        return ret ? new ResponseEntity<>(tagAttachment, HttpStatus.ACCEPTED) 
: new ResponseEntity<>(
+                GriffinErr.dbUpdateError.buildErrorEntity(),
+                HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+
+    @DeleteMapping(value = TAGS + "/{id}")
+    @ResponseStatus(HttpStatus.OK)
+    public ResponseEntity<BaseEntity> deleteMetricD(@PathVariable @NonNull 
String id) {
+        boolean ret = tagAttachmentDao.deleteById(id);
+        return ret ? new ResponseEntity<>(HttpStatus.OK) : new 
ResponseEntity<>(
+                GriffinErr.dbDeletionError.buildErrorEntity(),
+                HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+}
diff --git 
a/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricVService.java
 
b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricVService.java
index f7344a2c..5974cb5c 100644
--- 
a/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricVService.java
+++ 
b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricVService.java
@@ -2,25 +2,66 @@ package org.apache.griffin.metric.service;
 
 import lombok.extern.slf4j.Slf4j;
 import org.apache.griffin.metric.dao.MetricVDao;
+import org.apache.griffin.metric.entity.BaseEntity;
 import org.apache.griffin.metric.entity.MetricV;
+import org.apache.griffin.metric.exception.GriffinErr;
+import org.apache.griffin.metric.exception.GriffinException;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.lang.NonNull;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 @Slf4j
 public class MetricVService {
+    public static final String METRIC_V_URI = "/metricV";
+
     private final MetricVDao metricVDao;
 
     public MetricVService(MetricVDao metricVDao) {
         this.metricVDao = metricVDao;
     }
 
-    @PutMapping(value = "/metricV",consumes = MediaType.APPLICATION_JSON_VALUE,
+    @PutMapping(value = METRIC_V_URI,consumes = 
MediaType.APPLICATION_JSON_VALUE,
+            produces = MediaType.APPLICATION_JSON_VALUE)
+    @ResponseStatus(HttpStatus.CREATED)
+    public ResponseEntity<BaseEntity> createMetricV(@RequestBody MetricV 
metricV) {
+        try {
+            int id = metricVDao.addMetricV(metricV);
+            if (id != 1) {
+                return new 
ResponseEntity<>(GriffinErr.dbInsertionError.buildErrorEntity(),
+                        HttpStatus.INTERNAL_SERVER_ERROR);
+            }
+            return new ResponseEntity<>(metricV, HttpStatus.CREATED);
+        } catch (GriffinException e) {
+            return new 
ResponseEntity<>(e.getError().buildErrorEntity(e.getMessage()),
+                    HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    @PostMapping(value = METRIC_V_URI, consumes = 
MediaType.APPLICATION_JSON_VALUE,
             produces = MediaType.APPLICATION_JSON_VALUE)
-    public MetricV createMetricV(@RequestBody MetricV metricV) {
-        int id = metricVDao.addMetricV(metricV);
-        return metricV;
+    @ResponseStatus(HttpStatus.ACCEPTED)
+    public ResponseEntity<BaseEntity> updateMetricV(@RequestBody MetricV 
metricV) {
+        boolean ret = metricVDao.updateById(metricV);
+        return ret ? new ResponseEntity<>(metricV, HttpStatus.ACCEPTED) : new 
ResponseEntity<>(
+                GriffinErr.dbUpdateError.buildErrorEntity(),
+                HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+
+    @DeleteMapping(value = METRIC_V_URI + "/{id}")
+    @ResponseStatus(HttpStatus.OK)
+    public ResponseEntity<BaseEntity> deleteMetricV(@PathVariable @NonNull 
String id) {
+        boolean ret = metricVDao.deleteById(id);
+        return ret ? new ResponseEntity<>(HttpStatus.OK) : new 
ResponseEntity<>(
+                GriffinErr.dbDeletionError.buildErrorEntity(),
+                HttpStatus.INTERNAL_SERVER_ERROR);
     }
 }
diff --git a/griffin-metric/src/main/resources/application.yaml 
b/griffin-metric/src/main/resources/application.yaml
index 3636ed0c..85a33f8c 100644
--- a/griffin-metric/src/main/resources/application.yaml
+++ b/griffin-metric/src/main/resources/application.yaml
@@ -14,23 +14,42 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-#server:
-#  port: 8888
+server:
+  port: 8888
 
 spring:
   application:
     name: persist-service
+#  sql:
+#    init:
+#      schema-locations: classpath:sql/create_mysql.sql
+#  datasource:
+#    driver-class-name: com.mysql.cj.jdbc.Driver
+#    url: jdbc:mysql://127.0.0.1:3306/griffin
+#    username: root
+#    password: ""
+#    hikari:
+#      maximum-pool-size: 5
   sql:
     init:
-      schema-locations: classpath:sql/create_mysql.sql
+      schema-locations: classpath:sql/create_h2.sql
+      continue-on-error: false
+      mode: embedded
   datasource:
-    driver-class-name: com.mysql.cj.jdbc.Driver
-    url: jdbc:mysql://127.0.0.1:3306/griffin
-    username: root
+    driver-class-name: org.h2.Driver
+    url: jdbc:h2:mem:griffin
+    username: sa
     password: ""
     hikari:
       maximum-pool-size: 5
 
+  h2:
+    console:
+      path: /h2-console
+      enabled: on
+      settings:
+        web-allow-others: true
+        trace: true
 
 mybatis-plus:
   mapper-locations: classpath:mapper/*Mapper.xml
diff --git a/griffin-metric/src/main/resources/sql/create_h2.sql 
b/griffin-metric/src/main/resources/sql/create_h2.sql
index be65820b..5bdc3671 100644
--- a/griffin-metric/src/main/resources/sql/create_h2.sql
+++ b/griffin-metric/src/main/resources/sql/create_h2.sql
@@ -1,3 +1,4 @@
+DROP TABLE IF EXISTS t_metric_d;
 CREATE TABLE t_metric_d (
     mid BIGINT PRIMARY KEY AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
@@ -7,21 +8,28 @@ CREATE TABLE t_metric_d (
     mtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 );
 
+DROP TABLE IF EXISTS t_tag_d;
 CREATE TABLE t_tag_d (
-    mid BIGINT PRIMARY KEY AUTO_INCREMENT,
+    tid BIGINT PRIMARY KEY AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
-    val DOUBLE NOT NULL
+    val DOUBLE NOT NULL,
+    ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+    mtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 );
 
+DROP TABLE IF EXISTS t_metric_tag;
 CREATE TABLE t_metric_tag (
-    tid BIGINT PRIMARY KEY references t_metric_d(mid),
-    mtid BIGINT references t_tag_d(mid)
+    mid BIGINT references t_metric_d(mid),
+    tid BIGINT references t_tag_d(tid),
+    ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+    mtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+    PRIMARY KEY(tid,mid)
 );
 
+DROP TABLE IF EXISTS t_metric_v;
 CREATE TABLE t_metric_v (
     mid BIGINT PRIMARY KEY references t_metric_d(mid),
     val DOUBLE NOT NULL,
-    tid BIGINT references t_metric_tag(tid),
     ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
     mtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 );
diff --git 
a/griffin-metric/src/test/java/org/apache/griffin/metric/entity/MetricTest.java 
b/griffin-metric/src/test/java/org/apache/griffin/metric/entity/MetricTest.java
index 74de5eca..64277856 100644
--- 
a/griffin-metric/src/test/java/org/apache/griffin/metric/entity/MetricTest.java
+++ 
b/griffin-metric/src/test/java/org/apache/griffin/metric/entity/MetricTest.java
@@ -31,7 +31,7 @@ public class MetricTest {
     private MetricD metricD;
     private MetricV metricV1;
     private MetricV metricV2;
-    private Tags tags;
+    private TagAttachment tagAttachment;
 
     @BeforeEach
     public void setUp() {
@@ -47,7 +47,7 @@ public class MetricTest {
         metricV1 = MetricV.builder()
                 .metricId(1L)
                 .value(100.5)
-                .tags(Tags.builder()
+                .tags(TagAttachment.builder()
                         .metricId(1L)
                         .metricTags(createSampleTags())
                         .build())
@@ -56,14 +56,14 @@ public class MetricTest {
         metricV2 = MetricV.builder()
                 .metricId(1L)
                 .value(200.75)
-                .tags(Tags.builder()
+                .tags(TagAttachment.builder()
                         .metricId(1L)
                         .metricTags(createSampleTags())
                         .build())
                 .build();
 
         // Initialize Tags
-        tags = Tags.builder()
+        tagAttachment = TagAttachment.builder()
                 .metricId(1L)
                 .metricTags(createSampleTags())
                 .build();
@@ -93,14 +93,14 @@ public class MetricTest {
     public void testFetchMetricDWithTags() {
         // Mock fetch logic here. This would typically involve querying a 
database or service.
         MetricD fetchedMetricD = metricD;  // Simulate fetching
-        Tags fetchedTags = tags;  // Simulate fetching tags
+        TagAttachment fetchedTagAttachment = tagAttachment;  // Simulate 
fetching tags
 
         assertNotNull(fetchedMetricD);
         assertEquals(1L, fetchedMetricD.getMetricId());
 
-        assertNotNull(fetchedTags);
-        assertEquals(1L, fetchedTags.getMetricId());
-        assertEquals(2, fetchedTags.getMetricTags().size());
+        assertNotNull(fetchedTagAttachment);
+        assertEquals(1L, fetchedTagAttachment.getMetricId());
+        assertEquals(2, fetchedTagAttachment.getMetricTags().size());
     }
 
     private List<MetricTagD> createSampleTags() {


Reply via email to