Skip to content

Commit

Permalink
#4523 Embed queries in dashboards
Browse files Browse the repository at this point in the history
  • Loading branch information
stroomdev66 committed Oct 30, 2024
1 parent ee52638 commit 17e0b67
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public class EmbeddedQueryPresenter
private boolean initialised;
private Timer autoRefreshTimer;
private ExpressionOperator currentDecoration;
private DocRef loadedQueryRef;

@Inject
public EmbeddedQueryPresenter(final EventBus eventBus,
Expand Down Expand Up @@ -289,7 +290,8 @@ public void setQueryModel(final QueryModel queryModel) {
eventBus,
restFactory,
dateTimeSettingsFactory,
resultStoreModel);
resultStoreModel,
() -> getQuerySettings().getQueryTablePreferences());
queryModel.addResultComponent(QueryModel.TABLE_COMPONENT_ID, tableResultConsumer);
queryModel.addResultComponent(QueryModel.VIS_COMPONENT_ID, visResultConsumer);
queryModel.addSearchStateListener(this);
Expand Down Expand Up @@ -401,9 +403,13 @@ private void updateVisibleResult() {
private void createNewTable() {
if (currentTablePresenter == null) {
currentTablePresenter = tablePresenterProvider.get();
currentTablePresenter.setQueryTablePreferencesSupplier(() ->
getQuerySettings().getQueryTablePreferences());
currentTablePresenter.setQueryTablePreferencesConsumer(queryTablePreferences ->
setSettings(getQuerySettings().copy().queryTablePreferences(queryTablePreferences).build()));
currentTablePresenter.setQueryModel(queryModel);
currentTablePresenter.setTaskMonitorFactory(this);
currentTablePresenter.read(queryModel.getQueryTablePreferences());
currentTablePresenter.update();
tableHandlerRegistrations.add(currentTablePresenter.addDirtyHandler(e -> setDirty(true)));
tableHandlerRegistrations.add(currentTablePresenter.getSelectionModel()
.addSelectionHandler(event -> getComponents().fireComponentChangeEvent(this)));
Expand All @@ -423,9 +429,8 @@ private void destroyCurrentTable() {
private void createNewVis() {
if (currentVisPresenter == null) {
final VisSelectionModel visSelectionModel = new VisSelectionModel();
visSelectionModel.addSelectionHandler(event -> {
getComponents().fireComponentChangeEvent(EmbeddedQueryPresenter.this);
});
visSelectionModel.addSelectionHandler(event ->
getComponents().fireComponentChangeEvent(EmbeddedQueryPresenter.this));

currentVisPresenter = visPresenterProvider.get();
currentVisPresenter.setQueryModel(queryModel);
Expand Down Expand Up @@ -540,7 +545,8 @@ private void loadEmbeddedQuery() {
initialised = false;
final EmbeddedQueryComponentSettings settings = getQuerySettings();
final DocRef queryRef = settings.getQueryRef();
if (queryRef != null) {
if (queryRef != null && !Objects.equals(queryRef, loadedQueryRef)) {
loadedQueryRef = queryRef;
restFactory
.create(QUERY_RESOURCE)
.method(res -> res.fetch(queryRef.getUuid()))
Expand Down Expand Up @@ -571,18 +577,25 @@ private void loadEmbeddedQuery() {
// }
// }

final QueryTablePreferences queryTablePreferences;
if (settings.getQueryTablePreferences() != null) {
queryTablePreferences = settings.getQueryTablePreferences();
} else if (result.getQueryTablePreferences() != null) {
queryTablePreferences = result.getQueryTablePreferences();
} else {
queryTablePreferences = QueryTablePreferences.builder().build();
if (settings.getQueryTablePreferences() == null && result.getQueryTablePreferences() != null) {
setSettings(settings
.copy()
.queryTablePreferences(result.getQueryTablePreferences())
.build());
}

// final QueryTablePreferences queryTablePreferences;
// if (settings.getQueryTablePreferences() != null) {
// queryTablePreferences = settings.getQueryTablePreferences();
// } else if (result.getQueryTablePreferences() != null) {
// queryTablePreferences = result.getQueryTablePreferences();
// } else {
// queryTablePreferences = QueryTablePreferences.builder().build();
// }

// Read expression.
queryModel.init(result.asDocRef());
queryModel.setQueryTablePreferences(queryTablePreferences);
// queryModel.setQueryTablePreferences(queryTablePreferences);
query = result.getQuery();
initialised = true;
if (queryOnOpen) {
Expand All @@ -596,18 +609,20 @@ private void loadEmbeddedQuery() {
}

@Override
public ComponentConfig write() {
QueryTablePreferences queryTablePreferences = queryModel.getQueryTablePreferences();
public void setSettings(final ComponentSettings componentSettings) {
super.setSettings(componentSettings);
if (currentTablePresenter != null) {
queryTablePreferences = currentTablePresenter.write();
currentTablePresenter.refresh();
}
}

@Override
public ComponentConfig write() {
// Write expression.
setSettings(getQuerySettings()
.copy()
.lastQueryKey(queryModel.getCurrentQueryKey())
.lastQueryNode(queryModel.getCurrentNode())
.queryTablePreferences(queryTablePreferences)
.build());
return super.write();
}
Expand Down Expand Up @@ -733,6 +748,9 @@ public void run() {
@Override
protected void changeSettings() {
super.changeSettings();
if (currentTablePresenter != null) {
currentTablePresenter.refresh();
}
loadEmbeddedQuery();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import stroom.dashboard.client.main.SettingsPresenter;
import stroom.dashboard.client.query.SelectionHandlersPresenter;
import stroom.dashboard.client.table.cf.RulesPresenter;
import stroom.dispatch.client.DefaultErrorHandler;
import stroom.dispatch.client.RestFactory;
import stroom.docref.DocRef;
Expand All @@ -42,6 +43,7 @@ public class EmbeddedQuerySettingsPresenter extends SettingsPresenter {
public EmbeddedQuerySettingsPresenter(final EventBus eventBus,
final LinkTabsLayoutView view,
final BasicEmbeddedQuerySettingsPresenter basicSettingsPresenter,
final RulesPresenter rulesPresenter,
final SelectionHandlersPresenter selectionHandlersPresenter,
final RestFactory restFactory) {
super(eventBus, view);
Expand Down Expand Up @@ -74,6 +76,7 @@ public EmbeddedQuerySettingsPresenter(final EventBus eventBus,
});

addTab("Basic", basicSettingsPresenter);
addTab("Conditional Formatting", rulesPresenter);
addTab("Selection Handlers", selectionHandlersPresenter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import stroom.dashboard.client.main.AbstractSettingsTabPresenter;
import stroom.dashboard.client.table.TablePresenter;
import stroom.dashboard.shared.ComponentConfig;
import stroom.dashboard.shared.EmbeddedQueryComponentSettings;
import stroom.dashboard.shared.TableComponentSettings;
import stroom.datasource.api.v2.QueryField;
import stroom.document.client.event.DirtyEvent;
Expand Down Expand Up @@ -208,7 +209,7 @@ private void add() {
selectionBoxModel.addItems(fields);
editRulePresenter.read(newRule, selectionBoxModel);

final PopupSize popupSize = PopupSize.resizable(800, 550);
final PopupSize popupSize = PopupSize.resizable(1000, 700);
ShowPopupEvent.builder(editRulePresenter)
.popupType(PopupType.OK_CANCEL_DIALOG)
.popupSize(popupSize)
Expand Down Expand Up @@ -266,8 +267,18 @@ private void edit(final ConditionalFormattingRule existingRule) {

@Override
public void read(final ComponentConfig componentConfig) {
final TableComponentSettings settings = (TableComponentSettings) componentConfig.getSettings();
if (componentConfig.getSettings() instanceof TableComponentSettings) {
final TableComponentSettings tableComponentSettings =
(TableComponentSettings) componentConfig.getSettings();
read(tableComponentSettings);
} else if (componentConfig.getSettings() instanceof EmbeddedQueryComponentSettings) {
final EmbeddedQueryComponentSettings embeddedQueryComponentSettings =
(EmbeddedQueryComponentSettings) componentConfig.getSettings();
read(embeddedQueryComponentSettings.getQueryTablePreferences());
}
}

private void read(final TableComponentSettings settings) {
// final Predicate<Field> nonSpecialFieldsPredicate = field -> !field.isSpecial();

// final Function<Field, DataSourceField.DataSourceFieldType> typeMapper = field -> {
Expand Down Expand Up @@ -328,12 +339,26 @@ public void read(final QueryTablePreferences queryTablePreferences) {

@Override
public ComponentConfig write(final ComponentConfig componentConfig) {
final TableComponentSettings oldSettings = (TableComponentSettings) componentConfig.getSettings();
final TableComponentSettings newSettings = oldSettings
.copy()
.conditionalFormattingRules(rules)
.build();
return componentConfig.copy().settings(newSettings).build();
if (componentConfig.getSettings() instanceof TableComponentSettings) {
final TableComponentSettings oldSettings =
(TableComponentSettings) componentConfig.getSettings();
final TableComponentSettings newSettings = oldSettings
.copy()
.conditionalFormattingRules(rules)
.build();
return componentConfig.copy().settings(newSettings).build();
} else if (componentConfig.getSettings() instanceof EmbeddedQueryComponentSettings) {
final EmbeddedQueryComponentSettings oldSettings =
(EmbeddedQueryComponentSettings) componentConfig.getSettings();
final QueryTablePreferences queryTablePreferences =
write(oldSettings.getQueryTablePreferences());
final EmbeddedQueryComponentSettings newSettings = oldSettings
.copy()
.queryTablePreferences(queryTablePreferences)
.build();
return componentConfig.copy().settings(newSettings).build();
}
throw new RuntimeException("Unexpected type");
}

public QueryTablePreferences write(final QueryTablePreferences queryTablePreferences) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class QueryEditPresenter
private final QueryToolbarPresenter queryToolbarPresenter;
private final EditorPresenter editorPresenter;
private final QueryResultTableSplitPresenter queryResultPresenter;
private final QueryTablePreferencesHolder queryTablePreferencesHolder = new QueryTablePreferencesHolder();
private boolean dirty;
private boolean reading;
private boolean readOnly = true;
Expand Down Expand Up @@ -107,6 +108,9 @@ public QueryEditPresenter(final EventBus eventBus,
this.linkTabsLayoutView = linkTabsLayoutView;
this.queryInfo = queryInfo;

queryResultPresenter.setQueryTablePreferencesSupplier(queryTablePreferencesHolder);
queryResultPresenter.setQueryTablePreferencesConsumer(queryTablePreferencesHolder);

final ResultComponent resultConsumer = new ResultComponent() {
boolean start;
boolean hasData;
Expand Down Expand Up @@ -181,7 +185,8 @@ public void setQueryModel(final QueryModel queryModel) {
eventBus,
restFactory,
dateTimeSettingsFactory,
resultStoreModel);
resultStoreModel,
queryTablePreferencesHolder);
queryModel.addResultComponent(QueryModel.TABLE_COMPONENT_ID, queryResultPresenter);
queryModel.addResultComponent(QueryModel.VIS_COMPONENT_ID, resultConsumer);

Expand Down Expand Up @@ -416,11 +421,11 @@ public void setTaskMonitorFactory(final TaskMonitorFactory taskMonitorFactory) {
}

public QueryTablePreferences write() {
return queryResultPresenter.write();
return queryTablePreferencesHolder.get();
}

public void read(final QueryTablePreferences queryTablePreferences) {
queryResultPresenter.read(queryTablePreferences);
queryTablePreferencesHolder.accept(queryTablePreferences);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class QueryModel implements HasTaskMonitorFactory, HasHandlers {

Expand All @@ -72,7 +73,7 @@ public class QueryModel implements HasTaskMonitorFactory, HasHandlers {
private boolean polling;
private SourceType sourceType = SourceType.QUERY_UI;
private Set<String> currentHighlights;
private QueryTablePreferences queryTablePreferences;
private final Supplier<QueryTablePreferences> queryTablePreferencesSupplier;

private final List<SearchStateListener> searchStateListeners = new ArrayList<>();
private final List<SearchErrorListener> errorListeners = new ArrayList<>();
Expand All @@ -82,11 +83,13 @@ public class QueryModel implements HasTaskMonitorFactory, HasHandlers {
public QueryModel(final EventBus eventBus,
final RestFactory restFactory,
final DateTimeSettingsFactory dateTimeSettingsFactory,
final ResultStoreModel resultStoreModel) {
final ResultStoreModel resultStoreModel,
final Supplier<QueryTablePreferences> queryTablePreferencesSupplier) {
this.eventBus = eventBus;
this.restFactory = restFactory;
this.dateTimeSettingsFactory = dateTimeSettingsFactory;
this.resultStoreModel = resultStoreModel;
this.queryTablePreferencesSupplier = queryTablePreferencesSupplier;
}

public void addResultComponent(final String componentId, final ResultComponent resultComponent) {
Expand Down Expand Up @@ -175,7 +178,7 @@ public void startNewSearch(final String query,
.query(query)
.queryContext(currentQueryContext)
.incremental(incremental)
.queryTablePreferences(queryTablePreferences)
.queryTablePreferences(queryTablePreferencesSupplier.get())
.build();
// }
// }
Expand Down Expand Up @@ -212,7 +215,7 @@ public void refresh(final String componentId, final Consumer<Result> resultConsu
.storeHistory(false)
.openGroups(resultComponent.getOpenGroups())
.requestedRange(resultComponent.getRequestedRange())
.queryTablePreferences(queryTablePreferences)
.queryTablePreferences(queryTablePreferencesSupplier.get())
.build();

exec = true;
Expand Down Expand Up @@ -285,7 +288,7 @@ private void poll(final boolean storeHistory) {
.storeHistory(storeHistory)
.openGroups(openGroups)
.requestedRange(requestedRange)
.queryTablePreferences(queryTablePreferences)
.queryTablePreferences(queryTablePreferencesSupplier.get())
.build();

restFactory
Expand Down Expand Up @@ -441,12 +444,4 @@ public String getCurrentNode() {
public Set<String> getCurrentHighlights() {
return currentHighlights;
}

public void setQueryTablePreferences(final QueryTablePreferences queryTablePreferences) {
this.queryTablePreferences = queryTablePreferences;
}

public QueryTablePreferences getQueryTablePreferences() {
return queryTablePreferences;
}
}
Loading

0 comments on commit 17e0b67

Please sign in to comment.