Skip to content

Commit

Permalink
Adjusted ExperimentsDTO
Browse files Browse the repository at this point in the history
  • Loading branch information
dschiese committed Aug 30, 2024
1 parent 58b1018 commit e130b3d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public ResponseEntity<?> storeCorrectness(@PathVariable String hash, @PathVariab
}
}

@PostMapping(value = "/storeunderstandability/{id}/{tuple}", consumes = "application/json")
public ResponseEntity<?> storeUnderstandability(@PathVariable String id, @PathVariable String tuple, @RequestBody Understandability_Metric understandabilityMetric) {
@PostMapping(value = "/storeunderstandability/{temperature}/{id}/{tuple}", consumes = "application/json")
public ResponseEntity<?> storeUnderstandability(@PathVariable String temperature, @PathVariable String id, @PathVariable String tuple, @RequestBody Understandability_Metric understandabilityMetric) {
try {
service.storeUnderstandabilityEntry(id, tuple, understandabilityMetric);
service.storeUnderstandabilityEntry(temperature, id, tuple, understandabilityMetric);
return new ResponseEntity<>("Entry updated successfully", HttpStatus.OK);
} catch(Exception e) {
return new ResponseEntity<>("Failed to update entry", HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/wseresearch/backend/helper/ExperimentsDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class ExperimentsDTO {
@Id
private String id;
private Map<String, Integer> correctnessExperiments; // Hash, errors
private Map<String, Understandability_Metric> understandabilityExperiments; // Tuple, {best,worst}
private Map<String, Map<String,Understandability_Metric>> understandabilityExperiments; // Tuple, {best,worst}

public ExperimentsDTO() {
}

public ExperimentsDTO(String id, Map<String, Integer> correctnessExperiments, Map<String, Understandability_Metric> understandabilityExperiments) {
public ExperimentsDTO(String id, Map<String, Integer> correctnessExperiments, Map<String, Map<String,Understandability_Metric>> understandabilityExperiments) {
this.id = id;
this.correctnessExperiments = correctnessExperiments;
this.understandabilityExperiments = understandabilityExperiments;
Expand All @@ -26,7 +26,7 @@ public Map<String, Integer> getCorrectnessExperiments() {
return correctnessExperiments;
}

public Map<String, Understandability_Metric> getUnderstandabilityExperiments() {
public Map<String, Map<String,Understandability_Metric>> getUnderstandabilityExperiments() {
return understandabilityExperiments;
}

Expand All @@ -38,11 +38,11 @@ public void setCorrectnessExperiment(String hash, Integer errors) {
this.correctnessExperiments.replace(hash, errors);
}

public void setUnderstandabilityExperiment(String tuple, Understandability_Metric understandabilityMetric) {
this.understandabilityExperiments.replace(tuple, understandabilityMetric);
public void setUnderstandabilityExperiment(String temperature, String tuple, Understandability_Metric understandabilityMetric) {
this.understandabilityExperiments.get(temperature).replace(tuple, understandabilityMetric);
}

public void setUnderstandabilityExperiments(Map<String, Understandability_Metric> understandabilityExperiments) {
public void setUnderstandabilityExperiments(Map<String, Map<String,Understandability_Metric>> understandabilityExperiments) {
this.understandabilityExperiments = understandabilityExperiments;
}

Expand Down
28 changes: 23 additions & 5 deletions src/main/java/org/wseresearch/backend/services/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.transaction.TransactionSystemException;
import org.wseresearch.backend.helper.*;
import org.wseresearch.backend.repositories.ExperimentsRepository;

@org.springframework.stereotype.Service
public class Service {
public class Service {

@Autowired
private ExperimentsRepository experimentsRepository;
Expand Down Expand Up @@ -45,7 +47,7 @@ public void storeExperiment(ExperimentsDTO experimentsDTO) {

public ExperimentsDTO createUser() {
String id = UUID.randomUUID().toString();
ExperimentsDTO experimentsDTO = new ExperimentsDTO(id, correctnessExperiments, understandabilityExperiments);
ExperimentsDTO experimentsDTO = new ExperimentsDTO(id, correctnessExperiments, /*understandabilityExperiments*/null);//TODO!
try {
experimentsRepository.insert(experimentsDTO);
} catch(Exception e) {
Expand All @@ -62,6 +64,7 @@ public boolean checkUserExist(String id) {
return experimentsRepository.existsById(id);
}

// TODO!
private Map<String, Understandability_Metric> initUnderstandabilityExperiments(String experiment) throws IOException {
Map<String, Understandability_Metric> understandabilityExperiments = new HashMap<>();
try(InputStream inputStream = new ClassPathResource(experiment).getInputStream()) {
Expand All @@ -76,10 +79,25 @@ private Map<String, Understandability_Metric> initUnderstandabilityExperiments(S
return understandabilityExperiments;
}

public void storeUnderstandabilityEntry(String id, String tuple, Understandability_Metric understandabilityMetric) {

public void storeUnderstandabilityEntry(String temperature, String id, String tuple, Understandability_Metric understandabilityMetric) {
ExperimentsDTO experimentsDTO = this.findExperimentsByUser(id);
experimentsDTO.setUnderstandabilityExperiment(tuple,understandabilityMetric);
this.experimentsRepository.save(experimentsDTO);
experimentsDTO.setUnderstandabilityExperiment(temperature, tuple, understandabilityMetric);
try {
// Assuming savedEntity is the result of the save operation
ExperimentsDTO savedEntity = experimentsRepository.save(experimentsDTO);
if (savedEntity != null && savedEntity.getId() != null) {
logger.info("Save operation was successful.");
} else {
logger.info("Save operation might have failed or the entity doesn't get its ID generated upon saving.");
}
} catch (DataIntegrityViolationException e) {
logger.error("Data integrity violation while saving the entity: " + e.getMessage());
} catch (TransactionSystemException e) {
logger.error("Transaction system exception while saving the entity: " + e.getMessage());
} catch (Exception e) {
logger.error("An unexpected error occurred while saving the entity: " + e.getMessage());
}
}

private Map<String, Integer> initCorrectnessExperiments(String experiment) throws IOException {
Expand Down

0 comments on commit e130b3d

Please sign in to comment.