This commit is contained in:
DOHA
2015-03-27 00:22:33 +02:00
parent 052f2feb78
commit 60f029855a
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;
}
}