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] Makes mlExplorerDashboardService independent of angularjs #23874

Merged
merged 3 commits into from
Oct 5, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import $ from 'jquery';
import { ExplorerChartsContainer } from './explorer_charts_container';
import { explorerChartsContainerServiceFactory } from './explorer_charts_container_service';
import { mlChartTooltipService } from '../../components/chart_tooltip/chart_tooltip_service';
import { mlExplorerDashboardService } from '../explorer_dashboard_service';

import { uiModules } from 'ui/modules';
const module = uiModules.get('apps/ml');

module.directive('mlExplorerChartsContainer', function (
mlExplorerDashboardService,
mlSelectSeverityService
) {

Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/ml/public/explorer/explorer_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { loadIndexPatterns, getIndexPatterns } from 'plugins/ml/util/index_utils
import { refreshIntervalWatcher } from 'plugins/ml/util/refresh_interval_watcher';
import { IntervalHelperProvider, getBoundsRoundedToInterval } from 'plugins/ml/util/ml_time_buckets';
import { ml } from 'plugins/ml/services/ml_api_service';
import { mlExplorerDashboardService } from './explorer_dashboard_service';
import { mlResultsService } from 'plugins/ml/services/results_service';
import { mlJobService } from 'plugins/ml/services/job_service';
import { mlFieldFormatService } from 'plugins/ml/services/field_format_service';
Expand Down Expand Up @@ -76,7 +77,6 @@ module.controller('MlExplorerController', function (
AppState,
Private,
mlCheckboxShowChartsService,
mlExplorerDashboardService,
mlSelectLimitService,
mlSelectIntervalService,
mlSelectSeverityService) {
Expand Down Expand Up @@ -360,7 +360,6 @@ module.controller('MlExplorerController', function (
MlTimeBuckets: TimeBuckets,
swimlaneData: getSwimlaneData(swimlaneType),
swimlaneType,
mlExplorerDashboardService,
selection: $scope.appState.mlExplorerSwimlane
};
}
Expand Down
30 changes: 16 additions & 14 deletions x-pack/plugins/ml/public/explorer/explorer_dashboard_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@
* components in the Explorer dashboard.
*/

import { uiModules } from 'ui/modules';
const module = uiModules.get('apps/ml');

import { listenerFactoryProvider } from 'plugins/ml/factories/listener_factory';

module.service('mlExplorerDashboardService', function () {
this.allowCellRangeSelection = false;
function mlExplorerDashboardServiceFactory() {
const service = {
allowCellRangeSelection: false
};

const listenerFactory = listenerFactoryProvider();
const dragSelect = this.dragSelect = listenerFactory();
const swimlaneCellClick = this.swimlaneCellClick = listenerFactory();
const swimlaneDataChange = this.swimlaneDataChange = listenerFactory();
const swimlaneRenderDone = this.swimlaneRenderDone = listenerFactory();
const chartsInitDone = this.chartsInitDone = listenerFactory();
this.anomalyDataChange = listenerFactory();

this.init = function () {
const dragSelect = service.dragSelect = listenerFactory();
const swimlaneCellClick = service.swimlaneCellClick = listenerFactory();
const swimlaneDataChange = service.swimlaneDataChange = listenerFactory();
const swimlaneRenderDone = service.swimlaneRenderDone = listenerFactory();
const chartsInitDone = service.chartsInitDone = listenerFactory();
service.anomalyDataChange = listenerFactory();

service.init = function () {
// Clear out any old listeners.
dragSelect.unwatchAll();
swimlaneCellClick.unwatchAll();
Expand All @@ -36,4 +35,7 @@ module.service('mlExplorerDashboardService', function () {
chartsInitDone.unwatchAll();
};

});
return service;
}

export const mlExplorerDashboardService = mlExplorerDashboardServiceFactory();
6 changes: 1 addition & 5 deletions x-pack/plugins/ml/public/explorer/explorer_swimlane.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { numTicksForDateFormat } from '../util/chart_utils';
import { getSeverityColor } from '../../common/util/anomaly_utils';
import { mlEscape } from '../util/string_utils';
import { mlChartTooltipService } from '../components/chart_tooltip/chart_tooltip_service';
import { mlExplorerDashboardService } from './explorer_dashboard_service';
import { DRAG_SELECT_ACTION } from './explorer_constants';

export class ExplorerSwimlane extends React.Component {
Expand All @@ -33,7 +34,6 @@ export class ExplorerSwimlane extends React.Component {
laneLabels: PropTypes.array.isRequired
}).isRequired,
swimlaneType: PropTypes.string.isRequired,
mlExplorerDashboardService: PropTypes.object.isRequired,
selection: PropTypes.object
}

Expand All @@ -43,15 +43,13 @@ export class ExplorerSwimlane extends React.Component {
cellMouseoverActive = true;

componentWillUnmount() {
const { mlExplorerDashboardService } = this.props;
mlExplorerDashboardService.dragSelect.unwatch(this.boundDragSelectListener);
const element = d3.select(this.rootNode);
element.html('');
}

componentDidMount() {
const element = d3.select(this.rootNode.parentNode);
const { mlExplorerDashboardService } = this.props;

// Consider the setting to support to select a range of cells
if (!mlExplorerDashboardService.allowCellRangeSelection) {
Expand Down Expand Up @@ -124,7 +122,6 @@ export class ExplorerSwimlane extends React.Component {
selectCell(cellsToSelect, { laneLabels, bucketScore, times }) {
const {
selection,
mlExplorerDashboardService,
swimlaneData,
swimlaneType
} = this.props;
Expand Down Expand Up @@ -222,7 +219,6 @@ export class ExplorerSwimlane extends React.Component {
MlTimeBuckets,
swimlaneData,
swimlaneType,
mlExplorerDashboardService,
selection
} = this.props;

Expand Down
36 changes: 18 additions & 18 deletions x-pack/plugins/ml/public/explorer/explorer_swimlane.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import moment from 'moment-timezone';
import { mount } from 'enzyme';
import React from 'react';

import { mlExplorerDashboardService } from './explorer_dashboard_service';
import { ExplorerSwimlane } from './explorer_swimlane';

jest.mock('ui/chrome', () => ({
Expand All @@ -19,8 +20,8 @@ jest.mock('ui/chrome', () => ({
}),
}));

function getExplorerSwimlaneMocks() {
const mlExplorerDashboardService = {
jest.mock('./explorer_dashboard_service', () => ({
mlExplorerDashboardService: {
allowCellRangeSelection: false,
dragSelect: {
watch: jest.fn(),
Expand All @@ -32,8 +33,10 @@ function getExplorerSwimlaneMocks() {
swimlaneRenderDone: {
changed: jest.fn()
}
};
}
}));

function getExplorerSwimlaneMocks() {
const MlTimeBucketsMethods = {
setInterval: jest.fn(),
getScaledDateFormat: jest.fn()
Expand All @@ -44,7 +47,6 @@ function getExplorerSwimlaneMocks() {
const swimlaneData = { laneLabels: [] };

return {
mlExplorerDashboardService,
MlTimeBuckets,
swimlaneData
};
Expand Down Expand Up @@ -72,7 +74,6 @@ describe('ExplorerSwimlane', () => {
MlTimeBuckets={mocks.MlTimeBuckets}
swimlaneData={mocks.swimlaneData}
swimlaneType="overall"
mlExplorerDashboardService={mocks.mlExplorerDashboardService}
/>);

expect(wrapper.html()).toBe(
Expand All @@ -81,12 +82,12 @@ describe('ExplorerSwimlane', () => {
);

// test calls to mock functions
expect(mocks.mlExplorerDashboardService.swimlaneRenderDone.changed.mock.calls).toHaveLength(1);
expect(mocks.mlExplorerDashboardService.dragSelect.watch.mock.calls).toHaveLength(1);
expect(mocks.mlExplorerDashboardService.dragSelect.unwatch.mock.calls).toHaveLength(0);
expect(mocks.mlExplorerDashboardService.swimlaneCellClick.changed.mock.calls).toHaveLength(0);
expect(mocks.MlTimeBuckets.mockMethods.setInterval.mock.calls).toHaveLength(1);
expect(mocks.MlTimeBuckets.mockMethods.getScaledDateFormat.mock.calls).toHaveLength(1);
expect(mlExplorerDashboardService.swimlaneRenderDone.changed.mock.calls.length).toBeGreaterThanOrEqual(1);
expect(mlExplorerDashboardService.dragSelect.watch.mock.calls.length).toBeGreaterThanOrEqual(1);
expect(mlExplorerDashboardService.dragSelect.unwatch.mock.calls).toHaveLength(0);
expect(mlExplorerDashboardService.swimlaneCellClick.changed.mock.calls).toHaveLength(0);
expect(mocks.MlTimeBuckets.mockMethods.setInterval.mock.calls.length).toBeGreaterThanOrEqual(1);
expect(mocks.MlTimeBuckets.mockMethods.getScaledDateFormat.mock.calls.length).toBeGreaterThanOrEqual(1);
});

test('Overall swimlane', () => {
Expand All @@ -97,17 +98,16 @@ describe('ExplorerSwimlane', () => {
MlTimeBuckets={mocks.MlTimeBuckets}
swimlaneData={mockOverallSwimlaneData}
swimlaneType="overall"
mlExplorerDashboardService={mocks.mlExplorerDashboardService}
/>);

expect(wrapper.html()).toMatchSnapshot();

// test calls to mock functions
expect(mocks.mlExplorerDashboardService.swimlaneRenderDone.changed.mock.calls).toHaveLength(1);
expect(mocks.mlExplorerDashboardService.dragSelect.watch.mock.calls).toHaveLength(1);
expect(mocks.mlExplorerDashboardService.dragSelect.unwatch.mock.calls).toHaveLength(0);
expect(mocks.mlExplorerDashboardService.swimlaneCellClick.changed.mock.calls).toHaveLength(0);
expect(mocks.MlTimeBuckets.mockMethods.setInterval.mock.calls).toHaveLength(1);
expect(mocks.MlTimeBuckets.mockMethods.getScaledDateFormat.mock.calls).toHaveLength(1);
expect(mlExplorerDashboardService.swimlaneRenderDone.changed.mock.calls.length).toBeGreaterThanOrEqual(1);
expect(mlExplorerDashboardService.dragSelect.watch.mock.calls.length).toBeGreaterThanOrEqual(1);
expect(mlExplorerDashboardService.dragSelect.unwatch.mock.calls).toHaveLength(0);
expect(mlExplorerDashboardService.swimlaneCellClick.changed.mock.calls).toHaveLength(0);
expect(mocks.MlTimeBuckets.mockMethods.setInterval.mock.calls.length).toBeGreaterThanOrEqual(1);
expect(mocks.MlTimeBuckets.mockMethods.getScaledDateFormat.mock.calls.length).toBeGreaterThanOrEqual(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { ExplorerSwimlane } from './explorer_swimlane';
import { uiModules } from 'ui/modules';
const module = uiModules.get('apps/ml');

module.directive('mlExplorerSwimlane', function (mlExplorerDashboardService) {
import { mlExplorerDashboardService } from './explorer_dashboard_service';

module.directive('mlExplorerSwimlane', function () {

function link(scope, element) {
function swimlaneDataChangeListener(props) {
Expand Down