Skip to content
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

[ML] Fix navigation for the Basic licence #139469

Merged
merged 6 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x-pack/plugins/ml/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const renderApp = (

appMountParams.onAppLeave((actions) => actions.default());

const mlLicense = setLicenseCache(deps.licensing, [
const mlLicense = setLicenseCache(deps.licensing, coreStart.application, [
() =>
ReactDOM.render(
<App coreStart={coreStart} deps={deps} appMountParams={appMountParams} />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getToastNotificationService,
toastNotificationServiceProvider,
} from '../../../services/toast_notification_service';
import { getToastNotifications } from '../../../util/dependency_cache';
import { getApplication, getToastNotifications } from '../../../util/dependency_cache';
import { ml } from '../../../services/ml_api_service';
import { stringMatch } from '../../../util/string_utils';
import { getDataViewNames } from '../../../util/index_utils';
Expand All @@ -22,6 +22,8 @@ import { JOB_ACTION } from '../../../../../common/constants/job_actions';
import { parseInterval } from '../../../../../common/util/parse_interval';
import { mlCalendarService } from '../../../services/calendar_service';
import { isPopulatedObject } from '@kbn/ml-is-populated-object';
import { ML_PAGES } from '../../../../../common/constants/locator';
import { PLUGIN_ID } from '../../../../../common/constants/app';

export function loadFullJob(jobId) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -287,7 +289,7 @@ export async function cloneJob(jobId) {
);
}

window.location.href = '#/jobs/new_job';
getApplication().navigateToApp(PLUGIN_ID, { path: ML_PAGES.ANOMALY_DETECTION_CREATE_JOB });
} catch (error) {
getToastNotificationService().displayErrorToast(
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { LicensingPluginSetup } from '@kbn/licensing-plugin/public';
import type { LicensingPluginSetup } from '@kbn/licensing-plugin/public';
import type { CoreStart } from '@kbn/core/public';
import { MlLicense } from '../../../common/license';
import { MlClientLicense } from './ml_client_license';

Expand All @@ -16,13 +17,16 @@ let mlLicense: MlClientLicense | null = null;
*
* @export
* @param {LicensingPluginSetup} licensingSetup
* @param application
* @param postInitFunctions
* @returns {MlClientLicense}
*/
export function setLicenseCache(
licensingSetup: LicensingPluginSetup,
application: CoreStart['application'],
postInitFunctions?: Array<(lic: MlLicense) => void>
) {
mlLicense = new MlClientLicense();
mlLicense = new MlClientLicense(application);
mlLicense.setup(licensingSetup.license$, postInitFunctions);
return mlLicense;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import { Observable, Subject } from 'rxjs';
import { ILicense } from '@kbn/licensing-plugin/common/types';

import { MlClientLicense } from './ml_client_license';
import { applicationServiceMock } from '@kbn/core/public/application/application_service.mock';

describe('MlClientLicense', () => {
const startApplicationContractMock = applicationServiceMock.createStartContract();

test('should miss the license update when initialized without postInitFunction', () => {
const mlLicense = new MlClientLicense();
const mlLicense = new MlClientLicense(startApplicationContractMock);

// upon instantiation the full license doesn't get set
expect(mlLicense.isFullLicense()).toBe(false);
Expand All @@ -35,7 +38,7 @@ describe('MlClientLicense', () => {
});

test('should not miss the license update when initialized with postInitFunction', (done) => {
const mlLicense = new MlClientLicense();
const mlLicense = new MlClientLicense(startApplicationContractMock);

// upon instantiation the full license doesn't get set
expect(mlLicense.isFullLicense()).toBe(false);
Expand Down
35 changes: 21 additions & 14 deletions x-pack/plugins/ml/public/application/license/ml_client_license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,36 @@
* 2.0.
*/

import type { CoreStart } from '@kbn/core/public';
import { ML_PAGES } from '../../../common/constants/locator';
import { MlLicense } from '../../../common/license';
import { showExpiredLicenseWarning } from './expired_warning';
import { PLUGIN_ID } from '../../../common/constants/app';

export class MlClientLicense extends MlLicense {
fullLicenseResolver() {
constructor(private application: CoreStart['application']) {
super();
}

private redirectToKibana() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these should still return Promise.reject(); as the originals did.
They are used in the page loading resolver, if any of the promises reject at he beginning, the function will return early.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in e8767d4

this.application.navigateToApp('home');
return Promise.reject();
}

private redirectToBasic() {
this.application.navigateToApp(PLUGIN_ID, { path: ML_PAGES.DATA_VISUALIZER });
return Promise.reject();
}

fullLicenseResolver(): Promise<void> {
if (this.isMlEnabled() === false || this.isMinimumLicense() === false) {
// ML is not enabled or the license isn't at least basic
return redirectToKibana();
return this.redirectToKibana();
}

if (this.isFullLicense() === false) {
// ML is enabled, but only with a basic or gold license
return redirectToBasic();
return this.redirectToBasic();
}

// ML is enabled
Expand All @@ -30,7 +47,7 @@ export class MlClientLicense extends MlLicense {
basicLicenseResolver() {
if (this.isMlEnabled() === false || this.isMinimumLicense() === false) {
// ML is not enabled or the license isn't at least basic
return redirectToKibana();
return this.redirectToKibana();
}

// ML is enabled
Expand All @@ -40,13 +57,3 @@ export class MlClientLicense extends MlLicense {
return Promise.resolve();
}
}

function redirectToKibana() {
window.location.href = '/';
return Promise.reject();
}

function redirectToBasic() {
window.location.href = '#/datavisualizer';
return Promise.reject();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* 2.0.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../ftr_provider_context';

import { USER } from '../../../../functional/services/ml/security_common';

export default function ({ getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const ml = getService('ml');
const browser = getService('browser');

const testUsers = [
{ user: USER.ML_POWERUSER, discoverAvailable: true },
Expand Down Expand Up @@ -57,6 +58,10 @@ export default function ({ getService }: FtrProviderContext) {
await ml.testExecution.logTestStep('should load the ML app');
await ml.navigation.navigateToMl();

await ml.testExecution.logTestStep('should redirect to the "Data Visualizer" page');
const browserURl = await browser.getCurrentUrl();
expect(browserURl).to.contain('/ml/datavisualizer');

await ml.testExecution.logTestStep('should display the disabled "Overview" tab');
await ml.navigation.assertOverviewTabEnabled(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* 2.0.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../ftr_provider_context';

import { USER } from '../../../../functional/services/ml/security_common';

export default function ({ getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const ml = getService('ml');
const browser = getService('browser');

const testUsers = [
{ user: USER.ML_VIEWER, discoverAvailable: true },
Expand Down Expand Up @@ -57,6 +58,10 @@ export default function ({ getService }: FtrProviderContext) {
await ml.testExecution.logTestStep('should load the ML app');
await ml.navigation.navigateToMl();

await ml.testExecution.logTestStep('should redirect to the "Data Visualizer" page');
const browserURl = await browser.getCurrentUrl();
expect(browserURl).to.contain('/ml/datavisualizer');

pheyos marked this conversation as resolved.
Show resolved Hide resolved
await ml.testExecution.logTestStep('should display the disabled "Overview" tab');
await ml.navigation.assertOverviewTabEnabled(false);

Expand Down