Search in sources :

Example 1 with ViewEntity

use of org.graylog2.contentpacks.model.entities.ViewEntity in project graylog2-server by Graylog2.

the class ViewFacade method decode.

protected NativeEntity<ViewDTO> decode(EntityV1 entityV1, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities, User user) {
    final ViewEntity viewEntity = objectMapper.convertValue(entityV1.data(), ViewEntity.class);
    final Map<String, ViewStateDTO> viewStateMap = new LinkedHashMap<>(viewEntity.state().size());
    for (Map.Entry<String, ViewStateEntity> entry : viewEntity.state().entrySet()) {
        final ViewStateEntity entity = entry.getValue();
        viewStateMap.put(entry.getKey(), entity.toNativeEntity(parameters, nativeEntities));
    }
    final ViewDTO.Builder viewBuilder = viewEntity.toNativeEntity(parameters, nativeEntities);
    viewBuilder.state(viewStateMap);
    final Search search = viewEntity.search().toNativeEntity(parameters, nativeEntities);
    final Search persistedSearch = searchDbService.save(search);
    final ViewDTO persistedView = viewService.saveWithOwner(viewBuilder.searchId(persistedSearch.id()).build(), user);
    return NativeEntity.create(entityV1.id(), persistedView.id(), getModelType(), persistedView.title(), persistedView);
}
Also used : ViewStateEntity(org.graylog2.contentpacks.model.entities.ViewStateEntity) ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) ViewStateDTO(org.graylog.plugins.views.search.views.ViewStateDTO) ViewEntity(org.graylog2.contentpacks.model.entities.ViewEntity) Search(org.graylog.plugins.views.search.Search) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ViewEntity

use of org.graylog2.contentpacks.model.entities.ViewEntity in project graylog2-server by Graylog2.

the class DashboardV1Facade method resolveEntityV1.

@SuppressWarnings("UnstableApiUsage")
private Graph<Entity> resolveEntityV1(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Entity> entities) {
    final DashboardEntity dashboardEntity = objectMapper.convertValue(entity.data(), DashboardEntity.class);
    final ViewEntity viewEntity = entityConverter.convert(dashboardEntity, parameters);
    return resolveViewEntity(entity, viewEntity, entities);
}
Also used : DashboardEntity(org.graylog2.contentpacks.model.entities.DashboardEntity) ViewEntity(org.graylog2.contentpacks.model.entities.ViewEntity)

Example 3 with ViewEntity

use of org.graylog2.contentpacks.model.entities.ViewEntity in project graylog2-server by Graylog2.

the class EntityConverter method convert.

public ViewEntity convert(DashboardEntity dashboardEntity, Map<String, ValueReference> parameters) {
    this.parameters = parameters;
    this.dashboardEntity = dashboardEntity;
    final String queryId = UUID.randomUUID().toString();
    final Map<DashboardWidgetEntity, List<WidgetEntity>> widgets = convertWidgets();
    final Map<String, WidgetPositionDTO> widgetPositionMap = DashboardEntity.positionMap(parameters, widgets);
    final Titles titles = DashboardEntity.widgetTitles(widgets, parameters);
    final Map<String, Set<String>> widgetMapping = new HashMap<>();
    final Set<SearchTypeEntity> searchTypes = new HashSet<>();
    createSearchTypes(widgets, widgetMapping, searchTypes);
    SearchEntity searchEntity;
    try {
        searchEntity = createSearchEntity(queryId, searchTypes);
    } catch (InvalidRangeParametersException e) {
        throw new IllegalArgumentException("The provided entity does not have a valid TimeRange", e);
    }
    final ViewStateEntity viewStateEntity = ViewStateEntity.builder().widgets(widgets.values().stream().flatMap(Collection::stream).collect(Collectors.toSet())).titles(titles).widgetMapping(widgetMapping).widgetPositions(widgetPositionMap).build();
    final Map<String, ViewStateEntity> viewStateEntityMap = ImmutableMap.of(queryId, viewStateEntity);
    return ViewEntity.builder().search(searchEntity).state(viewStateEntityMap).title(dashboardEntity.title()).properties(Collections.emptySet()).description(dashboardEntity.description()).requires(Collections.emptyMap()).summary(ValueReference.of("Converted Dashboard")).createdAt(DateTime.now(DateTimeZone.UTC)).type(ViewEntity.Type.DASHBOARD).build();
}
Also used : ViewStateEntity(org.graylog2.contentpacks.model.entities.ViewStateEntity) SearchTypeEntity(org.graylog2.contentpacks.model.entities.SearchTypeEntity) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) InvalidRangeParametersException(org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException) HashMap(java.util.HashMap) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString) Titles(org.graylog.plugins.views.search.views.Titles) SearchEntity(org.graylog2.contentpacks.model.entities.SearchEntity) DashboardWidgetEntity(org.graylog2.contentpacks.model.entities.DashboardWidgetEntity) Collection(java.util.Collection) List(java.util.List) WidgetPositionDTO(org.graylog.plugins.views.search.views.WidgetPositionDTO) HashSet(java.util.HashSet)

Example 4 with ViewEntity

use of org.graylog2.contentpacks.model.entities.ViewEntity in project graylog2-server by Graylog2.

the class ViewFacadeTest method itShouldCreateADTOFromAnEntity.

@Test
@MongoDBFixtures("ViewFacadeTest.json")
public void itShouldCreateADTOFromAnEntity() throws Exception {
    final StreamImpl stream = new StreamImpl(Collections.emptyMap());
    final Entity viewEntity = createViewEntity();
    final Map<EntityDescriptor, Object> nativeEntities = new HashMap<>(1);
    nativeEntities.put(EntityDescriptor.create(newStreamId, ModelTypes.STREAM_V1), stream);
    final UserImpl fakeUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "testuser"));
    when(userService.load("testuser")).thenReturn(fakeUser);
    final NativeEntity<ViewDTO> nativeEntity = facade.createNativeEntity(viewEntity, Collections.emptyMap(), nativeEntities, "testuser");
    assertThat(nativeEntity.descriptor().title()).isEqualTo("title");
    assertThat(nativeEntity.descriptor().type()).isEqualTo(ModelTypes.SEARCH_V1);
    Optional<ViewDTO> resultedView = viewService.get(nativeEntity.descriptor().id().id());
    assertThat(resultedView).isPresent();
    Optional<Search> search = searchDbService.get(resultedView.get().searchId());
    assertThat(search).isPresent();
    final Query query = search.get().queries().iterator().next();
    assertThat(query.filter()).isNotNull();
    assertThat(query.filter().filters()).isNotEmpty();
    final StreamFilter streamFilter = (StreamFilter) query.filter().filters().iterator().next();
    assertThat(streamFilter.streamId()).doesNotMatch(newStreamId);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) PivotEntity(org.graylog2.contentpacks.model.entities.PivotEntity) QueryEntity(org.graylog2.contentpacks.model.entities.QueryEntity) EventListEntity(org.graylog2.contentpacks.model.entities.EventListEntity) ViewEntity(org.graylog2.contentpacks.model.entities.ViewEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) SearchEntity(org.graylog2.contentpacks.model.entities.SearchEntity) ViewStateEntity(org.graylog2.contentpacks.model.entities.ViewStateEntity) StreamEntity(org.graylog2.contentpacks.model.entities.StreamEntity) MessageListEntity(org.graylog2.contentpacks.model.entities.MessageListEntity) Query(org.graylog.plugins.views.search.Query) HashMap(java.util.HashMap) StreamFilter(org.graylog.plugins.views.search.filter.StreamFilter) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) StreamImpl(org.graylog2.streams.StreamImpl) Search(org.graylog.plugins.views.search.Search) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 5 with ViewEntity

use of org.graylog2.contentpacks.model.entities.ViewEntity in project graylog2-server by Graylog2.

the class ViewFacadeTest method itShouldCreateAViewEntity.

@Test
@MongoDBFixtures("ViewFacadeTest.json")
public void itShouldCreateAViewEntity() {
    final ViewDTO viewDTO = viewService.get(viewId).orElseThrow(() -> new NotFoundException("Missing view with id: " + viewId));
    final EntityDescriptor searchDescriptor = EntityDescriptor.create(viewDTO.id(), ModelTypes.SEARCH_V1);
    final EntityDescriptor streamDescriptor = EntityDescriptor.create(streamId, ModelTypes.STREAM_V1);
    final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(searchDescriptor, streamDescriptor);
    final Optional<Entity> optionalEntity = facade.exportEntity(searchDescriptor, entityDescriptorIds);
    assertThat(optionalEntity).isPresent();
    final Entity entity = optionalEntity.get();
    assertThat(entity).isInstanceOf(EntityV1.class);
    final EntityV1 entityV1 = (EntityV1) entity;
    assertThat(entityV1.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(searchDescriptor).orElse(null)));
    assertThat(entityV1.type()).isEqualTo(ModelTypes.SEARCH_V1);
    final ViewEntity viewEntity = objectMapper.convertValue(entityV1.data(), ViewEntity.class);
    assertThat(viewEntity.title().asString()).isEqualTo(viewDTO.title());
    assertThat(viewEntity.type().toString()).isEqualTo(ViewDTO.Type.SEARCH.toString());
    assertThat(viewEntity.search().queries().size()).isEqualTo(1);
    final QueryEntity queryEntity = viewEntity.search().queries().iterator().next();
    assertThat(queryEntity.filter().filters().size()).isEqualTo(1);
    final StreamFilter filter = (StreamFilter) queryEntity.filter().filters().iterator().next();
    assertThat(filter.streamId()).doesNotMatch(streamId);
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) PivotEntity(org.graylog2.contentpacks.model.entities.PivotEntity) QueryEntity(org.graylog2.contentpacks.model.entities.QueryEntity) EventListEntity(org.graylog2.contentpacks.model.entities.EventListEntity) ViewEntity(org.graylog2.contentpacks.model.entities.ViewEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) SearchEntity(org.graylog2.contentpacks.model.entities.SearchEntity) ViewStateEntity(org.graylog2.contentpacks.model.entities.ViewStateEntity) StreamEntity(org.graylog2.contentpacks.model.entities.StreamEntity) MessageListEntity(org.graylog2.contentpacks.model.entities.MessageListEntity) ViewEntity(org.graylog2.contentpacks.model.entities.ViewEntity) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) NotFoundException(javax.ws.rs.NotFoundException) QueryEntity(org.graylog2.contentpacks.model.entities.QueryEntity) StreamFilter(org.graylog.plugins.views.search.filter.StreamFilter) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

ViewEntity (org.graylog2.contentpacks.model.entities.ViewEntity)7 SearchEntity (org.graylog2.contentpacks.model.entities.SearchEntity)5 ViewStateEntity (org.graylog2.contentpacks.model.entities.ViewStateEntity)5 Search (org.graylog.plugins.views.search.Search)3 ViewDTO (org.graylog.plugins.views.search.views.ViewDTO)3 QueryEntity (org.graylog2.contentpacks.model.entities.QueryEntity)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 HashMap (java.util.HashMap)2 StreamFilter (org.graylog.plugins.views.search.filter.StreamFilter)2 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)2 DashboardEntity (org.graylog2.contentpacks.model.entities.DashboardEntity)2 Entity (org.graylog2.contentpacks.model.entities.Entity)2 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)2 EventListEntity (org.graylog2.contentpacks.model.entities.EventListEntity)2 MessageListEntity (org.graylog2.contentpacks.model.entities.MessageListEntity)2 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)2 PivotEntity (org.graylog2.contentpacks.model.entities.PivotEntity)2 StreamEntity (org.graylog2.contentpacks.model.entities.StreamEntity)2 Test (org.junit.Test)2 ImmutableSet (com.google.common.collect.ImmutableSet)1