fix metric service

This commit is contained in:
Ralph Soika 2025-01-23 23:57:16 +01:00
parent 8fad186e39
commit 8db4320348
5 changed files with 101 additions and 13 deletions

View file

@ -28,6 +28,9 @@ public class MetricCreditorRestService {
@Inject
MetricCreditorService metricService;
@Inject
MetricDataService metricDataService;
@GET
@Path("/ping")
@Produces({ MediaType.TEXT_PLAIN })
@ -39,6 +42,8 @@ public class MetricCreditorRestService {
/**
* This method refreshes all creditor metrics by iterating through the metric
* entities.
* This will refresh the metrics view in Wildfly only and not computing the
* metrics itself.
*
* @return
*/
@ -65,7 +70,8 @@ public class MetricCreditorRestService {
/**
* This method initializes the metrics for all creditors with open invoices.
* The method creates or updates the metric entires for each creditor.
* The method deletes all existing metrics and creates new metric entires for
* each creditor.
*
* @return
*/
@ -73,11 +79,17 @@ public class MetricCreditorRestService {
@Path("/init")
@Produces({ MediaType.TEXT_PLAIN })
public Response initMetrics() {
// Map<String, ItemCollection> metricCache = new HashMap<String,
// ItemCollection>();
long l = System.currentTimeMillis();
logger.info("├── init cdtr metrics...");
try {
// first clear the metric cache
metricService.reset();
logger.info("│   ├── reset metric cache");
metricDataService.deleteAllMetrics(MetricCreditorService.TYPE_METRIC_CREDITOR);
logger.info("│   ├── delete metrics");
groupInvoicesByCreditor();
logger.info("│   ├── grouping invoices finished in " + (System.currentTimeMillis() - l) + "ms");
@ -118,7 +130,7 @@ public class MetricCreditorRestService {
*
*/
private void groupInvoicesByCreditor() {
logger.info("│   │   ├── group invoices by creditor...");
logger.info("│   ├── group invoices by creditor...");
try {
List<ItemCollection> invoices = documentService.find(
"($modelversion:rechnungseingang-* OR $modelversion:gutschriftabgleich-*) " +

View file

@ -78,6 +78,14 @@ public class MetricCreditorService {
@ConfigProperty(name = "metrics.enabled", defaultValue = "false")
private boolean metricsEnabled;
/**
* Reset internal metricCache and clear registered Gauges.
*/
public void reset() {
metricCache.clear();
registeredGauges.clear();
}
/**
* Process Metric only if some data has changed....
*
@ -110,10 +118,12 @@ public class MetricCreditorService {
// load the last invoice metric and reduce the saldo...
ItemCollection lastInvoice = metricDataService.readDirtyWorkitem(invoice.getUniqueID());
ItemCollection lastMetricData = getMetricByInvoice(lastInvoice);
subtractInvoice(lastMetricData, lastInvoice);
putMetric(lastMetricData);
metricDataService.saveMetric(lastMetricData);
if (lastInvoice != null) {
ItemCollection lastMetricData = getMetricByInvoice(lastInvoice);
subtractInvoice(lastMetricData, lastInvoice);
putMetric(lastMetricData);
metricDataService.saveMetric(lastMetricData);
}
// load the invoice metric and add the saldo...
ItemCollection metricData = getMetricByInvoice(invoice);
@ -142,6 +152,9 @@ public class MetricCreditorService {
* @throws PluginException
*/
public ItemCollection getMetricByInvoice(ItemCollection invoice) throws PluginException {
if (invoice == null) {
return null;
}
String metricKey = MetricDataService.buildKeyByInvoice(invoice);
ItemCollection metricData = metricCache.get(metricKey);
if (metricData == null) {
@ -194,6 +207,9 @@ public class MetricCreditorService {
*/
private ItemCollection loadMetric(ItemCollection invoice) throws PluginException {
ItemCollection creditorMetric = null;
if (invoice == null) {
return null;
}
try {
String metricKey = MetricDataService.buildKeyByInvoice(invoice);
String query = "(type:" + TYPE_METRIC_CREDITOR + ") AND (name:" + metricKey + ")";
@ -219,6 +235,9 @@ public class MetricCreditorService {
* @return
*/
private ItemCollection createMetaData(ItemCollection invoice) {
if (invoice == null) {
return null;
}
String key = MetricDataService.buildKeyByInvoice(invoice);
ItemCollection metricData = new ItemCollection();
metricData.setType(TYPE_METRIC_CREDITOR);
@ -301,6 +320,9 @@ public class MetricCreditorService {
}
public void subtractInvoice(ItemCollection metricData, ItemCollection invoice) {
if (metricData == null || invoice == null) {
return;
}
double invoiceTotal = invoice.getItemValueDouble(ITEM_TOTAL);
if (!"workitem".equals(invoice.getType()) || invoice.getTaskID() >= 5800) {
// vorgang ist archiviert oder gelöscht worden => saldo = 0!

View file

@ -1,9 +1,12 @@
package com.alexanderlogistics.metrics;
import java.util.List;
import java.util.Objects;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.exceptions.PluginException;
import org.imixs.workflow.exceptions.QueryException;
import jakarta.annotation.security.DeclareRoles;
import jakarta.annotation.security.RunAs;
@ -48,6 +51,26 @@ public class MetricDataService {
return dirtyInvoice;
}
/**
* This method deletes all metrics
*
* @throws PluginException
*
*/
public void deleteAllMetrics(String metricType) throws PluginException {
try {
String query = "(type:" + metricType + ")";
List<ItemCollection> result = documentService.find(query, -1, 0);
for (ItemCollection metric : result) {
documentService.remove(metric);
}
} catch (IllegalArgumentException | QueryException e) {
throw new PluginException(PluginException.class.getName(),
"Failed to delete metrics", e.getMessage(), e);
}
}
/**
* Builds the metric hash key by the invoice attributes. The returned key can be
* used for caching the metric.

View file

@ -28,6 +28,9 @@ public class MetricDebitorRestService {
@Inject
MetricDebitorService metricService;
@Inject
MetricDataService metricDataService;
@GET
@Path("/ping")
@Produces({ MediaType.TEXT_PLAIN })
@ -79,7 +82,14 @@ public class MetricDebitorRestService {
// ItemCollection>();
long l = System.currentTimeMillis();
logger.info("├── init dbtr metrics...");
try {
// first clear the metric cache
metricService.reset();
logger.info("│   ├── reset metric cache");
metricDataService.deleteAllMetrics(MetricDebitorService.TYPE_METRIC_DEBITOR);
logger.info("│   ├── delete metrics");
groupInvoicesByDebitor();
logger.info("│   ├── grouping invoices finished in " + (System.currentTimeMillis() - l) + "ms");

View file

@ -70,6 +70,14 @@ public class MetricDebitorService {
@ConfigProperty(name = "metrics.enabled", defaultValue = "false")
private boolean metricsEnabled;
/**
* Reset internal metricCache and clear registered Gauges.
*/
public void reset() {
metricCache.clear();
registeredGauges.clear();
}
/**
* Process Metric only if some data has changed....
*
@ -97,13 +105,14 @@ public class MetricDebitorService {
try {
// update metric and the metric cache
if (processingEvent.getEventType() == ProcessingEvent.AFTER_PROCESS) {
// load the last invoice metric and reduce the saldo...
ItemCollection lastInvoice = metricDataService.readDirtyWorkitem(invoice.getUniqueID());
ItemCollection lastMetricData = getMetricByInvoice(lastInvoice);
subtractInvoice(lastMetricData, lastInvoice);
putMetric(lastMetricData);
metricDataService.saveMetric(lastMetricData);
if (lastInvoice != null) {
ItemCollection lastMetricData = getMetricByInvoice(lastInvoice);
subtractInvoice(lastMetricData, lastInvoice);
putMetric(lastMetricData);
metricDataService.saveMetric(lastMetricData);
}
// load the invoice metric and add the saldo...
ItemCollection metricData = getMetricByInvoice(invoice);
@ -132,6 +141,9 @@ public class MetricDebitorService {
* @throws PluginException
*/
public ItemCollection getMetricByInvoice(ItemCollection invoice) throws PluginException {
if (invoice == null) {
return null;
}
String metricKey = MetricDataService.buildKeyByInvoice(invoice);
ItemCollection metricData = metricCache.get(metricKey);
if (metricData == null) {
@ -183,6 +195,9 @@ public class MetricDebitorService {
* @throws PluginException
*/
private ItemCollection loadMetric(ItemCollection invoice) throws PluginException {
if (invoice == null) {
return null;
}
ItemCollection debitorMetric = null;
try {
String metricKey = MetricDataService.buildKeyByInvoice(invoice);
@ -208,6 +223,9 @@ public class MetricDebitorService {
* @return
*/
private ItemCollection createMetaData(ItemCollection invoice) {
if (invoice == null) {
return null;
}
String key = MetricDataService.buildKeyByInvoice(invoice);
ItemCollection metricData = new ItemCollection();
metricData.setType(TYPE_METRIC_DEBITOR);
@ -291,6 +309,9 @@ public class MetricDebitorService {
}
public void subtractInvoice(ItemCollection metricData, ItemCollection invoice) {
if (metricData == null || invoice == null) {
return;
}
double invoiceTotal = invoice.getItemValueDouble(ITEM_SALDO);
if (!"workitem".equals(invoice.getType()) || invoice.getTaskID() >= 5800) {
// vorgang ist archiviert oder gelöscht worden => saldo = 0!