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 595312b477..42fc78f611 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.ActuatorMetricService2; +import org.baeldung.web.metric.IActuatorMetricService; 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 ActuatorMetricService2 actMetricService; + private IActuatorMetricService actMetricService; public RootController() { super(); diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java index 3d13fb5b59..d0ed765c68 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java @@ -6,20 +6,16 @@ import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.actuate.metrics.CounterService; +import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics; import org.springframework.boot.actuate.metrics.Metric; -import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository; -import org.springframework.boot.actuate.metrics.repository.MetricRepository; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ActuatorMetricService implements IActuatorMetricService { - private MetricRepository repo; - @Autowired - private CounterService counter; + private MetricReaderPublicMetrics publicMetrics; private final List> statusMetric; private final List statusList; @@ -27,20 +23,10 @@ public class ActuatorMetricService implements IActuatorMetricService { public ActuatorMetricService() { super(); - repo = new InMemoryMetricRepository(); statusMetric = new ArrayList>(); statusList = new ArrayList(); } - // API - - @Override - public void increaseCount(final int status) { - counter.increment("status." + status); - if (!statusList.contains("counter.status." + status)) { - statusList.add("counter.status." + status); - } - } @Override public Object[][] getGraphData() { @@ -49,24 +35,26 @@ public class ActuatorMetricService implements IActuatorMetricService { 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++) { - result[i][j] = temp.get(j - 1); + 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; } @@ -75,18 +63,39 @@ public class ActuatorMetricService implements IActuatorMetricService { @Scheduled(fixedDelay = 60000) private void exportMetrics() { - Metric metric; - final ArrayList statusCount = new ArrayList(); - for (final String status : statusList) { - metric = repo.findOne(status); - if (metric != null) { - statusCount.add(metric.getValue().intValue()); - repo.reset(status); - } else { - statusCount.add(0); - } + final ArrayList statusCount = initializeCounterList(statusList.size()); + for (final Metric counterMetric : publicMetrics.metrics()) { + updateStatusCount(counterMetric, statusCount); } + statusMetric.add(statusCount); } + + private ArrayList initializeCounterList(final int size) { + final ArrayList counterList = new ArrayList(); + for (int i = 0; i < size; i++) { + counterList.add(0); + } + return counterList; + } + + private void updateStatusCount(final Metric counterMetric, final ArrayList statusCount) { + String status = ""; + int index = -1; + int oldCount = 0; + + if (counterMetric.getName().contains("counter.status.")) { + status = counterMetric.getName().substring(15, 18); // example 404, 200 + if (!statusList.contains(status)) { + statusList.add(status); + statusCount.add(0); + } + index = statusList.indexOf(status); + oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index); + statusCount.set(index, counterMetric.getValue().intValue() + oldCount); + } + } + + // } \ No newline at end of file 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/CustomActuatorMetricService.java similarity index 55% rename from spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService2.java rename to spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java index 2e6efef62c..7df7e199b4 100644 --- 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/CustomActuatorMetricService.java @@ -6,34 +6,50 @@ 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.CounterService; import org.springframework.boot.actuate.metrics.Metric; +import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository; +import org.springframework.boot.actuate.metrics.repository.MetricRepository; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service -public class ActuatorMetricService2 { +public class CustomActuatorMetricService implements ICustomActuatorMetricService { + + private MetricRepository repo; @Autowired - private MetricReaderPublicMetrics publicMetrics; + private CounterService counter; private final List> statusMetric; private final List statusList; private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - public ActuatorMetricService2() { + public CustomActuatorMetricService() { super(); + repo = new InMemoryMetricRepository(); statusMetric = new ArrayList>(); statusList = new ArrayList(); } + // API + @Override + public void increaseCount(final int status) { + counter.increment("status." + status); + if (!statusList.contains("counter.status." + status)) { + statusList.add("counter.status." + status); + } + } + + @Override 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; @@ -41,19 +57,16 @@ public class ActuatorMetricService2 { } 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); + result[i][j] = temp.get(j - 1); } while (j < colCount) { result[i][j] = 0; j++; } - last = temp; } return result; } @@ -62,32 +75,18 @@ public class ActuatorMetricService2 { @Scheduled(fixedDelay = 60000) private void exportMetrics() { + Metric metric; 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()); + for (final String status : statusList) { + metric = repo.findOne(status); + if (metric != null) { + statusCount.add(metric.getValue().intValue()); + repo.reset(status); + } else { + statusCount.add(0); } + } statusMetric.add(statusCount); - - // for (final Metric metric : publicMetrics.metrics()) { - // System.out.println(metric.getName() + " = " + metric.getValue() + " = " + metric.getTimestamp()); - // } } } \ No newline at end of file diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/IActuatorMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/IActuatorMetricService.java index b7528ce372..191d347070 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/IActuatorMetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/IActuatorMetricService.java @@ -1,8 +1,5 @@ package org.baeldung.web.metric; public interface IActuatorMetricService { - - void increaseCount(final int status); - Object[][] getGraphData(); } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ICustomActuatorMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ICustomActuatorMetricService.java new file mode 100644 index 0000000000..19ab7164ac --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ICustomActuatorMetricService.java @@ -0,0 +1,8 @@ +package org.baeldung.web.metric; + +public interface ICustomActuatorMetricService { + + void increaseCount(final int status); + + Object[][] getGraphData(); +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java index 06665f820b..d77aec7919 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java @@ -20,13 +20,13 @@ public class MetricFilter implements Filter { private IMetricService metricService; @Autowired - private IActuatorMetricService actMetricService; + private ICustomActuatorMetricService actMetricService; @Override public void init(final FilterConfig config) throws ServletException { if (metricService == null || actMetricService == null) { metricService = (IMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("metricService"); - actMetricService = (IActuatorMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("actuatorMetricService"); + actMetricService = (ICustomActuatorMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("actuatorMetricService"); } } diff --git a/spring-security-rest-full/src/main/webapp/WEB-INF/view/graph.jsp b/spring-security-rest-full/src/main/webapp/WEB-INF/view/graph.jsp index cb037f5f6c..e1d5fdc987 100644 --- a/spring-security-rest-full/src/main/webapp/WEB-INF/view/graph.jsp +++ b/spring-security-rest-full/src/main/webapp/WEB-INF/view/graph.jsp @@ -11,7 +11,7 @@ }); function drawChart() { - $.get("", + $.get("", function(mydata) { var data = google.visualization.arrayToDataTable(mydata);