From 8e9bf9a7391dcb3e7a0e1be7c5a0a3522691bf84 Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 23 Mar 2015 19:17:48 +0200 Subject: [PATCH] add graph --- .../java/org/baeldung/spring/WebConfig.java | 16 +++++ .../web/controller/RootController.java | 10 +++ .../baeldung/web/metric/MetricService.java | 61 +++++++++++++++++++ .../src/main/webapp/WEB-INF/view/graph.jsp | 42 +++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 spring-security-rest-full/src/main/webapp/WEB-INF/view/graph.jsp diff --git a/spring-security-rest-full/src/main/java/org/baeldung/spring/WebConfig.java b/spring-security-rest-full/src/main/java/org/baeldung/spring/WebConfig.java index 19fcde5fab..806d372d09 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/spring/WebConfig.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/spring/WebConfig.java @@ -1,9 +1,13 @@ package org.baeldung.spring; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @ComponentScan("org.baeldung.web") @@ -14,6 +18,18 @@ public class WebConfig extends WebMvcConfigurerAdapter { super(); } + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setPrefix("/WEB-INF/view/"); + viewResolver.setSuffix(".jsp"); + return viewResolver; + } // API + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + registry.addViewController("/graph.html"); + } } \ No newline at end of file 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 95b3f996ec..a1364d0dae 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 @@ -51,4 +51,14 @@ public class RootController { public String getStatusMetric() { return metricService.getStatusMetric(); } + + @RequestMapping(value = "/metric-graph", method = RequestMethod.GET) + @ResponseBody + public Object[][] drawMetric() { + final Object[][] result = metricService.getGraphData(); + for (int i = 1; i < result[0].length; i++) { + result[0][i] = result[0][i].toString(); + } + return result; + } } 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 4fd7955b75..677c73df49 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 @@ -1,6 +1,10 @@ package org.baeldung.web.metric; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.HashMap; +import java.util.Map.Entry; +import java.util.Set; import org.springframework.stereotype.Service; @@ -9,12 +13,20 @@ public class MetricService { private static HashMap> metricMap = new HashMap>(); private static HashMap statusMetric = new HashMap(); + private static HashMap> timeMap = new HashMap>(); + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public MetricService() { super(); } public void increaseCount(final String request, final int status) { + increaseMainMetric(request, status); + increaseStatusMetric(status); + updateTimeMap(status); + } + + private void increaseMainMetric(final String request, final int status) { HashMap statusMap = metricMap.get(request); if (statusMap == null) { statusMap = new HashMap(); @@ -28,7 +40,9 @@ public class MetricService { } statusMap.put(status, count); metricMap.put(request, statusMap); + } + private void increaseStatusMetric(final int status) { final Integer statusCount = statusMetric.get(status); if (statusCount == null) { statusMetric.put(status, 1); @@ -37,6 +51,23 @@ public class MetricService { } } + private void updateTimeMap(final int status) { + final String time = dateFormat.format(new Date()); + HashMap statusMap = timeMap.get(time); + if (statusMap == null) { + statusMap = new HashMap(); + } + + Integer count = statusMap.get(status); + if (count == null) { + count = 1; + } else { + count++; + } + statusMap.put(status, count); + timeMap.put(time, statusMap); + } + public String getFullMetric() { return metricMap.entrySet().toString(); } @@ -44,4 +75,34 @@ public class MetricService { public String getStatusMetric() { return statusMetric.entrySet().toString(); } + + public Object[][] getGraphData(){ + final int colCount = statusMetric.keySet().size()+1; + final Set allStatus = statusMetric.keySet(); + final int rowCount = timeMap.keySet().size()+1; + + final Object[][] result = new Object[rowCount][colCount]; + result[0][0] = "Time"; + + int j = 1; + for (final int status : allStatus) { + result[0][j] = status; + j++; + } + int i = 1; + HashMap tempMap; + for (final Entry> entry : timeMap.entrySet()) { + result[i][0] = entry.getKey(); + tempMap = entry.getValue(); + for (j = 1; j < colCount; j++) { + result[i][j] = tempMap.get(result[0][j]); + if (result[i][j] == null) { + result[i][j] = 0; + } + } + i++; + } + + return result; + } } 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 new file mode 100644 index 0000000000..a1a8050201 --- /dev/null +++ b/spring-security-rest-full/src/main/webapp/WEB-INF/view/graph.jsp @@ -0,0 +1,42 @@ + + +Metric Graph + + + + + +
+ + \ No newline at end of file