-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[ML] Add annotation markers to the Anomaly Swimlane axis #97202
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2101c11
[ML] Add annotation
qn895 af34336
[ML] Clean up
qn895 e942924
[ML] Clean up load all annotations
qn895 d28c594
Merge upstream/master into branch
qn895 df8f981
Fix allAnnotations
qn895 7a5bedf
Remove unrelated change
qn895 bc00e98
[ML] Add annotation border, label text, rename allAnnotations -> over…
qn895 85401ba
[ML] Update annotation label styling
qn895 f213802
[ML] Fix swimlane embeddable broken
qn895 1a8cee3
[ML] Re-enable tests
qn895 a0c887d
Merge remote-tracking branch 'upstream/master' into ml-annotations-sw…
qn895 985fced
[ML] Fix markers overflowing beyond the start and ending time range
qn895 229b4fa
[ML] Make styling more consistent with SMV
qn895 ce159b0
[ML] Renable tests
qn895 49d3a8e
[ML] Fix model snapshot annotation missing
qn895 c65cd32
[ML] Fix model snapshot annotation missing
qn895 55b5362
[ML] Fix duration to account for contextXRangeStart
qn895 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
x-pack/plugins/ml/public/application/explorer/swimlane_annotation_container.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) => { | ||
| 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} />; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.