cleanup work

This commit is contained in:
eugenp 2015-03-26 22:32:21 +02:00
parent 6287aa44ab
commit 052f2feb78
1 changed files with 46 additions and 40 deletions

View File

@ -3,6 +3,7 @@ package org.baeldung.web.metric;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@ -11,9 +12,9 @@ import org.springframework.stereotype.Service;
@Service
public class MetricService {
private HashMap<String, HashMap<Integer, Integer>> metricMap;
private HashMap<Integer, Integer> statusMetric;
private HashMap<String, HashMap<Integer, Integer>> timeMap;
private Map<String, HashMap<Integer, Integer>> metricMap;
private Map<Integer, Integer> statusMetric;
private Map<String, HashMap<Integer, Integer>> timeMap;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public MetricService() {
@ -23,12 +24,54 @@ public class MetricService {
timeMap = new HashMap<String, HashMap<Integer, Integer>>();
}
// API
public void increaseCount(final String request, final int status) {
increaseMainMetric(request, status);
increaseStatusMetric(status);
updateTimeMap(status);
}
public String getFullMetric() {
return metricMap.entrySet().toString();
}
public String getStatusMetric() {
return statusMetric.entrySet().toString();
}
public Object[][] getGraphData() {
final int colCount = statusMetric.keySet().size() + 1;
final Set<Integer> 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<Integer, Integer> tempMap;
for (final Entry<String, HashMap<Integer, Integer>> 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;
}
// NON-API
private void increaseMainMetric(final String request, final int status) {
HashMap<Integer, Integer> statusMap = metricMap.get(request);
if (statusMap == null) {
@ -71,41 +114,4 @@ public class MetricService {
timeMap.put(time, statusMap);
}
public String getFullMetric() {
return metricMap.entrySet().toString();
}
public String getStatusMetric() {
return statusMetric.entrySet().toString();
}
public Object[][] getGraphData(){
final int colCount = statusMetric.keySet().size()+1;
final Set<Integer> 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<Integer, Integer> tempMap;
for (final Entry<String, HashMap<Integer, Integer>> 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;
}
}