130 lines
4.1 KiB
Java
130 lines
4.1 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 com.alexanderlogistics.InvoiceUtil;
|
|
|
|
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/dbtr")
|
|
@Stateless
|
|
public class MetricDebitorRestService {
|
|
|
|
private static Logger logger = Logger.getLogger(MetricDebitorRestService.class.getName());
|
|
|
|
@Inject
|
|
DocumentService documentService;
|
|
|
|
@Inject
|
|
MetricDebitorService metricDebitorService;
|
|
|
|
@Inject
|
|
MetricDataService metricDataService;
|
|
|
|
@GET
|
|
@Path("/ping")
|
|
@Produces({ MediaType.TEXT_PLAIN })
|
|
public String ping() {
|
|
logger.info("GET ping");
|
|
return "ping: " + System.currentTimeMillis();
|
|
}
|
|
|
|
/**
|
|
* This method initializes the metrics for all debitors with open invoices.
|
|
*
|
|
* The method first deletes all existing metrics and than creates or updates the
|
|
* metric entires for each debitor.
|
|
*
|
|
* @return
|
|
*/
|
|
@GET
|
|
@Path("/rebuild")
|
|
@Produces({ MediaType.TEXT_PLAIN })
|
|
public Response rebuildMetrics() {
|
|
// Map<String, ItemCollection> metricCache = new HashMap<String,
|
|
// ItemCollection>();
|
|
long l = System.currentTimeMillis();
|
|
logger.info("├── init dbtr metrics...");
|
|
|
|
try {
|
|
// first clear the metric cache
|
|
metricDebitorService.reset();
|
|
logger.info("│ ├── reset metric cache");
|
|
|
|
computeMetrics();
|
|
logger.info("│ ├── computing metrics finished in " + (System.currentTimeMillis() - l) + "ms");
|
|
|
|
logger.info("│ ├── save and init metrics ...");
|
|
// run in new transaction!
|
|
metricDebitorService.refreshGauges();
|
|
|
|
String message = "├── rebuild dbtr 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();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Diese Methode berechnet alle Metriken auf basis der existierenden Rechnungen
|
|
* neu
|
|
*
|
|
*/
|
|
public void computeMetrics() {
|
|
|
|
logger.info("│ │ ├── group invoices by debitor...");
|
|
try {
|
|
int count = 0;
|
|
List<ItemCollection> invoices = documentService.find(
|
|
"$modelversion:rechnungsausgang-* AND type:workitem",
|
|
9999, 0,
|
|
"invoice.number", false);
|
|
|
|
logger.info("│ │ ├── found " + invoices.size() + " open invoices");
|
|
for (ItemCollection invoice : invoices) {
|
|
try {
|
|
|
|
ItemCollection metricData = metricDebitorService.getMetricByInvoice(invoice);
|
|
|
|
// Jetzt Rechnung addieren
|
|
metricDebitorService.addInvoice(metricData, invoice);
|
|
logger.info("│ │ │ ├──update metric " + InvoiceUtil.getBPId(invoice));
|
|
metricDebitorService.updateMetric(metricData);
|
|
count++;
|
|
} catch (PluginException e) {
|
|
// invalid invoice - e.g. no dbtr. number
|
|
}
|
|
|
|
}
|
|
logger.info("│ │ ├── updated metric for " + count + " invoices.");
|
|
} catch (QueryException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
private void log(String message, StringBuffer messageLog) {
|
|
logger.info(message);
|
|
messageLog.append(message + "\n");
|
|
|
|
}
|
|
}
|