Merge pull request #176 from Doha2012/master

cleanup
This commit is contained in:
Eugen 2015-03-27 00:32:46 +02:00
commit 8732875d4d
7 changed files with 65 additions and 41 deletions

View File

@ -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<String, Object> model, Principal principal) {

View File

@ -0,0 +1,8 @@
package org.baeldung.metric;
public interface IMetricService {
void increaseCount(final int status);
Object[][] getGraphData();
}

View File

@ -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 {

View File

@ -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<ArrayList<Integer>> statusMetric;
private List<ArrayList<Integer>> statusMetric;
private List<String> 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<String>();
}
// 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<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;
@ -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<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;
}
}

View File

@ -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();

View File

@ -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();
}

View File

@ -10,7 +10,7 @@ import java.util.Set;
import org.springframework.stereotype.Service;
@Service
public class MetricService {
public class MetricService implements IMetricService {
private Map<String, HashMap<Integer, Integer>> metricMap;
private Map<Integer, Integer> 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<Integer> allStatus = statusMetric.keySet();
@ -54,7 +58,7 @@ public class MetricService {
j++;
}
int i = 1;
HashMap<Integer, Integer> tempMap;
Map<Integer, Integer> tempMap;
for (final Entry<String, HashMap<Integer, Integer>> entry : timeMap.entrySet()) {
result[i][0] = entry.getKey();
tempMap = entry.getValue();