-
Notifications
You must be signed in to change notification settings - Fork 33
feat: DH-18348: Implement SimplePivotWidget plugin #2383
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
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
5c38ab3
Implement SimplePivotWidget plugin
vbabich ac69c60
debug logs
vbabich 0b2ebd3
WIP
vbabich d0c8091
Totals row snapshot and cell color
vbabich be0387a
Cleanup, fix copy selection with headers
vbabich 205abbd
Disable Search Bar, Download CSV, Sort, Filter on non-row columns
vbabich 3717221
Hide Organize Columns
vbabich 5ba8621
Clean up IrisGridSimplePivotModel
vbabich 1e37a4a
Remove unused log from the grid package
vbabich 7255bcb
Remove log
vbabich 800b66c
Remove log
vbabich 29026f3
Remove logs
vbabich 2856456
Resolve TODOs
vbabich c66c33c
Resolve TODOs
vbabich 9e4ea6a
Update packages/dashboard-core-plugins/src/SimplePivotWidgetPlugin.tsx
vbabich 5505a39
Remove log
vbabich 5ddc6a4
Check EVENT_MESSAGE for error in response data
vbabich ad0cadb
Merge remote-tracking branch 'origin/main' into simple_pivot
vbabich c7c7da2
Merge remote-tracking branch 'origin/main' into simple_pivot
vbabich 1d96b08
Fix column groups
vbabich 1fb03c8
Fix out of sync metrics on model update
vbabich 57f0d8c
Expose DisplayColumn type export in iris-grid
vbabich ffd6314
Merge remote-tracking branch 'origin/main' into simple_pivot
vbabich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
15 changes: 15 additions & 0 deletions
15
packages/dashboard-core-plugins/src/SimplePivotPluginConfig.ts
This file contains hidden or 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 @@ | ||
| import { PluginType, type WidgetPlugin } from '@deephaven/plugin'; | ||
| import { dhTable } from '@deephaven/icons'; | ||
| import type { dh } from '@deephaven/jsapi-types'; | ||
| import { SimplePivotWidgetPlugin } from './SimplePivotWidgetPlugin'; | ||
|
|
||
| const SimplePivotPluginConfig: WidgetPlugin<dh.Widget> = { | ||
| name: 'SimplePivotPanel', | ||
| title: 'SimplePivot', | ||
| type: PluginType.WIDGET_PLUGIN, | ||
| component: SimplePivotWidgetPlugin, | ||
| supportedTypes: 'simplepivot.SimplePivotTable', | ||
| icon: dhTable, | ||
| }; | ||
|
|
||
| export default SimplePivotPluginConfig; |
111 changes: 111 additions & 0 deletions
111
packages/dashboard-core-plugins/src/SimplePivotWidgetPlugin.tsx
This file contains hidden or 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,111 @@ | ||
| import { useCallback } from 'react'; | ||
| import { type WidgetComponentProps } from '@deephaven/plugin'; | ||
| import { type dh as DhType } from '@deephaven/jsapi-types'; | ||
| import IrisGrid, { | ||
| getSimplePivotColumnMap, | ||
| KEY_TABLE_PIVOT_COLUMN, | ||
| type KeyColumnArray, | ||
| type KeyTableSubscriptionData, | ||
| } from '@deephaven/iris-grid'; | ||
| import { useApi } from '@deephaven/jsapi-bootstrap'; | ||
| import { LoadingOverlay } from '@deephaven/components'; | ||
| import { getErrorMessage } from '@deephaven/utils'; | ||
| import { | ||
| useIrisGridSimplePivotModel, | ||
| type SimplePivotFetchResult, | ||
| } from './useIrisGridSimplePivotModel'; | ||
|
|
||
| export function SimplePivotWidgetPlugin({ | ||
| fetch, | ||
| }: WidgetComponentProps<DhType.Widget>): JSX.Element | null { | ||
| const dh = useApi(); | ||
| const loadKeys = useCallback( | ||
| (keyTable: DhType.Table): Promise<KeyColumnArray> => | ||
| new Promise((resolve, reject) => { | ||
| const pivotIdColumn = keyTable.findColumn(KEY_TABLE_PIVOT_COLUMN); | ||
| const columns = keyTable.columns.filter( | ||
| c => c.name !== KEY_TABLE_PIVOT_COLUMN | ||
| ); | ||
| const subscription = keyTable.subscribe(keyTable.columns); | ||
| subscription.addEventListener<KeyTableSubscriptionData>( | ||
| dh.Table.EVENT_UPDATED, | ||
| e => { | ||
| subscription.close(); | ||
| resolve(getSimplePivotColumnMap(e.detail, columns, pivotIdColumn)); | ||
| } | ||
| ); | ||
| }), | ||
| [dh] | ||
| ); | ||
|
|
||
| const fetchTable = useCallback( | ||
| async function fetchModel() { | ||
| const pivotWidget = await fetch(); | ||
| const schema = JSON.parse(pivotWidget.getDataAsString()); | ||
|
|
||
| // The initial state is our keys to use for column headers | ||
| const keyTablePromise = pivotWidget.exportedObjects[0].fetch(); | ||
| const columnMapPromise = keyTablePromise.then(loadKeys); | ||
|
|
||
| return new Promise<SimplePivotFetchResult>((resolve, reject) => { | ||
| // Add a listener for each pivot schema change, so we get the first update, with the table to render. | ||
| // Note that there is no await between this line and the pivotWidget being returned, or we would miss the first update | ||
| const removeEventListener = pivotWidget.addEventListener<DhType.Widget>( | ||
| dh.Widget.EVENT_MESSAGE, | ||
| async e => { | ||
| removeEventListener(); | ||
| const data = e.detail.getDataAsString(); | ||
| const response = JSON.parse(data === '' ? '{}' : data); | ||
| if (response.error != null) { | ||
| reject(new Error(response.error)); | ||
| return; | ||
| } | ||
| // Get the object, and make sure the keytable is fetched and usable | ||
| const tables = e.detail.exportedObjects; | ||
| const tableToRenderPromise = tables[0].fetch(); | ||
| const totalsPromise = | ||
| tables.length === 2 ? tables[1].fetch() : Promise.resolve(null); | ||
|
|
||
| // Wait for all four promises to have resolved, then render the table. Note that after | ||
| // the first load, the keytable will remain loaded, we'll only wait for the main table, | ||
| // and optionally the totals table. | ||
| const fetchResult = await Promise.all([ | ||
| tableToRenderPromise, | ||
| totalsPromise, | ||
| keyTablePromise, | ||
| columnMapPromise, | ||
| ]).then(([table, totalsTable, keyTable, columnMap]) => ({ | ||
| table, | ||
| totalsTable, | ||
| keyTable, | ||
| columnMap, | ||
| })); | ||
| resolve({ ...fetchResult, schema, pivotWidget }); | ||
| } | ||
| ); | ||
| }); | ||
| }, | ||
| [fetch, dh, loadKeys] | ||
| ); | ||
|
|
||
| const fetchResult = useIrisGridSimplePivotModel(fetchTable); | ||
|
|
||
| if (fetchResult.status === 'loading') { | ||
| return <LoadingOverlay isLoading />; | ||
| } | ||
|
|
||
| if (fetchResult.status === 'error') { | ||
| return ( | ||
| <LoadingOverlay | ||
| errorMessage={getErrorMessage(fetchResult.error)} | ||
| isLoading={false} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| const { model } = fetchResult; | ||
|
|
||
| return <IrisGrid model={model} />; | ||
| } | ||
|
|
||
| export default SimplePivotWidgetPlugin; | ||
This file contains hidden or 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
134 changes: 134 additions & 0 deletions
134
packages/dashboard-core-plugins/src/useIrisGridSimplePivotModel.ts
This file contains hidden or 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,134 @@ | ||
| import { type dh } from '@deephaven/jsapi-types'; | ||
| import { useApi } from '@deephaven/jsapi-bootstrap'; | ||
| import { useCallback, useEffect, useState } from 'react'; | ||
| import { | ||
| type IrisGridModel, | ||
| IrisGridSimplePivotModel, | ||
| type KeyColumnArray, | ||
| type SimplePivotSchema, | ||
| } from '@deephaven/iris-grid'; | ||
| import Log from '@deephaven/log'; | ||
|
|
||
| const log = Log.module('useIrisGridSimplePivotModel'); | ||
|
|
||
| export interface SimplePivotFetchResult { | ||
| columnMap: KeyColumnArray; | ||
| schema: SimplePivotSchema; | ||
| table: dh.Table; | ||
| keyTable: dh.Table; | ||
| totalsTable: dh.Table | null; | ||
| pivotWidget: dh.Widget; | ||
| } | ||
|
|
||
| export type IrisGridModelFetch = () => Promise<SimplePivotFetchResult>; | ||
|
|
||
| export type IrisGridModelFetchErrorResult = { | ||
| error: NonNullable<unknown>; | ||
| status: 'error'; | ||
| }; | ||
|
|
||
| export type IrisGridModelFetchLoadingResult = { | ||
| status: 'loading'; | ||
| }; | ||
|
|
||
| export type IrisGridModelFetchSuccessResult = { | ||
| status: 'success'; | ||
| model: IrisGridModel; | ||
| }; | ||
|
|
||
| export type IrisGridModelFetchResult = ( | ||
| | IrisGridModelFetchErrorResult | ||
| | IrisGridModelFetchLoadingResult | ||
| | IrisGridModelFetchSuccessResult | ||
| ) & { | ||
| reload: () => void; | ||
| }; | ||
|
|
||
| /** Pass in a table `fetch` function, will load the model and handle any errors */ | ||
| export function useIrisGridSimplePivotModel( | ||
| fetch: IrisGridModelFetch | ||
| ): IrisGridModelFetchResult { | ||
| const dh = useApi(); | ||
| const [model, setModel] = useState<IrisGridModel>(); | ||
| const [error, setError] = useState<unknown>(); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
|
|
||
| log.debug('render useIrisGridSimplePivotModel', model, error); | ||
|
|
||
| // Close the model when component is unmounted | ||
| useEffect( | ||
| () => () => { | ||
| if (model) { | ||
| model.close(); | ||
| } | ||
| }, | ||
| [model] | ||
| ); | ||
|
|
||
| const makeModel = useCallback(async () => { | ||
| log.debug('Fetching model'); | ||
| const { columnMap, keyTable, pivotWidget, schema, table, totalsTable } = | ||
| await fetch(); | ||
| log.debug('Fetching model before new Model'); | ||
| return new IrisGridSimplePivotModel( | ||
| dh, | ||
| table, | ||
| keyTable, | ||
| totalsTable, | ||
| columnMap, | ||
| schema, | ||
| pivotWidget | ||
| ); | ||
| }, [dh, fetch]); | ||
|
|
||
| const reload = useCallback(async () => { | ||
| setIsLoading(true); | ||
| setError(undefined); | ||
| try { | ||
| const newModel = await makeModel(); | ||
| setModel(newModel); | ||
| setIsLoading(false); | ||
| } catch (e) { | ||
| setError(e); | ||
| setIsLoading(false); | ||
| } | ||
| }, [makeModel]); | ||
|
|
||
| useEffect(() => { | ||
| log.debug('useEffect makeModel'); | ||
| let cancelled = false; | ||
| async function init() { | ||
| setIsLoading(true); | ||
| setError(undefined); | ||
| try { | ||
| const newModel = await makeModel(); | ||
| if (!cancelled) { | ||
| setModel(newModel); | ||
| setIsLoading(false); | ||
| } | ||
| } catch (e) { | ||
| if (!cancelled) { | ||
| setError(e); | ||
| setIsLoading(false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| init(); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [makeModel]); | ||
|
|
||
| if (isLoading) { | ||
| return { reload, status: 'loading' }; | ||
| } | ||
| if (error != null) { | ||
| return { error, reload, status: 'error' }; | ||
| } | ||
| if (model != null) { | ||
| return { model, reload, status: 'success' }; | ||
| } | ||
| throw new Error('Invalid state'); | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we be checking the
getDataAsString()to verify it's the message we're expecting?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.
The example didn't have this check, but yes, we should probably verify.
e.detail.getDataAsString()seems to return an empty string on success. @niloc132 is that expected or should it be a parsable JSON instead?