Skip to content
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

feat: add drag support to alert history horizontal graph #5928

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Changes from 1 commit
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
51 changes: 49 additions & 2 deletions frontend/src/container/AlertHistory/Timeline/Graph/Graph.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Color } from '@signozhq/design-tokens';
import Uplot from 'components/Uplot';
import { QueryParams } from 'constants/query';
import { useIsDarkMode } from 'hooks/useDarkMode';
import { useResizeObserver } from 'hooks/useDimensions';
import useUrlQuery from 'hooks/useUrlQuery';
import history from 'lib/history';
import heatmapPlugin from 'lib/uPlotLib/plugins/heatmapPlugin';
import timelinePlugin from 'lib/uPlotLib/plugins/timelinePlugin';
import { useMemo, useRef } from 'react';
import { useDispatch } from 'react-redux';
import { UpdateTimeInterval } from 'store/actions';
import { AlertRuleTimelineGraphResponse } from 'types/api/alerts/def';
import uPlot, { AlignedData } from 'uplot';

Expand Down Expand Up @@ -44,11 +49,13 @@ function HorizontalTimelineGraph({
return [timestamps, states];
}, [data]);

const urlQuery = useUrlQuery();
const dispatch = useDispatch();

const options: uPlot.Options = useMemo(
() => ({
width,
height: 85,
cursor: { show: false },

axes: [
{
Expand All @@ -69,6 +76,46 @@ function HorizontalTimelineGraph({
label: 'States',
},
],
hooks: {
setSelect: [
(self): void => {
const selection = self.select;
if (selection) {
const startTime = self.posToVal(selection.left, 'x');
const endTime = self.posToVal(selection.left + selection.width, 'x');
ahmadshaheer marked this conversation as resolved.
Show resolved Hide resolved

const diff = endTime - startTime;

if (diff > 0) {
if (urlQuery.has(QueryParams.relativeTime)) {
urlQuery.delete(QueryParams.relativeTime);
}

const startTimestamp = Math.floor(startTime * 1000);
const endTimestamp = Math.floor(endTime * 1000);

if (startTimestamp !== endTimestamp) {
dispatch(UpdateTimeInterval('custom', [startTimestamp, endTimestamp]));
}

urlQuery.set(
QueryParams.startTime,
Math.floor(startTime * 1000).toString(),
ahmadshaheer marked this conversation as resolved.
Show resolved Hide resolved
ahmadshaheer marked this conversation as resolved.
Show resolved Hide resolved
);
urlQuery.set(
QueryParams.endTime,
Math.floor(endTime * 1000).toString(),
ahmadshaheer marked this conversation as resolved.
Show resolved Hide resolved
);

history.push({
search: urlQuery.toString(),
});
}
}
},
],
},

plugins:
transformedData?.length > 1
? [
Expand All @@ -79,7 +126,7 @@ function HorizontalTimelineGraph({
]
: [],
}),
[width, isDarkMode, transformedData],
[width, isDarkMode, transformedData.length, urlQuery, dispatch],
);
return <Uplot data={transformedData} options={options} />;
}
Expand Down
Loading