Skip to content

Commit

Permalink
[ML] Disables annotations feature in TSV if annotations fail to load.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Mar 4, 2019
1 parent d178eb3 commit 3b0b058
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import _ from 'lodash';
import d3 from 'd3';
import moment from 'moment';

import chrome from 'ui/chrome';

import {
getSeverityWithLow,
getMultiBucketImpactLabel,
Expand Down Expand Up @@ -58,8 +56,6 @@ import {

import { injectI18n } from '@kbn/i18n/react';

const mlAnnotationsEnabled = chrome.getInjected('mlAnnotationsEnabled', false);

const focusZoomPanelHeight = 25;
const focusChartHeight = 310;
const focusHeight = focusZoomPanelHeight + focusChartHeight;
Expand Down Expand Up @@ -93,6 +89,7 @@ function getSvgHeight() {

const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Component {
static propTypes = {
annotationsEnabled: PropTypes.bool,
annotation: PropTypes.object,
autoZoomDuration: PropTypes.number,
contextAggregationInterval: PropTypes.object,
Expand Down Expand Up @@ -133,6 +130,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo

componentDidMount() {
const {
annotationsEnabled,
svgWidth
} = this.props;

Expand Down Expand Up @@ -165,7 +163,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo
this.fieldFormat = undefined;

// Annotations Brush
if (mlAnnotationsEnabled) {
if (annotationsEnabled) {
this.annotateBrush = getAnnotationBrush.call(this);
}

Expand Down Expand Up @@ -212,14 +210,15 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo

this.renderFocusChart();

if (mlAnnotationsEnabled && this.props.annotation === null) {
if (this.props.annotationsEnabled && this.props.annotation === null) {
const chartElement = d3.select(this.rootNode);
chartElement.select('g.mlAnnotationBrush').call(this.annotateBrush.extent([0, 0]));
}
}

renderChart() {
const {
annotationsEnabled,
contextChartData,
contextForecastData,
detectorIndex,
Expand Down Expand Up @@ -321,7 +320,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo
.attr('transform', 'translate(' + margin.left + ',' + (focusHeight + margin.top + chartSpacing) + ')');

// Mask to hide annotations overflow
if (mlAnnotationsEnabled) {
if (annotationsEnabled) {
const annotationsMask = svg
.append('defs')
.append('mask')
Expand Down Expand Up @@ -397,6 +396,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo
// as we want to re-render the paths and points when the zoom area changes.

const {
annotationsEnabled,
contextForecastData
} = this.props;

Expand All @@ -413,7 +413,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo
this.createZoomInfoElements(zoomGroup, fcsWidth);

// Create the elements for annotations
if (mlAnnotationsEnabled) {
if (annotationsEnabled) {
const annotateBrush = this.annotateBrush.bind(this);

fcsGroup.append('g')
Expand Down Expand Up @@ -495,6 +495,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo

renderFocusChart() {
const {
annotationsEnabled,
focusAggregationInterval,
focusAnnotationData,
focusChartData,
Expand Down Expand Up @@ -596,7 +597,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo

// if annotations are present, we extend yMax to avoid overlap
// between annotation labels, chart lines and anomalies.
if (mlAnnotationsEnabled && focusAnnotationData && focusAnnotationData.length > 0) {
if (annotationsEnabled && focusAnnotationData && focusAnnotationData.length > 0) {
const levels = getAnnotationLevels(focusAnnotationData);
const maxLevel = d3.max(Object.keys(levels).map(key => levels[key]));
// TODO needs revisiting to be a more robust normalization
Expand Down Expand Up @@ -632,7 +633,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo
.classed('hidden', !showModelBounds);
}

if (mlAnnotationsEnabled) {
if (annotationsEnabled) {
renderAnnotations(
focusChart,
focusAnnotationData,
Expand Down Expand Up @@ -1236,6 +1237,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo

showFocusChartTooltip(marker, circle) {
const {
annotationsEnabled,
modelPlotEnabled,
intl
} = this.props;
Expand Down Expand Up @@ -1376,7 +1378,7 @@ const TimeseriesChartIntl = injectI18n(class TimeseriesChart extends React.Compo
});
}

if (mlAnnotationsEnabled && _.has(marker, 'annotation')) {
if (annotationsEnabled && _.has(marker, 'annotation')) {
contents = mlEscape(marker.annotation);
contents += `<br />${moment(marker.timestamp).format('MMMM Do YYYY, HH:mm')}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ const module = uiModules.get('apps/ml');

import { I18nContext } from 'ui/i18n';

import chrome from 'ui/chrome';
const mlAnnotationsEnabled = chrome.getInjected('mlAnnotationsEnabled', false);

module.directive('mlTimeseriesChart', function ($timeout) {

function link(scope, element) {
Expand All @@ -44,6 +41,7 @@ module.directive('mlTimeseriesChart', function ($timeout) {
svgWidth = Math.max(angular.element('.results-container').width(), 0);

const props = {
annotationsEnabled: scope.annotationsEnabled,
autoZoomDuration: scope.autoZoomDuration,
contextAggregationInterval: scope.contextAggregationInterval,
contextChartData: scope.contextChartData,
Expand Down Expand Up @@ -91,7 +89,7 @@ module.directive('mlTimeseriesChart', function ($timeout) {
scope.$watchCollection('focusForecastData', renderFocusChart);
scope.$watchCollection('focusChartData', renderFocusChart);
scope.$watchGroup(['showModelBounds', 'showForecast'], renderFocusChart);
if (mlAnnotationsEnabled) {
if (scope.annotationsEnabled) {
scope.$watchCollection('focusAnnotationData', renderFocusChart);
scope.$watch('showAnnotations', renderFocusChart);
}
Expand All @@ -116,6 +114,7 @@ module.directive('mlTimeseriesChart', function ($timeout) {

return {
scope: {
annotationsEnabled: '=',
selectedJob: '=',
detectorIndex: '=',
modelPlotEnabled: '=',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
<div class="ml-timeseries-chart">

<ml-timeseries-chart style="width: 1200px; height: 400px;"
annotations-enabled="showAnnotationsCheckbox"
selected-job="selectedJob"
detector-index="detectorId"
model-plot-enabled="modelPlotEnabled"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import { severity$ } from '../components/controls/select_severity/select_severit


import chrome from 'ui/chrome';
const mlAnnotationsEnabled = chrome.getInjected('mlAnnotationsEnabled', false);
let mlAnnotationsEnabled = chrome.getInjected('mlAnnotationsEnabled', false);

uiRoutes
.when('/timeseriesexplorer/?', {
Expand Down Expand Up @@ -416,6 +416,12 @@ module.controller('MlTimeSeriesExplorerController', function (
console.log('Time series explorer focus chart data set:', $scope.focusChartData);

$scope.loading = false;

// If the annotations failed to load and the feature flag is set to `false`,
// make sure the checkbox toggle gets hidden.
if (mlAnnotationsEnabled === false) {
$scope.showAnnotationsCheckbox = false;
}
});
}
}
Expand Down Expand Up @@ -490,19 +496,24 @@ module.controller('MlTimeSeriesExplorerController', function (
latestMs: searchBounds.max.valueOf(),
maxAnnotations: ANNOTATIONS_TABLE_DEFAULT_QUERY_SIZE
}).then((resp) => {
refreshFocusData.focusAnnotationData = resp.annotations[$scope.selectedJob.job_id]
.sort((a, b) => {
return a.timestamp - b.timestamp;
})
.map((d, i) => {
d.key = String.fromCharCode(65 + i);
return d;
});
refreshFocusData.focusAnnotationData = [];

if (Array.isArray(resp.annotations[$scope.selectedJob.job_id])) {
refreshFocusData.focusAnnotationData = resp.annotations[$scope.selectedJob.job_id]
.sort((a, b) => {
return a.timestamp - b.timestamp;
})
.map((d, i) => {
d.key = String.fromCharCode(65 + i);
return d;
});
}

finish();
}).catch(() => {
// silent fail
// silently fail and disable annotations feature if loading annotations fails.
refreshFocusData.focusAnnotationData = [];
mlAnnotationsEnabled = false;
finish();
});
} else {
Expand Down

0 comments on commit 3b0b058

Please sign in to comment.