org.graylog2.contentpacks.facades.dashboardV1.DashboardWidgetConverter Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.contentpacks.facades.dashboardV1;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString;
import org.graylog.plugins.views.search.views.WidgetConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.AggregationConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.AreaVisualizationConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.AutoIntervalDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.BarVisualizationConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.Interpolation;
import org.graylog.plugins.views.search.views.widgets.aggregation.LineVisualizationConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.NumberVisualizationConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.PivotDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.SeriesConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.SeriesDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.TimeHistogramConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.ValueConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.VisualizationConfigDTO;
import org.graylog.plugins.views.search.views.widgets.aggregation.sort.PivotSortConfig;
import org.graylog.plugins.views.search.views.widgets.aggregation.sort.SortConfigDTO;
import org.graylog2.contentpacks.model.entities.DashboardWidgetEntity;
import org.graylog2.contentpacks.model.entities.WidgetConfig;
import org.graylog2.contentpacks.model.entities.WidgetEntity;
import org.graylog2.contentpacks.model.entities.references.ValueReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
public class DashboardWidgetConverter {
private Map parameters;
private DashboardWidgetEntity dashboardWidgetEntity;
private WidgetConfig config;
public List convert(DashboardWidgetEntity dashboardWidgetEntity,
Map parameters) {
this.dashboardWidgetEntity = dashboardWidgetEntity;
this.config = new WidgetConfig(dashboardWidgetEntity.configuration(), parameters);
this.parameters = parameters;
final String type = dashboardWidgetEntity.type().asString(parameters);
switch (type.toUpperCase(Locale.ENGLISH)) {
case "SEARCH_RESULT_CHART":
return createHistogramWidget();
case "FIELD_CHART":
return createFieldChartWidget();
case "STACKED_CHART":
return createStackedChartWidget();
case "STATS_COUNT":
return createStatsCountWidget();
case "QUICKVALUES":
return createQuickValueWidgets();
case "STREAM_SEARCH_RESULT_COUNT":
case "SEARCH_RESULT_COUNT":
return createSearchResultCount();
case "QUICKVALUES_HISTOGRAM":
return createQuickValueHistogramWidgets();
case "ORG.GRAYLOG.PLUGINS.MAP.WIDGET.STRATEGY.MAPWIDGETSTRATEGY":
return createMapWidget();
default: {
throw new RuntimeException("The provided entity does not have a valid Widget type: " + type);
}
}
}
private WidgetConfigDTO defaultWidgetConfig() {
return AggregationConfigDTO.Builder.builder()
.series(ImmutableList.of(
SeriesDTO.Builder.create()
.config(SeriesConfigDTO.empty())
.function("count()").build()
))
.rowPivots(ImmutableList.of(PivotDTO.Builder.builder()
.type("time")
.field("timestamp")
.config(TimeHistogramConfigDTO.Builder.builder()
.interval(AutoIntervalDTO.Builder.builder().build()).build())
.build()))
.visualization("bar")
.columnPivots(Collections.emptyList())
.sort(Collections.emptyList())
.build();
}
private WidgetEntity.Builder aggregationWidgetBuilder() {
final WidgetEntity.Builder widgetEntityBuilder = WidgetEntity.builder()
.type(AggregationConfigDTO.NAME)
.id(UUID.randomUUID().toString())
.timerange(dashboardWidgetEntity.timeRange().convert(parameters));
final Optional streamId = config.getOptionalString("stream_id");
if (streamId.isPresent()) {
final ImmutableSet.Builder streams = new ImmutableSet.Builder<>();
streams.add(streamId.get());
widgetEntityBuilder.streams(streams.build());
}
return widgetEntityBuilder;
}
private List createHistogramWidget() {
final WidgetConfigDTO widgetConfigDTO = defaultWidgetConfig();
final WidgetEntity.Builder widgetEntityBuilder = aggregationWidgetBuilder()
.config(widgetConfigDTO);
final Optional query = config.getOptionalString("query");
query.ifPresent(s -> widgetEntityBuilder.query(ElasticsearchQueryString.of(s)));
return ImmutableList.of(widgetEntityBuilder.build());
}
private String mapRendererToVisualization(String renderer) {
switch (renderer) {
case "bar":
case "line":
case "area":
return renderer;
case "scatterplot": return "scatter";
}
throw new RuntimeException("Unable to map renderer to visualization: " + renderer);
}
private Interpolation fromLegacyInterpolation(String legacyValue) {
switch (legacyValue) {
case "linear": return Interpolation.linear;
case "step-after": return Interpolation.stepAfter;
case "cardinal":
case "basis":
case "bundle":
case "monotone": return Interpolation.spline;
}
throw new RuntimeException("Invalid interpolation value: " + legacyValue);
}
private Optional createVisualizationConfig() {
final String renderer = config.getString("renderer");
final String interpolation = config.getString("interpolation");
switch (renderer) {
case "line":
return Optional.of(
LineVisualizationConfigDTO.Builder.builder()
.interpolation(fromLegacyInterpolation(interpolation))
.build()
);
case "area":
return Optional.of(
AreaVisualizationConfigDTO.Builder.builder()
.interpolation(fromLegacyInterpolation(interpolation))
.build()
);
}
return Optional.empty();
}
private WidgetEntity fieldChartWidget(String renderer, String valueType, String field, String query) {
final AggregationConfigDTO.Builder configBuilder = AggregationConfigDTO.Builder.builder()
.series(ImmutableList.of(createSeriesDTO(valueType, field)))
.visualization(mapRendererToVisualization(renderer))
.columnPivots(Collections.emptyList())
.sort(Collections.emptyList())
.rowPivots(Collections.singletonList(
PivotDTO.Builder.builder()
.field("timestamp")
.type("time")
.config(TimeHistogramConfigDTO.Builder.builder().interval(AutoIntervalDTO.Builder.builder().build()).build())
.build()
));
final WidgetEntity.Builder widgetEntityBuilder = aggregationWidgetBuilder();
widgetEntityBuilder.query(ElasticsearchQueryString.of(query));
return widgetEntityBuilder
.config(createVisualizationConfig().map(configBuilder::visualizationConfig).orElse(configBuilder).build())
.build();
}
private List createFieldChartWidget() {
final String renderer = config.getString("renderer");
final String valueType = config.getString("valuetype");
final String field = config.getString("field");
final String query = config.getOptionalString("query").orElse("");
return ImmutableList.of(fieldChartWidget(renderer, valueType, field, query));
}
@SuppressWarnings({"rawtypes", "unchecked"})
private List createStackedChartWidget() {
final String renderer = config.getString("renderer");
final List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy