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);
}
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);
}
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();
}
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);
}
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);
}
Aggregations