Skip to content

Commit

Permalink
[ML] Persisted URL state for Anomalies table
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Nov 25, 2020
1 parent a763d33 commit 93fb19b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import { mlTableService } from '../../services/table_service';
import { RuleEditorFlyout } from '../rule_editor';
import { ml } from '../../services/ml_api_service';
import { INFLUENCERS_LIMIT, ANOMALIES_TABLE_TABS, MAX_CHARS } from './anomalies_table_constants';
import { usePageUrlState } from '../../util/url_state';

class AnomaliesTable extends Component {
export class AnomaliesTableInternal extends Component {
constructor(props) {
super(props);

Expand Down Expand Up @@ -145,8 +146,20 @@ class AnomaliesTable extends Component {
});
};

onTableChange = ({ page, sort }) => {
const { tableState, updateTableState } = this.props;
const result = {
pageIndex: page && page.index !== undefined ? page.index : tableState.pageIndex,
pageSize: page && page.size !== undefined ? page.size : tableState.pageSize,
sortField: sort && sort.field !== undefined ? sort.field : tableState.sortField,
sortDirection:
sort && sort.direction !== undefined ? sort.direction : tableState.sortDirection,
};
updateTableState(result);
};

render() {
const { bounds, tableData, filter, influencerFilter } = this.props;
const { bounds, tableData, filter, influencerFilter, tableState } = this.props;

if (
tableData === undefined ||
Expand Down Expand Up @@ -186,8 +199,8 @@ class AnomaliesTable extends Component {

const sorting = {
sort: {
field: 'severity',
direction: 'desc',
field: tableState.sortField,
direction: tableState.sortDirection,
},
};

Expand All @@ -199,8 +212,15 @@ class AnomaliesTable extends Component {
};
};

const pagination = {
pageIndex: tableState.pageIndex,
pageSize: tableState.pageSize,
totalItemCount: tableData.anomalies.length,
pageSizeOptions: [10, 25, 100],
};

return (
<React.Fragment>
<>
<RuleEditorFlyout
setShowFunction={this.setShowRuleEditorFlyoutFunction}
unsetShowFunction={this.unsetShowRuleEditorFlyoutFunction}
Expand All @@ -209,26 +229,46 @@ class AnomaliesTable extends Component {
className="ml-anomalies-table eui-textOverflowWrap"
items={tableData.anomalies}
columns={columns}
pagination={{
pageSizeOptions: [10, 25, 100],
initialPageSize: 25,
}}
pagination={pagination}
sorting={sorting}
itemId="rowId"
itemIdToExpandedRowMap={this.state.itemIdToExpandedRowMap}
compressed={true}
rowProps={getRowProps}
data-test-subj="mlAnomaliesTable"
onTableChange={this.onTableChange}
/>
</React.Fragment>
</>
);
}
}
AnomaliesTable.propTypes = {

export const getDefaultAnomaliesTableState = () => ({
pageIndex: 0,
pageSize: 25,
sortField: 'severity',
sortDirection: 'desc',
});

export const AnomaliesTable = (props) => {
const [tableState, updateTableState] = usePageUrlState(
'mlAnomaliesTable',
getDefaultAnomaliesTableState()
);
return (
<AnomaliesTableInternal
{...props}
tableState={tableState}
updateTableState={updateTableState}
/>
);
};

AnomaliesTableInternal.propTypes = {
bounds: PropTypes.object.isRequired,
tableData: PropTypes.object,
filter: PropTypes.func,
influencerFilter: PropTypes.func,
tableState: PropTypes.object.isRequired,
updateTableState: PropTypes.func.isRequired,
};

export { AnomaliesTable };
2 changes: 1 addition & 1 deletion x-pack/plugins/ml/public/application/util/url_state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const useUrlState = (accessor: Accessor) => {
return [urlState, setUrlState];
};

type AppStateKey = 'mlSelectSeverity' | 'mlSelectInterval' | MlPages;
type AppStateKey = 'mlSelectSeverity' | 'mlSelectInterval' | 'mlAnomaliesTable' | MlPages;

/**
* Hook for managing the URL state of the page.
Expand Down

0 comments on commit 93fb19b

Please sign in to comment.