Skip to content

Commit

Permalink
Merge pull request eugenp#169 from Doha2012/master
Browse files Browse the repository at this point in the history
add graph and readme
  • Loading branch information
Eugen committed Mar 23, 2015
2 parents dd14fd0 + aad8586 commit 8abb2ee
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
29 changes: 29 additions & 0 deletions spring-security-login-and-registration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=========

## Login and Registration Example Project with Spring Security


### Relevant Articles:
- [Spring Security Registration Tutorial](http://www.baeldung.com/spring-security-registration)



### Build the Project
```
mvn clean install
```


### Set up MySQL
```
mysql -u root -p
> CREATE USER 'tutorialuser'@'localhost' IDENTIFIED BY 'tutorialmy5ql';
> GRANT ALL PRIVILEGES ON *.* TO 'tutorialuser'@'localhost';
> FLUSH PRIVILEGES;
```


### Set up Email

You need to configure the email by renaming file "email.properties.sample" to "email.properties" and provide your own username and password.
You also need to use your own host, you can use Amazon or Google for example.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.baeldung.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan("org.baeldung.web")
Expand All @@ -14,6 +18,18 @@ public WebConfig() {
super();
}

@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
// API
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/graph.html");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ public String getMetric() {
public String getStatusMetric() {
return metricService.getStatusMetric();
}

@RequestMapping(value = "/metric-graph", method = RequestMethod.GET)
@ResponseBody
public Object[][] drawMetric() {
final Object[][] result = metricService.getGraphData();
for (int i = 1; i < result[0].length; i++) {
result[0][i] = result[0][i].toString();
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.baeldung.web.metric;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.stereotype.Service;

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

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

public MetricService() {
super();
}

public void increaseCount(final String request, final int status) {
increaseMainMetric(request, status);
increaseStatusMetric(status);
updateTimeMap(status);
}

private void increaseMainMetric(final String request, final int status) {
HashMap<Integer, Integer> statusMap = metricMap.get(request);
if (statusMap == null) {
statusMap = new HashMap<Integer, Integer>();
Expand All @@ -28,7 +40,9 @@ public void increaseCount(final String request, final int status) {
}
statusMap.put(status, count);
metricMap.put(request, statusMap);
}

private void increaseStatusMetric(final int status) {
final Integer statusCount = statusMetric.get(status);
if (statusCount == null) {
statusMetric.put(status, 1);
Expand All @@ -37,11 +51,58 @@ public void increaseCount(final String request, final int status) {
}
}

private void updateTimeMap(final int status) {
final String time = dateFormat.format(new Date());
HashMap<Integer, Integer> statusMap = timeMap.get(time);
if (statusMap == null) {
statusMap = new HashMap<Integer, Integer>();
}

Integer count = statusMap.get(status);
if (count == null) {
count = 1;
} else {
count++;
}
statusMap.put(status, count);
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;
}
}
42 changes: 42 additions & 0 deletions spring-security-rest-full/src/main/webapp/WEB-INF/view/graph.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<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/spring-security-rest-full/metric-graph",
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>

0 comments on commit 8abb2ee

Please sign in to comment.