dnet-hadoop/dhp-common/src/main/java/eu/dnetlib/dhp/monitor/model/Metric.java

153 lines
3.1 KiB
Java

package eu.dnetlib.dhp.monitor.model;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.influxdb.client.write.Point;
import java.time.Instant;
/**
* The base Metric class.
* It contains the common attribute of the DHP Metrics like
* - name
* - isoDate
* - Total value of the metric that is a numeric value
*
* @param <T> the type parameter
*/
public abstract class Metric<T extends Number> {
// The name of the metric
private String name;
// The sting date in ISO format
private String isoDate;
private Date convertedDate;
//Total value of the metric
private T total;
/**
* Instantiates a new Metric.
*/
public Metric() {
}
/**
* Instantiates a new Metric.
*
* @param name the name
* @param isoDate the iso date
* @param total the total
* @throws MonitorException the monitor exception
*/
public Metric(final String name, final String isoDate, final T total) throws MonitorException {
this.name = name;
setIsoDate(isoDate);
this.total = total;
}
/**
* Gets iso date.
*
* @return the iso date
*/
public String getIsoDate() {
return isoDate;
}
private String valiDate(final String aDate) throws MonitorException {
try {
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
convertedDate = df1.parse(aDate);
return df1.format(convertedDate);
} catch (ParseException e) {
throw new MonitorException("Error the date is not in the format yyyy-MM-dd'T'HH:mm:ss ", e);
}
}
/**
* Gets name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets name.
*
* @param name the name
* @return the name
*/
public Metric<T> setName(String name) {
this.name = name;
return this;
}
/**
* Sets iso date.
* <p>
* This method verify that the Date is in ISO FORMAT like yyyy-MM-dd'T'HH:mm:ss
*
* @param isoDate the iso date
* @return the iso date
* @throws MonitorException the monitor exception
*/
public Metric<T> setIsoDate(String isoDate) throws MonitorException {
this.isoDate = valiDate(isoDate);
return this;
}
/**
* This method convert the ISO Date to Instant
* useful for influxDB
*
* @return the isoDate in Instant
*/
public Instant getTimestampDate() {
return convertedDate.toInstant();
}
/**
* Gets total.
*
* @return the total
*/
public T getTotal() {
return total;
}
/**
* Sets total.
*
* @param total the total
* @return the total
*/
public Metric<T> setTotal(T total) {
this.total = total;
return this;
}
/**
* This method convert the metric data model
* into the corresponding influxDB Point
*
* @return the point
*/
public abstract Point asInfluxDBPoint();
}