Skip to content

Commit

Permalink
return pattern values as percentages (#18)
Browse files Browse the repository at this point in the history
* return pattern values as percentages

* remove unnecessary variable & update framework name
  • Loading branch information
horozal authored Feb 16, 2023
1 parent 6079e92 commit 7fde50b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArchitecturalPatterns, Integer> evaluateSurveyInput(@RequestBody List<String> input) {
return calculatePatternValues(input);
}

private HashMap<ArchitecturalPatterns, Integer> calculatePatternValues(final List<String> input) {
// Initialize
final JsonNode surveyEvaluationNode = surveyJsonHandler.getSurveyEvaluationNode();
final HashMap<ArchitecturalPatterns, Integer> 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<ArchitecturalPatterns, Integer> 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<ArchitecturalPatterns, Integer> evaluateSurveyInput(@RequestBody List<String> input) {
return calculatePatternValues(input);
}

private HashMap<ArchitecturalPatterns, Integer> calculatePatternValues(final List<String> input) {
// Initialize
final JsonNode surveyEvaluationNode = surveyJsonHandler.getSurveyEvaluationNode();
final HashMap<ArchitecturalPatterns, Integer> 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<ArchitecturalPatterns, Integer> entry : patternValues.entrySet()) {
double percentage = (entry.getValue() * 100) / (double) totalValue;
patternValues.put(entry.getKey(), (int) percentage);
}
return patternValues;
}

private HashMap<ArchitecturalPatterns, Integer> 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));
}
}
2 changes: 1 addition & 1 deletion src/main/resources/jsonfiles/projectURLs.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 7fde50b

Please sign in to comment.