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

View file

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

View file

@ -1,9 +1,12 @@
package com.alexanderlogistics.metrics; package com.alexanderlogistics.metrics;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import org.imixs.workflow.ItemCollection; import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.engine.DocumentService; 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.DeclareRoles;
import jakarta.annotation.security.RunAs; import jakarta.annotation.security.RunAs;
@ -48,6 +51,26 @@ public class MetricDataService {
return dirtyInvoice; 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 * Builds the metric hash key by the invoice attributes. The returned key can be
* used for caching the metric. * used for caching the metric.

View file

@ -28,6 +28,9 @@ public class MetricDebitorRestService {
@Inject @Inject
MetricDebitorService metricService; MetricDebitorService metricService;
@Inject
MetricDataService metricDataService;
@GET @GET
@Path("/ping") @Path("/ping")
@Produces({ MediaType.TEXT_PLAIN }) @Produces({ MediaType.TEXT_PLAIN })
@ -79,7 +82,14 @@ public class MetricDebitorRestService {
// ItemCollection>(); // ItemCollection>();
long l = System.currentTimeMillis(); long l = System.currentTimeMillis();
logger.info("├── init dbtr metrics..."); logger.info("├── init dbtr metrics...");
try { try {
// first clear the metric cache
metricService.reset();
logger.info("│   ├── reset metric cache");
metricDataService.deleteAllMetrics(MetricDebitorService.TYPE_METRIC_DEBITOR);
logger.info("│   ├── delete metrics");
groupInvoicesByDebitor(); groupInvoicesByDebitor();
logger.info("│   ├── grouping invoices finished in " + (System.currentTimeMillis() - l) + "ms"); 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") @ConfigProperty(name = "metrics.enabled", defaultValue = "false")
private boolean metricsEnabled; 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.... * Process Metric only if some data has changed....
* *
@ -97,13 +105,14 @@ public class MetricDebitorService {
try { try {
// update metric and the metric cache // update metric and the metric cache
if (processingEvent.getEventType() == ProcessingEvent.AFTER_PROCESS) { if (processingEvent.getEventType() == ProcessingEvent.AFTER_PROCESS) {
// load the last invoice metric and reduce the saldo... // load the last invoice metric and reduce the saldo...
ItemCollection lastInvoice = metricDataService.readDirtyWorkitem(invoice.getUniqueID()); ItemCollection lastInvoice = metricDataService.readDirtyWorkitem(invoice.getUniqueID());
ItemCollection lastMetricData = getMetricByInvoice(lastInvoice); if (lastInvoice != null) {
subtractInvoice(lastMetricData, lastInvoice); ItemCollection lastMetricData = getMetricByInvoice(lastInvoice);
putMetric(lastMetricData); subtractInvoice(lastMetricData, lastInvoice);
metricDataService.saveMetric(lastMetricData); putMetric(lastMetricData);
metricDataService.saveMetric(lastMetricData);
}
// load the invoice metric and add the saldo... // load the invoice metric and add the saldo...
ItemCollection metricData = getMetricByInvoice(invoice); ItemCollection metricData = getMetricByInvoice(invoice);
@ -132,6 +141,9 @@ public class MetricDebitorService {
* @throws PluginException * @throws PluginException
*/ */
public ItemCollection getMetricByInvoice(ItemCollection invoice) throws PluginException { public ItemCollection getMetricByInvoice(ItemCollection invoice) throws PluginException {
if (invoice == null) {
return null;
}
String metricKey = MetricDataService.buildKeyByInvoice(invoice); String metricKey = MetricDataService.buildKeyByInvoice(invoice);
ItemCollection metricData = metricCache.get(metricKey); ItemCollection metricData = metricCache.get(metricKey);
if (metricData == null) { if (metricData == null) {
@ -183,6 +195,9 @@ public class MetricDebitorService {
* @throws PluginException * @throws PluginException
*/ */
private ItemCollection loadMetric(ItemCollection invoice) throws PluginException { private ItemCollection loadMetric(ItemCollection invoice) throws PluginException {
if (invoice == null) {
return null;
}
ItemCollection debitorMetric = null; ItemCollection debitorMetric = null;
try { try {
String metricKey = MetricDataService.buildKeyByInvoice(invoice); String metricKey = MetricDataService.buildKeyByInvoice(invoice);
@ -208,6 +223,9 @@ public class MetricDebitorService {
* @return * @return
*/ */
private ItemCollection createMetaData(ItemCollection invoice) { private ItemCollection createMetaData(ItemCollection invoice) {
if (invoice == null) {
return null;
}
String key = MetricDataService.buildKeyByInvoice(invoice); String key = MetricDataService.buildKeyByInvoice(invoice);
ItemCollection metricData = new ItemCollection(); ItemCollection metricData = new ItemCollection();
metricData.setType(TYPE_METRIC_DEBITOR); metricData.setType(TYPE_METRIC_DEBITOR);
@ -291,6 +309,9 @@ public class MetricDebitorService {
} }
public void subtractInvoice(ItemCollection metricData, ItemCollection invoice) { public void subtractInvoice(ItemCollection metricData, ItemCollection invoice) {
if (metricData == null || invoice == null) {
return;
}
double invoiceTotal = invoice.getItemValueDouble(ITEM_SALDO); double invoiceTotal = invoice.getItemValueDouble(ITEM_SALDO);
if (!"workitem".equals(invoice.getType()) || invoice.getTaskID() >= 5800) { if (!"workitem".equals(invoice.getType()) || invoice.getTaskID() >= 5800) {
// vorgang ist archiviert oder gelöscht worden => saldo = 0! // vorgang ist archiviert oder gelöscht worden => saldo = 0!