refactoring metric api
This commit is contained in:
parent
a715e00a1d
commit
3b5bb12c32
7 changed files with 1811 additions and 68 deletions
|
|
@ -38,6 +38,18 @@ public class InvoiceUtil {
|
||||||
return bd.doubleValue();
|
return bd.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rounds a Flat Value
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static float roundToFloat(double value) {
|
||||||
|
BigDecimal bd = BigDecimal.valueOf(value);
|
||||||
|
bd = bd.setScale(2, RoundingMode.HALF_UP);
|
||||||
|
return bd.floatValue();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hilfsmethode zum validieren der Mailadresse
|
* Hilfsmethode zum validieren der Mailadresse
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -173,7 +173,7 @@ public class OPListController implements Serializable {
|
||||||
float rate = invoice.getItemValueFloat("invoice.rate");
|
float rate = invoice.getItemValueFloat("invoice.rate");
|
||||||
float maxSaldo = invoice.getItemValueFloat("invoice.saldo");
|
float maxSaldo = invoice.getItemValueFloat("invoice.saldo");
|
||||||
if (rate != 0) {
|
if (rate != 0) {
|
||||||
maxSaldo = maxSaldo / rate;
|
maxSaldo = InvoiceUtil.roundToFloat(maxSaldo / rate);
|
||||||
invoice.setItemValue("invoice.saldo.byrate", maxSaldo);
|
invoice.setItemValue("invoice.saldo.byrate", maxSaldo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -160,7 +160,7 @@ public class ZahlungseingangSaldoAdapter implements SignalAdapter {
|
||||||
invoiceSaldo = invoiceSaldo - invoicePaymentAmount;
|
invoiceSaldo = invoiceSaldo - invoicePaymentAmount;
|
||||||
}
|
}
|
||||||
invoiceSaldo = (float) InvoiceUtil.round(invoiceSaldo);
|
invoiceSaldo = (float) InvoiceUtil.round(invoiceSaldo);
|
||||||
invoice.setItemValue("invoice.saldo", invoiceSaldo);
|
invoice.setItemValue("invoice.saldo", InvoiceUtil.roundToFloat(invoiceSaldo));
|
||||||
invoice.event(ZahlungseingangService.EVENT_UPDATE_INVOICE);
|
invoice.event(ZahlungseingangService.EVENT_UPDATE_INVOICE);
|
||||||
zahlungseingangService.processInvoice(invoice);
|
zahlungseingangService.processInvoice(invoice);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -621,8 +621,8 @@ public class CargosoftMigrationRestService implements Serializable {
|
||||||
total = total + tax;
|
total = total + tax;
|
||||||
saldo = saldo + tax;
|
saldo = saldo + tax;
|
||||||
}
|
}
|
||||||
workitem.setItemValue("invoice.total", total);
|
workitem.setItemValue("invoice.total", InvoiceUtil.round(total));
|
||||||
workitem.setItemValue("invoice.saldo", saldo);
|
workitem.setItemValue("invoice.saldo", InvoiceUtil.round(saldo));
|
||||||
workitem.setItemValue("cargosoft.fix.xmlerror", tax);
|
workitem.setItemValue("cargosoft.fix.xmlerror", tax);
|
||||||
log = log + "....... total neu=" + total + " saldo neu=" + saldo + "\n";
|
log = log + "....... total neu=" + total + " saldo neu=" + saldo + "\n";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
package com.alexanderlogistics;
|
package com.alexanderlogistics.metrics;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -9,11 +7,12 @@ import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.imixs.marty.team.TeamService;
|
import org.imixs.marty.team.TeamService;
|
||||||
import org.imixs.workflow.ItemCollection;
|
import org.imixs.workflow.ItemCollection;
|
||||||
import org.imixs.workflow.ItemCollectionComparator;
|
|
||||||
import org.imixs.workflow.engine.DocumentService;
|
import org.imixs.workflow.engine.DocumentService;
|
||||||
import org.imixs.workflow.exceptions.PluginException;
|
import org.imixs.workflow.exceptions.PluginException;
|
||||||
import org.imixs.workflow.exceptions.QueryException;
|
import org.imixs.workflow.exceptions.QueryException;
|
||||||
|
|
||||||
|
import com.alexanderlogistics.InvoiceUtil;
|
||||||
|
|
||||||
import jakarta.ejb.Stateless;
|
import jakarta.ejb.Stateless;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import jakarta.ws.rs.GET;
|
import jakarta.ws.rs.GET;
|
||||||
|
|
@ -88,22 +87,15 @@ public class MetricDebitorRestService {
|
||||||
long l = System.currentTimeMillis();
|
long l = System.currentTimeMillis();
|
||||||
logger.info("├── init metrics...");
|
logger.info("├── init metrics...");
|
||||||
try {
|
try {
|
||||||
// compute all departments
|
groupInvoicesByDebitor(metricCache);
|
||||||
List<String> spaceNames = new ArrayList<String>();
|
|
||||||
List<ItemCollection> spaces = teamService.getSpaces();
|
|
||||||
// sort by space.name
|
|
||||||
Collections.sort(spaces, new ItemCollectionComparator("space.name", true));
|
|
||||||
for (ItemCollection space : spaces) {
|
|
||||||
String spaceName = space.getItemValueString("space.name");
|
|
||||||
spaceNames.add(spaceName);
|
|
||||||
logger.info("│ ├── grouping invoices by debitor...");
|
|
||||||
groupInvoicesByWeek(space.getUniqueID(), spaceName, metricCache);
|
|
||||||
}
|
|
||||||
logger.info("│ ├── grouping invoices finished in " + (System.currentTimeMillis() - l) + "ms");
|
logger.info("│ ├── grouping invoices finished in " + (System.currentTimeMillis() - l) + "ms");
|
||||||
|
|
||||||
rebuildMetrics(metricCache);
|
rebuildMetrics(metricCache);
|
||||||
logger.info("├── init metrics completed in " + (System.currentTimeMillis() - l) + "ms");
|
String message = "├── init " + metricCache.size() + " metrics completed in "
|
||||||
return Response.ok().entity("init metrics completed in " + (System.currentTimeMillis() - l) + "ms").build();
|
+ (System.currentTimeMillis() - l)
|
||||||
|
+ "ms";
|
||||||
|
logger.info(message);
|
||||||
|
return Response.ok().entity(message).build();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
@ -118,13 +110,8 @@ public class MetricDebitorRestService {
|
||||||
|
|
||||||
for (String dbtrNumber : metricCache.keySet()) {
|
for (String dbtrNumber : metricCache.keySet()) {
|
||||||
ItemCollection metricData = metricCache.get(dbtrNumber);
|
ItemCollection metricData = metricCache.get(dbtrNumber);
|
||||||
// String dbtrName = metricData.getItemValueString("dbtr.name");
|
|
||||||
|
|
||||||
// ItemCollection metric = metricService.loadMetric(dbtrNumber, dbtrName);
|
|
||||||
// metric.setItemValue(MetricDebitorService.ITEM_TOTALS, metricData.totals);
|
|
||||||
documentService.save(metricData);
|
documentService.save(metricData);
|
||||||
metricService.initMetric(metricData);
|
metricService.initMetric(metricData);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,18 +125,17 @@ public class MetricDebitorRestService {
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private void groupInvoicesByWeek(String spaceID, String spaceName,
|
private void groupInvoicesByDebitor(
|
||||||
Map<String, ItemCollection> metricCache) {
|
Map<String, ItemCollection> metricCache) {
|
||||||
|
|
||||||
logger.info("│ │ ├── group invoices for " + spaceName + "/" + spaceID);
|
logger.info("│ │ ├── group invoices by debitor...");
|
||||||
try {
|
try {
|
||||||
List<ItemCollection> invoices = documentService.find(
|
List<ItemCollection> invoices = documentService.find(
|
||||||
"$modelversion:rechnungsausgang-* AND type:workitem AND $uniqueidref:" +
|
"$modelversion:rechnungsausgang-* AND type:workitem",
|
||||||
spaceID,
|
|
||||||
9999, 0,
|
9999, 0,
|
||||||
"invoice.number", false);
|
"invoice.number", false);
|
||||||
|
|
||||||
logger.fine(" found " + invoices.size() + " for space " + spaceName);
|
logger.fine(" found " + invoices.size() + " open invoices");
|
||||||
for (ItemCollection invoice : invoices) {
|
for (ItemCollection invoice : invoices) {
|
||||||
|
|
||||||
String debitorName = invoice.getItemValueString("dbtr.name");
|
String debitorName = invoice.getItemValueString("dbtr.name");
|
||||||
|
|
@ -157,15 +143,26 @@ public class MetricDebitorRestService {
|
||||||
|
|
||||||
ItemCollection metricData = metricCache.get(debitorNumber);
|
ItemCollection metricData = metricCache.get(debitorNumber);
|
||||||
if (metricData == null) {
|
if (metricData == null) {
|
||||||
metricData = metricService.createMetaData(debitorNumber, debitorName);
|
try {
|
||||||
|
metricData = metricService.loadMetric(debitorNumber, debitorName);
|
||||||
|
// reset old metrics now!
|
||||||
|
for (String itemName : metricData.getItemNames()) {
|
||||||
|
if (itemName.startsWith(MetricDebitorService.ITEM_SALDO + ".")) {
|
||||||
|
metricData.setItemValue(itemName, 0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (PluginException e) {
|
||||||
|
logger.severe(
|
||||||
|
"Unable to load metric for invoice " + invoice.getUniqueID() + " : " + e.getMessage());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Jetzt Rechnung addieren
|
// Jetzt Rechnung addieren
|
||||||
addInvoice(metricData, invoice);
|
addInvoice(metricData, invoice);
|
||||||
// invoiceData.add(invoice);
|
// invoiceData.add(invoice);
|
||||||
metricCache.put(debitorNumber, metricData);
|
metricCache.put(debitorNumber, metricData);
|
||||||
}
|
}
|
||||||
|
logger.info("│ │ ├── grouped " + invoices.size() + " invoices.");
|
||||||
} catch (QueryException e) {
|
} catch (QueryException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
@ -180,7 +177,7 @@ public class MetricDebitorRestService {
|
||||||
*/
|
*/
|
||||||
public void addInvoice(ItemCollection metricData, ItemCollection invoice) {
|
public void addInvoice(ItemCollection metricData, ItemCollection invoice) {
|
||||||
String currency = invoice.getItemValueString("invoice.currency");
|
String currency = invoice.getItemValueString("invoice.currency");
|
||||||
double saldo = invoice.getItemValueDouble("invoice.saldo");
|
double saldo = invoice.getItemValueDouble(MetricDebitorService.ITEM_SALDO);
|
||||||
double saldoOld = metricData.getItemValueDouble(MetricDebitorService.ITEM_SALDO + "." + currency);
|
double saldoOld = metricData.getItemValueDouble(MetricDebitorService.ITEM_SALDO + "." + currency);
|
||||||
double saldoNew = InvoiceUtil.round(saldoOld + saldo);
|
double saldoNew = InvoiceUtil.round(saldoOld + saldo);
|
||||||
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
package com.alexanderlogistics;
|
package com.alexanderlogistics.metrics;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
@ -16,6 +17,8 @@ import org.imixs.workflow.engine.ProcessingEvent;
|
||||||
import org.imixs.workflow.exceptions.PluginException;
|
import org.imixs.workflow.exceptions.PluginException;
|
||||||
import org.imixs.workflow.exceptions.QueryException;
|
import org.imixs.workflow.exceptions.QueryException;
|
||||||
|
|
||||||
|
import com.alexanderlogistics.InvoiceUtil;
|
||||||
|
|
||||||
import jakarta.annotation.security.DeclareRoles;
|
import jakarta.annotation.security.DeclareRoles;
|
||||||
import jakarta.annotation.security.RolesAllowed;
|
import jakarta.annotation.security.RolesAllowed;
|
||||||
import jakarta.annotation.security.RunAs;
|
import jakarta.annotation.security.RunAs;
|
||||||
|
|
@ -49,6 +52,7 @@ public class MetricDebitorService {
|
||||||
|
|
||||||
private static Logger logger = Logger.getLogger(MetricDebitorService.class.getName());
|
private static Logger logger = Logger.getLogger(MetricDebitorService.class.getName());
|
||||||
private final ConcurrentHashMap<String, ItemCollection> metricCache = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<String, ItemCollection> metricCache = new ConcurrentHashMap<>();
|
||||||
|
private final Set<String> registeredGauges = ConcurrentHashMap.newKeySet();
|
||||||
|
|
||||||
public static final String TYPE_METRIC_DEBITOR = "metric.debitor";
|
public static final String TYPE_METRIC_DEBITOR = "metric.debitor";
|
||||||
public static final String ITEM_SALDO = "invoice.saldo";
|
public static final String ITEM_SALDO = "invoice.saldo";
|
||||||
|
|
@ -84,6 +88,10 @@ public class MetricDebitorService {
|
||||||
|
|
||||||
// verify if saldo has changed.....
|
// verify if saldo has changed.....
|
||||||
double invoiceSaldo = invoice.getItemValueDouble(ITEM_SALDO);
|
double invoiceSaldo = invoice.getItemValueDouble(ITEM_SALDO);
|
||||||
|
if (!"workitem".equals(invoice.getType())) {
|
||||||
|
// vorgang ist archiviert oder gelöscht worden => saldo = 0!
|
||||||
|
invoiceSaldo = 0.0;
|
||||||
|
}
|
||||||
String dbtrNumber = invoice.getItemValueString("dbtr.number");
|
String dbtrNumber = invoice.getItemValueString("dbtr.number");
|
||||||
String dbtrName = invoice.getItemValueString("dbtr.name");
|
String dbtrName = invoice.getItemValueString("dbtr.name");
|
||||||
String currency = invoice.getItemValueString("invoice.currency");
|
String currency = invoice.getItemValueString("invoice.currency");
|
||||||
|
|
@ -115,7 +123,7 @@ public class MetricDebitorService {
|
||||||
documentService.save(metricData);
|
documentService.save(metricData);
|
||||||
|
|
||||||
// Gauge registrieren
|
// Gauge registrieren
|
||||||
registerGauge(metricData, currency);
|
updateGauge(metricData, currency);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -181,7 +189,7 @@ public class MetricDebitorService {
|
||||||
* @param dbtrName
|
* @param dbtrName
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public ItemCollection createMetaData(String dbtrNumber, String dbtrName) {
|
private ItemCollection createMetaData(String dbtrNumber, String dbtrName) {
|
||||||
ItemCollection metricData = new ItemCollection();
|
ItemCollection metricData = new ItemCollection();
|
||||||
|
|
||||||
metricData = new ItemCollection();
|
metricData = new ItemCollection();
|
||||||
|
|
@ -192,6 +200,38 @@ public class MetricDebitorService {
|
||||||
return metricData;
|
return metricData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to register a gauge for a debitor
|
||||||
|
*
|
||||||
|
* @param dbtrNumber - the debitor number
|
||||||
|
* @param dbtrName - the debitor name
|
||||||
|
*/
|
||||||
|
private void updateGauge(ItemCollection metricData, String currency) {
|
||||||
|
String dbtrNumber = metricData.getItemValueString("dbtr.number");
|
||||||
|
String dbtrName = metricData.getItemValueString("dbtr.name");
|
||||||
|
String metricKey = "dbtr_" + dbtrNumber + "_" + dbtrName + "_" + currency;
|
||||||
|
// Prüfen ob Gauge bereits registriert ist
|
||||||
|
if (registeredGauges.add(metricKey)) { // returns true newly added
|
||||||
|
List<Tag> tags = new ArrayList<>();
|
||||||
|
tags.add(new Tag("type", "dbtr"));
|
||||||
|
tags.add(new Tag("number", dbtrNumber));
|
||||||
|
tags.add(new Tag("name", dbtrName));
|
||||||
|
tags.add(new Tag("currency", currency));
|
||||||
|
logger.info("register new metric for debitor: " + dbtrNumber + " -> " + currency);
|
||||||
|
Metadata metadata = Metadata.builder()
|
||||||
|
.withName("dbtr.invoice.saldo")
|
||||||
|
.withDescription("Debitor Balance by Currency")
|
||||||
|
.build();
|
||||||
|
metricRegistry.gauge(metadata,
|
||||||
|
() -> metricCache.getOrDefault(dbtrNumber, createMetaData(dbtrNumber, dbtrName))
|
||||||
|
.getItemValueDouble(ITEM_SALDO + "." + currency),
|
||||||
|
tags.toArray(new Tag[0]));
|
||||||
|
} else {
|
||||||
|
logger.fine("Gauge already registered for debitor: " + dbtrNumber + " -> " + currency);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hilfsmethode um Metriken initial aufzubauen. Wird vom Metric Rest Service
|
* Hilfsmethode um Metriken initial aufzubauen. Wird vom Metric Rest Service
|
||||||
* verwendet.
|
* verwendet.
|
||||||
|
|
@ -206,38 +246,11 @@ public class MetricDebitorService {
|
||||||
if (itemName.startsWith(ITEM_SALDO + ".")) {
|
if (itemName.startsWith(ITEM_SALDO + ".")) {
|
||||||
// currency value found
|
// currency value found
|
||||||
int pos = (ITEM_SALDO + ".").length();
|
int pos = (ITEM_SALDO + ".").length();
|
||||||
String currency = itemName.substring(pos + 1);
|
String currency = itemName.substring(pos).toUpperCase();
|
||||||
logger.info("Init metric for debitor " + dbtrNumber + " -> " + currency);
|
logger.info("│ │ ├──init metric for debitor " + dbtrNumber + " -> " + currency);
|
||||||
registerGauge(metric, currency);
|
updateGauge(metric, currency);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper method to register a gauge for a debitor
|
|
||||||
*
|
|
||||||
* @param dbtrNumber - the debitor number
|
|
||||||
* @param dbtrName - the debitor name
|
|
||||||
*/
|
|
||||||
private void registerGauge(ItemCollection metricData, String currency) {
|
|
||||||
String dbtrNumber = metricData.getItemValueString("dbtr.number");
|
|
||||||
String dbtrName = metricData.getItemValueString("dbtr.name");
|
|
||||||
List<Tag> tags = new ArrayList<>();
|
|
||||||
tags.add(new Tag("type", "dbtr"));
|
|
||||||
tags.add(new Tag("number", dbtrNumber));
|
|
||||||
tags.add(new Tag("name", dbtrName));
|
|
||||||
tags.add(new Tag("currency", currency));
|
|
||||||
|
|
||||||
logger.info("Register metric for debitor: " + dbtrNumber + " -> " + currency);
|
|
||||||
Metadata metadata = Metadata.builder()
|
|
||||||
.withName("dbtr.invoice.saldo")
|
|
||||||
.withDescription("Debitor Balance by Currency")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
metricRegistry.gauge(metadata,
|
|
||||||
() -> metricCache.getOrDefault(dbtrNumber, createMetaData(dbtrNumber, dbtrName))
|
|
||||||
.getItemValueDouble(ITEM_SALDO + "." + currency),
|
|
||||||
tags.toArray(new Tag[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
1721
workflow/dwc/rechnungsausgang-dwc-1.0.1.bpmn
Normal file
1721
workflow/dwc/rechnungsausgang-dwc-1.0.1.bpmn
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue