hmci/src/main/java/biz/nellemann/hmci/Resource.java

137 lines
4.5 KiB
Java
Raw Normal View History

package biz.nellemann.hmci;
import biz.nellemann.hmci.dto.json.ProcessedMetrics;
import biz.nellemann.hmci.dto.json.SystemUtil;
2022-12-05 14:18:42 +00:00
import biz.nellemann.hmci.dto.json.UtilSample;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
2022-12-05 14:18:42 +00:00
import java.util.ArrayList;
2022-12-05 14:18:42 +00:00
public abstract class Resource {
private final static Logger log = LoggerFactory.getLogger(Resource.class);
private final ObjectMapper objectMapper = new ObjectMapper();
2022-12-05 14:18:42 +00:00
private final ArrayList<String> sampleHistory = new ArrayList<>();
protected SystemUtil metric;
2022-12-07 15:12:29 +00:00
protected final int maxNumberOfSamples = 60;
2022-12-05 14:18:42 +00:00
protected final int minNumberOfSamples = 5;
2022-12-07 15:12:29 +00:00
protected int noOfSamples = maxNumberOfSamples;
2022-12-05 14:18:42 +00:00
Resource() {
objectMapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
}
void deserialize(String json) {
if(json == null || json.length() < 1) {
return;
}
try {
ProcessedMetrics processedMetrics = objectMapper.readValue(json, ProcessedMetrics.class);
metric = processedMetrics.systemUtil;
2022-12-05 14:18:42 +00:00
log.trace("deserialize() - samples: {}", metric.samples.size());
} catch (Exception e) {
log.error("deserialize() - error: {}", e.getMessage());
}
}
Instant getTimestamp() {
Instant instant = Instant.now();
if (metric == null) {
return instant;
}
String timestamp = metric.getSample().sampleInfo.timestamp;
try {
log.trace("getTimeStamp() - PMC Timestamp: {}", timestamp);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[XXX][X]");
instant = Instant.from(dateTimeFormatter.parse(timestamp));
log.trace("getTimestamp() - Instant: {}", instant.toString());
} catch(DateTimeParseException e) {
log.warn("getTimestamp() - parse error: {}", timestamp);
}
return instant;
}
2022-12-05 14:18:42 +00:00
Instant getTimestamp(int sampleNumber) {
Instant instant = Instant.now();
if (metric == null) {
return instant;
}
String timestamp = metric.getSample(sampleNumber).sampleInfo.timestamp;
try {
log.trace("getTimeStamp() - PMC Timestamp: {}", timestamp);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[XXX][X]");
instant = Instant.from(dateTimeFormatter.parse(timestamp));
log.trace("getTimestamp() - Instant: {}", instant.toString());
} catch(DateTimeParseException e) {
log.warn("getTimestamp() - parse error: {}", timestamp);
}
return instant;
}
public void process() {
if(metric == null) {
return;
}
2022-12-07 15:12:29 +00:00
int processed = 0;
int sampleSize = metric.samples.size();
log.debug("process() - Samples Returned: {}, Samples in History: {}, Fetch Next Counter: {}", sampleSize, sampleHistory.size(), noOfSamples);
for(int i = 0; i<sampleSize; i++) {
2022-12-05 14:18:42 +00:00
UtilSample sample = metric.getSample(i);
String timestamp = sample.getInfo().timestamp;
if(sampleHistory.contains(timestamp)) {
//log.info("process() - Sample \"{}\" already processed", timestamp);
continue; // Already processed
}
2022-12-07 15:12:29 +00:00
try {
process(i);
processed++;
sampleHistory.add(timestamp); // Add to processed history
} catch (NullPointerException e) {
2023-01-05 13:28:12 +00:00
log.warn("process() - error", e);
2022-12-07 15:12:29 +00:00
}
2022-12-05 14:18:42 +00:00
}
// Remove old elements from history
2022-12-07 15:12:29 +00:00
for(int n = noOfSamples; n < sampleHistory.size(); n++) {
2022-12-05 14:18:42 +00:00
//log.info("process() - Removing element no. {} from sampleHistory: {}", n, sampleHistory.get(0));
sampleHistory.remove(0);
}
// Decrease down to minSamples
2022-12-07 15:12:29 +00:00
if(noOfSamples > minNumberOfSamples) {
noOfSamples = Math.min( (noOfSamples - 1), Math.max( (noOfSamples - processed) + 5, minNumberOfSamples));
2022-12-05 14:18:42 +00:00
}
}
2022-12-07 15:12:29 +00:00
public abstract void process(int sample) throws NullPointerException;
2022-12-05 14:18:42 +00:00
}