Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add service interface for metricD/metricV/metricTag tables. #667

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion griffin-metric/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ under the License.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
<!-- <scope>test</scope>-->
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,37 @@
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
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.TagAttachment;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TagsMapper extends BaseMapper<TagAttachment> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Licensed to the Apache Software Foundation (ASF) under one
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;
Expand All @@ -28,19 +30,22 @@ Licensed to the Apache Software Foundation (ASF) under one
* 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;

/**
* creation time
*/
@JsonProperty(value = "creation_time")
@TableField(value = "ctime")
protected Date ctime;

/**
* update time
*/
@JsonProperty(value = "update_time")
@TableField(value = "mtime")
protected Date mtime;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ 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 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.
*/
Expand All @@ -38,6 +38,7 @@ Licensed to the Apache Software Foundation (ASF) under one
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
@TableName("t_metric_d")
public class MetricD extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,4 @@ public class MetricV extends BaseEntity {
*/
@TableField(value = "val")
private double value;

/**
* The tag property assigned.
*/
private Tags tags;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,8 +27,6 @@
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* A common tag entity represents the relationships among metric entities and metric tag entities.
*/
Expand All @@ -37,16 +36,17 @@
@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;
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading