Skip to content
Snippets Groups Projects
Commit 2be5907e authored by Pavel Šeda's avatar Pavel Šeda
Browse files

Merge branch '100-automatic-check-if-logging-works' into 'develop'

Resolve "Automatic check if logging works"

See merge request !113
parents 01f0c2d1 bdd0385b
No related branches found
No related tags found
2 merge requests!119Develop,!113Resolve "Automatic check if logging works"
Pipeline #198834 passed with stages
in 7 minutes and 41 seconds
......@@ -33,6 +33,10 @@ public class TrainingRunDTO {
private Long sandboxInstanceRefId;
@ApiModelProperty(value = "Reference to participant of training run.")
private UserRefDTO participantRef;
@ApiModelProperty(value = "Boolean to check whether event logging works.", example = "true")
private boolean eventLoggingState;
@ApiModelProperty(value = "Boolean to check whether command logging works.", example = "true")
private boolean commandLoggingState;
/**
* Gets id.
......@@ -142,6 +146,43 @@ public class TrainingRunDTO {
this.participantRef = participantRef;
}
/**
* Gets event logging state
*
* @return the event logging state
*/
public boolean getEventLoggingState() {
return eventLoggingState;
}
/**
* Sets event logging state
*
* @param eventLoggingState the new event logging state
*/
public void setEventLoggingState(boolean eventLoggingState) {
this.eventLoggingState = eventLoggingState;
}
/**
* Gets command logging state
*
* @return the command logging state
*/
public boolean getCommandLoggingState() {
return commandLoggingState;
}
/**
* Sets command logging state
*
* @param commandLoggingState the new command logging state
*/
public void setCommandLoggingState(boolean commandLoggingState) {
this.commandLoggingState = commandLoggingState;
}
@Override
public String toString() {
return "TrainingRunDTO{" +
......@@ -151,6 +192,8 @@ public class TrainingRunDTO {
", state=" + state +
", sandboxInstanceRefId=" + sandboxInstanceRefId +
", participantRef=" + participantRef +
", eventLoggingState=" + eventLoggingState +
", commandLoggingState=" + commandLoggingState +
'}';
}
......
......@@ -34,8 +34,10 @@ import org.springframework.stereotype.Service;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
......@@ -330,11 +332,24 @@ public class TrainingInstanceFacade {
@TransactionalRO
public PageResultResource<TrainingRunDTO> findTrainingRunsByTrainingInstance(Long trainingInstanceId, Boolean isActive, Pageable pageable) {
Page<TrainingRun> trainingRuns = trainingInstanceService.findTrainingRunsByTrainingInstance(trainingInstanceId, isActive, pageable);
PageResultResource<TrainingRunDTO> trainingRunDTOsPageResult = trainingRunMapper.mapToPageResultResource(trainingRuns);
Set<Long> runIdsWithWorkingEventLogging = checkLogging(trainingRuns, trainingRunService::checkRunEventLogging);
Set<Long> runIdsWithWorkingCommandLogging = checkLogging(trainingRuns, trainingRunService::checkRunCommandLogging);
PageResultResource<TrainingRunDTO> trainingRunDTOsPageResult = trainingRunMapper.mapToPageResultResourceLogging(trainingRuns, runIdsWithWorkingEventLogging, runIdsWithWorkingCommandLogging);
addParticipantsToTrainingRunDTOs(trainingRunDTOsPageResult.getContent());
return trainingRunDTOsPageResult;
}
private Set<Long> checkLogging(Page<TrainingRun> runs, Function<TrainingRun, Boolean> checker) {
Set<Long> runIdsWithWorkingLogging = new HashSet<>();
runs.forEach(trainingRun -> {
if (checker.apply(trainingRun)) {
runIdsWithWorkingLogging.add(trainingRun.getId());
}
});
return runIdsWithWorkingLogging;
}
private void addParticipantsToTrainingRunDTOs(List<TrainingRunDTO> trainingRunDTOS) {
trainingRunDTOS.forEach(trainingRunDTO ->
trainingRunDTO.setParticipantRef(userManagementServiceApi.getUserRefDTOByUserRefId(trainingRunDTO.getParticipantRef().getUserRefId())));
......
......@@ -56,6 +56,17 @@ public interface TrainingRunMapper extends ParentMapper {
return new PageResultResource<>(mapped, createPagination(objects));
}
default PageResultResource<TrainingRunDTO> mapToPageResultResourceLogging(Page<TrainingRun> objects, Set<Long> workingEvents, Set<Long> workingCommands) {
List<TrainingRunDTO> mapped = new ArrayList<>();
objects.forEach(object -> {
TrainingRunDTO runDTO = mapToDTO(object);
runDTO.setEventLoggingState(workingEvents.contains(runDTO.getId()));
runDTO.setCommandLoggingState(workingCommands.contains(runDTO.getId()));
mapped.add(runDTO);
});
return new PageResultResource<>(mapped, createPagination(objects));
}
default PageResultResource<AccessedTrainingRunDTO> mapToPageResultResourceAccessed(Page<AccessedTrainingRunDTO> objects) {
List<AccessedTrainingRunDTO> mapped = new ArrayList<>();
objects.forEach(mapped::add);
......
......@@ -220,6 +220,36 @@ public class TrainingRunService {
return trainingRunRepository.findAllByTrainingInstanceId(trainingInstanceId);
}
/**
* Check if run event logging works
*
* @param run run to check
* @return resulting boolean
*/
public boolean checkRunEventLogging(TrainingRun run) {
return !elasticsearchServiceApi.findAllEventsFromTrainingRun(run).isEmpty();
}
/**
* Check if run command logging works
*
* @param run run to check
* @return resulting boolean
*/
public boolean checkRunCommandLogging(TrainingRun run) {
List<Map<String, Object>> runCommands;
if (run.getTrainingInstance().isLocalEnvironment()) {
String accessToken = run.getTrainingInstance().getAccessToken();
Long userId = run.getParticipantRef().getUserRefId();
runCommands = elasticsearchServiceApi.findAllConsoleCommandsByAccessTokenAndUserId(accessToken, userId);
} else {
Long sandboxId = run.getSandboxInstanceRefId() == null ? run.getPreviousSandboxInstanceRefId() : run.getSandboxInstanceRefId();
runCommands = elasticsearchServiceApi.findAllConsoleCommandsBySandbox(sandboxId);
}
return !runCommands.isEmpty();
}
/**
* Gets next phase of given Training Run and set new current phase.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment