145 lines
5 KiB
Java
145 lines
5 KiB
Java
package com.alexanderlogistics.metrics;
|
|
|
|
import java.util.List;
|
|
import java.util.logging.Logger;
|
|
|
|
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.ejb.Stateless;
|
|
import jakarta.inject.Inject;
|
|
import jakarta.ws.rs.GET;
|
|
import jakarta.ws.rs.Path;
|
|
import jakarta.ws.rs.Produces;
|
|
import jakarta.ws.rs.core.MediaType;
|
|
import jakarta.ws.rs.core.Response;
|
|
|
|
@Path("metrics/cdtr")
|
|
@Stateless
|
|
public class MetricCreditorRestService {
|
|
|
|
private static Logger logger = Logger.getLogger(MetricCreditorRestService.class.getName());
|
|
|
|
@Inject
|
|
DocumentService documentService;
|
|
|
|
@Inject
|
|
MetricCreditorService metricService;
|
|
|
|
@GET
|
|
@Path("/ping")
|
|
@Produces({ MediaType.TEXT_PLAIN })
|
|
public String ping() {
|
|
logger.info("GET ping");
|
|
return "ping: " + System.currentTimeMillis();
|
|
}
|
|
|
|
/**
|
|
* This method refreshes all creditor metrics by iterating through the metric
|
|
* entities.
|
|
*
|
|
* @return
|
|
*/
|
|
@GET
|
|
@Path("/refresh")
|
|
@Produces({ MediaType.TEXT_PLAIN })
|
|
public Response refreshMetrics() {
|
|
try {
|
|
// Alle Creditor Metriken laden
|
|
String query = "(type:" + MetricCreditorService.TYPE_METRIC_CREDITOR + ")";
|
|
List<ItemCollection> metrics = documentService.find(query, 9999, 0);
|
|
|
|
// Metriken initialisieren
|
|
for (ItemCollection metric : metrics) {
|
|
metricService.initMetric(metric);
|
|
}
|
|
return Response.ok().entity("Initialized " + metrics.size() + " creditor metrics").build();
|
|
} catch (Exception e) {
|
|
return Response.serverError()
|
|
.entity("Failed to initialize metrics: " + e.getMessage())
|
|
.build();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This method initializes the metrics for all creditors with open invoices.
|
|
* The method creates or updates the metric entires for each creditor.
|
|
*
|
|
* @return
|
|
*/
|
|
@GET
|
|
@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 {
|
|
groupInvoicesByCreditor();
|
|
logger.info("│ ├── grouping invoices finished in " + (System.currentTimeMillis() - l) + "ms");
|
|
|
|
rebuildMetrics();
|
|
String message = "├── init cdtr metrics completed in "
|
|
+ (System.currentTimeMillis() - l)
|
|
+ "ms";
|
|
logger.info(message);
|
|
return Response.ok().entity(message).build();
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return Response.serverError()
|
|
.entity("Failed to initialize metrics: " + e.getMessage())
|
|
.build();
|
|
}
|
|
}
|
|
|
|
private void rebuildMetrics() throws PluginException {
|
|
logger.info("│ ├── rebuild metrics...");
|
|
List<String> keys = metricService.getMetricKeys();
|
|
for (String hashKey : keys) {
|
|
ItemCollection metricData = metricService.getMetric(hashKey);
|
|
// metricCache.get(dbtrNumber);
|
|
documentService.save(metricData);
|
|
metricService.initMetric(metricData);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Diese Methode gruppiert eine Rechnungsliste nach creditoren
|
|
*
|
|
* @param spaceID - Space Ref to select a list of invoices associated with
|
|
* a
|
|
* space
|
|
* @param metricCache - a local cache storing all invoices by week
|
|
*
|
|
*
|
|
*/
|
|
private void groupInvoicesByCreditor() {
|
|
logger.info("│ │ ├── group invoices by creditor...");
|
|
try {
|
|
List<ItemCollection> invoices = documentService.find(
|
|
"($modelversion:rechnungseingang-* OR $modelversion:gutschriftabgleich-*) " +
|
|
" AND type:workitem",
|
|
9999, 0,
|
|
"invoice.number", false);
|
|
logger.info("│ │ ├──found " + invoices.size() + " open invoices");
|
|
for (ItemCollection invoice : invoices) {
|
|
if (invoice.getItemValueString("cdtr.number").trim().isEmpty()) {
|
|
// skip event
|
|
continue;
|
|
}
|
|
ItemCollection metricData = metricService.getMetricByInvoice(invoice);
|
|
// Jetzt Rechnung addieren
|
|
metricService.addInvoice(metricData, invoice);
|
|
logger.fine("....put invoice " + invoice.getUniqueID());
|
|
metricService.putMetric(metricData);
|
|
}
|
|
logger.info("│ │ ├── grouped " + invoices.size() + " invoices.");
|
|
} catch (QueryException | PluginException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|