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.8 KB
Raw Blame
import { Button } from '@signozhq/ui/button';
import { Tooltip } from '@signozhq/ui/tooltip';
import { Typography } from '@signozhq/ui/typography';
import { Compass } from '@signozhq/icons';
import { TextNoData } from '../../../components/TextNoData';
import { AppLink } from 'lib/router/AppLink';

import { InfraMonitoringEntity } from '../../../constants';
import { getDrawerDurationMs } from '../../useDrawerLifecycleStore';
import { buildK8sListNavigationUrl } from '../../utils';
import styles from './EntityCountsSection.module.scss';
import { logInfraExplorerNavigatedEvent } from 'container/InfraMonitoringK8sV2/Base/events';

export interface EntityCountConfig<T> {
	label: string;
	getValue: (entity: T) => number;
	targetCategory: InfraMonitoringEntity;
}

interface EntityCountsSectionProps<T> {
	entity: T;
	countsConfig: EntityCountConfig<T>[];
	selectedItem: string;
	filterExpression: string;
	closeDrawer: () => void;
	entityType: InfraMonitoringEntity;
	activeTab: string;
}

export function EntityCountsSection<T>({
	entity,
	countsConfig,
	selectedItem,
	filterExpression,
	closeDrawer,
	entityType,
	activeTab,
}: EntityCountsSectionProps<T>): JSX.Element {
	const handleCardNavigate = (cardLabel: string): void => {
		logInfraExplorerNavigatedEvent({
			entityType,
			destination: 'k8s_list',
			source: 'stats_card',
			tab: activeTab,
			sourceKey: cardLabel,
			drawerDurationMsAtNavigation: getDrawerDurationMs(),
		});
		closeDrawer();
	};

	return (
		<div className={styles.countsContainer}>
			{countsConfig.map((config) => (
				<div
					key={config.label}
					className={styles.countCard}
					data-testid={`count-card-${config.label.toLowerCase().replace(/\s+/g, '-')}`}
				>
					<Typography.Text
						color="muted"
						size="small"
						weight="medium"
						className={styles.countLabel}
					>
						{config.label}
					</Typography.Text>
					{config.getValue(entity) ? (
						<Typography.Text
							className={styles.countValue}
							size="xl"
							weight="semibold"
						>
							{config.getValue(entity)}
						</Typography.Text>
					) : (
						<TextNoData type="typography" className={styles.countValue} />
					)}
					<AppLink
						to={buildK8sListNavigationUrl(config.targetCategory, filterExpression)}
						onClick={(): void => handleCardNavigate(config.label)}
						data-testid={`navigate-${config.label.toLowerCase().replace(/\s+/g, '-')}`}
						className={styles.navigateButton}
					>
						<Tooltip
							title={`View ${config.label.toLowerCase()} of '${selectedItem}'`}
							side="top"
						>
							<Button
								aria-label="Action"
								size="sm"
								icon
								variant="ghost"
								color="secondary"
							>
								<Compass size={14} />
							</Button>
						</Tooltip>
					</AppLink>
				</div>
			))}
		</div>
	);
}