Skip to content

Commit a3d81da

Browse files
committed
Show all partitions and overall anomaly score in annotation tooltip for overall chart
1 parent 7be35d2 commit a3d81da

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

x-pack/legacy/plugins/infra/public/pages/logs/analysis/sections/anomalies/chart.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export const AnomaliesChart: React.FunctionComponent<{
3333
timeRange: TimeRange;
3434
series: Array<{ time: number; value: number }>;
3535
annotations: Record<MLSeverityScoreCategories, RectAnnotationDatum[]>;
36-
}> = ({ chartId, series, annotations, setTimeRange, timeRange }) => {
36+
renderAnnotationTooltip?: (details?: string) => JSX.Element;
37+
}> = ({ chartId, series, annotations, setTimeRange, timeRange, renderAnnotationTooltip }) => {
3738
const [dateFormat] = useKibanaUiSetting('dateFormat');
3839
const [isDarkMode] = useKibanaUiSetting('theme:darkMode');
3940

@@ -96,21 +97,25 @@ export const AnomaliesChart: React.FunctionComponent<{
9697
dataValues={annotations.warning}
9798
annotationId={warningAnnotationsId}
9899
style={{ fill: '#006BB4', opacity: 0.8 }}
100+
renderTooltip={renderAnnotationTooltip}
99101
/>
100102
<RectAnnotation
101103
dataValues={annotations.minor}
102104
annotationId={minorAnnotationsId}
103105
style={{ fill: '#017D73', opacity: 0.8 }}
106+
renderTooltip={renderAnnotationTooltip}
104107
/>
105108
<RectAnnotation
106109
dataValues={annotations.major}
107110
annotationId={majorAnnotationsId}
108111
style={{ fill: '#F5A700', opacity: 0.8 }}
112+
renderTooltip={renderAnnotationTooltip}
109113
/>
110114
<RectAnnotation
111115
dataValues={annotations.critical}
112116
annotationId={criticalAnnotationsId}
113117
style={{ fill: '#BD271E', opacity: 0.8 }}
118+
renderTooltip={renderAnnotationTooltip}
114119
/>
115120
<Settings
116121
onBrushEnd={handleBrushEnd}

x-pack/legacy/plugins/infra/public/pages/logs/analysis/sections/anomalies/index.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export const AnomaliesResults = ({
117117
timeRange={timeRange}
118118
series={logEntryRateSeries}
119119
annotations={anomalyAnnotations}
120+
renderAnnotationTooltip={renderAnnotationTooltip}
120121
/>
121122
<EuiSpacer size="l" />
122123
<AnomaliesTable results={results} setTimeRange={setTimeRange} timeRange={timeRange} />
@@ -125,3 +126,50 @@ export const AnomaliesResults = ({
125126
</>
126127
);
127128
};
129+
130+
interface ParsedAnnotationDetails {
131+
overallAnomalyScore: number;
132+
anomalyScoresByPartition: Record<string, number>;
133+
}
134+
135+
const AnnotationTooltip: React.FunctionComponent<{ details: string }> = ({ details }) => {
136+
const parsedDetails: ParsedAnnotationDetails = JSON.parse(details);
137+
return (
138+
<div>
139+
<span>{`Overall anomaly score: ${parsedDetails.overallAnomalyScore}`}</span>
140+
<ul>
141+
{Object.entries(parsedDetails.anomalyScoresByPartition).map((entry, index) => {
142+
return (
143+
<li key={`${index}-overall-anomaly-chart-${entry[0]}-partition-score-${entry[1]}`}>
144+
<>
145+
<span>{entry[0]}</span>
146+
{': '}
147+
<span>
148+
<b>{entry[1]}</b>
149+
</span>
150+
</>
151+
</li>
152+
);
153+
})}
154+
</ul>
155+
</div>
156+
);
157+
};
158+
159+
const renderAnnotationTooltip = (details?: string) => {
160+
// Seems to be necessary to get things typed correctly all the way through to elastic-charts components
161+
if (!details) {
162+
return <div></div>;
163+
}
164+
return <AnnotationTooltip details={details} />;
165+
};
166+
167+
// i18n.translate(
168+
// 'xpack.infra.logs.analysis.logRateBucketMaxAnomalyScoreAnnotationLabel',
169+
// {
170+
// defaultMessage: 'Anomaly score: {sumPartitionMaxAnomalyScores}',
171+
// values: {
172+
// sumPartitionMaxAnomalyScores: Number(sumPartitionMaxAnomalyScores).toFixed(0),
173+
// },
174+
// }
175+
// ),

x-pack/legacy/plugins/infra/public/pages/logs/analysis/sections/helpers/data_formatters.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,24 @@ export const getAnnotationsForPartition = (
149149
export const getAnnotationsForAll = (results: GetLogEntryRateSuccessResponsePayload['data']) => {
150150
return results.histogramBuckets.reduce<Record<MLSeverityScoreCategories, RectAnnotationDatum[]>>(
151151
(annotatedBucketsBySeverity, bucket) => {
152-
const sumPartitionMaxAnomalyScores = bucket.partitions.reduce<number>(
153-
(scoreSum, partition) => {
154-
return scoreSum + partition.maximumAnomalyScore;
152+
const maxAnomalyScoresByPartition = bucket.partitions.reduce<Record<string, number>>(
153+
(bucketMaxAnomalyScoresByPartition, partition) => {
154+
if (partition.maximumAnomalyScore < ML_SEVERITY_SCORES.warning) {
155+
return bucketMaxAnomalyScoresByPartition;
156+
}
157+
return {
158+
...bucketMaxAnomalyScoresByPartition,
159+
[partition.partitionId ? partition.partitionId : 'unknown']: parseInt(
160+
Number(partition.maximumAnomalyScore).toFixed(0),
161+
10
162+
),
163+
};
164+
},
165+
{}
166+
);
167+
const sumPartitionMaxAnomalyScores = Object.entries(maxAnomalyScoresByPartition).reduce(
168+
(sumMaxAnomalyScore, entry) => {
169+
return (sumMaxAnomalyScore += entry[1]);
155170
},
156171
0
157172
);
@@ -172,15 +187,10 @@ export const getAnnotationsForAll = (results: GetLogEntryRateSuccessResponsePayl
172187
x0: bucket.startTime,
173188
x1: bucket.startTime + results.bucketDuration,
174189
},
175-
details: i18n.translate(
176-
'xpack.infra.logs.analysis.logRateBucketMaxAnomalyScoreAnnotationLabel',
177-
{
178-
defaultMessage: 'Anomaly score: {sumPartitionMaxAnomalyScores}',
179-
values: {
180-
sumPartitionMaxAnomalyScores: Number(sumPartitionMaxAnomalyScores).toFixed(0),
181-
},
182-
}
183-
),
190+
details: JSON.stringify({
191+
overallAnomalyScore: parseInt(Number(sumPartitionMaxAnomalyScores).toFixed(0), 10),
192+
anomalyScoresByPartition: maxAnomalyScoresByPartition,
193+
}),
184194
},
185195
],
186196
};

0 commit comments

Comments
 (0)