diff --git a/griffin-bom/pom.xml b/griffin-bom/pom.xml index 8bc23407c..b6ed319f2 100644 --- a/griffin-bom/pom.xml +++ b/griffin-bom/pom.xml @@ -53,7 +53,7 @@ under the License. 1.7.36 1.2.11 2.2.220 - 8.0.16 + 8.0.28 2.11.0 0.238.1 2.10.13 diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/DaoConfiguration.java b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/DaoConfiguration.java index 93306f4a6..bd15ca51d 100644 --- a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/DaoConfiguration.java +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/DaoConfiguration.java @@ -21,20 +21,20 @@ package org.apache.griffin.metric.dao; import org.mybatis.spring.annotation.MapperScan; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; - @Configuration @ComponentScan("org.apache.griffin.metric") @EnableAutoConfiguration @MapperScan(basePackages = "org.apache.griffin.metric.dao.mapper", sqlSessionFactoryRef = "sqlSessionFactory") public class DaoConfiguration { - @Autowired(required = false) - public DataSourceScriptDatabaseInitializer dataSourceScriptDatabaseInitializer; + public final DataSourceScriptDatabaseInitializer dataSourceScriptDatabaseInitializer; + public DaoConfiguration(DataSourceScriptDatabaseInitializer dsScriptDatabaseInitializer) { + this.dataSourceScriptDatabaseInitializer = dsScriptDatabaseInitializer; + } } diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/IDao.java b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/IDao.java index e47dcad2f..835bda248 100644 --- a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/IDao.java +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/IDao.java @@ -28,9 +28,16 @@ public interface IDao { /** * Query by the primary key. + * @param id Unique identity of E + * @return E if exist */ E queryById(@NonNull Serializable id); + /** + * Query by the primary key. + * @param id Unique identity of E + * @return {@code Optional} + */ Optional queryOptionalById(@NonNull Serializable id); List queryByIds(Collection ids); diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricTagDDao.java b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricTagDDao.java new file mode 100644 index 000000000..77265b420 --- /dev/null +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricTagDDao.java @@ -0,0 +1,27 @@ +package org.apache.griffin.metric.dao; + +import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; +import org.apache.griffin.metric.dao.mapper.MetricTagDMapper; +import org.apache.griffin.metric.entity.MetricTagD; +import org.springframework.stereotype.Repository; + +@Repository +@Slf4j +public class MetricTagDDao extends BaseDao{ + + public MetricTagDDao(@NonNull MetricTagDMapper mybatisMapper) { + super(mybatisMapper); + } + + public int addMetricTagD(MetricTagD metricTagD) { + if (metricTagD == null) { + log.warn("metricTagD is invalid"); + return 0; + } + + int count = mybatisMapper.insert(metricTagD); + log.info("add metricTagD: {}, count: {}", metricTagD, count); + return count; + } +} diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricVDao.java b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricVDao.java new file mode 100644 index 000000000..4dd9f5fae --- /dev/null +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/MetricVDao.java @@ -0,0 +1,25 @@ +package org.apache.griffin.metric.dao; + +import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; +import org.apache.griffin.metric.dao.mapper.MetricVMapper; +import org.apache.griffin.metric.entity.MetricV; +import org.springframework.stereotype.Repository; + +@Repository +@Slf4j +public class MetricVDao extends BaseDao{ + public MetricVDao(@NonNull MetricVMapper mybatisMapper) { + super(mybatisMapper); + } + + public int addMetricV(MetricV metricV) { + if (metricV == null) { + log.warn("metricV is invalid"); + return 0; + } + int count = mybatisMapper.insert(metricV); + log.info("add metricV: {}, count: {}", metricV, count); + return count; + } +} diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/mapper/MetricTagDMapper.java b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/mapper/MetricTagDMapper.java new file mode 100644 index 000000000..5771732ac --- /dev/null +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/mapper/MetricTagDMapper.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.MetricTagD; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface MetricTagDMapper extends BaseMapper { +} diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/dao/mapper/MetricVMapper.java b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/mapper/MetricVMapper.java new file mode 100644 index 000000000..2570507e9 --- /dev/null +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/dao/mapper/MetricVMapper.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.MetricV; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface MetricVMapper extends BaseMapper { +} diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTag.java b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTagD.java similarity index 76% rename from griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTag.java rename to griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTagD.java index 34ccdc39a..65a46f128 100644 --- a/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTag.java +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/entity/MetricTagD.java @@ -19,6 +19,10 @@ Licensed to the Apache Software Foundation (ASF) under one 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; import lombok.Builder; import lombok.Data; @@ -33,20 +37,24 @@ Licensed to the Apache Software Foundation (ASF) under one @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = false) -public class MetricTag extends BaseEntity { +@TableName("t_tag_d") +public class MetricTagD extends BaseEntity { /** * An unique identity for a metric tag. */ - private long id; + @TableId(value="mid", type = IdType.ASSIGN_ID) + private Long id; /** * Key name */ + @TableField(value = "name") private String tagKey; /** * The value corresponding to a key */ + @TableField(value = "val") private String tagValue; } 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 01d6c7023..600f37397 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 @@ -47,7 +47,7 @@ public class MetricV extends BaseEntity { * An unique identity for a metric. */ @TableId(value="mid", type = IdType.AUTO) - private long metricId; + private Long metricId; /** * A double number to store metric value. 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/Tags.java index 74d76e1e6..45f20167e 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/Tags.java @@ -17,6 +17,9 @@ package org.apache.griffin.metric.entity; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -33,15 +36,17 @@ @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = false) +@TableName("t_metric_tag") public class Tags extends BaseEntity { /** * Metric entity's identity. */ - private long metricId; + @TableId(value="mid", type = IdType.AUTO) + private Long metricId; /** * All tag properties assigning to a metric entity. */ - private List metricTags; + private List metricTags; } 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 52dd8d8be..f1aedd6ee 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 @@ -3,9 +3,14 @@ import lombok.extern.slf4j.Slf4j; import org.apache.griffin.metric.dao.MetricDDao; import org.apache.griffin.metric.entity.MetricD; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; +import org.springframework.lang.NonNull; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +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.RestController; import java.util.List; @@ -13,16 +18,39 @@ @RestController @Slf4j public class MetricDService { + + private final MetricDDao metricDDao; + + public MetricDService(MetricDDao metricDDao) { + this.metricDDao = metricDDao; + } + @GetMapping(value = "/ping", produces = MediaType.APPLICATION_JSON_VALUE) public String ping(){ return "hello"; } - @Autowired - private MetricDDao metricDDao; - @GetMapping(value = "/allMetricDs", produces = MediaType.APPLICATION_JSON_VALUE) public List allMetricDs(){ return metricDDao.queryAll(); } + + @PutMapping(value = "/metricD", consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE) + public MetricD createMetricD(@RequestBody MetricD metricD){ + int id = metricDDao.addMetricD(metricD); + return metricD; + } + + @PostMapping(value = "/metricD", consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE) + public MetricD updateMetricD(MetricD metricD){ + boolean ret = metricDDao.updateById(metricD); + return ret ? metricD : null; + } + + @DeleteMapping(value = "/metricD/{id}") + public boolean deleteMetricD(@PathVariable @NonNull String id){ + return metricDDao.deleteById(id); + } } 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 new file mode 100644 index 000000000..94270fe08 --- /dev/null +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricTagDService.java @@ -0,0 +1,26 @@ +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/MetricVService.java b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricVService.java new file mode 100644 index 000000000..f7344a2c5 --- /dev/null +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricVService.java @@ -0,0 +1,26 @@ +package org.apache.griffin.metric.service; + +import lombok.extern.slf4j.Slf4j; +import org.apache.griffin.metric.dao.MetricVDao; +import org.apache.griffin.metric.entity.MetricV; +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 MetricVService { + private final MetricVDao metricVDao; + + public MetricVService(MetricVDao metricVDao) { + this.metricVDao = metricVDao; + } + + @PutMapping(value = "/metricV",consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE) + public MetricV createMetricV(@RequestBody MetricV metricV) { + int id = metricVDao.addMetricV(metricV); + return metricV; + } +} diff --git a/griffin-metric/src/main/resources/sql/create_h2.sql b/griffin-metric/src/main/resources/sql/create_h2.sql index a8d0f69e7..be65820b0 100644 --- a/griffin-metric/src/main/resources/sql/create_h2.sql +++ b/griffin-metric/src/main/resources/sql/create_h2.sql @@ -7,9 +7,21 @@ CREATE TABLE t_metric_d ( mtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -CREATE TABLE t_metric_v ( +CREATE TABLE t_tag_d ( mid BIGINT PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + val DOUBLE NOT NULL +); + +CREATE TABLE t_metric_tag ( + tid BIGINT PRIMARY KEY references t_metric_d(mid), + mtid BIGINT references t_tag_d(mid) +); + +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 5bf091b38..74de5ecac 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 @@ -103,10 +103,10 @@ public void testFetchMetricDWithTags() { assertEquals(2, fetchedTags.getMetricTags().size()); } - private List createSampleTags() { - List tags = new ArrayList<>(); - tags.add(new MetricTag(1L, "key1", "value1")); - tags.add(new MetricTag(2L, "key2", "value2")); + private List createSampleTags() { + List tags = new ArrayList<>(); + tags.add(new MetricTagD(1L, "key1", "value1")); + tags.add(new MetricTagD(2L, "key2", "value2")); return tags; } }