Skip to content

Commit

Permalink
handling spaces being disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Nov 17, 2020
1 parent 78bc482 commit 21a176c
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ import { SpacesManager, Space } from '../../../../../spaces/public';
export interface SpacesContextValue {
spacesManager: SpacesManager;
allSpaces: Space[];
spacesEnabled: boolean;
}

export const SpacesContext = createContext<Partial<SpacesContextValue>>({});

export function createSpacesContext(http: HttpSetup) {
return { spacesManager: new SpacesManager(http), allSpaces: [] } as SpacesContextValue;
return {
spacesManager: new SpacesManager(http),
allSpaces: [],
spacesEnabled: false,
} as SpacesContextValue;
}

export function useSpacesContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ function getItemIdToExpandedRowMap(
interface Props {
isManagementTable?: boolean;
isMlEnabledInSpace?: boolean;
spacesEnabled?: boolean;
blockRefresh?: boolean;
}
export const DataFrameAnalyticsList: FC<Props> = ({
isManagementTable = false,
isMlEnabledInSpace = true,
spacesEnabled = false,
blockRefresh = false,
}) => {
const [isInitialized, setIsInitialized] = useState(false);
Expand Down Expand Up @@ -180,6 +182,7 @@ export const DataFrameAnalyticsList: FC<Props> = ({
setExpandedRowItemIds,
isManagementTable,
isMlEnabledInSpace,
spacesEnabled,
refresh
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const useColumns = (
setExpandedRowItemIds: React.Dispatch<React.SetStateAction<DataFrameAnalyticsId[]>>,
isManagementTable: boolean = false,
isMlEnabledInSpace: boolean = true,
spacesEnabled: boolean = true,
refresh: () => void = () => {}
) => {
const { actions, modals } = useActions(isManagementTable);
Expand Down Expand Up @@ -275,23 +276,24 @@ export const useColumns = (
];

if (isManagementTable === true) {
// insert before last column
columns.splice(columns.length - 1, 0, {
name: i18n.translate('xpack.ml.jobsList.analyticsSpacesLabel', {
defaultMessage: 'Spaces',
}),
render: (item: DataFrameAnalyticsListRow) =>
Array.isArray(item.spaceIds) ? (
<JobSpacesList
spaceIds={item.spaceIds ?? []}
jobId={item.id}
jobType="data-frame-analytics"
refresh={refresh}
/>
) : null,
width: '90px',
});

if (spacesEnabled === true) {
// insert before last column
columns.splice(columns.length - 1, 0, {
name: i18n.translate('xpack.ml.jobsList.analyticsSpacesLabel', {
defaultMessage: 'Spaces',
}),
render: (item: DataFrameAnalyticsListRow) =>
Array.isArray(item.spaceIds) ? (
<JobSpacesList
spaceIds={item.spaceIds ?? []}
jobId={item.id}
jobType="data-frame-analytics"
refresh={refresh}
/>
) : null,
width: '90px',
});
}
// Remove actions if Ml not enabled in current space
if (isMlEnabledInSpace === false) {
columns.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class JobsList extends Component {
}

render() {
const { loading, isManagementTable } = this.props;
const { loading, isManagementTable, spacesEnabled } = this.props;
const selectionControls = {
selectable: (job) => job.deleting !== true,
selectableMessage: (selectable, rowItem) =>
Expand Down Expand Up @@ -242,20 +242,22 @@ export class JobsList extends Component {
];

if (isManagementTable === true) {
// insert before last column
columns.splice(columns.length - 1, 0, {
name: i18n.translate('xpack.ml.jobsList.spacesLabel', {
defaultMessage: 'Spaces',
}),
render: (item) => (
<JobSpacesList
spaceIds={item.spaceIds}
jobId={item.id}
jobType="anomaly-detector"
refresh={this.props.refreshJobs}
/>
),
});
if (spacesEnabled === true) {
// insert before last column
columns.splice(columns.length - 1, 0, {
name: i18n.translate('xpack.ml.jobsList.spacesLabel', {
defaultMessage: 'Spaces',
}),
render: (item) => (
<JobSpacesList
spaceIds={item.spaceIds}
jobId={item.id}
jobType="anomaly-detector"
refresh={this.props.refreshJobs}
/>
),
});
}
// Remove actions if Ml not enabled in current space
if (this.props.isMlEnabledInSpace === false) {
columns.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class JobsListView extends Component {
deletingJobIds: [],
};

this.spacesEnabled = props.spacesEnabled ?? false;
this.updateFunctions = {};

this.showEditJobFlyout = () => {};
Expand Down Expand Up @@ -253,7 +254,7 @@ export class JobsListView extends Component {
const expandedJobsIds = Object.keys(this.state.itemIdToExpandedRowMap);
try {
let spaces = {};
if (this.props.isManagementTable) {
if (this.props.spacesEnabled && this.props.isManagementTable) {
const allSpaces = await ml.savedObjects.jobsSpaces();
spaces = allSpaces['anomaly-detector'];
}
Expand All @@ -267,7 +268,10 @@ export class JobsListView extends Component {
}
job.latestTimestampSortValue = job.latestTimestampMs || 0;
job.spaceIds =
this.props.isManagementTable && spaces && spaces[job.id] !== undefined
this.props.spacesEnabled &&
this.props.isManagementTable &&
spaces &&
spaces[job.id] !== undefined
? spaces[job.id]
: [];
return job;
Expand Down Expand Up @@ -379,6 +383,7 @@ export class JobsListView extends Component {
loading={loading}
isManagementTable={true}
isMlEnabledInSpace={this.props.isMlEnabledInSpace}
spacesEnabled={this.props.spacesEnabled}
jobsViewState={this.props.jobsViewState}
onJobsViewStateUpdate={this.props.onJobsViewStateUpdate}
refreshJobs={() => this.refreshJobSummaryList(true)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { JobsListView } from '../../../../jobs/jobs_list/components/jobs_list_vi
import { DataFrameAnalyticsList } from '../../../../data_frame_analytics/pages/analytics_management/components/analytics_list';
import { AccessDeniedPage } from '../access_denied_page';
import { SharePluginStart } from '../../../../../../../../../src/plugins/share/public';
import { SpacesPluginStart } from '../../../../../../../spaces/public';
import {
AnomalyDetectionJobsListState,
getDefaultAnomalyDetectionJobsListState,
Expand All @@ -48,7 +49,7 @@ interface Tab extends EuiTabbedContentTab {
'data-test-subj': string;
}

function useTabs(isMlEnabledInSpace: boolean): Tab[] {
function useTabs(isMlEnabledInSpace: boolean, spacesEnabled: boolean): Tab[] {
const [jobsViewState, setJobsViewState] = useState<AnomalyDetectionJobsListState>(
getDefaultAnomalyDetectionJobsListState()
);
Expand Down Expand Up @@ -79,6 +80,7 @@ function useTabs(isMlEnabledInSpace: boolean): Tab[] {
onJobsViewStateUpdate={updateState}
isManagementTable={true}
isMlEnabledInSpace={isMlEnabledInSpace}
spacesEnabled={spacesEnabled}
/>
</Fragment>
),
Expand All @@ -95,6 +97,7 @@ function useTabs(isMlEnabledInSpace: boolean): Tab[] {
<DataFrameAnalyticsList
isManagementTable={true}
isMlEnabledInSpace={isMlEnabledInSpace}
spacesEnabled={spacesEnabled}
/>
</Fragment>
),
Expand All @@ -108,21 +111,26 @@ export const JobsListPage: FC<{
coreStart: CoreStart;
share: SharePluginStart;
history: ManagementAppMountParams['history'];
}> = ({ coreStart, share, history }) => {
spaces?: SpacesPluginStart;
}> = ({ coreStart, share, history, spaces }) => {
const spacesEnabled = spaces !== undefined;
const [initialized, setInitialized] = useState(false);
const [accessDenied, setAccessDenied] = useState(false);
const [showRepairFlyout, setShowRepairFlyout] = useState(false);
const [isMlEnabledInSpace, setIsMlEnabledInSpace] = useState(false);
const tabs = useTabs(isMlEnabledInSpace);
const tabs = useTabs(isMlEnabledInSpace, spacesEnabled);
const [currentTabId, setCurrentTabId] = useState(tabs[0].id);
const I18nContext = coreStart.i18n.Context;
const spacesContext = useMemo(() => createSpacesContext(coreStart.http), []);

const check = async () => {
try {
const checkPrivilege = await checkGetManagementMlJobsResolver();
setIsMlEnabledInSpace(checkPrivilege.mlFeatureEnabledInSpace);
spacesContext.allSpaces = await spacesContext.spacesManager.getSpaces();
const { mlFeatureEnabledInSpace } = await checkGetManagementMlJobsResolver();
setIsMlEnabledInSpace(mlFeatureEnabledInSpace);
spacesContext.spacesEnabled = spacesEnabled;
if (spacesEnabled) {
spacesContext.allSpaces = await spacesContext.spacesManager.getSpaces();
}
} catch (e) {
setAccessDenied(true);
}
Expand Down Expand Up @@ -223,15 +231,17 @@ export const JobsListPage: FC<{
</EuiTitle>
<EuiSpacer size="l" />
<EuiPageContentBody>
<>
<EuiButtonEmpty onClick={() => setShowRepairFlyout(true)}>
{i18n.translate('xpack.ml.management.jobsList.repairFlyoutButton', {
defaultMessage: 'Repair saved objects',
})}
</EuiButtonEmpty>
{showRepairFlyout && <JobSpacesRepairFlyout onClose={onCloseRepairFlyout} />}
</>
<EuiSpacer size="s" />
{spacesEnabled && (
<>
<EuiButtonEmpty onClick={() => setShowRepairFlyout(true)}>
{i18n.translate('xpack.ml.management.jobsList.repairFlyoutButton', {
defaultMessage: 'Repair saved objects',
})}
</EuiButtonEmpty>
{showRepairFlyout && <JobSpacesRepairFlyout onClose={onCloseRepairFlyout} />}
<EuiSpacer size="s" />
</>
)}
{renderTabs()}
</EuiPageContentBody>
</EuiPageContent>
Expand Down
18 changes: 14 additions & 4 deletions x-pack/plugins/ml/public/application/management/jobs_list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ import { getJobsListBreadcrumbs } from '../breadcrumbs';
import { setDependencyCache, clearCache } from '../../util/dependency_cache';
import './_index.scss';
import { SharePluginStart } from '../../../../../../../src/plugins/share/public';
import { SpacesPluginStart } from '../../../../../spaces/public';

const renderApp = (
element: HTMLElement,
history: ManagementAppMountParams['history'],
coreStart: CoreStart,
share: SharePluginStart
share: SharePluginStart,
spaces?: SpacesPluginStart
) => {
ReactDOM.render(React.createElement(JobsListPage, { coreStart, history, share }), element);
ReactDOM.render(
React.createElement(JobsListPage, { coreStart, history, share, spaces }),
element
);
return () => {
unmountComponentAtNode(element);
clearCache();
Expand All @@ -42,6 +47,11 @@ export async function mountApp(
});

params.setBreadcrumbs(getJobsListBreadcrumbs());

return renderApp(params.element, params.history, coreStart, pluginsStart.share);
return renderApp(
params.element,
params.history,
coreStart,
pluginsStart.share,
pluginsStart.spaces
);
}
2 changes: 2 additions & 0 deletions x-pack/plugins/ml/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { DataPublicPluginStart } from 'src/plugins/data/public';
import type { HomePublicPluginSetup } from 'src/plugins/home/public';
import type { IndexPatternManagementSetup } from 'src/plugins/index_pattern_management/public';
import type { EmbeddableSetup } from 'src/plugins/embeddable/public';
import type { SpacesPluginStart } from '../../spaces/public';

import { AppStatus, AppUpdater, DEFAULT_APP_CATEGORIES } from '../../../../src/core/public';
import type { UiActionsSetup, UiActionsStart } from '../../../../src/plugins/ui_actions/public';
Expand All @@ -50,6 +51,7 @@ export interface MlStartDependencies {
share: SharePluginStart;
kibanaLegacy: KibanaLegacyStart;
uiActions: UiActionsStart;
spaces?: SpacesPluginStart;
}
export interface MlSetupDependencies {
security?: SecurityPluginSetup;
Expand Down

0 comments on commit 21a176c

Please sign in to comment.