Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

8.7 KB
Raw Blame
import { useCallback, useEffect, useMemo, useState } from 'react';
// eslint-disable-next-line no-restricted-imports
import { useSelector } from 'react-redux';
import { Spacing } from '@signozhq/design-tokens';
import { Button, Drawer } from 'antd';
import { ToggleGroup } from '@signozhq/ui/toggle-group';
import { Divider } from '@signozhq/ui/divider';
import { Typography } from '@signozhq/ui/typography';
import DateTimeSelectionV2 from 'container/TopNav/DateTimeSelectionV2';
import {
	CustomTimeType,
	Time,
} from 'container/TopNav/DateTimeSelectionV2/types';
import GetMinMax from 'lib/getMinMax';
import { ArrowDown, ArrowUp, X } from '@signozhq/icons';
import { AppState } from 'store/reducers';
import { IBuilderQuery } from 'types/api/queryBuilder/queryBuilderData';
import { GlobalReducer } from 'types/reducer/globalTime';

import { useApiMonitoringParams } from '../../../queryParams';
import AllEndPoints from './AllEndPoints';
import DomainMetrics from './components/DomainMetrics';
import { VIEW_TYPES, VIEWS } from './constants';
import EndPointDetails from './EndPointDetails';
import TopErrors from './TopErrors';

import './DomainDetails.styles.scss';

const TimeRangeOffset = 1000000000;

function DomainDetails({
	domainData,
	handleClose,
	selectedDomainIndex,
	setSelectedDomainIndex,
	domainListLength,
	domainListFilters,
}: {
	domainData: any;
	handleClose: () => void;
	selectedDomainIndex: number;
	setSelectedDomainIndex: (index: number) => void;
	domainListLength: number;
	domainListFilters: IBuilderQuery['filters'];
}): JSX.Element {
	const [params, setParams] = useApiMonitoringParams();
	const [selectedView, setSelectedView] = useState<VIEWS>(
		(params.selectedView as VIEWS) || VIEWS.ALL_ENDPOINTS,
	);
	const [selectedEndPointName, setSelectedEndPointName] = useState<string>(
		params.selectedEndPointName || '',
	);
	const [endPointsGroupBy, setEndPointsGroupBy] = useState<
		IBuilderQuery['groupBy']
	>([]);
	const [initialFiltersEndPointStats, setInitialFiltersEndPointStats] =
		useState<IBuilderQuery['filters']>(domainListFilters);

	const handleTabChange = (value: string): void => {
		setSelectedView(value as VIEWS);
		setParams({ selectedView: value });
	};

	const handleEndPointChange = (name: string): void => {
		setSelectedEndPointName(name);
		setParams({ selectedEndPointName: name });
	};

	useEffect(() => {
		if (params.selectedView && params.selectedView !== selectedView) {
			setSelectedView(params.selectedView as VIEWS);
		}
		if (
			params.selectedEndPointName !== undefined &&
			params.selectedEndPointName !== selectedEndPointName
		) {
			setSelectedEndPointName(params.selectedEndPointName);
		}
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [params.selectedView, params.selectedEndPointName]);

	const { maxTime, minTime, selectedTime } = useSelector<
		AppState,
		GlobalReducer
	>((state) => state.globalTime);

	const startMs = useMemo(
		() => Math.floor(Number(minTime) / TimeRangeOffset),
		[minTime],
	);
	const endMs = useMemo(
		() => Math.floor(Number(maxTime) / TimeRangeOffset),
		[maxTime],
	);

	const [selectedInterval, setSelectedInterval] = useState<Time>(
		(params.selectedInterval as Time) || (selectedTime as Time),
	);

	// Sync params to local selectedInterval state on param change
	useEffect(() => {
		if (params.selectedInterval && params.selectedInterval !== selectedInterval) {
			setSelectedInterval(params.selectedInterval as Time);
		}
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [params.selectedInterval]);

	const [modalTimeRange, setModalTimeRange] = useState(() => {
		if (params.modalTimeRange) {
			return params.modalTimeRange;
		}
		return {
			startTime: startMs,
			endTime: endMs,
		};
	});

	// Sync params to local modalTimeRange state on param change
	useEffect(() => {
		if (
			params.modalTimeRange &&
			JSON.stringify(params.modalTimeRange) !== JSON.stringify(modalTimeRange)
		) {
			setModalTimeRange(params.modalTimeRange);
		}
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [params.modalTimeRange]);

	const handleTimeChange = useCallback(
		(interval: Time | CustomTimeType, dateTimeRange?: [number, number]): void => {
			setSelectedInterval(interval as Time);
			setParams({ selectedInterval: interval as string });

			if (interval === 'custom' && dateTimeRange) {
				const newRange = {
					startTime: Math.floor(dateTimeRange[0] / 1000),
					endTime: Math.floor(dateTimeRange[1] / 1000),
				};
				setModalTimeRange(newRange);
				setParams({ modalTimeRange: newRange });
			} else {
				const { maxTime, minTime } = GetMinMax(interval);

				const newRange = {
					startTime: Math.floor(minTime / TimeRangeOffset),
					endTime: Math.floor(maxTime / TimeRangeOffset),
				};
				setModalTimeRange(newRange);
				setParams({ modalTimeRange: newRange });
			}
		},
		[setParams],
	);

	return (
		<Drawer
			width="60%"
			title={
				<div className="domain-details-drawer-header">
					<div className="domain-details-drawer-header-title">
						<Divider type="vertical" />

						{domainData?.domainName && (
							<Typography.Text className="title" testId="domain-details-title">
								{domainData.domainName}
							</Typography.Text>
						)}
					</div>
					<div className="domain-details-drawer-header-right-container">
						<DateTimeSelectionV2
							showAutoRefresh={false}
							showRefreshText={false}
							onTimeChange={handleTimeChange}
							defaultRelativeTime="5m"
							isModalTimeSelection
							modalSelectedInterval={selectedInterval}
							modalInitialStartTime={modalTimeRange.startTime * 1000}
							modalInitialEndTime={modalTimeRange.endTime * 1000}
						/>
						<Button.Group className="domain-details-drawer-header-ctas">
							<Button
								className="domain-navigate-cta"
								onClick={(): void => {
									setSelectedDomainIndex(selectedDomainIndex - 1);
									setSelectedEndPointName('');
									setEndPointsGroupBy([]);
									setSelectedView(VIEW_TYPES.ALL_ENDPOINTS);
								}}
								icon={<ArrowUp size={16} />}
								disabled={selectedDomainIndex === 0}
								title="Previous domain"
							/>
							<Button
								className="domain-navigate-cta"
								onClick={(): void => {
									setSelectedDomainIndex(selectedDomainIndex + 1);
									setSelectedEndPointName('');
									setEndPointsGroupBy([]);
									setSelectedView(VIEW_TYPES.ALL_ENDPOINTS);
								}}
								icon={<ArrowDown size={16} />}
								disabled={selectedDomainIndex === domainListLength - 1}
								title="Next domain"
							/>
						</Button.Group>
					</div>
				</div>
			}
			placement="right"
			onClose={handleClose}
			open={!!domainData}
			style={{
				overscrollBehavior: 'contain',
				background: 'var(--l1-background)',
			}}
			className="domain-detail-drawer"
			data-testid="domain-details-drawer"
			destroyOnClose
			closeIcon={<X size={16} style={{ marginTop: Spacing.MARGIN_1 }} />}
		>
			{domainData && (
				<>
					<DomainMetrics
						domainName={domainData.domainName}
						domainListFilters={domainListFilters}
						timeRange={modalTimeRange}
					/>
					<div className="views-tabs-container">
						<ToggleGroup
							variant="outlined"
							color="secondary"
							type="single"
							onChange={handleTabChange}
							value={selectedView}
							size="md"
							items={[
								{
									value: VIEW_TYPES.ALL_ENDPOINTS,
									label: 'All Endpoints',
									testId: `domain-view-${VIEW_TYPES.ALL_ENDPOINTS}`,
								},
								{
									value: VIEW_TYPES.ENDPOINT_STATS,
									label: 'Endpoint(s) Stats',
									testId: `domain-view-${VIEW_TYPES.ENDPOINT_STATS}`,
								},
								{
									value: VIEW_TYPES.TOP_ERRORS,
									label: 'Top 10 Errors',
									testId: `domain-view-${VIEW_TYPES.TOP_ERRORS}`,
								},
							]}
						/>
					</div>
					{selectedView === VIEW_TYPES.ALL_ENDPOINTS && (
						<AllEndPoints
							domainName={domainData.domainName}
							setSelectedEndPointName={handleEndPointChange}
							setSelectedView={setSelectedView}
							groupBy={endPointsGroupBy}
							setGroupBy={setEndPointsGroupBy}
							timeRange={modalTimeRange}
							initialFilters={domainListFilters}
							setInitialFiltersEndPointStats={setInitialFiltersEndPointStats}
						/>
					)}

					{selectedView === VIEW_TYPES.ENDPOINT_STATS && (
						<EndPointDetails
							domainName={domainData.domainName}
							endPointName={selectedEndPointName}
							setSelectedEndPointName={handleEndPointChange}
							initialFilters={initialFiltersEndPointStats}
							timeRange={modalTimeRange}
							handleTimeChange={handleTimeChange}
						/>
					)}

					{selectedView === VIEW_TYPES.TOP_ERRORS && (
						<TopErrors
							domainName={domainData.domainName}
							timeRange={modalTimeRange}
							initialFilters={domainListFilters}
						/>
					)}
				</>
			)}
		</Drawer>
	);
}

export default DomainDetails;