-
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
Changes from 6 commits
2101c11
af34336
e942924
d28c594
df8f981
7a5bedf
bc00e98
85401ba
f213802
1a8cee3
a0c887d
985fced
229b4fa
ce159b0
49d3a8e
c65cd32
55b5362
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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 { 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; | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
const svg = chartElement | ||
.append('svg') | ||
.attr('width', '100%') | ||
.attr('height', ANNOTATION_HEIGHT); | ||
|
||
const xScale = scaleTime().domain([domain.min, domain.max]).range([startingXPos, endingXPos]); | ||
|
||
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} />; | ||
}; |
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.
Wondering if we should rename everything related to
allAnnotations
tooverallAnnotations
. "all" might be a bit misleading sinceANNOTATIONS_TABLE_DEFAULT_QUERY_SIZE = 500
limits how many annotations we fetch and it's not really "all".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.
Updated here
bc00e98
(#97202)