Merge branch 'add_piechart' into 'master'
Add piechart See merge request cse1105/2018-2019/oopp-group-43/template!77
This commit is contained in:
@@ -398,7 +398,11 @@ public class CalculatorController {
|
||||
"input_footprint_shopping_services_total",
|
||||
servicesLabel.getText().replace("€ / month", ""));
|
||||
}
|
||||
extraActivityController.updateExtras();
|
||||
try {
|
||||
extraActivityController.updateExtras();
|
||||
} catch (Exception ex) {
|
||||
System.out.println("continue");
|
||||
}
|
||||
Float footprint = userService.saveFootprint(userService.currentUser.getName());
|
||||
Window owner = saveButton.getScene().getWindow();
|
||||
controller.updateLeaderboard();
|
||||
|
||||
@@ -237,20 +237,6 @@ public class DashBoardController {
|
||||
developmentScore.setCellValueFactory(new PropertyValueFactory<>("Score"));
|
||||
friendUser.setCellValueFactory(new PropertyValueFactory<>("Friend"));
|
||||
friendScore.setCellValueFactory(new PropertyValueFactory<>("Score"));
|
||||
if (pieChart != null) {
|
||||
ObservableList<PieChart.Data> pieChartData =
|
||||
FXCollections.observableArrayList(
|
||||
new PieChart.Data("Vegan Meal", 100),
|
||||
new PieChart.Data("Public Transport", 200),
|
||||
new PieChart.Data("Home Temperature", 50),
|
||||
new PieChart.Data("Bike", 75),
|
||||
new PieChart.Data("Local Product", 110),
|
||||
new PieChart.Data("Solar Panel", 300)
|
||||
);
|
||||
pieChart.setTitle("FOOTPRINT DISTRIBUTION");
|
||||
pieChart.setMaxSize(1000, 1000);
|
||||
pieChart.setData(pieChartData);
|
||||
}
|
||||
List<String> friendList = userService.getFriendNames(userService.currentUser.getName());
|
||||
for (int i = 0; i < friendList.size(); i++) {
|
||||
Friend friend = new Friend(friendList.get(i),
|
||||
@@ -260,6 +246,7 @@ public class DashBoardController {
|
||||
friendsTable.setItems(data);
|
||||
updateLeaderboard();
|
||||
updateAchievements();
|
||||
updatePiechart();
|
||||
calculateFootPrintButton.setSkin(new ClickButtonSkin(calculateFootPrintButton));
|
||||
addFriendButton.setSkin(new ClickButtonSkin(addFriendButton));
|
||||
addExtraActivityButton.setSkin(new ClickButtonSkin(addExtraActivityButton));
|
||||
@@ -317,7 +304,7 @@ public class DashBoardController {
|
||||
* Sorts the scores of users.
|
||||
* @param users the list of users
|
||||
*/
|
||||
private void sortScores(List<String> users) {
|
||||
public void sortScores(List<String> users) throws InterruptedException {
|
||||
for (int i = 0; i < users.size(); i++) {
|
||||
for (int j = 0; j < users.size(); j++) {
|
||||
Double first = userService.getFootprint(users.get(i));
|
||||
@@ -340,7 +327,7 @@ public class DashBoardController {
|
||||
* Sorts the scores of users.
|
||||
* @param users the list of users
|
||||
*/
|
||||
private List<String> sortDiffScores(List<String> users) throws InterruptedException {
|
||||
public List<String> sortDiffScores(List<String> users) throws InterruptedException {
|
||||
for (int i = 0; i < users.size(); i++) {
|
||||
for (int j = 0; j < users.size(); j++) {
|
||||
Double firstDiff = userService.getFirstFootprint(users.get(i)) - userService
|
||||
@@ -366,7 +353,7 @@ public class DashBoardController {
|
||||
* Adds a fade transition for switching between the different panes.
|
||||
* @param node the node on which the transition needs to act
|
||||
*/
|
||||
private void addFadeTransition(Node node) {
|
||||
public void addFadeTransition(Node node) {
|
||||
fadeTrans = new FadeTransition(Duration.millis(400), node);
|
||||
fadeTrans.setFromValue(0);
|
||||
fadeTrans.setToValue(1.0);
|
||||
@@ -440,7 +427,7 @@ public class DashBoardController {
|
||||
* Displays the user profile pane.
|
||||
* @param event the event (clicking the button)
|
||||
*/
|
||||
public void displayUser(ActionEvent event) {
|
||||
public void displayUser(ActionEvent event) throws InterruptedException {
|
||||
System.out.println(userService.currentUser.getName());
|
||||
System.out.println(userService.getFootprint(userService.currentUser.getName()));
|
||||
footprintLabel.setText("" + userService.getFootprint(userService.currentUser.getName()));
|
||||
@@ -456,6 +443,7 @@ public class DashBoardController {
|
||||
userPane.setVisible(true);
|
||||
activitiesPane.setVisible(false);
|
||||
friendsPane.setVisible(false);
|
||||
updatePiechart();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -603,6 +591,32 @@ public class DashBoardController {
|
||||
developmentLeaderboard.setItems(developmentData);
|
||||
}
|
||||
|
||||
/**
|
||||
* The method updating piechart.
|
||||
* @throws InterruptedException exception
|
||||
*/
|
||||
public void updatePiechart() throws InterruptedException {
|
||||
Map<String, String> resultMap = userService.getResults(userService.currentUser.getName());
|
||||
if (pieChart != null) {
|
||||
ObservableList<PieChart.Data> pieChartData =
|
||||
FXCollections.observableArrayList(
|
||||
new PieChart.Data("Transport",
|
||||
Double.parseDouble(resultMap.get("transport"))),
|
||||
new PieChart.Data("Housing",
|
||||
Double.parseDouble(resultMap.get("housing"))),
|
||||
new PieChart.Data("Food",
|
||||
Double.parseDouble(resultMap.get("food"))),
|
||||
new PieChart.Data("Goods",
|
||||
Double.parseDouble(resultMap.get("goods"))),
|
||||
new PieChart.Data("Services",
|
||||
Double.parseDouble(resultMap.get("services")))
|
||||
);
|
||||
pieChart.setTitle("FOOTPRINT DISTRIBUTION");
|
||||
pieChart.setMaxSize(1000, 1000);
|
||||
pieChart.setData(pieChartData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Friends tables are updating.
|
||||
* @throws InterruptedException throws exception
|
||||
|
||||
@@ -301,6 +301,23 @@ public class UserService {
|
||||
.encode().toUri(), Map.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the results of a user.
|
||||
* @param name name of the user
|
||||
* @return Map with all results
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
public Map<String, String> getResults(String name) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getResults")
|
||||
.queryParam("name", name);
|
||||
HttpEntity<?> entity = new HttpEntity<>(headers);
|
||||
System.out.println(builder.build().encode().toUri());
|
||||
return this.restTemplate.getForObject(builder.build()
|
||||
.encode().toUri(), Map.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of all users.
|
||||
*/
|
||||
|
||||
@@ -163,6 +163,12 @@ public class UserServiceTest {
|
||||
userService.getAchievements("mika");
|
||||
Mockito.verify(userService).getAchievements("mika");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getResultsTest() throws Exception {
|
||||
userService.getResults("mika");
|
||||
Mockito.verify(userService).getResults("mika");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -185,5 +185,15 @@ public class UserController {
|
||||
public Map<String, Boolean> getAchievements(@RequestParam(value = "name") String name) {
|
||||
return userService.getAchievements(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method gets the result for schema.
|
||||
* @param name name of the user
|
||||
* @return map of all results of the user
|
||||
*/
|
||||
@RequestMapping("/getResults")
|
||||
public Map<String, String> getResults(@RequestParam(value = "name") String name) {
|
||||
return userService.getResults(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -108,5 +109,43 @@ public class CalculatorService {
|
||||
inputs.put("input_footprint_shopping_goods_total", netShopping + "");
|
||||
user.setFootPrintInputs(inputs);
|
||||
}
|
||||
|
||||
public Map<String, String> getResults(Map<String, String> map) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
headers.set("app_id", "a98272e3");
|
||||
headers.set("app_key", "b9167c4918cb2b3143614b595065d83b");
|
||||
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
|
||||
UriComponentsBuilder builder =
|
||||
UriComponentsBuilder.fromHttpUrl("https://apis.berkeley.edu/coolclimate/footprint");
|
||||
for (String inputId : map.keySet()) {
|
||||
builder = builder.queryParam(inputId, map.get(inputId));
|
||||
}
|
||||
ResponseEntity<String> response = restTemplate
|
||||
.exchange(builder.build().encode().toUri(), HttpMethod.GET,
|
||||
entity, String.class);
|
||||
String transport = response.getBody().substring(Objects.requireNonNull(response.getBody())
|
||||
.indexOf("<result_transport_total>")
|
||||
+ 24, response.getBody().indexOf("</result_transport_total>"));
|
||||
String housing = response.getBody().substring(Objects.requireNonNull(response.getBody())
|
||||
.indexOf("<result_housing_total>")
|
||||
+ 22, response.getBody().indexOf("</result_housing_total>"));
|
||||
String food = response.getBody().substring(Objects.requireNonNull(response.getBody())
|
||||
.indexOf("<result_food_total>")
|
||||
+ 19, response.getBody().indexOf("</result_food_total>"));
|
||||
String goods = response.getBody().substring(Objects.requireNonNull(response.getBody())
|
||||
.indexOf("<result_goods_total>")
|
||||
+ 20, response.getBody().indexOf("</result_goods_total>"));
|
||||
String services = response.getBody().substring(Objects.requireNonNull(response.getBody())
|
||||
.indexOf("<result_services_total>")
|
||||
+ 23, response.getBody().indexOf("</result_services_total>"));
|
||||
Map<String, String> resultMap = new HashMap<>();
|
||||
resultMap.put("transport", transport);
|
||||
resultMap.put("housing", housing);
|
||||
resultMap.put("food", food);
|
||||
resultMap.put("goods", goods);
|
||||
resultMap.put("services", services);
|
||||
return resultMap;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -280,6 +280,17 @@ public class UserService {
|
||||
return user.getAchievements();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method gets all achievements of a user.
|
||||
* @param name name of the user
|
||||
* @return map with all achievements of a user
|
||||
*/
|
||||
public Map<String, String> getResults(String name) {
|
||||
User user = userRepository.findByName(name);
|
||||
Map<String, String> results = calculatorService.getResults(user.getFootPrintInputs());
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method gets the list of all users.
|
||||
* @return list of all users
|
||||
|
||||
@@ -250,4 +250,16 @@ public class UserControllerTest {
|
||||
assertEquals("mika", arg1Captor.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getResultsTest() throws Exception {
|
||||
ArgumentCaptor<String> arg1Captor = ArgumentCaptor.forClass(String.class);
|
||||
mvc.perform(get("/getResults")
|
||||
.param("name", "mika")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
verify(userService, times(1)).getResults(arg1Captor.capture());
|
||||
assertEquals("mika", arg1Captor.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -173,4 +173,138 @@ public class CalculatorServiceTest {
|
||||
Float footPrint = service.invokeExternalService(map);
|
||||
Assert.assertEquals(new Float(11421.537), footPrint);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getResultsTest() {
|
||||
CalculatorService service = new CalculatorService();
|
||||
service.restTemplate = new RestTemplate();
|
||||
Map<String,String> map = new HashMap<String, String>() {{
|
||||
put("input_location", "Chicago");
|
||||
put("input_location_mode", "1");
|
||||
put("input_size", "1");
|
||||
put("input_income", "40000");
|
||||
put("input_population", "1");
|
||||
put("input_changed", "0");
|
||||
put("input_footprint_household_adults", "1");
|
||||
put("input_footprint_household_children", "0");
|
||||
put("input_footprint_transportation_num_vehicles", "1");
|
||||
put("input_footprint_transportation_miles1", "16100");
|
||||
put("input_footprint_transportation_mpg1", "6");
|
||||
put("input_footprint_transportation_fuel1", "2");
|
||||
put("input_footprint_transportation_miles2", "13200");
|
||||
put("input_footprint_transportation_fuel2", "0");
|
||||
put("input_footprint_transportation_mpg2", "22");
|
||||
put("input_footprint_transportation_miles3", "13200");
|
||||
put("input_footprint_transportation_fuel3", "0");
|
||||
put("input_footprint_transportation_mpg3", "22");
|
||||
put("input_footprint_transportation_miles4", "13200");
|
||||
put("input_footprint_transportation_fuel4", "0");
|
||||
put("input_footprint_transportation_mpg4", "22");
|
||||
put("input_footprint_transportation_miles5", "13200");
|
||||
put("input_footprint_transportation_fuel5", "0");
|
||||
put("input_footprint_transportation_mpg5", "22");
|
||||
put("input_footprint_transportation_miles6", "13200");
|
||||
put("input_footprint_transportation_fuel6", "0");
|
||||
put("input_footprint_transportation_mpg6", "22");
|
||||
put("input_footprint_transportation_miles7", "13200");
|
||||
put("input_footprint_transportation_fuel7", "0");
|
||||
put("input_footprint_transportation_mpg7", "22");
|
||||
put("input_footprint_transportation_miles8", "13200");
|
||||
put("input_footprint_transportation_fuel8", "0");
|
||||
put("input_footprint_transportation_mpg8", "22");
|
||||
put("input_footprint_transportation_miles9", "13200");
|
||||
put("input_footprint_transportation_fuel9", "0");
|
||||
put("input_footprint_transportation_mpg9", "22");
|
||||
put("input_footprint_transportation_miles10", "13200");
|
||||
put("input_footprint_transportation_fuel10", "0");
|
||||
put("input_footprint_transportation_mpg10", "22");
|
||||
put("input_footprint_transportation_groundtype", "436");
|
||||
put("input_footprint_transportation_publictrans", "436");
|
||||
put("input_footprint_transportation_bus", "436");
|
||||
put("input_footprint_transportation_transit", "436");
|
||||
put("input_footprint_transportation_commuter", "436");
|
||||
put("input_footprint_transportation_intercity", "436");
|
||||
put("input_footprint_transportation_airtype", "3900");
|
||||
put("input_footprint_transportation_airtotal", "3900");
|
||||
put("input_footprint_transportation_airshort", "3900");
|
||||
put("input_footprint_transportation_airmedium", "3900");
|
||||
put("input_footprint_transportation_airlong", "3900");
|
||||
put("input_footprint_transportation_airextended", "3900");
|
||||
put("input_footprint_housing_cdd", "40000");
|
||||
put("input_footprint_housing_hdd", "40000");
|
||||
put("input_footprint_housing_electricity_type", "40000");
|
||||
put("input_footprint_housing_electrivity_dollars", "40000");
|
||||
put("input_footprint_housing_electricity_kwh", "12632");
|
||||
put("input_footprint_housing_cleanpercent", "0");
|
||||
put("input_footprint_housing_naturalgas_type", "1");
|
||||
put("input_footprint_housing_naturalgas_dollars", "40000");
|
||||
put("input_footprint_housing_naturalgas_therms", "472");
|
||||
put("input_footprint_housing_naturalgas_cuft", "40000");
|
||||
put("input_footprint_housing_heatingoil_type", "40000");
|
||||
put("input_footprint_housing_heatingoil_dollars", "40000");
|
||||
put("input_footprint_housing_heatingoil_gallons", "73");
|
||||
put("input_footprint_housing_heatingoil_dollars_per_gallon", "40000");
|
||||
put("input_footprint_housing_squarefeet", "1850");
|
||||
put("input_footprint_housing_watersewage", "100");
|
||||
put("input_footprint_housing_gco2_per_kwh", "40000");
|
||||
put("input_footprint_shopping_food_meatfisheggs_default", "40000");
|
||||
put("input_footprint_shopping_food_meat_beefpork_default", "40000");
|
||||
put("input_footprint_shopping_food_meat_poultry_default", "40000");
|
||||
put("input_footprint_shopping_food_meat_fish_default", "40000");
|
||||
put("input_footprint_shopping_food_meat_other_default", "40000");
|
||||
put("input_footprint_shopping_food_fruitvegetables_default", "40000");
|
||||
put("input_footprint_shopping_food_dairy_default", "4.2");
|
||||
put("input_footprint_shopping_food_cereals_default", "40000");
|
||||
put("input_footprint_shopping_food_otherfood_default", "40000");
|
||||
put("input_footprint_shopping_food_meattype", "40000");
|
||||
put("input_footprint_shopping_food_meatfisheggs", "2.4");
|
||||
put("input_footprint_shopping_food_meat_beefpork", "2.4");
|
||||
put("input_footprint_shopping_food_meat_poultry", "2.4");
|
||||
put("input_footprint_shopping_food_meat_fish", "2.4");
|
||||
put("input_footprint_shopping_food_meat_other", "2.4");
|
||||
put("input_footprint_shopping_food_cereals", "4.1");
|
||||
put("input_footprint_shopping_food_dairy", "2.2");
|
||||
put("input_footprint_shopping_food_fruitvegetables", "3.5");
|
||||
put("input_footprint_shopping_food_otherfood", "3.4");
|
||||
put("input_footprint_shopping_goods_default_furnitureappliances", "1310");
|
||||
put("input_footprint_shopping_goods_default_clothing", "1310");
|
||||
put("input_footprint_shopping_goods_default_other_entertainment", "1310");
|
||||
put("input_footprint_shopping_goods_default_other_office", "1310");
|
||||
put("input_footprint_shopping_goods_default_other_personalcare", "1310");
|
||||
put("input_footprint_shopping_goods_default_other_autoparts", "1310");
|
||||
put("input_footprint_shopping_goods_default_other_medical", "1310");
|
||||
put("input_footprint_shopping_goods_type", "1310");
|
||||
put("input_footprint_shopping_goods_total", "1310");
|
||||
put("input_footprint_shopping_goods_furnitureappliances", "1310");
|
||||
put("input_footprint_shopping_goods_clothing", "1310");
|
||||
put("input_footprint_shopping_goods_other_type", "1310");
|
||||
put("input_footprint_shopping_goods_other_total", "1310");
|
||||
put("input_footprint_shopping_goods_other_entertainment", "1310");
|
||||
put("input_footprint_shopping_goods_other_office", "1310");
|
||||
put("input_footprint_shopping_goods_other_personalcare", "1310");
|
||||
put("input_footprint_shopping_goods_other_autoparts", "1310");
|
||||
put("input_footprint_shopping_goods_other_medical", "1310");
|
||||
put("input_footprint_shopping_services_type", "1310");
|
||||
put("input_footprint_shopping_services_total", "1310");
|
||||
put("input_footprint_shopping_services_healthcare", "1310");
|
||||
put("input_footprint_shopping_services_education", "1310");
|
||||
put("input_footprint_shopping_services_communications", "1310");
|
||||
put("input_footprint_shopping_services_vehicleservices", "1310");
|
||||
put("input_footprint_shopping_services_finance", "1310");
|
||||
put("input_footprint_shopping_services_household", "1310");
|
||||
put("input_footprint_shopping_services_charity", "1310");
|
||||
put("input_footprint_shopping_services_miscservices", "1310");
|
||||
put("internal_state_abbreviation", "US");
|
||||
put("input_footprint_shopping_services_total", "2413");
|
||||
}
|
||||
};
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("transport", "10769.855687");
|
||||
result.put("housing", "556.831359");
|
||||
result.put("food", "0.028481");
|
||||
result.put("goods", "55.256948");
|
||||
result.put("services", "39.564835");
|
||||
Map<String, String> footPrint = service.getResults(map);
|
||||
Assert.assertEquals(result, footPrint);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user