-
Notifications
You must be signed in to change notification settings - Fork 890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Vis Builder] Bug fixes for datasource picker and auto time interval #2632
Changes from all commits
c64a745
3fcc3a1
1566268
15cc636
b9eb5d2
3e68839
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ export const Workspace: FC = ({ children }) => { | |
useEffect(() => { | ||
async function loadExpression() { | ||
const schemas = ui.containerConfig.data.schemas; | ||
const [valid, errorMsg] = validateSchemaState(schemas, rootState); | ||
const [valid, errorMsg] = validateSchemaState(schemas, rootState.visualization); | ||
|
||
if (!valid) { | ||
if (errorMsg) { | ||
|
@@ -50,12 +50,13 @@ export const Workspace: FC = ({ children }) => { | |
setExpression(undefined); | ||
return; | ||
} | ||
const exp = await toExpression(rootState); | ||
|
||
const exp = await toExpression(rootState, searchContext); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now passing search context too since certain visualizations need the search context data to correctly configure the expression |
||
setExpression(exp); | ||
} | ||
|
||
loadExpression(); | ||
}, [rootState, toExpression, toasts, ui.containerConfig.data.schemas]); | ||
}, [rootState, toExpression, toasts, ui.containerConfig.data.schemas, searchContext]); | ||
|
||
useLayoutEffect(() => { | ||
const subscription = data.query.state$.subscribe(({ state }) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { cloneDeep } from 'lodash'; | ||
import { useLayoutEffect, useMemo, useState } from 'react'; | ||
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public'; | ||
import { VisBuilderServices } from '../../../types'; | ||
import { useTypedSelector, useTypedDispatch } from '../state_management'; | ||
import { useIndexPatterns } from './use_index_pattern'; | ||
|
||
/** | ||
* Returns common agg parameters from the store and app context | ||
* @returns { indexPattern, aggConfigs, aggs, timeRange } | ||
*/ | ||
export const useAggs = () => { | ||
const { | ||
services: { | ||
data: { | ||
search: { aggs: aggService }, | ||
query: { | ||
timefilter: { timefilter }, | ||
}, | ||
}, | ||
}, | ||
} = useOpenSearchDashboards<VisBuilderServices>(); | ||
const indexPattern = useIndexPatterns().selected; | ||
const [timeRange, setTimeRange] = useState(timefilter.getTime()); | ||
const aggConfigParams = useTypedSelector( | ||
(state) => state.visualization.activeVisualization?.aggConfigParams | ||
); | ||
const dispatch = useTypedDispatch(); | ||
|
||
const aggConfigs = useMemo(() => { | ||
const configs = | ||
indexPattern && aggService.createAggConfigs(indexPattern, cloneDeep(aggConfigParams)); | ||
return configs; | ||
}, [aggConfigParams, aggService, indexPattern]); | ||
|
||
useLayoutEffect(() => { | ||
const subscription = timefilter.getTimeUpdate$().subscribe(() => { | ||
setTimeRange(timefilter.getTime()); | ||
}); | ||
|
||
return () => { | ||
subscription.unsubscribe(); | ||
}; | ||
}, [dispatch, timefilter]); | ||
|
||
return { | ||
indexPattern, | ||
aggConfigs, | ||
aggs: aggConfigs?.aggs ?? [], | ||
timeRange, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,19 @@ | |
|
||
import { countBy } from 'lodash'; | ||
import { Schemas } from '../../../../vis_default_editor/public'; | ||
import { RootState } from './state_management'; | ||
import { VisualizationState } from './state_management'; | ||
|
||
export const validateSchemaState = (schemas: Schemas, state: RootState): [boolean, string?] => { | ||
const activeViz = state.visualization.activeVisualization; | ||
/** | ||
* Validate if the visualization state fits the vis type schema criteria | ||
* @param schemas Visualization type config Schema objects | ||
* @param state visualization state | ||
* @returns [Validity, 'Message'] | ||
*/ | ||
export const validateSchemaState = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplified this since we can only reliably validate on he visualization state. |
||
schemas: Schemas, | ||
state: VisualizationState | ||
): [boolean, string?] => { | ||
const activeViz = state.activeVisualization; | ||
const vizName = activeViz?.name; | ||
const aggs = activeViz?.aggConfigParams; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ import { | |
import { validateSchemaState } from '../application/utils/validate_schema_state'; | ||
import { getExpressionLoader, getTypeService } from '../plugin_services'; | ||
import { PersistedState } from '../../../visualizations/public'; | ||
import { RenderState, VisualizationState } from '../application/utils/state_management'; | ||
|
||
// Apparently this needs to match the saved object type for the clone and replace panel actions to work | ||
export const VISBUILDER_EMBEDDABLE = VISBUILDER_SAVED_OBJECT; | ||
|
@@ -121,24 +122,24 @@ export class VisBuilderEmbeddable extends Embeddable<SavedObjectEmbeddableInput, | |
const { visualization, style } = this.serializedState; | ||
|
||
const vizStateWithoutIndex = JSON.parse(visualization); | ||
const visualizationState = { | ||
const visualizationState: VisualizationState = { | ||
searchField: vizStateWithoutIndex.searchField, | ||
activeVisualization: vizStateWithoutIndex.activeVisualization, | ||
indexPattern: this.savedVisBuilder?.searchSourceFields?.index, | ||
}; | ||
const rootState = { | ||
const renderState: RenderState = { | ||
visualization: visualizationState, | ||
style: JSON.parse(style), | ||
}; | ||
const visualizationName = rootState.visualization?.activeVisualization?.name ?? ''; | ||
const visualizationName = renderState.visualization?.activeVisualization?.name ?? ''; | ||
const visualizationType = getTypeService().get(visualizationName); | ||
if (!visualizationType) { | ||
this.onContainerError(new Error(`Invalid visualization type ${visualizationName}`)); | ||
return; | ||
} | ||
const { toExpression, ui } = visualizationType; | ||
const schemas = ui.containerConfig.data.schemas; | ||
const [valid, errorMsg] = validateSchemaState(schemas, rootState); | ||
const [valid, errorMsg] = validateSchemaState(schemas, visualizationState); | ||
|
||
if (!valid) { | ||
if (errorMsg) { | ||
|
@@ -147,7 +148,11 @@ export class VisBuilderEmbeddable extends Embeddable<SavedObjectEmbeddableInput, | |
} | ||
} else { | ||
// TODO: handle error in Expression creation | ||
const exp = await toExpression(rootState); | ||
const exp = await toExpression(renderState, { | ||
filters: this.filters, | ||
query: this.query, | ||
timeRange: this.timeRange, | ||
}); | ||
return exp; | ||
} | ||
}; | ||
|
@@ -268,12 +273,15 @@ export class VisBuilderEmbeddable extends Embeddable<SavedObjectEmbeddableInput, | |
// Check if rootState has changed | ||
if (!isEqual(this.getSerializedState(), this.serializedState)) { | ||
this.serializedState = this.getSerializedState(); | ||
this.expression = (await this.getExpression()) ?? ''; | ||
dirty = true; | ||
} | ||
|
||
if (this.handler && dirty) { | ||
this.updateHandler(); | ||
if (dirty) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to recalculate the expression if the context changes too. |
||
this.expression = (await this.getExpression()) ?? ''; | ||
|
||
if (this.handler) { | ||
this.updateHandler(); | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is simply to remove the eslint exception