use of org.apache.nifi.web.api.dto.QueueSizeDTO in project nifi by apache.
the class ListFlowFilesEndpointMerger method mergeResponses.
@Override
protected void mergeResponses(ListingRequestDTO clientDto, Map<NodeIdentifier, ListingRequestDTO> dtoMap, Set<NodeResponse> successfulResponses, Set<NodeResponse> problematicResponses) {
final Comparator<FlowFileSummaryDTO> comparator = new Comparator<FlowFileSummaryDTO>() {
@Override
public int compare(final FlowFileSummaryDTO dto1, final FlowFileSummaryDTO dto2) {
int positionCompare = dto1.getPosition().compareTo(dto2.getPosition());
if (positionCompare != 0) {
return positionCompare;
}
final String address1 = dto1.getClusterNodeAddress();
final String address2 = dto2.getClusterNodeAddress();
if (address1 == null && address2 == null) {
return 0;
}
if (address1 == null) {
return 1;
}
if (address2 == null) {
return -1;
}
return address1.compareTo(address2);
}
};
final NavigableSet<FlowFileSummaryDTO> flowFileSummaries = new TreeSet<>(comparator);
ListFlowFileState state = null;
int numStepsCompleted = 0;
int numStepsTotal = 0;
int objectCount = 0;
long byteCount = 0;
boolean finished = true;
for (final Map.Entry<NodeIdentifier, ListingRequestDTO> entry : dtoMap.entrySet()) {
final NodeIdentifier nodeIdentifier = entry.getKey();
final String nodeAddress = nodeIdentifier.getApiAddress() + ":" + nodeIdentifier.getApiPort();
final ListingRequestDTO nodeRequest = entry.getValue();
numStepsTotal++;
if (Boolean.TRUE.equals(nodeRequest.getFinished())) {
numStepsCompleted++;
}
final QueueSizeDTO nodeQueueSize = nodeRequest.getQueueSize();
objectCount += nodeQueueSize.getObjectCount();
byteCount += nodeQueueSize.getByteCount();
if (!nodeRequest.getFinished()) {
finished = false;
}
if (nodeRequest.getLastUpdated().after(clientDto.getLastUpdated())) {
clientDto.setLastUpdated(nodeRequest.getLastUpdated());
}
// Keep the state with the lowest ordinal value (the "least completed").
final ListFlowFileState nodeState = ListFlowFileState.valueOfDescription(nodeRequest.getState());
if (state == null || state.compareTo(nodeState) > 0) {
state = nodeState;
}
if (nodeRequest.getFlowFileSummaries() != null) {
for (final FlowFileSummaryDTO summaryDTO : nodeRequest.getFlowFileSummaries()) {
if (summaryDTO.getClusterNodeId() == null || summaryDTO.getClusterNodeAddress() == null) {
summaryDTO.setClusterNodeId(nodeIdentifier.getId());
summaryDTO.setClusterNodeAddress(nodeAddress);
}
flowFileSummaries.add(summaryDTO);
// Keep the set from growing beyond our max
if (flowFileSummaries.size() > clientDto.getMaxResults()) {
flowFileSummaries.pollLast();
}
}
}
if (nodeRequest.getFailureReason() != null) {
clientDto.setFailureReason(nodeRequest.getFailureReason());
}
}
final List<FlowFileSummaryDTO> summaryDTOs = new ArrayList<>(flowFileSummaries);
clientDto.setFlowFileSummaries(summaryDTOs);
// depends on invariant if numStepsTotal is 0, so is numStepsCompleted, all steps being completed
// would be 1
final int percentCompleted = (numStepsTotal == 0) ? 1 : numStepsCompleted / numStepsTotal;
clientDto.setPercentCompleted(percentCompleted);
clientDto.setFinished(finished);
clientDto.getQueueSize().setByteCount(byteCount);
clientDto.getQueueSize().setObjectCount(objectCount);
}
Aggregations