223 lines
6.9 KiB
Java
223 lines
6.9 KiB
Java
package eu.dnetlib.dhp.monitor.model;
|
|
|
|
import com.influxdb.client.domain.WritePrecision;
|
|
import com.influxdb.client.write.Point;
|
|
import eu.dnetlib.dhp.monitor.MonitorConstant;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* The type Aggregation metric
|
|
* models the metric about the aggregation statistics
|
|
* like collection or transformation,
|
|
* giving the stats
|
|
*/
|
|
public class AggregationMetric extends Metric <Long> {
|
|
|
|
private List<MetricLabel> labels;
|
|
|
|
|
|
/**
|
|
* Instantiates a new Aggregation metric.
|
|
*/
|
|
public AggregationMetric() {
|
|
super();
|
|
}
|
|
|
|
@Override
|
|
public Point asInfluxDBPoint() {
|
|
final Point point = Point.measurement(getName())
|
|
.time(getTimestampDate(), WritePrecision.MS)
|
|
.addField("total", getTotal());
|
|
if (labels!= null && labels.size()>0)
|
|
labels.forEach(l -> point.addTag(l.getName(), l.getValue()));
|
|
return point;
|
|
}
|
|
|
|
/**
|
|
* Instantiates a new Aggregation metric.
|
|
*
|
|
* @param name the name
|
|
* @param isoDate the iso date
|
|
* @param value the value
|
|
* @param labels the labels
|
|
* @throws MonitorException the monitor exception
|
|
*/
|
|
public AggregationMetric(final String name, final String isoDate, final Long value, final List<MetricLabel> labels) throws MonitorException {
|
|
super(name, isoDate, value);
|
|
this.labels = labels;
|
|
}
|
|
|
|
/**
|
|
* Gets labels.
|
|
*
|
|
* @return the labels
|
|
*/
|
|
public List<MetricLabel> getLabels() {
|
|
return labels;
|
|
}
|
|
|
|
/**
|
|
* Sets labels.
|
|
*
|
|
* @param labels the labels
|
|
* @return the labels
|
|
*/
|
|
public AggregationMetric setLabels(List<MetricLabel> labels) {
|
|
this.labels = labels;
|
|
return this;
|
|
}
|
|
|
|
|
|
/**
|
|
* The Aggregation builder is a builder class utility to create Aggregation metrics.
|
|
*/
|
|
public static class AggregationBuilder {
|
|
|
|
private MonitorConstant.AggregationType aggregationType;
|
|
|
|
private MonitorConstant.AggregationMode aggregationMode;
|
|
|
|
private String datasourceId;
|
|
|
|
private String datasourceName;
|
|
|
|
private String api;
|
|
|
|
|
|
/**
|
|
* Creates aggregation builder already set the metric type of aggregation collection.
|
|
*
|
|
* @return the aggregation builder
|
|
*/
|
|
public AggregationBuilder createCollectionMetrics() {
|
|
this.aggregationType = MonitorConstant.AggregationType.COLLECTION;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Creates aggregation builder already set the metric type of aggregation transformation.
|
|
*
|
|
* @return the aggregation builder
|
|
*/
|
|
public AggregationBuilder createTransformationMetrics() {
|
|
this.aggregationType = MonitorConstant.AggregationType.TRANSFORMATION;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Creates aggregation builder already set the aggregation mode REFRESH.
|
|
*
|
|
* @return the aggregation builder
|
|
*/
|
|
public AggregationBuilder withRefreshMode(){
|
|
this.aggregationMode = MonitorConstant.AggregationMode.REFRESH;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Creates aggregation builder already set the aggregation mode INCREMENTAL.
|
|
*
|
|
* @return the aggregation builder
|
|
*/
|
|
public AggregationBuilder withIncrementalMode(){
|
|
this.aggregationMode = MonitorConstant.AggregationMode.INCREMENTAL;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Set the datasource Identifier to the aggregation builder
|
|
*
|
|
* @param datasourceId the datasource id
|
|
* @return the aggregation builder
|
|
*/
|
|
public AggregationBuilder withDatasourceId(final String datasourceId) {
|
|
this.datasourceId = datasourceId;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Set the datasource Name to the aggregation builder
|
|
*
|
|
* @param datasourceName the datasource name
|
|
* @return the aggregation builder
|
|
*/
|
|
public AggregationBuilder withDatasourceName(final String datasourceName) {
|
|
this.datasourceName = datasourceName;
|
|
return this;
|
|
}
|
|
|
|
|
|
/**
|
|
* Set the datasource API Identifier to the aggregation builder
|
|
*
|
|
* @param api the api
|
|
* @return the aggregation builder
|
|
*/
|
|
public AggregationBuilder withDatasourceAPI(final String api) {
|
|
this.api = api;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Build aggregation metric.
|
|
*
|
|
* Here the method check if all the fields are already set, otherwise it
|
|
* raises a Monitor Exception
|
|
*
|
|
* @param isoDate the iso date
|
|
* @param total the total
|
|
* @return the aggregation metric
|
|
* @throws MonitorException the monitor exception
|
|
*/
|
|
public AggregationMetric build(final String isoDate, final Long total) throws MonitorException {
|
|
|
|
final AggregationMetric metric= new AggregationMetric();
|
|
|
|
if(aggregationType ==null)
|
|
throw new MonitorException("Aggregation type needed please instantiate builder calling createCollectionMetrics or createTransformationMetrics");
|
|
|
|
switch(aggregationType) {
|
|
case COLLECTION:
|
|
metric.setName(MonitorConstant.AGGREGATION_COLLECTION_METRIC);
|
|
break;
|
|
case TRANSFORMATION:
|
|
metric.setName(MonitorConstant.AGGREGATION_TRANSFORM_METRIC);
|
|
break;
|
|
}
|
|
|
|
if(aggregationMode ==null)
|
|
throw new MonitorException("Aggregation mode needed please instantiate builder calling withRefreshMode or withIncrementalMode");
|
|
|
|
final List<MetricLabel> labels = new ArrayList<>();
|
|
metric.setLabels(labels);
|
|
|
|
labels.add(new MetricLabel(MonitorConstant.AGGREGATION_MODE_LABEL, aggregationMode.toString()));
|
|
|
|
if(StringUtils.isBlank(datasourceName))
|
|
throw new MonitorException("Datasource name should be not blank please set using 'withDatasourceName' function ");
|
|
labels.add(new MetricLabel(MonitorConstant.DATASOURCE_NAME_LABEL_NAME, datasourceName));
|
|
|
|
if(StringUtils.isBlank(datasourceId))
|
|
throw new MonitorException("Datasource Identifier should be not blank please set using 'withDatasourceId' function ");
|
|
labels.add(new MetricLabel(MonitorConstant.DATASOURCE_ID_LABEL_NAME, datasourceId));
|
|
|
|
if(StringUtils.isBlank(api))
|
|
throw new MonitorException("Datasource API should be not blank please set using 'withDatasourceIAPI' function ");
|
|
labels.add(new MetricLabel(MonitorConstant.DATASOURCE_API_LABEL_NAME, api));
|
|
|
|
metric.setIsoDate(isoDate);
|
|
metric.setTotal(total);
|
|
return metric;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|