Skip to content

Commit

Permalink
add service interface for metricD table.
Browse files Browse the repository at this point in the history
  • Loading branch information
toyboxman committed Sep 17, 2024
1 parent 3973c4f commit 66fb54b
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 19 deletions.
2 changes: 1 addition & 1 deletion griffin-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ under the License.
<slf4j.version>1.7.36</slf4j.version>
<logback.version>1.2.11</logback.version>
<h2.version>2.2.220</h2.version>
<mysql-connector.version>8.0.16</mysql-connector.version>
<mysql-connector.version>8.0.28</mysql-connector.version>
<commons-io.version>2.11.0</commons-io.version>
<presto-jdbc.version>0.238.1</presto-jdbc.version>
<joda-time.version>2.10.13</joda-time.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ public interface IDao<E> {

/**
* 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<E> queryOptionalById(@NonNull Serializable id);

List<E> queryByIds(Collection<? extends Serializable> ids);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MetricTagD, MetricTagDMapper>{

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;
}
}
Original file line number Diff line number Diff line change
@@ -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<MetricV, MetricVMapper>{
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;
}
}
Original file line number Diff line number Diff line change
@@ -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<MetricTagD> {
}
Original file line number Diff line number Diff line change
@@ -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<MetricV> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<MetricTag> metricTags;
private List<MetricTagD> metricTags;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,54 @@
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;

@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<MetricD> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 13 additions & 1 deletion griffin-metric/src/main/resources/sql/create_h2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ public void testFetchMetricDWithTags() {
assertEquals(2, fetchedTags.getMetricTags().size());
}

private List<MetricTag> createSampleTags() {
List<MetricTag> tags = new ArrayList<>();
tags.add(new MetricTag(1L, "key1", "value1"));
tags.add(new MetricTag(2L, "key2", "value2"));
private List<MetricTagD> createSampleTags() {
List<MetricTagD> tags = new ArrayList<>();
tags.add(new MetricTagD(1L, "key1", "value1"));
tags.add(new MetricTagD(2L, "key2", "value2"));
return tags;
}
}
Expand Down

0 comments on commit 66fb54b

Please sign in to comment.