NIFI-5924 Labels should be searchable

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #4070
This commit is contained in:
MatthewKnight-NG 2019-11-22 15:14:22 -05:00 committed by Matthew Burgess
parent 9848eb4409
commit acaf321af0
No known key found for this signature in database
GPG Key ID: 05D3DEB8126DAD24
4 changed files with 98 additions and 2 deletions

View File

@ -35,6 +35,7 @@ public class SearchResultsDTO {
private List<ComponentSearchResultDTO> outputPortResults = new ArrayList<>();
private List<ComponentSearchResultDTO> remoteProcessGroupResults = new ArrayList<>();
private List<ComponentSearchResultDTO> funnelResults = new ArrayList<>();
private List<ComponentSearchResultDTO> labelResults = new ArrayList<>();
private List<ComponentSearchResultDTO> parameterContextResults = new ArrayList<>();
private List<ComponentSearchResultDTO> parameterResults = new ArrayList<>();
@ -136,6 +137,20 @@ public class SearchResultsDTO {
this.funnelResults = funnelResults;
}
/**
* @return labels that matched the search
*/
@ApiModelProperty(
value = "The labels that matched the search."
)
public List<ComponentSearchResultDTO> getLabelResults() {
return labelResults;
}
public void setLabelResults(List<ComponentSearchResultDTO> labelResults) {
this.labelResults = labelResults;
}
/**
* @return parameter contexts that matched the search.
*/

View File

@ -31,6 +31,7 @@ import org.apache.nifi.connectable.Port;
import org.apache.nifi.controller.FlowController;
import org.apache.nifi.controller.ProcessorNode;
import org.apache.nifi.controller.ScheduledState;
import org.apache.nifi.controller.label.Label;
import org.apache.nifi.controller.queue.FlowFileQueue;
import org.apache.nifi.flowfile.FlowFilePrioritizer;
import org.apache.nifi.groups.ProcessGroup;
@ -74,8 +75,8 @@ public class ControllerSearchService {
* Searches term in the controller beginning from a given process group.
*
* @param results Search results
* @param search The search term
* @param group The init process group
* @param search The search term
* @param group The init process group
*/
public void search(final SearchResultsDTO results, final String search, final ProcessGroup group) {
final NiFiUser user = NiFiUserUtils.getNiFiUser();
@ -162,6 +163,18 @@ public class ControllerSearchService {
}
}
for (final Label label : group.getLabels()) {
if (label.isAuthorized(authorizer, RequestAction.READ, user)) {
final ComponentSearchResultDTO match = search(search, label);
if (match != null) {
match.setGroupId(group.getIdentifier());
match.setParentGroup(buildResultGroup(group, user));
match.setVersionedGroup(buildVersionedGroup(group, user));
results.getLabelResults().add(match);
}
}
}
for (final ProcessGroup processGroup : group.getProcessGroups()) {
search(results, search, processGroup);
}
@ -511,6 +524,22 @@ public class ControllerSearchService {
return dto;
}
private ComponentSearchResultDTO search(final String searchStr, final Label label) {
final List<String> matches = new ArrayList<>();
addIfAppropriate(searchStr, label.getIdentifier(), "Id", matches);
addIfAppropriate(searchStr, label.getValue(), "Value", matches);
if (matches.isEmpty()) {
return null;
}
final ComponentSearchResultDTO dto = new ComponentSearchResultDTO();
dto.setId(label.getIdentifier());
dto.setName(label.getValue());
dto.setMatches(matches);
return dto;
}
private ComponentSearchResultDTO search(final String searchString, final ParameterContext parameterContext) {
final List<String> matches = new ArrayList<>();
addIfAppropriate(searchString, parameterContext.getIdentifier(), "Id", matches);

View File

@ -23,6 +23,7 @@ import org.apache.nifi.controller.FlowController;
import org.apache.nifi.controller.ProcessorNode;
import org.apache.nifi.controller.StandardProcessorNode;
import org.apache.nifi.controller.flow.FlowManager;
import org.apache.nifi.controller.label.Label;
import org.apache.nifi.groups.ProcessGroup;
import org.apache.nifi.parameter.Parameter;
import org.apache.nifi.parameter.ParameterContext;
@ -403,6 +404,49 @@ public class ControllerSearchServiceTest {
assertEquals(0, searchResultsDTO.getParameterResults().size());
}
@Test
public void testSearchLabels() {
// root level PG
final ProcessGroup rootProcessGroup = setupMockedProcessGroup("root", null, true, variableRegistry, null);
// setup labels
setupMockedLabels(rootProcessGroup);
// perform search for foo
service.search(searchResultsDTO, "FOO", rootProcessGroup);
assertTrue(searchResultsDTO.getLabelResults().size() == 1);
assertTrue(searchResultsDTO.getLabelResults().get(0).getId().equals("foo"));
assertTrue(searchResultsDTO.getLabelResults().get(0).getName().equals("Value for label foo"));
}
/**
* Mocks Labels including isAuthorized() and their identifier and value
*
* @param containingProcessGroup The process group
*/
private static void setupMockedLabels(final ProcessGroup containingProcessGroup) {
final Label label1 = mock(Label.class);
Mockito.doReturn(true).when(label1).isAuthorized(AdditionalMatchers.or(any(Authorizer.class), isNull()), eq(RequestAction.READ),
AdditionalMatchers.or(any(NiFiUser.class), isNull()));
Mockito.doReturn("foo").when(label1).getIdentifier();
Mockito.doReturn("Value for label foo").when(label1).getValue();
final Label label2 = mock(Label.class);
Mockito.doReturn(false).when(label2).isAuthorized(AdditionalMatchers.or(any(Authorizer.class), isNull()), eq(RequestAction.READ),
AdditionalMatchers.or(any(NiFiUser.class), isNull()));
Mockito.doReturn("bar").when(label2).getIdentifier();
Mockito.doReturn("Value for label bar, but FOO is in here too").when(label2).getValue();
// assign labels to the PG
Mockito.doReturn(new HashSet<Label>() {
{
add(label1);
add(label2);
}
}).when(containingProcessGroup).getLabels();
}
/**
* Sets up a mock Parameter Context including isAuthorized()
* @param name name of the parameter context

View File

@ -200,6 +200,14 @@
});
}
// show all labels
if (!nfCommon.isEmpty(searchResults.labelResults)) {
ul.append('<li class="search-header"><div class="search-result-icon icon icon-label"></div>Labels</li>');
$.each(searchResults.labelResults, function (i, labelMatch) {
nfSearchAutocomplete._renderItem(ul, $.extend({}, labelMatch, { type: 'label' }));
});
}
// show all parameter contexts and parameters
if (!nfCommon.isEmpty(searchResults.parameterContextResults)) {
ul.append('<li class="search-header"><div class="search-result-icon icon"></div>Parameter Contexts</li>');