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] Add annotation markers to the Anomaly Swimlane axis #97202

Merged
merged 17 commits into from
Apr 20, 2021
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 @@ -23,6 +23,7 @@ import {
loadAnomaliesTableData,
loadFilteredTopInfluencers,
loadTopInfluencers,
loadOverallAnnotations,
AppStateSelectedCells,
ExplorerJob,
} from '../explorer_utils';
Expand Down Expand Up @@ -55,6 +56,10 @@ const memoize = <T extends (...a: any[]) => any>(func: T, context?: any) => {
return memoizeOne(wrapWithLastRefreshArg<T>(func, context) as any, memoizeIsEqual);
};

const memoizedLoadOverallAnnotations = memoize<typeof loadOverallAnnotations>(
loadOverallAnnotations
);

const memoizedLoadAnnotationsTableData = memoize<typeof loadAnnotationsTableData>(
loadAnnotationsTableData
);
Expand Down Expand Up @@ -149,9 +154,17 @@ const loadExplorerDataProvider = (

const dateFormatTz = getDateFormatTz();

const interval = swimlaneBucketInterval.asSeconds();

// First get the data where we have all necessary args at hand using forkJoin:
// annotationsData, anomalyChartRecords, influencers, overallState, tableData, topFieldValues
return forkJoin({
overallAnnotations: memoizedLoadOverallAnnotations(
lastRefresh,
selectedJobs,
interval,
bounds
),
annotationsData: memoizedLoadAnnotationsTableData(
lastRefresh,
selectedCells,
Expand Down Expand Up @@ -214,6 +227,7 @@ const loadExplorerDataProvider = (
tap(explorerService.setChartsDataLoading),
mergeMap(
({
overallAnnotations,
anomalyChartRecords,
influencers,
overallState,
Expand Down Expand Up @@ -271,6 +285,7 @@ const loadExplorerDataProvider = (
}),
map(({ viewBySwimlaneState, filteredTopInfluencers }) => {
return {
overallAnnotations,
annotations: annotationsData,
influencers: filteredTopInfluencers as any,
loading: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const AnomalyTimeline: FC<AnomalyTimelineProps> = React.memo(
viewByPerPage,
swimlaneLimit,
loading,
overallAnnotations,
} = explorerState;

const menuItems = useMemo(() => {
Expand Down Expand Up @@ -240,6 +241,7 @@ export const AnomalyTimeline: FC<AnomalyTimelineProps> = React.memo(
isLoading={loading}
noDataWarning={<NoOverallData />}
showTimeline={false}
annotationsData={overallAnnotations.annotationsData}
/>

<EuiSpacer size="m" />
Expand All @@ -257,6 +259,7 @@ export const AnomalyTimeline: FC<AnomalyTimelineProps> = React.memo(
})
}
timeBuckets={timeBuckets}
showLegend={false}
swimlaneData={viewBySwimlaneData as ViewBySwimLaneData}
swimlaneType={SWIMLANE_TYPE.VIEW_BY}
selection={selectedCells}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ declare interface SwimlaneBounds {
latest: number;
}

export declare const loadOverallAnnotations: (
selectedJobs: ExplorerJob[],
interval: number,
bounds: TimeRangeBounds
) => Promise<AnnotationsTable>;

export declare const loadAnnotationsTableData: (
selectedCells: AppStateSelectedCells | undefined,
selectedJobs: ExplorerJob[],
Expand Down
51 changes: 51 additions & 0 deletions x-pack/plugins/ml/public/application/explorer/explorer_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,57 @@ export function getViewBySwimlaneOptions({
};
}

export function loadOverallAnnotations(selectedJobs, interval, bounds) {
const jobIds = selectedJobs.map((d) => d.id);
const timeRange = getSelectionTimeRange(undefined, interval, bounds);

return new Promise((resolve) => {
ml.annotations
.getAnnotations$({
jobIds,
earliestMs: timeRange.earliestMs,
latestMs: timeRange.latestMs,
maxAnnotations: ANNOTATIONS_TABLE_DEFAULT_QUERY_SIZE,
})
.toPromise()
.then((resp) => {
if (resp.error !== undefined || resp.annotations === undefined) {
const errorMessage = extractErrorMessage(resp.error);
return resolve({
annotationsData: [],
error: errorMessage !== '' ? errorMessage : undefined,
});
}

const annotationsData = [];
jobIds.forEach((jobId) => {
const jobAnnotations = resp.annotations[jobId];
if (jobAnnotations !== undefined) {
annotationsData.push(...jobAnnotations);
}
});

return resolve({
annotationsData: annotationsData
.sort((a, b) => {
return a.timestamp - b.timestamp;
})
.map((d, i) => {
d.key = (i + 1).toString();
return d;
}),
});
})
.catch((resp) => {
const errorMessage = extractErrorMessage(resp);
return resolve({
annotationsData: [],
error: errorMessage !== '' ? errorMessage : undefined,
});
});
});
}

export function loadAnnotationsTableData(selectedCells, selectedJobs, interval, bounds) {
const jobIds =
selectedCells !== undefined && selectedCells.viewByFieldName === VIEW_BY_JOB_LABEL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { SWIM_LANE_DEFAULT_PAGE_SIZE } from '../../explorer_constants';
import { InfluencersFilterQuery } from '../../../../../common/types/es_client';

export interface ExplorerState {
overallAnnotations: AnnotationsTable;
annotations: AnnotationsTable;
anomalyChartsDataLoading: boolean;
chartsData: ExplorerChartsData;
Expand Down Expand Up @@ -65,6 +66,11 @@ function getDefaultIndexPattern() {

export function getExplorerDefaultState(): ExplorerState {
return {
overallAnnotations: {
error: undefined,
annotationsData: [],
aggregations: {},
},
annotations: {
error: undefined,
annotationsData: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { FC, useEffect } from 'react';
import d3 from 'd3';
import { scaleTime } from 'd3-scale';
import { i18n } from '@kbn/i18n';
import { formatHumanReadableDateTimeSeconds } from '../../../common/util/date_utils';
import { AnnotationsTable } from '../../../common/types/annotations';
import { ChartTooltipService } from '../components/chart_tooltip';

export const Y_AXIS_LABEL_WIDTH = 170;
export const Y_AXIS_LABEL_PADDING = 8;
export const Y_AXIS_LABEL_FONT_COLOR = '#6a717d';
const ANNOTATION_CONTAINER_HEIGHT = 12;
const ANNOTATION_MARGIN = 2;
const ANNOTATION_MIN_WIDTH = 5;
const ANNOTATION_HEIGHT = ANNOTATION_CONTAINER_HEIGHT - 2 * ANNOTATION_MARGIN;

interface SwimlaneAnnotationContainerProps {
chartWidth: number;
domain: {
min: number;
max: number;
};
annotationsData?: AnnotationsTable['annotationsData'];
tooltipService: ChartTooltipService;
}

export const SwimlaneAnnotationContainer: FC<SwimlaneAnnotationContainerProps> = ({
chartWidth,
domain,
annotationsData,
tooltipService,
}) => {
const canvasRef = React.useRef<HTMLDivElement | null>(null);

useEffect(() => {
if (canvasRef.current !== null && Array.isArray(annotationsData)) {
const chartElement = d3.select(canvasRef.current);
chartElement.selectAll('*').remove();

const dimensions = canvasRef.current.getBoundingClientRect();

const startingXPos = Y_AXIS_LABEL_WIDTH + 2 * Y_AXIS_LABEL_PADDING;
const endingXPos = dimensions.width - 2 * Y_AXIS_LABEL_PADDING - 4;

const svg = chartElement
.append('svg')
.attr('width', '100%')
.attr('height', ANNOTATION_CONTAINER_HEIGHT);

const xScale = scaleTime().domain([domain.min, domain.max]).range([startingXPos, endingXPos]);

// Add Annotation y axis label
svg
.append('text')
.attr('text-anchor', 'end')
.attr('class', 'swimlaneAnnotationLabel')
.text(
i18n.translate('xpack.ml.explorer.swimlaneAnnotationLabel', {
defaultMessage: 'Annotations',
})
)
.attr('x', Y_AXIS_LABEL_WIDTH + Y_AXIS_LABEL_PADDING)
.attr('y', ANNOTATION_CONTAINER_HEIGHT)
.style('fill', Y_AXIS_LABEL_FONT_COLOR)
.style('font-size', '12px');

// Add border
svg
.append('rect')
.attr('x', startingXPos)
.attr('y', 0)
.attr('height', ANNOTATION_CONTAINER_HEIGHT)
.attr('width', endingXPos - startingXPos)
.style('stroke', '#cccccc')
.style('fill', 'none')
.style('stroke-width', 1);

// Add annotation marker
annotationsData.forEach((d) => {
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
const annotationWidth = d.end_timestamp
? xScale(Math.min(d.end_timestamp, domain.max)) -
Math.max(xScale(d.timestamp), startingXPos)
: 0;

svg
.append('rect')
.classed('mlAnnotationRect', true)
.attr('x', d.timestamp >= domain.min ? xScale(d.timestamp) : startingXPos)
.attr('y', ANNOTATION_MARGIN)
.attr('height', ANNOTATION_HEIGHT)
.attr('width', Math.max(annotationWidth, ANNOTATION_MIN_WIDTH))
.attr('rx', ANNOTATION_MARGIN)
.attr('ry', ANNOTATION_MARGIN)
.on('mouseover', function () {
const startingTime = formatHumanReadableDateTimeSeconds(d.timestamp);
const endingTime =
d.end_timestamp !== undefined
? formatHumanReadableDateTimeSeconds(d.end_timestamp)
: undefined;

const timeLabel = endingTime ? `${startingTime} - ${endingTime}` : startingTime;

const tooltipData = [
{
label: `${d.annotation}`,
seriesIdentifier: {
key: 'anomaly_timeline',
specId: d._id ?? `${d.annotation}-${d.timestamp}-label`,
},
valueAccessor: 'label',
},
{
label: `${timeLabel}`,
seriesIdentifier: {
key: 'anomaly_timeline',
specId: d._id ?? `${d.annotation}-${d.timestamp}-ts`,
},
valueAccessor: 'time',
},
];
if (d.partition_field_name !== undefined && d.partition_field_value !== undefined) {
tooltipData.push({
label: `${d.partition_field_name}: ${d.partition_field_value}`,
seriesIdentifier: {
key: 'anomaly_timeline',
specId: d._id
? `${d._id}-partition`
: `${d.partition_field_name}-${d.partition_field_value}-label`,
},
valueAccessor: 'partition',
});
}
// @ts-ignore we don't need all the fields for tooltip to show
tooltipService.show(tooltipData, this);
})
.on('mouseout', () => tooltipService.hide());
});
}
}, [chartWidth, domain, annotationsData]);

return <div ref={canvasRef} />;
};
Loading