Skip to content

Commit 8e9bf9a

Browse files
committed
add graph
1 parent ffe552a commit 8e9bf9a

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

spring-security-rest-full/src/main/java/org/baeldung/spring/WebConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.baeldung.spring;
22

3+
import org.springframework.context.annotation.Bean;
34
import org.springframework.context.annotation.ComponentScan;
45
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
57
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
69
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
711

812
@Configuration
913
@ComponentScan("org.baeldung.web")
@@ -14,6 +18,18 @@ public WebConfig() {
1418
super();
1519
}
1620

21+
@Bean
22+
public ViewResolver viewResolver() {
23+
final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
24+
viewResolver.setPrefix("/WEB-INF/view/");
25+
viewResolver.setSuffix(".jsp");
26+
return viewResolver;
27+
}
1728
// API
29+
@Override
30+
public void addViewControllers(final ViewControllerRegistry registry) {
31+
super.addViewControllers(registry);
32+
registry.addViewController("/graph.html");
33+
}
1834

1935
}

spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ public String getMetric() {
5151
public String getStatusMetric() {
5252
return metricService.getStatusMetric();
5353
}
54+
55+
@RequestMapping(value = "/metric-graph", method = RequestMethod.GET)
56+
@ResponseBody
57+
public Object[][] drawMetric() {
58+
final Object[][] result = metricService.getGraphData();
59+
for (int i = 1; i < result[0].length; i++) {
60+
result[0][i] = result[0][i].toString();
61+
}
62+
return result;
63+
}
5464
}

spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.baeldung.web.metric;
22

3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
35
import java.util.HashMap;
6+
import java.util.Map.Entry;
7+
import java.util.Set;
48

59
import org.springframework.stereotype.Service;
610

@@ -9,12 +13,20 @@ public class MetricService {
913

1014
private static HashMap<String, HashMap<Integer, Integer>> metricMap = new HashMap<String, HashMap<Integer, Integer>>();
1115
private static HashMap<Integer, Integer> statusMetric = new HashMap<Integer, Integer>();
16+
private static HashMap<String, HashMap<Integer, Integer>> timeMap = new HashMap<String, HashMap<Integer, Integer>>();
17+
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
1218

1319
public MetricService() {
1420
super();
1521
}
1622

1723
public void increaseCount(final String request, final int status) {
24+
increaseMainMetric(request, status);
25+
increaseStatusMetric(status);
26+
updateTimeMap(status);
27+
}
28+
29+
private void increaseMainMetric(final String request, final int status) {
1830
HashMap<Integer, Integer> statusMap = metricMap.get(request);
1931
if (statusMap == null) {
2032
statusMap = new HashMap<Integer, Integer>();
@@ -28,7 +40,9 @@ public void increaseCount(final String request, final int status) {
2840
}
2941
statusMap.put(status, count);
3042
metricMap.put(request, statusMap);
43+
}
3144

45+
private void increaseStatusMetric(final int status) {
3246
final Integer statusCount = statusMetric.get(status);
3347
if (statusCount == null) {
3448
statusMetric.put(status, 1);
@@ -37,11 +51,58 @@ public void increaseCount(final String request, final int status) {
3751
}
3852
}
3953

54+
private void updateTimeMap(final int status) {
55+
final String time = dateFormat.format(new Date());
56+
HashMap<Integer, Integer> statusMap = timeMap.get(time);
57+
if (statusMap == null) {
58+
statusMap = new HashMap<Integer, Integer>();
59+
}
60+
61+
Integer count = statusMap.get(status);
62+
if (count == null) {
63+
count = 1;
64+
} else {
65+
count++;
66+
}
67+
statusMap.put(status, count);
68+
timeMap.put(time, statusMap);
69+
}
70+
4071
public String getFullMetric() {
4172
return metricMap.entrySet().toString();
4273
}
4374

4475
public String getStatusMetric() {
4576
return statusMetric.entrySet().toString();
4677
}
78+
79+
public Object[][] getGraphData(){
80+
final int colCount = statusMetric.keySet().size()+1;
81+
final Set<Integer> allStatus = statusMetric.keySet();
82+
final int rowCount = timeMap.keySet().size()+1;
83+
84+
final Object[][] result = new Object[rowCount][colCount];
85+
result[0][0] = "Time";
86+
87+
int j = 1;
88+
for (final int status : allStatus) {
89+
result[0][j] = status;
90+
j++;
91+
}
92+
int i = 1;
93+
HashMap<Integer, Integer> tempMap;
94+
for (final Entry<String, HashMap<Integer, Integer>> entry : timeMap.entrySet()) {
95+
result[i][0] = entry.getKey();
96+
tempMap = entry.getValue();
97+
for (j = 1; j < colCount; j++) {
98+
result[i][j] = tempMap.get(result[0][j]);
99+
if (result[i][j] == null) {
100+
result[i][j] = 0;
101+
}
102+
}
103+
i++;
104+
}
105+
106+
return result;
107+
}
47108
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<html>
2+
<head>
3+
<title>Metric Graph</title>
4+
<script
5+
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
6+
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
7+
<script type="text/javascript">
8+
google.load("visualization", "1", {
9+
packages : [ "corechart" ]
10+
});
11+
12+
function drawChart() {
13+
$.get("http://localhost:8080/spring-security-rest-full/metric-graph",
14+
function(mydata) {
15+
16+
var data = google.visualization.arrayToDataTable(mydata);
17+
var options = {
18+
title : 'Website Metric',
19+
hAxis : {
20+
title : 'Time',
21+
titleTextStyle : {
22+
color : '#333'
23+
}
24+
},
25+
vAxis : {
26+
minValue : 0
27+
}
28+
};
29+
30+
var chart = new google.visualization.AreaChart(document
31+
.getElementById('chart_div'));
32+
chart.draw(data, options);
33+
34+
});
35+
36+
}
37+
</script>
38+
</head>
39+
<body onload="drawChart()">
40+
<div id="chart_div" style="width: 900px; height: 500px;"></div>
41+
</body>
42+
</html>

0 commit comments

Comments
 (0)