Move boot metric to rest full
This commit is contained in:
@@ -24,7 +24,6 @@ public class SampleLDAPApplication extends WebMvcConfigurerAdapter {
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/login").setViewName("login");
|
||||
registry.addViewController("/graph").setViewName("graph");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,15 +7,11 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.baeldung.metric.IMetricService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
/**
|
||||
* Spring Controller Definitions.
|
||||
@@ -23,9 +19,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@Controller
|
||||
public class MyController {
|
||||
|
||||
@Autowired
|
||||
private IMetricService metricService;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String init(Map<String, Object> model, Principal principal) {
|
||||
model.put("title", "PUBLIC AREA");
|
||||
@@ -44,12 +37,6 @@ public class MyController {
|
||||
return "home";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/metric-graph-data", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Object[][] getMetricData() {
|
||||
return metricService.getGraphData();
|
||||
}
|
||||
|
||||
private String getUserName(Principal principal) {
|
||||
if (principal == null) {
|
||||
return "anonymous";
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package org.baeldung.metric;
|
||||
|
||||
public interface IMetricService {
|
||||
|
||||
void increaseCount(final int status);
|
||||
|
||||
Object[][] getGraphData();
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package org.baeldung.metric;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MetricFilter implements Filter {
|
||||
|
||||
@Autowired
|
||||
private IMetricService metricService;
|
||||
|
||||
@Override
|
||||
public void init(final FilterConfig config) throws ServletException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws java.io.IOException, ServletException {
|
||||
chain.doFilter(request, response);
|
||||
|
||||
final int status = ((HttpServletResponse) response).getStatus();
|
||||
metricService.increaseCount(status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package org.baeldung.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.metrics.CounterService;
|
||||
import org.springframework.boot.actuate.metrics.Metric;
|
||||
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MetricService implements IMetricService {
|
||||
|
||||
@Autowired
|
||||
private MetricRepository repo;
|
||||
|
||||
@Autowired
|
||||
private CounterService counter;
|
||||
|
||||
private final List<ArrayList<Integer>> statusMetric;
|
||||
private final List<String> statusList;
|
||||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
|
||||
public MetricService() {
|
||||
super();
|
||||
statusMetric = new ArrayList<ArrayList<Integer>>();
|
||||
statusList = new ArrayList<String>();
|
||||
}
|
||||
|
||||
// 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;
|
||||
j++;
|
||||
}
|
||||
|
||||
List<Integer> 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;
|
||||
final ArrayList<Integer> statusCount = new ArrayList<Integer>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Metric Graph</title>
|
||||
<script
|
||||
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
|
||||
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
|
||||
<script type="text/javascript">
|
||||
google.load("visualization", "1", {
|
||||
packages : [ "corechart" ]
|
||||
});
|
||||
|
||||
function drawChart() {
|
||||
$.get("http://localhost:8080/metric-graph-data",
|
||||
function(mydata) {
|
||||
|
||||
var data = google.visualization.arrayToDataTable(mydata);
|
||||
var options = {
|
||||
title : 'Website Metric',
|
||||
hAxis : {
|
||||
title : 'Time',
|
||||
titleTextStyle : {
|
||||
color : '#333'
|
||||
}
|
||||
},
|
||||
vAxis : {
|
||||
minValue : 0
|
||||
}
|
||||
};
|
||||
|
||||
var chart = new google.visualization.AreaChart(document
|
||||
.getElementById('chart_div'));
|
||||
chart.draw(data, options);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload="drawChart()">
|
||||
<div id="chart_div" style="width: 900px; height: 500px;"></div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user