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.

2.5 KB
Raw Blame
import { useMemo, useState } from 'react';
import { Empty } from 'antd';
import SignozRadioGroup from 'components/SignozRadioGroup/SignozRadioGroup';
import { History, Table } from '@signozhq/icons';
import { DataSource } from 'types/common/queryBuilder';

import { VIEW_TYPES } from './constants';
import NodeMetrics from './NodeMetrics';
import PodMetrics from './PodMetrics';

import './InfraMetrics.styles.scss';

interface MetricsDataProps {
	podName: string;
	nodeName: string;
	hostName: string;
	clusterName: string;
	timestamp: string;
	dataSource: DataSource.LOGS | DataSource.TRACES;
}

function InfraMetrics({
	podName,
	nodeName,
	hostName,
	clusterName,
	timestamp,
	dataSource = DataSource.LOGS,
}: MetricsDataProps): JSX.Element {
	const [selectedView, setSelectedView] = useState<string>(() =>
		nodeName || hostName ? VIEW_TYPES.NODE : VIEW_TYPES.POD,
	);

	const viewOptions = useMemo(() => {
		const options = [
			{
				icon: <Table size={14} />,
				label: 'Node',
				value: VIEW_TYPES.NODE,
			},
		];

		if (podName) {
			options.push({
				icon: <History size={14} />,
				label: 'Pod',
				value: VIEW_TYPES.POD,
			});
		}

		return options;
	}, [podName]);

	const handleModeChange = (value: string): void => {
		// single toggle-group emits '' on re-click of the pressed item
		if (!value) {
			return;
		}
		setSelectedView(value);
	};

	if (!podName && !nodeName && !hostName) {
		const emptyStateDescription =
			dataSource === DataSource.TRACES
				? 'No data available. Please select a span containing a pod, node, or host attributes to view metrics.'
				: 'No data available. Please select a valid log line containing a pod, node, or host attributes to view metrics.';

		return (
			<div className="empty-container">
				<Empty
					image={Empty.PRESENTED_IMAGE_SIMPLE}
					description={emptyStateDescription}
				/>
			</div>
		);
	}

	return (
		<div className="infra-metrics-container">
			<SignozRadioGroup
				value={selectedView}
				onChange={handleModeChange}
				options={viewOptions}
			/>
			{/* TODO(Rahul): Make a common config driven component for this and other infra metrics components */}
			{selectedView === VIEW_TYPES.NODE && (
				<NodeMetrics
					nodeName={nodeName}
					clusterName={clusterName}
					hostName={hostName}
					timestamp={timestamp}
				/>
			)}
			{selectedView === VIEW_TYPES.POD && podName && (
				<PodMetrics
					podName={podName}
					clusterName={clusterName}
					timestamp={timestamp}
				/>
			)}
		</div>
	);
}

export default InfraMetrics;