diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java index 08a274a956..a3fbb129cb 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java @@ -7,7 +7,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import org.baeldung.metric.MetricService; +import org.baeldung.metric.IMetricService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; @@ -24,7 +24,7 @@ import org.springframework.web.bind.annotation.ResponseBody; public class MyController { @Autowired - private MetricService metricService; + private IMetricService metricService; @RequestMapping("/") public String init(Map model, Principal principal) { diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/IMetricService.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/IMetricService.java new file mode 100644 index 0000000000..6814573138 --- /dev/null +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/IMetricService.java @@ -0,0 +1,8 @@ +package org.baeldung.metric; + +public interface IMetricService { + + void increaseCount(final int status); + + Object[][] getGraphData(); +} diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java index 2c76001889..4c00fda530 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java @@ -15,7 +15,7 @@ import org.springframework.stereotype.Component; public class MetricFilter implements Filter { @Autowired - private MetricService metricService; + private IMetricService metricService; @Override public void init(final FilterConfig config) throws ServletException { diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java index 9b1e100ce1..73c9f538e7 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java @@ -13,7 +13,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service -public class MetricService { +public class MetricService implements IMetricService { @Autowired private MetricRepository repo; @@ -21,9 +21,8 @@ public class MetricService { @Autowired private CounterService counter; - private ArrayList> statusMetric; + private List> statusMetric; private List statusList; - private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public MetricService() { @@ -32,6 +31,8 @@ public class MetricService { statusList = new ArrayList(); } + // API + public void increaseCount(final int status) { counter.increment("status." + status); if (!statusList.contains("counter.status." + status)) { @@ -39,6 +40,36 @@ public class MetricService { } } + public Object[][] getGraphData() { + Date current = new Date(); + int colCount = statusList.size() + 1; + int rowCount = statusMetric.size() + 1; + 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; + 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); + } + while (j < colCount) { + result[i][j] = 0; + j++; + } + } + return result; + } + + // Non - API + @Scheduled(fixedDelay = 60000) private void exportMetrics() { Metric metric; @@ -55,35 +86,4 @@ public class MetricService { } statusMetric.add(statusCount); } - - public Object[][] getGraphData() { - Date current = new Date(); - int colCount = statusList.size() + 1; - int rowCount = statusMetric.size() + 1; - - Object[][] result = new Object[rowCount][colCount]; - result[0][0] = "Time"; - - int j = 1; - for (final String status : statusList) { - result[0][j] = status; - j++; - } - - ArrayList temp; - 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); - } - while (j < colCount) { - result[i][j] = 0; - j++; - } - } - - return result; - } - } 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 a1364d0dae..6e4fd738aa 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 @@ -5,7 +5,7 @@ import java.net.URI; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.baeldung.web.metric.MetricService; +import org.baeldung.web.metric.IMetricService; import org.baeldung.web.util.LinkUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -20,7 +20,7 @@ import org.springframework.web.util.UriTemplate; public class RootController { @Autowired - private MetricService metricService; + private IMetricService metricService; public RootController() { super(); diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/IMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/IMetricService.java new file mode 100644 index 0000000000..fe64366a45 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/IMetricService.java @@ -0,0 +1,12 @@ +package org.baeldung.web.metric; + +public interface IMetricService { + + void increaseCount(final String request, final int status); + + String getFullMetric(); + + String getStatusMetric(); + + Object[][] getGraphData(); +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java index df633e492f..ed33b70ba3 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java @@ -10,7 +10,7 @@ import java.util.Set; import org.springframework.stereotype.Service; @Service -public class MetricService { +public class MetricService implements IMetricService { private Map> metricMap; private Map statusMetric; @@ -26,20 +26,24 @@ public class MetricService { // API + @Override public void increaseCount(final String request, final int status) { increaseMainMetric(request, status); increaseStatusMetric(status); updateTimeMap(status); } + @Override public String getFullMetric() { return metricMap.entrySet().toString(); } + @Override public String getStatusMetric() { return statusMetric.entrySet().toString(); } + @Override public Object[][] getGraphData() { final int colCount = statusMetric.keySet().size() + 1; final Set allStatus = statusMetric.keySet(); @@ -54,7 +58,7 @@ public class MetricService { j++; } int i = 1; - HashMap tempMap; + Map tempMap; for (final Entry> entry : timeMap.entrySet()) { result[i][0] = entry.getKey(); tempMap = entry.getValue();