forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dataset quality] State management (elastic#174906)
## 📝 Summary This PR contains `dataset-quality` plugin state management, we have decided to go with xstate. The general idea, and following elastic#170200 as inspiration, we wanted to detach `dataset-quality` plugin state from its consumers, in this way our plugin wouldn't perform side effect, such as updating routes, outside its scope. The flow of the information now looks like <img width="543" alt="image" src="https://github.com/elastic/kibana/assets/1313018/6a723f5b-047b-4f81-aef5-dbfce1ac0181"> Current internal state of the plugin is fairly simple but it's envisioned to grow in a near future with for example filters, flyout options, etc. ## 💡 Notes for Logs UX reviewers ### Dataset Quality plugin #### Goals **Decoupling from global state**: The primary goal is to decouple the `<DatasetQuality>` component from direct dependencies on the URL and other page-wide side effects. Decoupling from global state enables its use in other applications without interfering with their page state. **Stable and Strictly Typed Public API**: Introduce a public API for the `<DatasetQuality>` component. This API provides consumers the ability to subscribe to the component's state and/or initialize the state from the outside. #### Architecture The architecture of the `<DatasetQuality>` plugin has been designed to provide a modular structure, with a focus on robust state management. This structure is primarily composed of the uncontrolled `<DatasetQuality>` component and a separately instantiable controller. **Uncontrolled `<DatasetQuality>` component**: The `<DatasetQuality>` is designed as an uncontrolled component. All the logic around this component is handled by hooks, such as `useDatasetQualityTable` with the ability to change or replace the underlying dependencies. **Separately instantiable controller**: A controller is introduced to encapsulate and manage the business logic associated with the `<DatasetQuality>` component. The controller centralizes the business logic, separating it from the UI layer. This provides flexibility in managing different instances of the `<DatasetQuality>` and reusability from different consumers. #### App statechart <img width="1041" alt="image" src="https://github.com/elastic/kibana/assets/1313018/7da9e424-8577-4a5e-8c3e-46fb1df83948"> ### Observability Logs Explorer App #### Goals **URL persistence**: Implement a versioned data structure for URL persistence, this give us the flexibility to extend or change the app state without workarounds. The general idea of having the public state of the dataset quality plugin stored in the URL of this consumer is the ability to share an exact state with colleagues, customers, etc. #### Changes **Introduction of versioned URL schema**: This new schema will standarize how URL-based state is managed, providing a clear and consistent mechanism for encoding and decoding state information in the URL. The versioning will allow us to evolve the data structure in a backwards-compatible way incorporate features added or changed in the future. **Page-level statechart implementation**: This introduces a page-level statechart to orchestrate the initialization and instantiation of the `<DatasetQuality>` controller. #### App statechart <img width="936" alt="image" src="https://github.com/elastic/kibana/assets/1313018/e3f1c61b-322b-4054-a30e-157eadb6de6b"> --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
7b24ddd
commit 288e365
Showing
54 changed files
with
1,505 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
x-pack/plugins/dataset_quality/public/components/flyout/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { FlyoutDataset } from '../../state_machines/dataset_quality_controller'; | ||
|
||
export interface FlyoutProps { | ||
dataset: FlyoutDataset; | ||
closeFlyout: () => void; | ||
} |
66 changes: 66 additions & 0 deletions
66
x-pack/plugins/dataset_quality/public/controller/create_controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { CoreStart } from '@kbn/core/public'; | ||
import { getDevToolsOptions } from '@kbn/xstate-utils'; | ||
import equal from 'fast-deep-equal'; | ||
import { distinctUntilChanged, from, map } from 'rxjs'; | ||
import { interpret } from 'xstate'; | ||
import { DataStreamsStatsService } from '../services/data_streams_stats'; | ||
import { | ||
createDatasetQualityControllerStateMachine, | ||
DEFAULT_CONTEXT, | ||
} from '../state_machines/dataset_quality_controller'; | ||
import { DatasetQualityStartDeps } from '../types'; | ||
import { getContextFromPublicState, getPublicStateFromContext } from './public_state'; | ||
import { DatasetQualityController, DatasetQualityPublicStateUpdate } from './types'; | ||
|
||
type InitialState = DatasetQualityPublicStateUpdate; | ||
|
||
interface Dependencies { | ||
core: CoreStart; | ||
plugins: DatasetQualityStartDeps; | ||
} | ||
|
||
export const createDatasetQualityControllerFactory = | ||
({ core }: Dependencies) => | ||
async ({ | ||
initialState = DEFAULT_CONTEXT, | ||
}: { | ||
initialState?: InitialState; | ||
}): Promise<DatasetQualityController> => { | ||
const initialContext = getContextFromPublicState(initialState ?? {}); | ||
|
||
const dataStreamStatsClient = new DataStreamsStatsService().start({ | ||
http: core.http, | ||
}).client; | ||
|
||
const machine = createDatasetQualityControllerStateMachine({ | ||
initialContext, | ||
toasts: core.notifications.toasts, | ||
dataStreamStatsClient, | ||
}); | ||
|
||
const service = interpret(machine, { | ||
devTools: getDevToolsOptions(), | ||
}); | ||
|
||
const state$ = from(service).pipe( | ||
map(({ context }) => getPublicStateFromContext(context)), | ||
distinctUntilChanged(equal) | ||
); | ||
|
||
return { | ||
state$, | ||
service, | ||
}; | ||
}; | ||
|
||
export type CreateDatasetQualityControllerFactory = typeof createDatasetQualityControllerFactory; | ||
export type CreateDatasetQualityController = ReturnType< | ||
typeof createDatasetQualityControllerFactory | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './create_controller'; | ||
export * from './provider'; | ||
export * from './types'; |
15 changes: 15 additions & 0 deletions
15
x-pack/plugins/dataset_quality/public/controller/lazy_create_controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { CreateDatasetQualityControllerFactory } from './create_controller'; | ||
|
||
export const createDatasetQualityControllerLazyFactory: CreateDatasetQualityControllerFactory = | ||
(dependencies) => async (args) => { | ||
const { createDatasetQualityControllerFactory } = await import('./create_controller'); | ||
|
||
return createDatasetQualityControllerFactory(dependencies)(args); | ||
}; |
Oops, something went wrong.