-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from 9 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 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 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 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 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 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
138 changes: 138 additions & 0 deletions
138
x-pack/plugins/ml/public/application/explorer/swimlane_annotation_container.tsx
This file contains 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,138 @@ | ||
/* | ||
* 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'; | ||
|
||
const ANNOTATION_HEIGHT = 12; | ||
export const Y_AXIS_LABEL_WIDTH = 170; | ||
export const Y_AXIS_LABEL_PADDING = 8; | ||
export const Y_AXIS_LABEL_FONT_COLOR = '#6a717d'; | ||
|
||
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 startingXPos = Y_AXIS_LABEL_WIDTH + 2 * Y_AXIS_LABEL_PADDING; | ||
const endingXPos = startingXPos + chartWidth - Y_AXIS_LABEL_PADDING; | ||
|
||
const svg = chartElement | ||
.append('svg') | ||
.attr('width', '100%') | ||
.attr('height', ANNOTATION_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_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_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(d.end_timestamp) - xScale(d.timestamp) : 0; | ||
svg | ||
.append('rect') | ||
.classed('mlAnnotationRect', true) | ||
.attr('x', xScale(d.timestamp)) | ||
peteharverson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.attr('y', 0) | ||
.attr('height', ANNOTATION_HEIGHT) | ||
.attr('width', Math.max(annotationWidth, 5)) | ||
.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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the end of the annotation chart need adjusting? It always seems to extend past the swim lane for me:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussing with Dima, we think the best way to fix this issue is through some modifications with the elastic-charts component itself. We can provide a callback function that exposes the positions of the heatmap cells whenever the swimlane is resized. Added this to the meta issue for Annotations.