Skip to content

Commit

Permalink
[ML] Fix Anomaly Table update trigger (elastic#18769) (elastic#18774)
Browse files Browse the repository at this point in the history
Fixes an issue with the anomaly table not correctly updating in the anomaly explorer and the anomaly markers not showing up in the time series viewer.
- moves the initialization of scope.anomalyRecords = []; to the controllers of anomaly explorer and time series viewer. having it in the anomaly table directive caused a race condition to overwrite already fetched records.
- use $watchCollection instead of $watch to track anomalyRecords and focusChartData changes.
replaces $timeouts with scope.$evalAsync
- fixes how focusChartData gets updated correctly to fix missing anomaly markers in the time series viewer.
  • Loading branch information
walterra committed May 4, 2018
1 parent 3e31069 commit a77e2bf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ module.directive('mlAnomaliesTable', function (
scope.table.columns = [];
scope.table.rows = [];
scope.rowScopes = [];
scope.anomalyRecords = [];

scope.influencersLimit = 5;

Expand All @@ -94,7 +93,7 @@ module.directive('mlAnomaliesTable', function (
mlSelectIntervalService.state.watch(updateTableData);
mlSelectSeverityService.state.watch(updateTableData);

scope.$watch('anomalyRecords', updateTableData);
scope.$watchCollection('anomalyRecords', updateTableData);

element.on('$destroy', () => {
mlSelectIntervalService.state.unwatch(updateTableData);
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/ml/public/explorer/explorer_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ module.controller('MlExplorerController', function (
const ALLOW_CELL_RANGE_SELECTION = mlExplorerDashboardService.allowCellRangeSelection;
let disableDragSelectOnMouseLeave = true;
$scope.queryFilters = [];
$scope.anomalyRecords = [];

const dragSelect = new DragSelect({
selectables: document.querySelectorAll('.sl-cell'),
Expand Down Expand Up @@ -171,11 +172,11 @@ module.controller('MlExplorerController', function (
)
.then((resp) => {
// Need to use $timeout to ensure the update happens after the child scope is updated with the new data.
$timeout(() => {
$scope.$evalAsync(() => {
// Sort in descending time order before storing in scope.
$scope.anomalyRecords = _.chain(resp.records).sortBy(record => record[$scope.timeFieldName]).reverse().value();
console.log('Explorer anomalies table data set:', $scope.anomalyRecords);
}, 0);
});
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ module.directive('mlTimeseriesChart', function (
drawContextChartSelection();
});

scope.$watch('focusChartData', renderFocusChart);
scope.$watch('showModelBounds', renderFocusChart);
scope.$watch('showForecast', renderFocusChart);
scope.$watchCollection('focusChartData', renderFocusChart);
scope.$watchGroup(['showModelBounds', 'showForecast'], renderFocusChart);

// Redraw the charts when the container is resize.
const resizeChecker = new ResizeChecker(angular.element('.ml-timeseries-chart'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module.controller('MlTimeSeriesExplorerController', function (
$scope.loading = true;
$scope.loadCounter = 0;
$scope.hasResults = false;
$scope.anomalyRecords = [];

$scope.modelPlotEnabled = false;
$scope.showModelBounds = true; // Toggles display of model bounds in the focus chart
Expand Down Expand Up @@ -350,20 +351,21 @@ module.controller('MlTimeSeriesExplorerController', function (
if (awaitingCount === 0) {
// Tell the results container directives to render the focus chart.
// Need to use $timeout to ensure the broadcast happens after the child scope is updated with the new data.
$timeout(() => {
processDataForFocusAnomalies(
$scope.focusChartData,
$scope.anomalyRecords,
$scope.timeFieldName);
let updatedFocusChartData = processDataForFocusAnomalies(
$scope.focusChartData,
$scope.anomalyRecords,
$scope.timeFieldName);

processScheduledEventsForChart(
$scope.focusChartData,
$scope.scheduledEvents);
updatedFocusChartData = processScheduledEventsForChart(
$scope.focusChartData,
$scope.scheduledEvents);

$scope.$evalAsync(() => {
$scope.focusChartData = updatedFocusChartData;
console.log('Time series explorer focus chart data set:', $scope.focusChartData);

$scope.loading = false;
}, 0);
});
}
}

Expand Down

0 comments on commit a77e2bf

Please sign in to comment.