From 7fde50b154b5b53e79795adb0276fdc7dfc5b6ed Mon Sep 17 00:00:00 2001 From: horozal <36187605+horozal@users.noreply.github.com> Date: Thu, 16 Feb 2023 14:14:08 +0100 Subject: [PATCH] return pattern values as percentages (#18) * return pattern values as percentages * remove unnecessary variable & update framework name --- .../selection/PatternSelectionController.java | 94 +++++++++---------- src/main/resources/jsonfiles/projectURLs.json | 2 +- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/eclipse/opensmartclide/architecturalpatterns/selection/PatternSelectionController.java b/src/main/java/org/eclipse/opensmartclide/architecturalpatterns/selection/PatternSelectionController.java index 92ce998..e56c39f 100644 --- a/src/main/java/org/eclipse/opensmartclide/architecturalpatterns/selection/PatternSelectionController.java +++ b/src/main/java/org/eclipse/opensmartclide/architecturalpatterns/selection/PatternSelectionController.java @@ -13,52 +13,50 @@ @RestController public class PatternSelectionController { - private final ArchitecturalPatternsJsonHandler surveyJsonHandler; - - public PatternSelectionController(final ArchitecturalPatternsJsonHandler surveyJsonHandler) { - this.surveyJsonHandler = surveyJsonHandler; - } - - @PostMapping( - value = "/evaluation", - consumes = MediaType.APPLICATION_JSON_VALUE, - produces = MediaType.APPLICATION_JSON_VALUE - ) - public Map evaluateSurveyInput(@RequestBody List input) { - return calculatePatternValues(input); - } - - private HashMap calculatePatternValues(final List input) { - // Initialize - final JsonNode surveyEvaluationNode = surveyJsonHandler.getSurveyEvaluationNode(); - final HashMap patternValues = initializePatternValues(); - - // Iterate over survey question IDs - for (String id : input) { - if (surveyEvaluationNode.get(id) == null) { - throw new IllegalArgumentException("Invalid survey input received: " + id + "is not a valid question ID."); - } - JsonNode valuesJsonNode = surveyEvaluationNode.get(id); - - for (ArchitecturalPatterns pattern : ArchitecturalPatterns.values()) { - int newValue = patternValues.get(pattern) + valuesJsonNode.get(pattern.name()).asInt(); - // Update evaluation score for pattern - patternValues.put(pattern, newValue); - } - } - return patternValues; - } - - private HashMap initializePatternValues() { - return new HashMap<>( - Map.of( - ArchitecturalPatterns.LAYERED, 0, - ArchitecturalPatterns.EVENT_DRIVEN, 0, - ArchitecturalPatterns.MICROKERNEL, 0, - ArchitecturalPatterns.MICROSERVICES, 0, - ArchitecturalPatterns.SERVICE_ORIENTED, 0, - ArchitecturalPatterns.SPACE_BASED, 0 - ) - ); - } + private final ArchitecturalPatternsJsonHandler surveyJsonHandler; + + public PatternSelectionController(final ArchitecturalPatternsJsonHandler surveyJsonHandler) { + this.surveyJsonHandler = surveyJsonHandler; + } + + @PostMapping(value = "/evaluation", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public Map evaluateSurveyInput(@RequestBody List input) { + return calculatePatternValues(input); + } + + private HashMap calculatePatternValues(final List input) { + // Initialize + final JsonNode surveyEvaluationNode = surveyJsonHandler.getSurveyEvaluationNode(); + final HashMap patternValues = initializePatternValues(); + int totalValue = 0; + + // Iterate over survey question IDs + for (String id : input) { + if (surveyEvaluationNode.get(id) == null) { + throw new IllegalArgumentException( + "Invalid survey input received: " + id + "is not a valid question ID."); + } + JsonNode valuesJsonNode = surveyEvaluationNode.get(id); + + for (ArchitecturalPatterns pattern : ArchitecturalPatterns.values()) { + int value = valuesJsonNode.get(pattern.name()).asInt(); + int newValue = patternValues.get(pattern) + value; + // Update evaluation score for pattern + patternValues.put(pattern, newValue); + totalValue = totalValue + value; + } + } + + for (Map.Entry entry : patternValues.entrySet()) { + double percentage = (entry.getValue() * 100) / (double) totalValue; + patternValues.put(entry.getKey(), (int) percentage); + } + return patternValues; + } + + private HashMap initializePatternValues() { + return new HashMap<>(Map.of(ArchitecturalPatterns.LAYERED, 0, ArchitecturalPatterns.EVENT_DRIVEN, 0, + ArchitecturalPatterns.MICROKERNEL, 0, ArchitecturalPatterns.MICROSERVICES, 0, + ArchitecturalPatterns.SERVICE_ORIENTED, 0, ArchitecturalPatterns.SPACE_BASED, 0)); + } } diff --git a/src/main/resources/jsonfiles/projectURLs.json b/src/main/resources/jsonfiles/projectURLs.json index ddfd1a5..bae2561 100644 --- a/src/main/resources/jsonfiles/projectURLs.json +++ b/src/main/resources/jsonfiles/projectURLs.json @@ -1,5 +1,5 @@ { - "Java with Spring Boot and MySQL": { + "Java_with_Spring_Boot_and_MySQL": { "Layered": "https://github.com/che-samples/web-java-spring-boot", "Event-driven": "https://github.com/spring-projects/spring-kafka", "Microkernel": "https://github.com/devexpress-albania/microkernel",