Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/alert-above-threshold-highlight.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/app': patch
---

Fix the alert preview chart highlighting the region below the threshold for "above" alerts. Recharts v3 flipped the default edge for open-ended reference areas, so "above" and "not between" alerts shaded the wrong side of the threshold line.
6 changes: 3 additions & 3 deletions packages/app/src/components/Alerts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ export const getAlertReferenceLines = ({
return [
<ReferenceArea
key="not-between-lower"
y2={threshold}
y1={threshold}
ifOverflow="extendDomain"
fill="red"
strokeWidth={0}
fillOpacity={0.05}
/>,
<ReferenceArea
key="not-between-upper"
y1={thresholdMax}
y2={thresholdMax}
ifOverflow="extendDomain"
fill="red"
strokeWidth={0}
Expand Down Expand Up @@ -231,7 +231,7 @@ export const getAlertReferenceLines = ({
) {
return (
<ReferenceArea
y1={threshold}
y2={threshold}
ifOverflow="extendDomain"
fill="red"
strokeWidth={0}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React from 'react';
import { Line, LineChart, ReferenceLine, XAxis, YAxis } from 'recharts';
import { AlertThresholdType } from '@hyperdx/common-utils/dist/types';
import { render } from '@testing-library/react';

import { getAlertReferenceLines } from '@/components/Alerts';

type Rect = { top: number; bottom: number };

function renderAreas(
thresholdType: AlertThresholdType,
threshold: number,
thresholdMax?: number,
): { rects: Rect[]; thresholdY: number; thresholdMaxY?: number } {
const { container } = render(
<LineChart
width={400}
height={300}
data={[
{ t: 0, v: 10 },
{ t: 1, v: 80 },
]}
>
<XAxis dataKey="t" />
<YAxis domain={[0, 100]} />
<Line dataKey="v" isAnimationActive={false} />
{getAlertReferenceLines({ threshold, thresholdMax, thresholdType })}
<ReferenceLine y={threshold} />
{thresholdMax != null ? <ReferenceLine y={thresholdMax} /> : null}
</LineChart>,
);

const rects = Array.from(
container.querySelectorAll('.recharts-reference-area-rect'),
).map(r => {
const top = Number(r.getAttribute('y'));
return { top, bottom: top + Number(r.getAttribute('height')) };
});
const lineYs = Array.from(
container.querySelectorAll('.recharts-reference-line-line'),
).map(l => Number(l.getAttribute('y1')));

return {
rects,
thresholdY: lineYs[0],
thresholdMaxY: lineYs[1],
};
}

const EPS = 1;

describe('getAlertReferenceLines shading', () => {
it.each([AlertThresholdType.ABOVE, AlertThresholdType.ABOVE_EXCLUSIVE])(
'shades above the threshold line for %s',
thresholdType => {
const { rects, thresholdY } = renderAreas(thresholdType, 50);
expect(rects).toHaveLength(1);
expect(rects[0].top).toBeLessThan(thresholdY);
expect(rects[0].bottom).toBeLessThanOrEqual(thresholdY + EPS);
},
);

it.each([AlertThresholdType.BELOW, AlertThresholdType.BELOW_OR_EQUAL])(
'shades below the threshold line for %s',
thresholdType => {
const { rects, thresholdY } = renderAreas(thresholdType, 50);
expect(rects).toHaveLength(1);
expect(rects[0].top).toBeGreaterThanOrEqual(thresholdY - EPS);
expect(rects[0].bottom).toBeGreaterThan(thresholdY);
},
);

it('shades the band between the two thresholds for BETWEEN', () => {
const { rects, thresholdY, thresholdMaxY } = renderAreas(
AlertThresholdType.BETWEEN,
30,
70,
);
expect(rects).toHaveLength(1);
expect(thresholdMaxY).toBeDefined();
expect(rects[0].top).toBeCloseTo(thresholdMaxY!, 0);
expect(rects[0].bottom).toBeCloseTo(thresholdY, 0);
});

it('shades below the lower bound and above the upper bound for NOT_BETWEEN', () => {
const { rects, thresholdY, thresholdMaxY } = renderAreas(
AlertThresholdType.NOT_BETWEEN,
30,
70,
);
expect(rects).toHaveLength(2);
expect(thresholdMaxY).toBeDefined();

const lower = rects.find(r => r.bottom > thresholdY)!;
const upper = rects.find(r => r.top < thresholdMaxY!)!;
// Lower firing region: from the lower threshold line downward.
expect(lower.top).toBeCloseTo(thresholdY, 0);
// Upper firing region: from the upper threshold line upward.
expect(upper.bottom).toBeCloseTo(thresholdMaxY!, 0);
});
});
Loading