From c565c1e1810b2ce3483a1f65b2eaa9f38e8d57c1 Mon Sep 17 00:00:00 2001 From: DOHA Date: Wed, 9 Dec 2015 17:08:33 +0200 Subject: [PATCH] metrics improve --- .../web/controller/RootController.java | 4 +- .../web/metric/ActuatorMetricService2.java | 93 +++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService2.java diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java index 42fc78f611..595312b477 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java @@ -6,7 +6,7 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.baeldung.web.metric.IActuatorMetricService; +import org.baeldung.web.metric.ActuatorMetricService2; import org.baeldung.web.metric.IMetricService; import org.baeldung.web.util.LinkUtil; import org.springframework.beans.factory.annotation.Autowired; @@ -25,7 +25,7 @@ public class RootController { private IMetricService metricService; @Autowired - private IActuatorMetricService actMetricService; + private ActuatorMetricService2 actMetricService; public RootController() { super(); diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService2.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService2.java new file mode 100644 index 0000000000..2e6efef62c --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService2.java @@ -0,0 +1,93 @@ +package org.baeldung.web.metric; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics; +import org.springframework.boot.actuate.metrics.Metric; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Service +public class ActuatorMetricService2 { + + @Autowired + private MetricReaderPublicMetrics publicMetrics; + + private final List> statusMetric; + private final List statusList; + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + + public ActuatorMetricService2() { + super(); + statusMetric = new ArrayList>(); + statusList = new ArrayList(); + } + + + public Object[][] getGraphData() { + final Date current = new Date(); + final int colCount = statusList.size() + 1; + final int rowCount = statusMetric.size() + 1; + final Object[][] result = new Object[rowCount][colCount]; + result[0][0] = "Time"; + int j = 1; + for (final String status : statusList) { + result[0][j] = status; + j++; + } + + List temp; + List last = new ArrayList(); + for (int i = 1; i < rowCount; i++) { + temp = statusMetric.get(i - 1); + result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); + for (j = 1; j <= temp.size(); j++) { + System.out.println(last); + result[i][j] = temp.get(j - 1) - (last.size() >= j ? last.get(j - 1) : 0); + } + while (j < colCount) { + result[i][j] = 0; + j++; + } + last = temp; + } + return result; + } + + // Non - API + + @Scheduled(fixedDelay = 60000) + private void exportMetrics() { + final ArrayList statusCount = new ArrayList(); + String status = ""; + int index = -1; + int old = 0; + for (int i = 0; i < statusList.size(); i++) { + statusCount.add(0); + } + for (final Metric metric : publicMetrics.metrics()) { + if (metric.getName().contains("counter.status.")) { + status = metric.getName().substring(15, 18); + if (!statusList.contains(status)) { + statusList.add(status); + statusCount.add(0); + } + System.out.println(statusList + " == " + statusCount); + index = statusList.indexOf(status); + old = statusCount.get(index) == null ? 0 : statusCount.get(index); + statusCount.set(index, metric.getValue().intValue() + old); + // metric.set(0); + // repo.reset(metric.getName()); + } + } + statusMetric.add(statusCount); + + // for (final Metric metric : publicMetrics.metrics()) { + // System.out.println(metric.getName() + " = " + metric.getValue() + " = " + metric.getTimestamp()); + // } + } +} \ No newline at end of file